Java项目:用集合实现学生管理系统

package com.zzu.Test;

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

import com.zzu.vo.Student;

public class Main {
	
	
	private static ArrayList<Student> list = new ArrayList<Student>();
	

	public static void main(String[] args) {
	    System.out.println("*********************************");
	    System.out.println("*\t\t\t\t*");
	    System.out.println("*\t欢迎使用学生信息管理系统\t*");
	    System.out.println("*\t\t\t\t*");
	    System.out.println("*********************************");
	    while (true) {
	      menu();
	    }
	  }

	  static void menu() {
		  System.out.println("1、添加学生信息");
		  System.out.println("2、删除学生信息");
		  System.out.println("3、修改学生信息");//地址传递
		  System.out.println("4、查询学生信息");//name
		  System.out.println("请输入操作,以Enter键结束:");
		  Scanner scanner = new Scanner(System.in);
		  int option  = scanner.nextInt();
	  switch (option) {
      	case 1:{
      		System.out.println("Please enter student's id,name,mobile,address");
      		System.out.println("id");
      		String id = scanner.next();
      		System.out.println("name");
      		String name= scanner.next();
      		System.out.println("mobile");
      		String mobile = scanner.next();
      		System.out.println("address");
      		String address = scanner.next();
      		Student student = new Student(id,name,mobile,address);
      		if(list.contains(student)) {
      			System.out.println("The student is already exist");
      			return;
      		}
      		
      		if(list.add(student)) {
      			System.out.println("sucess");
      			return;
      		}
      		System.out.println("error");
      		break;
      	}
      case 2:{
    	  System.out.println("Please enter a student's id");
    	  String id = scanner.next();
    	  Student student = new Student(id);
    	  if(!list.contains(student)) {
    		  System.out.println("The student is not exist");
    		  return;
    	  }
    	  if(list.contains(student)){
    		  System.out.println("delete sucess");
    		  return;
    	  }
    	  System.out.println("delete error");
    	  break;
      }
    	 
      case 3:{
    	  System.out.println("Please enter a student's id");
    	  String id = scanner.next();
    	  Student student = new Student(id);
    	  if(!list.contains(student)) {
    		  System.out.println("The student is not existing");
    		  return;
    	  }
    	  for(Student stu:list) {
    		  if(id.equals(stu.getId())) {
    	      		System.out.println(" new name");
    	      		String name= scanner.next();
    	      		System.out.println("new mobile");
    	      		String mobile = scanner.next();
    	      		System.out.println("new address");
    	      		String address = scanner.next();
    	      		stu.setName(name);
    	      		stu.setMobile(mobile);
    	      		stu.setAddress(address);
    		  }	
    	  }
    	  break;
      }
      case 4:{
    	  System.out.println("Please enter a student's id");
    	  String id = scanner.next();
    	  Student student = new Student(id);
    	  if(!list.contains(student)) {
    		  System.out.println("The student is not existing");
    		  return;
    	  }
    	  for(Student stu:list) {
    		  if(id.equals(stu.getId())) {
    			  System.out.println(stu);
    		  }
    	  }
    	  break;
      }
      default:
        System.out.println("I'm Sorry,there is not the "+option+" option,please try again.");
	  					}		
	  				}			
	}
package com.zzu.vo;

public class Student {

	String id;
	String name;
	String mobile;
	String address;
	
	
	
	
	
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", mobile=" + mobile + ", address=" + address + "]";
	}


	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Student) {
			Student student = (Student)obj;
			return this.id.equals(student.id);
		}
		return false;
	}


	public Student() {
		super();
	}
	

	public Student(String id) {
		this.id = id;
	}


	public Student(String id, String name, String mobile, String address) {
		super();
		this.id = id;
		this.name = name;
		this.mobile = mobile;
		this.address = address;
	}

	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getMobile() {
		return mobile;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
}

你可能感兴趣的:(学生管理系统的实现,Java中级)