Using java to write the Student Information Management System (basic, entry)

Using java to write the Student Information Management System

Used to store collections of objects to ArrayLisat Student class.

StudentManagerTest main categories, Student class is encapsulated inside the corresponding data. Inside with a lot of write cycles, and use a lot of markers to determine whether to exit loop, such as flag and index.

Creating a collection class object format:
the ArrayList Array = new new the ArrayList (); required <> type to be explicitly passed in by the parameter list at the time of delivery methods

The main class

	    package zjh;
		import java.util.ArrayList;
		import java.util.Scanner;
		
		public class StudentManageTest {
		public static void main(String[] args) {
		ArrayList<Student> array = new ArrayList<Student>();
	
		while(true) {
		System.out.println("----欢迎来到学生信息管理系统----");
		System.out.println("请输入你想要进行的操作");
		System.out.println("1:查看所有学生信息");
		System.out.println("2:添加学生信息");
		System.out.println("3:删除学生信息");
		System.out.println("4:修改学生信息");
		System.out.println("5:退出");
		
		Scanner scanner = new Scanner(System.in);
		
		String choice = scanner.nextLine();
		


		switch (choice) {
		case "1":
			findAllStudents(array);
			break;
		case "2":
			addStudent(array);
			break;
		case "3":
			deleteStudent(array);
			break;
		case "4":
			updateStudent(array);
			break;
		case "5":
		default:
			System.out.println("正在退出系统,欢迎下次继续使用");
			break;
		}
		
	}
	}
	//修改学生信息
	public static void updateStudent(ArrayList<Student> array) {
		Scanner scanner = new Scanner(System.in);
		while(true) {
		System.out.println("请输入你要修改的学号:");
		String id = scanner.nextLine();
		int index = -1;
		for(int x=0; x<array.size(); x++) {
			Student student = array.get(x);
			if(student.getId().equals(id)) {
				index = x;
				break;
			}
		}
		if(index ==-1) {
			System.out.println("您输入的学号有误请重新输入");
			continue;
		}else {
			System.out.println("请输入新的姓名:");
			String name = scanner.nextLine();
			System.out.println("请输入新的年龄:");
			String age = scanner.nextLine();
			System.out.println("请输入新的地址");
			String address = scanner.nextLine();
			
			Student student = new Student();
			student.setId(id);
			student.setName(name);
			student.setAge(age);
			student.setAddress(address);
			
			array.set(index, student);
			break;
			}
		}
		System.out.println("修改学生成功");
		
	}
	
	//删除学生信息
	public static void deleteStudent(ArrayList<Student> array) {
		Scanner scanner = new Scanner(System.in);
		while(true) {
		System.out.println("请输入你要删除的学号");
		
		String id = scanner.nextLine();
		int index = -1;
		
	    for(int x=0; x<array.size(); x++) {
	    	Student student = array.get(x);
	    	if (student.getId().equals(id)) {
	    		index = x;
	    		break;
	    	}
	    }
	    if(index == -1) {
	    	System.out.println("您输入的学号有误 请重新输入");
	    	continue;
	    }else {
	    	array.remove(index);
	    	break;
	    }
		}
		System.out.println("删除学生信息成功!");
	}
	
	//添加学生信息
	public static void addStudent(ArrayList<Student> array) {
		Scanner scanner = new Scanner(System.in);
		String id;
		while(true) {
		System.out.println("请输入你要添加的学号:");
		int flag =0;
		id = scanner.nextLine();
		for(int x=0; x<array.size(); x++) {
			Student student =array.get(x);
			if(student.getId().equals(id)) {
				System.out.println("你输入的学号已被占用,请重新输入");
				break;
			}else {
				flag++;
			}
			}
		if(flag==array.size()) {
			break;
		}
		}
		System.out.println("请输入你要添加的姓名:");
		String name = scanner.nextLine();
		System.out.println("请输入你要添加的年龄:");
		String age = scanner.nextLine();
	    System.out.println("请输入你要添加的地址:");
	    String address = scanner.nextLine();
		Student student = new  Student();
		student.setId(id);
		student.setName(name);
		student.setAge(age);
		student.setAddress(address);
		
		array.add(student);
		
		
		System.out.println("添加信息成功");
		
	}

	
	//查看所有学生信息
	public static void findAllStudents(ArrayList<Student> array) {
		if(array.size()==0) {
			System.out.println("当前没有任何学生信息,请添加后再使用");
		}
		System.out.println("学号\t\t姓名\t年龄\t居住地");
		for(int x=0; x<array.size(); x++) {
			Student s = array.get(x);
			System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddress());
		}
	}
}

Student category

   package zjh;
	public class Student {
	private String id;
	private String name;
	private String age;
	private String address;
	public Student() {
		
	}
	public Student(String id, String name, String age, String address) {
		this.id = id;
		this.name = name;
		this.age = age;
		this.address = address;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	}

The following is the actual operation of some of the examples show
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Released nine original articles · won praise 17 · views 1092

Guess you like

Origin blog.csdn.net/qq_41821963/article/details/86692810