Java项目:学生管理系统(ArrayList)

项目要求

能够实现以下四种需求:
1、添加学生信息
2、删除学生信息
3、修改学生信息
4、查询学生信息

代码展示

main类:

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();
	    }
    }

	@SuppressWarnings("unlikely-arg-type")
	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("请输入学生信息:");
		    	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)) {//获取学生id,判断学号是否重复。
		    		System.out.println("学号不允许重复");
		    		return;
		    	}
		    	if(list.add(student)) {
		    		System.out.println("添加成功");
		    		break;
		    	}
		    	System.out.println("添加失败");
		    	break;
	    	}
	    	  
	    	case 2:{
	    		System.out.println("请输入学生学号:");
	    		String id = scanner.next();
	    		Student student = new Student (id);
	    		if( ! list.contains(student)) {
	    			System.out.println("学生不存在,删除失败");
	    			return;
	    		}
	    		if(list.remove(student)) {
	    			System.out.println("删除成功");
	    			return;
	    		}
	    		System.out.println("删除失败");
	    		break;
	    	}

	        case 3:{
	        	System.out.println("请输入学生学号:");
	    		String id = scanner.next();
	    		Student student = new Student (id);
	    		for(Student stu : list) {
	    			if(id.equals(stu.getId())) {
	    				System.out.println("请输入学生name");
	    		    	String name = scanner.next();
	    		    	System.out.println("请输入学生mobile");
	    		    	String mobile = scanner.next();
	    		    	System.out.println("请输入学生address");
	    		    	String address = scanner.next();
	    		    	stu.setName(name);
	    		    	stu.setAddress(address);
	    		    	stu.setMobile(mobile);
	    		    	System.out.println("修改成功");
	    		    	return;
	    			}
	    		}
	        	System.out.println("未找到学生,修改失败");
	        	break;
	        }
	        	
	        case 4:{
	        	System.out.println("请输入学生学号:");
	    		String id = scanner.next();
	    		Student student = new Student (id);
	    		if(!list.contains(student)) {
	    			System.out.println("学生不存在,查询失败");
	    			return;
	    		}
	    		for(Student stu : list) {
//	    			if(id.equals(stu.getId()))
//	    				System.out.println(stu);
	    			if(stu.equals(student))
	    				System.out.println(stu);
	    			return;
	    		}
	        	break;
	        }
	        default:
	        	System.out.println("I'm Sorry,there is not the "+option+" option,please try again.");
	      }
	}
}

Student类:

package com.zzu.vo;

public class Student {

	private String id;
	private String name;
	private String mobile;
	private String address;
	
	public Student() {
	}
	
	public Student(String id) {
		this.id = id;
	}
	
	public Student(String id, String name, String mobile, String address) {
		this.id = id;
		this.name = name;
		this.mobile = mobile;
		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 getMobile() {
		return mobile;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}

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

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

你可能感兴趣的:(Java项目)