Java实现学生管理系统(ArrayList)

功能:

1、添加学生信息
2、删除学生信息
3、修改学生信息
4、查询学生信息

代码:

Main:

package main;

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

import 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("请输入学号:");
    	String id = scanner.next();
    	Student student1 = new Student(id);
    	if(list.contains(student1)) {
    		System.out.println("学号不允许重复");
    		System.out.println("添加失败");
    		return;
    	}
    	System.out.println("请输入姓名:");
    	String name = scanner.next();
    	System.out.println("请输入手机号:");
    	String mobile = scanner.next();
    	System.out.println("请输入地址:");
    	String adress = scanner.next();
    	Student student = new Student(id,name,mobile,adress);
    	list.add(student);
    	System.out.println("添加成功");
        break;
      }
      case 2:{
        System.out.println("请输入学号");
    	String id = scanner.next();
    	Student student1 = new Student(id);
/*      	if(!list.contains(student1)) {
      		System.out.println("学号不存在,删除失败");
      		return;
      	} */
      	//remove方法删除成功则元素存在,删除失败则元素不存在
    	if(list.remove(student1)) {
    		System.out.println("删除成功");
    	}else {
    		System.out.println("删除失败");
    	}
        break;
      }
      case 3:{
    	System.out.println("请输入学号");
      	String id = scanner.next();
      	Student student1 = new Student(id);
      	if(!list.contains(student1)) {
      		System.out.println("学号不存在,修改失败");
      		return;
      	}  
    	for(Student stu : list) {
    		if(id.equals(stu.getId())) {
				System.out.println("请选择修改选项:");
				System.out.println("1、姓名");
				System.out.println("2、手机号码");
				System.out.println("3、家庭住址");
				System.out.println("4、除学号以外的信息");
				int m = scanner.nextInt();
				switch (m) {
				case 1:
					stu.setName(scanner.next());
					System.out.println("修改成功!");
					break;
				case 2:
					stu.setMobile(scanner.next());
					System.out.println("修改成功!");
					break;
				case 3:
					stu.setAddress(scanner.next());
					System.out.println("修改成功!");
					break;
				case 4:
					stu.setName(scanner.next());
					stu.setMobile(scanner.next());
					stu.setAddress(scanner.next());
					System.out.println("修改成功!");
    		}
    	}
      }
    	break;
      }
      case 4:{
        System.out.println("请输入学号");
    	String id = scanner.next();
    	Student student1 = new Student(id);
    	if(!list.contains(student1)) {
    		System.out.println("学号不存在,查询失败");
    		return;
    	}
    	for(Student stu : list) {
    		if(id.equals(stu.getId())) {
    			System.out.println(stu);
    		}
    	}
        break;
      }
      default:
        System.out.println("对不起,没有找到"+option+"选项,请重试");
    }
    
  }
}

Student:

package 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)