[Java案例]学生管理系统

环境

jdk 1.8

[Java案例]学生管理系统

开发工具-eclipse ide 2018-12

[Java案例]学生管理系统

目录

[Java案例]学生管理系统

代码

  • Student.java
package manger;

public class Student {
    private String sno;
    private String name;
    private String xueyuan;
    private String zhuanye;
    private String nianji;
    private String pwd;
    public String getSno() {
        return sno;
    }
    public void setSno(String sno) {
        this.sno = sno;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getXueyuan() {
        return xueyuan;
    }
    public void setXueyuan(String xueyuan) {
        this.xueyuan = xueyuan;
    }
    public String getZhuanye() {
        return zhuanye;
    }
    public void setZhuanye(String zhuanye) {
        this.zhuanye = zhuanye;
    }
    public String getNianji() {
        return nianji;
    }
    public void setNianji(String nianji) {
        this.nianji = nianji;
    }
    public void print_self()
    {
        System.out.println("学号:"+sno+" 姓名:"+name+" 学院:"+xueyuan+" 专业:"+zhuanye+" 年级:"+nianji);
    }
}
  • Award.java
package manger;

public class Award {
    private String sno;
    private String date;
    private String name;
    private String level;
    private String teacher;
    private String department;
    public String getSno() {
        return sno;
    }
    public void setSno(String sno) {
        this.sno = sno;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getLevel() {
        return level;
    }
    public void setLevel(String level) {
        this.level = level;
    }
    public String getTeacher() {
        return teacher;
    }
    public void setTeacher(String teacher) {
        this.teacher = teacher;
    }
    public String getDepartment() {
        return department;
    }
    public void setDepartment(String department) {
        this.department = department;
    }
    public void show()
    {
        System.out.println(sno+" "+date+" "+name+" "+level+" "+teacher+" "+department);
    }
}
  • Teacher.java

package manger;

public class Teacher {
    private String tno;
    private String name;
    private String xueyuan;
    private String pwd;
    public String getTno() {
        return tno;
    }
    public void setTno(String tno) {
        this.tno = tno;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getXueyuan() {
        return xueyuan;
    }
    public void setXueyuan(String xueyuan) {
        this.xueyuan = xueyuan;
    }
    public void print_self()
    {
        System.out.println(tno+"\t"+name+"\t"+xueyuan);
    }
    
}
  • Main.java
package manger;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    private static ArrayList<String> list = new ArrayList<String>();
    private static ArrayList<String> list_teacher = new ArrayList<String>();
    private static ArrayList<String> award_list = new ArrayList<String>();
    private static ArrayList<Student> students = new ArrayList<Student>();
    private static ArrayList<Teacher> teachers = new ArrayList<Teacher>();
    private static ArrayList<Award> awards = new ArrayList<Award>();
    static String student_file = "C:\\Users\\sd\\Documents\\hanaworkspace\\manger\\src\\manger\\student.txt";
    static String teacher_file = "C:\\Users\\sd\\Documents\\hanaworkspace\\manger\\src\\manger\\teacher.txt";
    static String award_file = "C:\\Users\\sd\\Documents\\hanaworkspace\\manger\\src\\manger\\award.txt";
    private static Scanner in;
    static void selectOption()
    {
        System.out.println("-----学生获奖信息收集系统------");
        System.out.println("-----1.学生登陆------");
        System.out.println("-----2.辅导员登陆------");
        System.out.println("-----0.退出------");
    }
    //加载文件数据到内存中
    static void load(String filePath) throws IOException 
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(  
                new FileInputStream(filePath)));  
  
