学生管理系统,实现学生的增删改查

先建立一个学生类:

package com.inherit;
public class Student {
    String name;
    String studentId;
    String gender;
    int grade;
    public Student(String name,String studentId,String gender,int grade){
        this.name= name;
        this.studentId= studentId;
        this.gender = gender;
        this.grade = grade;    
    }
    public int getGrade(){
        return grade;
    }
    public String getName(){
        return name;
    }
    public String getgender(){
        return gender;
    }
    public void setGrade(int g){
        this.grade = g;
    }
    public String getStudentId(){
        return studentId;
    }
    public static void choice() {
        // TODO Auto-generated method stub
    }
}


建立测试类:
package com.inherit;
import java.io.ObjectInputStream.GetField;
import java.util.*;
/**
 * 
 * @author 张泽
 *
 */
public class TestStudent {

    static List StudentList = new LinkedList();
    public static void main(String[] agrs){

        System.out.println("***************");
        System.out.println("*欢迎来到学生管理系统 *");
        String password = "562300";
        System.out.println("请输入密码:");
        Scanner sm = new Scanner(System.in);
        String password1 = sm.nextLine();
        if(password1.equals(password)){
                    Menu();        

    }else{
        System.out.println("你输入的密码错误!请重新运行!");

    }
    }

    private static void Menu(){
        System.out.println("1:增加学生    2:删除学生     3:修改成绩     4:查询成绩      5:显示全部学生     6:退出 ");
        System.out.println("您想选择的操作是:");
        Scanner ss = new Scanner(System.in);
        int choice = ss.nextInt();        
        switch(choice){

        //增加学生
        case 1:
            System.out.print("请输入学生的姓名:");
            Scanner Sname = new Scanner(System.in);
            String name = Sname.nextLine();
            System.out.print("请输入学生的性别:");
            Scanner Ssex = new Scanner(System.in);
            String gender = Ssex.nextLine();
            System.out.print("请输入学生的学号:");
            Scanner SId = new Scanner(System.in);
            String studentId = SId.nextLine();
            System.out.print("请输入学生的成绩:");
            Scanner Sgrade = new Scanner(System.in);
            int grade = Sgrade.nextInt();
            StudentList.add(new Student(name,studentId,gender,grade));
            System.out.println(" 学生个数有:"+StudentList.size());
            System.out.println("添加成功!!!!!");
            Menu();

          break;
        //删除学生成绩
        case 2:
            System.out.print("请告诉我需要删除学生的学号:");
            Scanner Sid = new Scanner(System.in);
            String SstudentId = Sid.nextLine();
            boolean isfindDelete = false;
            for (int i = 0; i < StudentList.size(); i++) {
                if(SstudentId.equals(StudentList.get(i).getStudentId())){
                    System.out.println("发现了该学生,正在删除...");
                    StudentList.remove(i);
                    System.out.println("删除成功!!!");
                    isfindDelete =true;
                }
            }
            if(!isfindDelete){
                System.out.println("抱歉,没有找到");
            }
            Menu();
            break;
        //修改学生成绩
        case 3:
            System.out.print("请告诉我需要修改成绩学生的学号:");
            Scanner GId = new Scanner(System.in);
            String GstudentId = GId.nextLine();
            boolean isfindChange = false;
            for (int j = 0; j < StudentList.size(); j++) {
                if(GstudentId.equals(StudentList.get(j).getStudentId())){
                    System.out.println("发现了该学生,正在修改...");
                    System.out.println("学生原成绩为"+StudentList.get(j).getGrade());
                    System.out.print("请输入修改后学生的成绩:");
                    Scanner Ggrade = new Scanner(System.in);
                    int grade2 = Ggrade.nextInt();
                    StudentList.get(j).setGrade(grade2);
                    System.out.println("修改成功!!!");
                    isfindChange =true;
                }else{

                }
            }
            if(!isfindChange){
                System.out.println("抱歉,没有找到");
            }
            Menu();
            break;
        //查看学生成绩
        case 4:
            System.out.print("请告诉我需要查询学生的学号:");
            Scanner CId = new Scanner(System.in);
            String CstudentId = CId.nextLine();
            boolean isfindData = false;
            for (int i = 0; i < StudentList.size(); i++) {
                if(CstudentId.equals(StudentList.get(i).getStudentId())){
                    System.out.println("姓名\t"+"性别\t"+"学号\t"+"成绩\t");
                    System.out.println(StudentList.get(i).getName()+"\t"+StudentList.get(i).getgender()+"\t"+StudentList.get(i).getStudentId()+"\t"+StudentList.get(i).getGrade());
                    isfindData = true;

                }
            }
            if(!isfindData){
            System.out.println("抱歉,没有找到");
            }
            Menu();
            break;
        default:
            System.out.println("您输入的数字有误,请重新输入:");
            break;

            //显示全部学生
        case 5:
            System.out.println("名册里有学生:"+StudentList.size());
            System.out.println("学生\t"+"性别\t"+"学号\t"+"年龄\t");
            for (int i = 0; i < StudentList.size(); i++) {
                Student s = StudentList.get(i);
                System.out.println(s.getName()+"\t"+s.getgender()+"\t"+s.getStudentId()+"\t"+s.getGrade()+"\t");
            }
            Menu();
            break;
          //退出
        case 6:
            System.out.println("谢谢使用,再见!");
            System.exit(choice);

            }
        }
    }

你可能感兴趣的:(Java程序练习)