一个简单的学生管理系统(增删改查)

很简单的一个学生管理系统,学生的属性只有学号和姓名,当然也可以多添加一些属性,包含最基本的增删改查:

package Student;

import java.util.ArrayList;
import java.util.Scanner;

public class Student1 {

	
	
	private long id;
	private String name;
	
	public Student1() {
		// TODO Auto-generated constructor stub
	}
	
	public Student1(long id, String name){
		
		this.id = id;
		this.name = name;
	}
	
	public void setId(long id){
		
		this.id = id;
	}
	
	public long getId(){
		
		return id;
	}
	
	public void setName(String name){
		
		this.name = name;
	}
	
	public String getName(){
		
		return name;
	}

	
	ArrayList<Student1> arrayList = new ArrayList<>();
	int flag = 0;
	
	private void inputStu(){
		
		Scanner scanner = new Scanner(System.in);
		Student1 stu = new Student1();
		System.out.println("请输入学号:");
		stu.id = scanner.nextLong();
		System.out.println("请输入姓名:");
		stu.name = scanner.next();
		arrayList.add(stu);
		flag++;
	}
	
	
	private void selectStu(){
		
		Scanner scanner = new Scanner(System.in);
		Student1 stu = new Student1();
		
		System.out.println("请输入学号");
		stu.id = scanner.nextLong();
		
		for(int i=0; i<arrayList.size(); i++){
			
			if(arrayList.get(i).id==stu.id){
				
				System.out.println("学号:"+arrayList.get(i).id+"\n姓名:"+arrayList.get(i).name);
				break;
			}
			else{
				
				System.out.println("该学生不存在");
			}
	
		}
		
	}
	
	private void updateStu(){
		
		Scanner scanner = new Scanner(System.in);
		Student1 stu = new Student1();
		
		System.out.println("请输入学号");
		stu.id = scanner.nextLong();
		
		for(int i=0; i<arrayList.size(); i++){
			
			if(arrayList.get(i).id == stu.id){
				
				System.out.println("请输入修改后的学号:");
				arrayList.get(i).id = scanner.nextLong();
				System.out.println("请输入修改后的姓名:");
				arrayList.get(i).name = scanner.next();
				
				break;
			}
			else{
				
				System.out.println("该学生不存在");
			}
			
		}
		
	}
	
	private void deleteStu(){
		
		Scanner scanner = new Scanner(System.in);
		Student1 stu = new Student1();
		System.out.println("请输入学号");
		stu.id = scanner.nextLong();
		
		for(int i=0; i<arrayList.size(); i++){
			
			if(arrayList.get(i).id == stu.id){
				
				arrayList.remove(i);
				break;
			}
			else{
				
				System.out.println("该学生不存在");
			}
			
			
		}
	}
	
	private static void jieMian(){
		
		System.out.println("请选择已下操作");
		System.out.print("0:退出\t");
		System.out.print("1:增加\t");
		System.out.print("2:查询\t");
		System.out.print("3:修改\t");
		System.out.print("4:删除\n");

		
	}
	public static void main(String[] args){
		
		System.out.println("欢迎使用学生管理系统");
		Student1 stu = new Student1();
		Scanner scanner = new Scanner(System.in);
		while(true){
			jieMian();
			int a = scanner.nextInt();
			switch(a){
			
			case 0:
				System.exit(0);
				break;
				
			case 1:
				stu.inputStu();
				break;
				
			case 2:
				stu.selectStu();
				break;
				
			case 3:
				stu.updateStu();
				break;
				
			case 4:
				stu.deleteStu();
				break;
			}
			
		}
		
	}
	
	
}

你可能感兴趣的:(一个简单的学生管理系统(增删改查))