        for (String line = br.readLine(); line != null; line = br.readLine()) {  
            list.add(line);
            String[] arrOfStr = line.split("\t");
            Student s= new Student();
            s.setSno(arrOfStr[0]);
            s.setName(arrOfStr[1]);
            s.setXueyuan(arrOfStr[2]);
            s.setZhuanye(arrOfStr[3]);
            s.setNianji(arrOfStr[4]);
            s.setPwd(arrOfStr[5]);
            students.add(s);
        }  
        br.close();  
       
    }
    //加载文件数据到内存中
    static void load_teacher(String filePath) throws IOException 
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(  
                new FileInputStream(filePath)));  
  
        for (String line = br.readLine(); line != null; line = br.readLine()) {  
            list.add(line);
            String[] arrOfStr = line.split("\t");
            Teacher s= new Teacher();
            s.setTno(arrOfStr[0]);
            s.setName(arrOfStr[1]);
            s.setXueyuan(arrOfStr[2]);
            s.setPwd(arrOfStr[3]);
            teachers.add(s);
        }  
        br.close();  
       
    }
    static void student_menu()
    {
        System.out.println("-----学生获奖信息收集系统_学生------");
        System.out.println("-----1.查看个人信息-----");
        System.out.println("-----2.修改密码------");
        System.out.println("-----3.添加个人获奖信息------");
        System.out.println("-----4.查看个人获奖信息------");
        System.out.println("-----5.删除个人获奖信息------");
        System.out.println("-----6.修改个人获奖信息------");
        
        System.out.println("-----0.返回上一菜单------");
    }
    static void teacher_menu()
    {
        System.out.println("-----学生获奖信息收集系统_老师------");
        System.out.println("-----1.查看个人信息-----");
        System.out.println("-----2.修改密码------");
        System.out.println("-----3.添加学生获奖信息------");
        System.out.println("-----4.查看学生获奖信息------");
        System.out.println("-----5.删除学生获奖信息------");
        System.out.println("-----6.修改学生获奖信息------");
        
        System.out.println("-----0.返回上一菜单------");
    }
    //显示学生信息
    static void show_student_info(String sno)
    {
        for(Student s:students)
        {
            if(sno.equals(s.getSno()))
            {
                s.print_self();
                break;
            }
        }
    }
    //显示老师的信息
    static void show_teacher_info(String tno)
    {
        for(Teacher s:teachers)
        {
            if(tno.equals(s.getTno()))
            {
                s.print_self();
                break;
            }
        }
    }
    //更新密码
    static void update(String sno_ ,String pwd) throws FileNotFoundException, IOException
    {
        FileOutputStream fos=new FileOutputStream(new File(student_file));
        OutputStreamWriter osw=new OutputStreamWriter(fos);
        BufferedWriter  bw=new BufferedWriter(osw);
        String info;
        for(Student s:students)
        {
            String sno = s.getSno();
            String name = s.getName();
            String xueyuan = s.getXueyuan();
            String zhuanye = s.getZhuanye();
            String nianji = s.getNianji();
            if(sno_.equals(s.getSno())) {
                info = sno + "\t"+name +"\t"+xueyuan+"\t"+zhuanye+"\t"+nianji+"\t"+pwd;
            }
            else
            {
                String password = s.getPwd();
                info = sno + "\t"+name +"\t"+xueyuan+"\t"+zhuanye+"\t"+nianji+"\t"+password;
            }
            bw.write(info+"\t\n");
        }
         bw.close();
         osw.close();
         fos.close();
    }
    //更新密码
    static void update_teacher(String tno_ ,String pwd) throws FileNotFoundException, IOException
    {
        FileOutputStream fos=new FileOutputStream(new File(teacher_file));
        OutputStreamWriter osw=new OutputStreamWriter(fos);
        BufferedWriter  bw=new BufferedWriter(osw);
        String info;
        for(Teacher s:teachers)
        {
            String tno = s.getTno();
            String name = s.getName();
            String xueyuan = s.getXueyuan();
            if(tno_.equals(s.getTno())) {
                info = tno + "\t"+name +"\t"+xueyuan+"\t"+pwd;
            }
            else
            {
                String password = s.getPwd();
                info = tno + "\t"+name +"\t"+xueyuan+"\t"+password;
            }
            bw.write(info+"\t\n");
        }
         bw.close();
         osw.close();
         fos.close();
    }
    //更新密码
    static void update_pwd(String sno,String old_pwd) throws IOException
    {
        do
        {
            String pwd;
            System.out.println("请输入新密码");
            pwd = in.next();
            if(old_pwd.equals(pwd))
            {
                System.out.println("原密码与新密码相同,请重新输入");
            }
            else
            {
                System.out.println("更新密码");
                update(sno,pwd);
                break;
            }
        }while(true);
        
        
    }
    //更新密码
    static void update_teacher_pwd(String tno,String old_pwd) throws IOException
    {
        do
        {
            String pwd;
            System.out.println("请输入新密码");
            pwd = in.next();
            if(old_pwd.equals(pwd))
            {
                System.out.println("原密码与新密码相同,请重新输入");
            }
            else
            {
                System.out.println("更新密码");
                update_teacher(tno,pwd);
                break;
            }
        }while(true);
        
        
    }
    
    static void add_award(String sno) throws IOException
    {
        FileWriter writer = new FileWriter(award_file, true); 
        BufferedReader buf = null;
        buf = new BufferedReader(new InputStreamReader(System.in));
        String date;
        String name;
        String level;
        String teacher;
        String department;
        System.out.println("请输入获奖时间:");
        date= buf.readLine();
        
        System.out.println("请输入奖项名称:");
        name= buf.readLine();

        System.out.println("请输入获奖等级:");
        level= buf.readLine();
        System.out.println("请输入指导老师:");
        teacher= buf.readLine();
        System.out.println("请输入授奖部门:");
        department= buf.readLine();
        
        System.out.println(date);
        System.out.println(name);
        System.out.println(level);
        System.out.println(teacher);
        System.out.println(department);
        
        Award award = new Award();
        award.setSno(sno);
        award.setDate(date);
        award.setName(name);
        award.setLevel(level);
        award.setTeacher(teacher);
        award.setDepartment(department);
        String info;
        info = sno + "\t"+date +"\t"+name+"\t"+level+"\t"+teacher+"\t"+department;
        
        awards.add(award);
        writer.write(info+"\n");  
        writer.close();  
    }
    
    static void print_list()
    {
        for(int i = 0;i < list.size(); i ++){
            System.out.println(list.get(i));
        }
    }
    static void show_award(String sno) throws IOException
    {
        for(Award a:awards) {
            if(a.getSno().equals(sno))
            {
                a.show();
            }
        }
    }
    static void show_all_award() throws IOException
    {
        for(Award a:awards) {
            a.show();
        }
    }
    static void load_award() throws IOException
    {
        awards.clear();
        BufferedReader br = new BufferedReader(new InputStreamReader(  
                new FileInputStream(award_file)));  
  
        for (String line = br.readLine(); line != null; line = br.readLine()) {  
            award_list.add(line);
            
            String[] arrOfStr = line.split("\t");
            Award award = new Award();
            award.setSno(arrOfStr[0]);
            award.setDate(arrOfStr[1]);
            award.setName(arrOfStr[2]);
            award.setLevel(arrOfStr[3]);
            award.setTeacher(arrOfStr[4]);
            award.setDepartment(arrOfStr[5]);
            
            awards.add(award);
        }  
        br.close();  
    }
    static void save_awards() throws IOException
    {
        FileWriter writer = new FileWriter(award_file, false); 
        
        for(Award a:awards) {
            String info =null;
            info = a.getSno()+"\t"+a.getDate()+"\t"+a.getName()+"\t"+a.getLevel()+"\t"+a.getTeacher()+"\t"+a.getDepartment();
            writer.write(info+"\n");  
        }
        writer.close();  
    }
    static void delete_award(int del)
    {
        int step =1;
        Award dela=null;
        for(Award a:awards) {
            if(del==step) {
                dela=a;
            }
            step++;
        }
        if(dela!=null)
        {
            awards.remove(dela);
            try {
                save_awards();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    static void update_award_info(String sno)
    {
        try {
            show_award(sno);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("enter you need to update record:");
        int choice = in.nextInt();
        
        int step =1;
        Award dela=null;
        for(Award a:awards) {
            if(choice==step) {
                dela=a;
            }
            step++;
        }
        if(dela!=null)
        {
            System.out.println("请输入新的获奖项:");
            String name = in.next();
            dela.setName(name);
            try {
                save_awards();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    static void show_student_info()
    {
        for(Student s:students) {
            s.print_self();
        }
    }
    public static void main(String[] args){
        
        int choice = 2;
        try {
            load(student_file);
            load_teacher(teacher_file);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        in = new Scanner(System.in);
        do
        {
            selectOption();
            
            choice = in.nextInt();
            System.out.println(choice);
            
            //System.out.println(choice);
            if(choice== 1)//学生登陆
            {
                String sno ;
                String pwd;
                System.out.println("请输入学号:");
                sno=in.next();
                System.out.println("请输入密码:");
                pwd=in.next();
                
                System.out.println("student_login...");

                int flag =-1;
                //查找该学号和密码的正确性
                for(Student s:students)
                {
                    if(sno.equals(s.getSno()) && pwd.equals(s.getPwd()))
                    {
                        flag = 1;
                        System.out.println("欢迎进入");
                        break;
                    }
                    else
                    {//失败退出
                        flag = 0;
                        System.out.println("学号或密码错误");
                        break;
                    }
                }
                
                if(flag == 1)
                {
                    //正确,进入学生菜单
                    System.out.println("student menu");
                    int choice2 =0;
                    
                    do
                    {
                        student_menu();
                        System.out.print("enter choice:");
                        choice2 = in.nextInt();
                        System.out.println(choice2);
                        switch(choice2)
                        {
                        case 1://查看个人信息
                            show_student_info(sno);
                            break;
                        case 2://修改密码
                            try {
                                update_pwd(sno,pwd);
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            break;
                        case 3://添加个人获奖信息
                            try {
                                add_award(sno);
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            break;
                        case 4://查看个人获奖信息
                            try {
                                load_award();
                                show_award(sno);
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            
                            break;
                        case 5://删除个人获奖信息
                            try {
                                load_award();
                                show_award(sno);
                                System.out.println("请输入要删除的行号:");   
                                int del;
                                del=in.nextInt();
                                delete_award(del);
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            break;
                        case 6://修改个人获奖信息
                            update_award_info(sno);
                            break;
                        }
                    }while(choice2!=0);
                }
                else {
                    return;
                }
                
            }
            if(choice == 2)//辅导员登陆
            {
                String tno ;
                String pwd;
                System.out.println("请输入工号:");
                tno=in.next();
                System.out.println("请输入密码:");
                pwd=in.next();
                
                System.out.println("teacher_login...");

                int flag =-1;
                
                for(Teacher s:teachers)
                {
                    if(tno.equals(s.getTno()) && pwd.equals(s.getPwd()))
                    {
                        flag = 1;
                        System.out.println("欢迎进入");
                        break;
                    }
                    else
                    {//失败退出
                        flag = 0;
                        System.out.println("工号或密码错误");
                        break;
                    }
                }
                
                if(flag == 1)
                {
                    //正确,进入教师菜单
                    System.out.println("tacher menu");
                    int choice2 =0;
                    
                    do
                    {
                        teacher_menu();
                        System.out.print("enter choice:");
                        choice2 = in.nextInt();
                        System.out.println(choice2);
                        switch(choice2)
                        {
                        case 1://查看个人信息
                            show_teacher_info(tno);
                            break;
                        case 2://修改密码
                            try {
                                update_teacher_pwd(tno,pwd);
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            break;
                        case 3://添加学生获奖信息
                            try {
                                show_student_info();
                                System.out.println("请输入学生学号:");
                                String sno = in.next();
                                add_award(sno);
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            break;
                        case 4://查看学生获奖信息
                            try {
                                load_award();
                                show_all_award();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            
                            break;
                        case 5://删除学生获奖信息
                            try {
                                load_award();
                                show_all_award();
                                System.out.println("请输入要删除的行号:");
                                int del;
                                del=in.nextInt();
                                delete_award(del);
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            break;
                        case 6://修改学生获奖信息
                            try {
                                load_award();
                                show_all_award();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            System.out.println("请输入学生学号:");
                            String sno = in.next();
                            update_award_info(sno);
                            break;
                        }
                    }while(choice2!=0);
                }
                else {
                    return;
                }
                
            }
        }while(choice!=0);
        in.close();
    }

}
上一篇:MySQL 其他基础知识


下一篇:学习数据库概论第六天