├── README.md ├── data ├── 2800 │ └── 777.txt ├── 4396 │ └── 765.txt ├── 5555 │ └── 423.txt ├── 7777 │ └── 4333.txt └── 9999 │ └── 23.txt ├── src └── com │ └── ceshi │ ├── ChangePassword.java │ ├── ChangeStudent.java │ ├── Classmates.java │ ├── DeleteStudent.java │ ├── GUI.java │ ├── Get5Students.java │ ├── GetAllStudents.java │ ├── LoginForm.java │ ├── MainFrame.java │ ├── PasswordChange.java │ ├── Student.java │ ├── User.java │ ├── addStudent.java │ └── niubi.java └── user.txt /README.md: -------------------------------------------------------------------------------- 1 | # java-course 2 | Java课程设计作业,带有图形化界面的学生信息管理系统 3 | 作业要求如下: 4 | 课程大作业一:班级信息管理系统 5 | 要求:设计一个班级信息管理系统,该系统被拆分为以下子任务: 6 | + 设计个人的详细信息,信息采用变量声明和赋值的方式,至少包含6 项基本信息4 种以上数据类型,根据身高体重判断胖瘦情况。 7 | + 根据个人的详细信息,抽象出一个学生类,并通过测试类创建个人对象,并调用一个输出方法输出个人的详细信息。 8 | + 在测试类中创建一个学生类数组,汇总全班所有同学的信息放入数组中,通过数组输出所有同学的详细信息。 9 | + 输入一位同学的学号和姓名信息,判断该同学是否在学生数组中,若是,输出该同学的详细信息,若否,输出提示信息。 10 | + 在测试类中随机从学生数组中抽取5 位同学,并输出他们的详细信息。 11 | + 将学生类中的基本信息全部私有化,提供构造方法以及setter 和getter 方法,修改对应测试类中的相关语句。 12 | + 根据对学生数组的操作,抽象出一个班级类,包含根据学号和姓名信息验证方法、获取所有同学信息和随机获取5位同学信息的方法,在测试类中创建一个班级对象并测试3种方法。 13 | + 写一个图形界面程序,包含登陆窗口,主窗口,查看菜单以及查看所有同学和随机同学信息菜单项以及对应的窗口。 14 | + 在图形界面程序中整合班级信息管理功能,完成登陆功能,使用表格格式显示学生信息。 15 | + 完善登陆功能,增加输入校验和异常处理。 16 | + 增加用户类,采用用户和密码登录的方式,增加用户密码修改功能。 17 | + 增加学生成绩信息,至少包含10 门课程成绩信息。在测试类中随机从学生数组中抽取5 位同学,并输出他们的详细信息。 18 | + 数据由数组存放变为采用文件存储、读取和写入的方式。将学生类中的基本信息全部私有化,提供构造方法以及setter 和getter 方法,修改对应测试类中的相关语句。 19 | + 增加学生信息和课程成绩的添加、修改和删除功能。根据对学生数组的操作,抽象出一个班级类,包含根据学号和姓名信息验证方法、获取所有同学信息和随机获取5位同学信息的方法,在测试类中创建一个班级对象并测试3种方法。 20 | + 多种样式的学生和课程成绩查询和统计方式。 21 | + 除上述要求外,可自行设计并增加班级信息管理系统功能。 22 | -------------------------------------------------------------------------------- /data/2800/777.txt: -------------------------------------------------------------------------------- 1 | 2800 2 | 777 3 | uzi 4 | 23 5 | 177 6 | 166 7 | 10 8 | 20 9 | 30 10 | 40 11 | 50 12 | 60 13 | 70 14 | 80 15 | 90 16 | 100 17 | -------------------------------------------------------------------------------- /data/4396/765.txt: -------------------------------------------------------------------------------- 1 | 4396 2 | 765 3 | www 4 | 123 5 | 21 6 | 32 7 | 10 8 | 20 9 | 30 10 | 23 11 | 66 12 | 45 13 | 34 14 | 23 15 | 87 16 | 54 17 | -------------------------------------------------------------------------------- /data/5555/423.txt: -------------------------------------------------------------------------------- 1 | 5555 2 | 423 3 | 121 4 | 12 5 | 21 6 | 12 7 | 43 8 | 32 9 | 32 10 | 43 11 | 12 12 | 1 13 | 65 14 | 45 15 | 34 16 | 23 17 | -------------------------------------------------------------------------------- /data/7777/4333.txt: -------------------------------------------------------------------------------- 1 | 7777 2 | 4333 3 | 明凯 4 | 12 5 | 32 6 | 12 7 | 12 8 | 12 9 | 21 10 | 22 11 | 33 12 | 4 13 | 23 14 | 32 15 | 12 16 | 65 17 | -------------------------------------------------------------------------------- /data/9999/23.txt: -------------------------------------------------------------------------------- 1 | 9999 2 | 23 3 | 123 4 | 231 5 | 23 6 | 43 7 | 23 8 | 43 9 | 54 10 | 54 11 | 34 12 | 23 13 | 4543 14 | 23 15 | 12 16 | 1 17 | -------------------------------------------------------------------------------- /src/com/ceshi/ChangePassword.java: -------------------------------------------------------------------------------- 1 | package com.ceshi; 2 | 3 | import javax.swing.*; 4 | 5 | public class ChangePassword extends JFrame { 6 | String password; 7 | 8 | public ChangePassword() { 9 | password = JOptionPane.showInputDialog("请输入新密码"); 10 | } 11 | 12 | public String getPassword() { 13 | return password; 14 | } 15 | 16 | public static void main(String[] args) { 17 | ChangePassword frame = new ChangePassword(); 18 | 19 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 20 | frame.setLocationRelativeTo(null); 21 | //frame.setSize(1000,300); 22 | frame.pack(); 23 | frame.setVisible(true); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/com/ceshi/ChangeStudent.java: -------------------------------------------------------------------------------- 1 | package com.ceshi; 2 | 3 | import javax.swing.*; 4 | import java.awt.*; 5 | import java.awt.event.ActionEvent; 6 | import java.awt.event.ActionListener; 7 | 8 | //班级号 学号 姓名 年龄 体重 身高 9 | 10 | public class ChangeStudent extends JFrame { 11 | private JPanel jp1, jp2, jp3, jp4, jp5, jp6, jp7; 12 | private JPanel grade1, grade2; 13 | private JLabel jl1, jl2, jl3, jl4, jl5, jl6; 14 | private JLabel g1,g2,g3,g4,g5,g6,g7,g8,g9,g10; 15 | private JTextField jtf1, jtf2, jtf3, jtf4, jtf5, jtf6; 16 | private JTextField f1,f2,f3,f4,f5,f6,f7,f8,f9,f10; 17 | private JButton okButton = new JButton("确认"); 18 | private JButton cancelButton = new JButton("取消"); 19 | 20 | public ChangeStudent() { 21 | 22 | this.setLayout(new GridLayout(9, 1)); 23 | jp1 = new JPanel(); 24 | jl1 = new JLabel("班级号:"); 25 | jtf1 = new JTextField(20); 26 | jp1.add(jl1); 27 | jp1.add(jtf1); 28 | this.add(jp1); 29 | 30 | jp2 = new JPanel(); 31 | jl2 = new JLabel("学号:"); 32 | jtf2 = new JTextField(20); 33 | jp2.add(jl2); 34 | jp2.add(jtf2); 35 | this.add(jp2); 36 | 37 | jp3 = new JPanel(); 38 | jl3 = new JLabel("姓名:"); 39 | jtf3 = new JTextField(20); 40 | jp3.add(jl3); 41 | jp3.add(jtf3); 42 | this.add(jp3); 43 | 44 | jp4 = new JPanel(); 45 | jl4 = new JLabel("年龄:"); 46 | jtf4 = new JTextField(20); 47 | jp4.add(jl4); 48 | jp4.add(jtf4); 49 | this.add(jp4); 50 | 51 | jp5 = new JPanel(); 52 | jl5 = new JLabel("体重:"); 53 | jtf5 = new JTextField(20); 54 | jp5.add(jl5); 55 | jp5.add(jtf5); 56 | this.add(jp5); 57 | 58 | jp6 = new JPanel(); 59 | jl6 = new JLabel("身高:"); 60 | jtf6 = new JTextField(20); 61 | jp6.add(jl6); 62 | jp6.add(jtf6); 63 | this.add(jp6); 64 | 65 | grade1 = new JPanel(); 66 | g1 = new JLabel("高等数学:"); 67 | f1 = new JTextField(3); 68 | g2 = new JLabel("数据结构:"); 69 | f2 = new JTextField(3); 70 | g3 = new JLabel("JAVA:"); 71 | f3 = new JTextField(3); 72 | g4 = new JLabel("离散数学·:"); 73 | f4 = new JTextField(3); 74 | g5 = new JLabel("编译原理:"); 75 | f5 = new JTextField(3); 76 | grade1.add(g1); 77 | grade1.add(f1); 78 | grade1.add(g2); 79 | grade1.add(f2); 80 | grade1.add(g3); 81 | grade1.add(f3); 82 | grade1.add(g4); 83 | grade1.add(f4); 84 | grade1.add(g5); 85 | grade1.add(f5); 86 | this.add(grade1); 87 | 88 | grade2 = new JPanel(); 89 | g6 = new JLabel("大学物理:"); 90 | f6 = new JTextField(3); 91 | g7 = new JLabel("计算机组成原理:"); 92 | f7 = new JTextField(3); 93 | g8 = new JLabel("操作系统:"); 94 | f8 = new JTextField(3); 95 | g9 = new JLabel("计算机网络·:"); 96 | f9 = new JTextField(3); 97 | g10 = new JLabel("线性代数:"); 98 | f10 = new JTextField(3); 99 | grade2.add(g6); 100 | grade2.add(f6); 101 | grade2.add(g7); 102 | grade2.add(f7); 103 | grade2.add(g8); 104 | grade2.add(f8); 105 | grade2.add(g9); 106 | grade2.add(f9); 107 | grade2.add(g10); 108 | grade2.add(f10); 109 | this.add(grade2); 110 | 111 | jp7 = new JPanel(); 112 | jp7.add(okButton); 113 | jp7.add(cancelButton); 114 | this.add(jp7); 115 | 116 | okButton.addActionListener(new ActionListener() { 117 | @Override 118 | public void actionPerformed(ActionEvent e) { 119 | String[] grade = {f1.getText(),f2.getText(),f3.getText(),f4.getText(),f5.getText(), 120 | f6.getText(),f7.getText(),f8.getText(),f9.getText(),f10.getText()}; 121 | 122 | niubi.addStudent(jtf1.getText(),jtf2.getText(),jtf3.getText(),jtf4.getText(),jtf5.getText(),jtf6.getText(),grade); 123 | dispose(); 124 | } 125 | }); 126 | 127 | 128 | cancelButton.addActionListener(new ActionListener() { 129 | @Override 130 | public void actionPerformed(ActionEvent e) { 131 | dispose(); 132 | } 133 | }); 134 | 135 | this.setTitle("添加学生信息"); 136 | this.setSize(580, 500); 137 | this.setLocationRelativeTo(null); 138 | this.setDefaultCloseOperation(EXIT_ON_CLOSE); 139 | this.setVisible(true); 140 | } 141 | 142 | 143 | 144 | } 145 | -------------------------------------------------------------------------------- /src/com/ceshi/Classmates.java: -------------------------------------------------------------------------------- 1 | package com.ceshi; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.File; 5 | import java.io.FileReader; 6 | import java.io.IOException; 7 | import java.util.ArrayList; 8 | 9 | public class Classmates { 10 | private ArrayList one_class_students; 11 | 12 | //构造方法 通过传递一个 班级号 来查找 班级文件夹 13 | public Classmates(String input_class_no) { 14 | String folder_name = "./data/"; 15 | File folder = new File(folder_name); 16 | String[] ClassList = folder.list(); 17 | 18 | one_class_students = new ArrayList<>(); 19 | 20 | for (int index = 0; index < ClassList.length; index++) { 21 | if (ClassList[index].equals(input_class_no)) { 22 | File classmate = new File(folder_name + ClassList[index]); 23 | String[] FileList = classmate.list(); 24 | for (int file_index = 0; file_index < FileList.length; file_index++) { 25 | //Mac OSX 环境下的 bUG 26 | if (FileList[file_index].equals(".DS_Store") ) 27 | continue; 28 | 29 | File file = new File(folder_name + ClassList[index] + "/" + FileList[file_index]); 30 | // System.out.println(FileList[file_index]); 输出文件名 31 | ArrayList arrayList = new ArrayList<>(); 32 | try { 33 | FileReader fr = new FileReader(file); 34 | BufferedReader bf = new BufferedReader(fr); 35 | String str; 36 | while ((str = bf.readLine()) != null) { 37 | arrayList.add(str); 38 | } 39 | } catch (IOException e) { 40 | e.printStackTrace(); 41 | } 42 | //读取学生 信息 43 | String class_no = arrayList.get(0); 44 | String id = arrayList.get((1)); 45 | String name = arrayList.get(2); 46 | String age = arrayList.get(3); 47 | String weight = arrayList.get(4); 48 | String height = arrayList.get(5); 49 | String[] grade = new String[10]; 50 | for (int i = 0; i < 10; i++) { 51 | grade[i] = arrayList.get(6+i); 52 | } 53 | Student student = new Student(class_no, id, name, age, weight, height, grade); 54 | one_class_students.add(student); 55 | } 56 | } 57 | } 58 | } 59 | 60 | //获取所有学生信息 61 | public void getAllstudentsInfo() { 62 | for (int i = 0; i < one_class_students.size(); i++) { 63 | System.out.println(one_class_students.get(i)); 64 | } 65 | } 66 | 67 | //获取随机5个学生信息 68 | public void getRandom5studentsInfo() { 69 | int random_num = (int) (Math.random() * (one_class_students.size() - 5)); 70 | for (int i = random_num; i < random_num + 5; i++) { 71 | System.out.println(one_class_students.get(i)); 72 | } 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /src/com/ceshi/DeleteStudent.java: -------------------------------------------------------------------------------- 1 | package com.ceshi; 2 | 3 | import javax.swing.*; 4 | import java.awt.*; 5 | import java.awt.event.ActionEvent; 6 | import java.awt.event.ActionListener; 7 | 8 | public class DeleteStudent extends JFrame { 9 | 10 | private JPanel jp1,jp2; 11 | private JLabel j1; 12 | private JTextField jtf; 13 | private JButton okButton = new JButton("确认"); 14 | private JButton cancelButton = new JButton("取消"); 15 | 16 | public DeleteStudent() { 17 | this.setTitle("删除学生个人信息"); 18 | this.setSize(320, 200); 19 | this.setLocationRelativeTo(null); 20 | this.setDefaultCloseOperation(EXIT_ON_CLOSE); 21 | 22 | this.setLayout(new GridLayout(3, 1)); 23 | jp1 = new JPanel(); 24 | j1 = new JLabel("输入删除的学生学号:"); 25 | jtf = new JTextField(20); 26 | jp1.add(j1); 27 | jp1.add(jtf); 28 | this.add(jp1); 29 | 30 | jp2 = new JPanel(); 31 | jp2.add(okButton); 32 | jp2.add(cancelButton); 33 | this.add(jp2); 34 | 35 | okButton.addActionListener(new ActionListener() { 36 | @Override 37 | public void actionPerformed(ActionEvent e) { 38 | String id = jtf.getText(); 39 | niubi.deleteStudent(id); 40 | clearText(); 41 | 42 | } 43 | }); 44 | 45 | cancelButton.addActionListener(new ActionListener() { 46 | @Override 47 | public void actionPerformed(ActionEvent e) { 48 | dispose(); 49 | } 50 | }); 51 | 52 | this.setVisible(true); 53 | } 54 | 55 | public void clearText() { 56 | jtf.setText(""); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/com/ceshi/GUI.java: -------------------------------------------------------------------------------- 1 | package com.ceshi; 2 | 3 | import javax.swing.*; 4 | import java.awt.*; 5 | import java.awt.event.ActionEvent; 6 | import java.awt.event.ActionListener; 7 | 8 | public class GUI extends JFrame { 9 | 10 | public GUI() { 11 | setLayout(new BorderLayout()); 12 | JButton button = new JButton("按钮"); 13 | 14 | //事件监听 15 | button.addActionListener(new ActionListener() { 16 | @Override 17 | public void actionPerformed(ActionEvent e) { 18 | System.out.println(((JButton)e.getSource()).getText()); 19 | } 20 | }); 21 | add(button); 22 | } 23 | public static void main(String[] args) { 24 | GUI frame = new GUI(); 25 | frame.setTitle("学生信息管理系统"); 26 | //Container container = frame.getContentPane(); 27 | //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 28 | //frame.setSize(300,200); 29 | //frame.setVisible(true); 30 | //JOptionPane.showMessageDialog(null,"meesgae"); 31 | } 32 | 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/com/ceshi/Get5Students.java: -------------------------------------------------------------------------------- 1 | package com.ceshi; 2 | 3 | import javax.swing.*; 4 | import java.awt.*; 5 | import java.util.ArrayList; 6 | 7 | public class Get5Students extends JFrame { 8 | 9 | public Get5Students() { 10 | 11 | 12 | 13 | //从文件读取学生信息 14 | ArrayList students = niubi.getAllstudentsInfo(); 15 | int random_num = (int) (Math.random() * (students.size() - 5)); 16 | 17 | //表行 18 | Object[][] cellData = new Object[students.size()][16]; 19 | //Object[][] cellData = {{"row1-col1", "row1-col2"},{"row2-col1", "row2-col2"}}; 20 | //表头 21 | String[] columnNames = {"班级号", "学号", "姓名", "年龄", "体重", "身高","高等数学","数据结构","JAVA","离散数学","编译原理","大学物理","计算机组成原理", 22 | "操作系统","计算机网络","线性代数"}; 23 | //String[] columnNames = {"col1", "col2"}; 24 | 25 | 26 | for (int i = random_num; i < random_num + 5; i++) { 27 | Student student = students.get(i); 28 | String class_no = student.getClass_no(); 29 | String name = student.getName(); 30 | String id = student.getId(); 31 | String age = student.getAge(); 32 | String weight = student.getWeight(); 33 | String height = student.getHeight(); 34 | String[] grade = student.getGrade(); 35 | 36 | cellData[i][0] = class_no; 37 | cellData[i][1] = id; 38 | cellData[i][2] = name; 39 | cellData[i][3] = age; 40 | cellData[i][4] = weight; 41 | cellData[i][5] = height; 42 | for (int index = 0; index < 10; index++) { 43 | cellData[i][6+index] = grade[index]; 44 | } 45 | } 46 | 47 | 48 | //建表 49 | JTable table = new JTable(cellData, columnNames); 50 | 51 | JPanel panel = new JPanel(new BorderLayout()); 52 | 53 | panel.add(table.getTableHeader(), BorderLayout.NORTH); 54 | // 把 表格内容 添加到容器中心 55 | panel.add(table, BorderLayout.CENTER); 56 | 57 | this.setContentPane(panel); 58 | this.pack(); 59 | this.setLocationRelativeTo(null); 60 | this.setVisible(true); 61 | 62 | this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 63 | this.setSize(850,200); 64 | this.setVisible(true); 65 | 66 | 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/com/ceshi/GetAllStudents.java: -------------------------------------------------------------------------------- 1 | package com.ceshi; 2 | 3 | import javax.swing.*; 4 | import java.awt.*; 5 | import java.util.ArrayList; 6 | 7 | public class GetAllStudents extends JFrame { 8 | 9 | public GetAllStudents() { 10 | 11 | //从文件读取学生信息 12 | ArrayList students = niubi.getAllstudentsInfo(); 13 | 14 | //表行 15 | Object[][] cellData = new Object[students.size()][16]; 16 | //Object[][] cellData = {{"row1-col1", "row1-col2"},{"row2-col1", "row2-col2"}}; 17 | //表头 18 | String[] columnNames = {"班级号", "学号", "姓名", "年龄", "体重", "身高","高等数学","数据结构","JAVA","离散数学","编译原理","大学物理","计算机组成原理", 19 | "操作系统","计算机网络","线性代数"}; 20 | //String[] columnNames = {"col1", "col2"}; 21 | 22 | 23 | for (int i = 0; i < students.size(); i++) { 24 | Student student = students.get(i); 25 | String class_no = student.getClass_no(); 26 | String name = student.getName(); 27 | String id = student.getId(); 28 | String age = student.getAge(); 29 | String weight = student.getWeight(); 30 | String height = student.getHeight(); 31 | String[] grade = student.getGrade(); 32 | 33 | cellData[i][0] = class_no; 34 | cellData[i][1] = id; 35 | cellData[i][2] = name; 36 | cellData[i][3] = age; 37 | cellData[i][4] = weight; 38 | cellData[i][5] = height; 39 | for (int index = 0; index < 10; index++) { 40 | cellData[i][6+index] = grade[index]; 41 | } 42 | } 43 | 44 | 45 | //建表 46 | JTable table = new JTable(cellData, columnNames); 47 | 48 | JPanel panel = new JPanel(new BorderLayout()); 49 | 50 | panel.add(table.getTableHeader(), BorderLayout.NORTH); 51 | // 把 表格内容 添加到容器中心 52 | panel.add(table, BorderLayout.CENTER); 53 | 54 | this.setContentPane(panel); 55 | this.pack(); 56 | this.setLocationRelativeTo(null); 57 | this.setVisible(true); 58 | 59 | this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 60 | this.setSize(900,400); 61 | this.setVisible(true); 62 | 63 | 64 | } 65 | 66 | public static void main(String[] args) { 67 | new GetAllStudents(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/com/ceshi/LoginForm.java: -------------------------------------------------------------------------------- 1 | package com.ceshi; 2 | 3 | import javax.swing.*; 4 | import java.awt.*; 5 | import java.awt.event.ActionEvent; 6 | import java.awt.event.ActionListener; 7 | 8 | public class LoginForm extends JFrame { 9 | private JPanel jp1, jp2, jp3; 10 | private JPanel jb1, jb2; 11 | private JLabel j11, j12; 12 | private JTextField jtf; 13 | private JPasswordField jpf; 14 | private JButton okButton = new JButton("确认"); 15 | private JButton cancelButton = new JButton("取消"); 16 | private JButton changePassword = new JButton("修改密码"); 17 | 18 | public static void main(String[] args) { 19 | new LoginForm(); 20 | } 21 | 22 | public LoginForm() { 23 | this.setTitle("用户登录"); 24 | this.setSize(320, 200); 25 | this.setLocationRelativeTo(null); 26 | this.setDefaultCloseOperation(EXIT_ON_CLOSE); 27 | 28 | this.setLayout(new GridLayout(3, 1)); 29 | jp1 = new JPanel(); 30 | j11 = new JLabel("用户名:"); 31 | jtf = new JTextField(20); 32 | jp1.add(j11); 33 | jp1.add(jtf); 34 | this.add(jp1); 35 | 36 | jp2 = new JPanel(); 37 | j12 = new JLabel("密码:"); 38 | jpf = new JPasswordField(20); 39 | jp2.add(j12); 40 | jp2.add(jpf); 41 | this.add(jp2); 42 | 43 | jp3 = new JPanel(); 44 | jp3.add(okButton); 45 | jp3.add(cancelButton); 46 | jp3.add(changePassword); 47 | this.add(jp3); 48 | 49 | 50 | //时间监听 51 | 52 | cancelButton.addActionListener(new ActionListener() { 53 | @Override 54 | public void actionPerformed(ActionEvent e) { 55 | System.exit(0); 56 | } 57 | }); 58 | 59 | //修改密码的时间监听 60 | changePassword.addActionListener(new ActionListener() { 61 | @Override 62 | public void actionPerformed(ActionEvent e) { 63 | String password; 64 | PasswordChange cp = new PasswordChange(); 65 | 66 | //cp.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 67 | 68 | } 69 | }); 70 | 71 | //主界面 72 | okButton.addActionListener(new ActionListener() { 73 | @Override 74 | public void actionPerformed(ActionEvent e) { 75 | String id = jtf.getText();// 取得用户名 76 | String key = new String(jpf.getPassword());// 取得密码 77 | User user = new User(); 78 | String correct_key = user.getPasssword(); 79 | if (id.equals("admin") && key.equals(correct_key)) {// 判断是否登录成功 80 | // 如果登录成功 81 | setVisible(false);// 本窗口隐藏, 82 | new MainFrame().setVisible(true);// 新窗口显示 83 | dispose();//本窗口销毁,释放内存资源 84 | } else { 85 | //如果登录失败 弹出提示 86 | JOptionPane.showMessageDialog(null, "用户名或者密码错误.", "通知", JOptionPane.ERROR_MESSAGE); 87 | clearText();//清空文本框 密码框的输入 88 | } 89 | } 90 | }); 91 | 92 | this.setVisible(true); 93 | 94 | } 95 | 96 | public void clearText() {//清空文本框, 密码框的输入 97 | 98 | jpf.setText(""); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/com/ceshi/MainFrame.java: -------------------------------------------------------------------------------- 1 | package com.ceshi; 2 | 3 | import javax.swing.*; 4 | import java.awt.event.ActionEvent; 5 | import java.awt.event.ActionListener; 6 | 7 | public class MainFrame extends JFrame { 8 | private JMenuBar menuBar = new JMenuBar(); 9 | private JMenu newMenu = new JMenu("添加"); 10 | private JMenu viewMenu = new JMenu("查询"); 11 | private JMenu changeMenu = new JMenu("修改"); 12 | private JMenu deleteMenu = new JMenu("删除"); 13 | 14 | private JMenuItem[] newItem = { new JMenuItem("添加学生信息")}; 15 | private JMenuItem[] viewItem = { new JMenuItem("全体学生"), new JMenuItem("随机5个")}; 16 | private JMenuItem[] changeItem = {new JMenuItem("修改学生信息")}; 17 | private JMenuItem[] deleteItem = {new JMenuItem("删除学生信息")}; 18 | 19 | public MainFrame() { 20 | // 监听函数 .. 21 | 22 | for (int i = 0; i < viewItem.length; i++) { 23 | viewMenu.add(viewItem[i]); 24 | } 25 | newMenu.add(newItem[0]); 26 | changeMenu.add(changeItem[0]); 27 | deleteMenu.add(deleteItem[0]); 28 | 29 | menuBar.add(newMenu); 30 | menuBar.add(viewMenu); 31 | menuBar.add(changeMenu); 32 | menuBar.add(deleteMenu); 33 | setJMenuBar(menuBar); 34 | 35 | this.setTitle("主页面"); 36 | this.setLocationRelativeTo(null); 37 | this.setDefaultCloseOperation(EXIT_ON_CLOSE); 38 | this.setSize(500,200); 39 | this.setLocationRelativeTo(null); 40 | this.setVisible(true); 41 | 42 | newItem[0].addActionListener(new ActionListener() { 43 | @Override 44 | public void actionPerformed(ActionEvent e) { 45 | addStudent newstudent = new addStudent(); 46 | } 47 | }); 48 | 49 | viewItem[0].addActionListener(new ActionListener() { 50 | @Override 51 | public void actionPerformed(ActionEvent e) { 52 | GetAllStudents allStudents = new GetAllStudents(); 53 | } 54 | }); 55 | 56 | viewItem[1].addActionListener(new ActionListener() { 57 | @Override 58 | public void actionPerformed(ActionEvent e) { 59 | Get5Students fivestudents = new Get5Students(); 60 | } 61 | }); 62 | 63 | changeItem[0].addActionListener(new ActionListener() { 64 | @Override 65 | public void actionPerformed(ActionEvent e) { 66 | ChangeStudent student = new ChangeStudent(); 67 | } 68 | }); 69 | 70 | deleteItem[0].addActionListener(new ActionListener() { 71 | @Override 72 | public void actionPerformed(ActionEvent e) { 73 | DeleteStudent student = new DeleteStudent(); 74 | } 75 | }); 76 | } 77 | 78 | public static void main(String[] args) { 79 | MainFrame frame = new MainFrame(); 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /src/com/ceshi/PasswordChange.java: -------------------------------------------------------------------------------- 1 | package com.ceshi; 2 | 3 | import javax.swing.*; 4 | import java.awt.*; 5 | import java.awt.event.ActionEvent; 6 | import java.awt.event.ActionListener; 7 | 8 | public class PasswordChange extends JFrame { 9 | private JPanel jp1, jp2; 10 | private JLabel jl; 11 | private JTextField jtf; 12 | private JButton okButton = new JButton("确认"); 13 | private JButton cancelButton = new JButton("取消"); 14 | 15 | String password; 16 | 17 | public static void main(String[] args) { 18 | PasswordChange frame = new PasswordChange(); 19 | frame.setVisible(true); 20 | } 21 | 22 | public PasswordChange() { 23 | this.setTitle("修改密码"); 24 | this.setSize(300, 200); 25 | this.setLocationRelativeTo(null); 26 | //this.setDefaultCloseOperation(EXIT_ON_CLOSE); 27 | //password = JOptionPane.showInputDialog("请输入新密码"); 28 | 29 | this.setLayout(new GridLayout(2, 1)); 30 | jp1 = new JPanel(); 31 | jl = new JLabel("新密码:"); 32 | jtf = new JTextField(20); 33 | jp1.add(jl); 34 | jp1.add(jtf); 35 | this.add(jp1); 36 | 37 | jp2 = new JPanel(); 38 | jp2.add(okButton); 39 | jp2.add(cancelButton); 40 | this.add(jp2); 41 | 42 | 43 | //事件监听 44 | okButton.addActionListener(new ActionListener() { 45 | @Override 46 | public void actionPerformed(ActionEvent e) { 47 | password = jtf.getText(); 48 | User user = new User(); 49 | user.changePassword(password); 50 | dispose(); 51 | } 52 | }); 53 | 54 | cancelButton.addActionListener(new ActionListener() { 55 | @Override 56 | public void actionPerformed(ActionEvent e) { 57 | dispose(); 58 | } 59 | }); 60 | 61 | this.setVisible(true); 62 | } 63 | 64 | public String getPassword() { 65 | return password; 66 | } 67 | 68 | 69 | 70 | 71 | } 72 | -------------------------------------------------------------------------------- /src/com/ceshi/Student.java: -------------------------------------------------------------------------------- 1 | package com.ceshi; 2 | 3 | import java.security.PrivateKey; 4 | 5 | public class Student { 6 | private String class_no; 7 | private String id; 8 | private String name; 9 | private String age; 10 | private String weight; 11 | private String height; 12 | private String[] grade; 13 | 14 | public Student(String class_no, String id, String name, String age, String weight, String height, String[] grade) { 15 | this.class_no = class_no; 16 | this.id = id; 17 | this.name = name; 18 | this.age = age; 19 | this.weight = weight; 20 | this.height = height; 21 | this.grade = grade; 22 | } 23 | 24 | public void setAge(String age) { 25 | this.age = age; 26 | } 27 | 28 | 29 | public void setHeight(String height) { 30 | this.height = height; 31 | } 32 | 33 | public void setWeight(String weight) { 34 | this.weight = weight; 35 | } 36 | 37 | public void setId(String id) { 38 | this.id = id; 39 | } 40 | 41 | public void setName(String name) { 42 | this.name = name; 43 | } 44 | 45 | public void setClass_no(String class_no) { 46 | this.class_no = class_no; 47 | } 48 | 49 | public void setGrade(String[] grade) { 50 | this.grade = grade; 51 | } 52 | 53 | public String getClass_no() { 54 | return class_no; 55 | } 56 | 57 | public String getId() { 58 | return id; 59 | } 60 | 61 | public String[] getGrade() { 62 | return grade; 63 | } 64 | 65 | public String getName() { 66 | return name; 67 | } 68 | 69 | public String getAge() { 70 | return age; 71 | } 72 | 73 | public String getHeight() { 74 | return height; 75 | } 76 | 77 | public String getWeight() { 78 | return weight; 79 | } 80 | 81 | @Override 82 | public String toString() { 83 | 84 | String body_info; //体重 85 | if(Double.parseDouble(height)/Double.parseDouble(weight) < 1.13) 86 | body_info = "体脂率高"; 87 | else if (Double.parseDouble(height)/Double.parseDouble(weight) >1.7 ) 88 | body_info = "身材太瘦"; 89 | else 90 | body_info = "身材合适"; 91 | 92 | String grades = ""; 93 | for (int index = 0; index < grade.length; index++) { 94 | grades += niubi.course[index] + ": " + grade[index] + " "; 95 | } 96 | 97 | return "id: "+id+"\nname: "+name+"\nage: "+age+"\nheight: "+height+"\nweight: "+weight 98 | + "\n身体状况: " + body_info + "\n" + grades + "\n---------------------------------"; 99 | } 100 | } 101 | /* 102 | public void show() { 103 | System.out.println("id: "+id+" name: "+name+" age: "+age+" height: "+height+" weight: "+weight); 104 | System.out.print("本学期修读的课程有: "); 105 | for(int i = 0; i < course.length; i++) { 106 | 107 | System.out.print(course[i] +" "); 108 | } 109 | System.out.println(""); 110 | if(height/weight < 1.13) 111 | System.out.println("too heavy"); 112 | else if (height/weight >1.7 ) 113 | System.out.println("too thin"); 114 | else 115 | System.out.println("healthy body"); 116 | } 117 | 118 | @Override 119 | public String toString() { 120 | String print_course = ""; //课程 121 | for(int i = 0; i < course.length; i++) 122 | print_course += course[i] + " "; 123 | 124 | String body_info; //体重 125 | if(height/weight < 1.13) 126 | body_info = "体脂率高"; 127 | else if (height/weight >1.7 ) 128 | body_info = "身材太瘦"; 129 | else 130 | body_info = "身材合适"; 131 | 132 | return "id: "+id+"\nname: "+name+"\nage: "+age+"\nheight: "+height+"\nweight: "+weight 133 | + "\n本学期修读的课程有: " + print_course 134 | + "\n身体状况: " + body_info; 135 | } 136 | } 137 | 138 | */ -------------------------------------------------------------------------------- /src/com/ceshi/User.java: -------------------------------------------------------------------------------- 1 | package com.ceshi; 2 | 3 | import java.io.*; 4 | import java.util.ArrayList; 5 | 6 | public class User { 7 | 8 | private String name; 9 | private String passsword; 10 | 11 | public User() { 12 | //文件路径: ./user.txt 13 | String folder_name = "user.txt"; 14 | File file = new File(folder_name); 15 | ArrayList arrayList = new ArrayList<>(); 16 | 17 | try { 18 | FileReader fr = new FileReader(file); 19 | BufferedReader bf = new BufferedReader(fr); 20 | String str; 21 | while ((str = bf.readLine()) != null) { 22 | arrayList.add(str); 23 | } 24 | } catch (IOException e) { 25 | e.printStackTrace(); 26 | } 27 | 28 | this.name = arrayList.get(0); 29 | this.passsword = arrayList.get(1); 30 | } 31 | 32 | //更改密码 33 | public void changePassword(String passsword) { 34 | this.passsword = passsword; 35 | 36 | //文件路径: ./user.txt 37 | String folder_name = "./user.txt"; 38 | File folder = new File(folder_name); 39 | try { 40 | FileWriter fileWritter = new FileWriter(folder_name); 41 | fileWritter.write(this.name+"\r\n"); 42 | fileWritter.write(this.passsword+"\r\n"); 43 | fileWritter.close(); 44 | }catch (IOException e) { 45 | e.printStackTrace(); 46 | } 47 | } 48 | 49 | public String getName() { 50 | return name; 51 | } 52 | 53 | public String getPasssword() { 54 | return passsword; 55 | } 56 | } 57 | 58 | -------------------------------------------------------------------------------- /src/com/ceshi/addStudent.java: -------------------------------------------------------------------------------- 1 | package com.ceshi; 2 | 3 | import javax.swing.*; 4 | import java.awt.*; 5 | import java.awt.event.ActionEvent; 6 | import java.awt.event.ActionListener; 7 | 8 | //班级号 学号 姓名 年龄 体重 身高 9 | 10 | public class addStudent extends JFrame { 11 | private JPanel jp1, jp2, jp3, jp4, jp5, jp6, jp7; 12 | private JPanel grade1, grade2; 13 | private JLabel jl1, jl2, jl3, jl4, jl5, jl6; 14 | private JLabel g1,g2,g3,g4,g5,g6,g7,g8,g9,g10; 15 | private JTextField jtf1, jtf2, jtf3, jtf4, jtf5, jtf6; 16 | private JTextField f1,f2,f3,f4,f5,f6,f7,f8,f9,f10; 17 | private JButton okButton = new JButton("确认"); 18 | private JButton cancelButton = new JButton("取消"); 19 | 20 | public addStudent() { 21 | 22 | this.setLayout(new GridLayout(9, 1)); 23 | jp1 = new JPanel(); 24 | jl1 = new JLabel("班级号:"); 25 | jtf1 = new JTextField(20); 26 | jp1.add(jl1); 27 | jp1.add(jtf1); 28 | this.add(jp1); 29 | 30 | jp2 = new JPanel(); 31 | jl2 = new JLabel("学号:"); 32 | jtf2 = new JTextField(20); 33 | jp2.add(jl2); 34 | jp2.add(jtf2); 35 | this.add(jp2); 36 | 37 | jp3 = new JPanel(); 38 | jl3 = new JLabel("姓名:"); 39 | jtf3 = new JTextField(20); 40 | jp3.add(jl3); 41 | jp3.add(jtf3); 42 | this.add(jp3); 43 | 44 | jp4 = new JPanel(); 45 | jl4 = new JLabel("年龄:"); 46 | jtf4 = new JTextField(20); 47 | jp4.add(jl4); 48 | jp4.add(jtf4); 49 | this.add(jp4); 50 | 51 | jp5 = new JPanel(); 52 | jl5 = new JLabel("体重:"); 53 | jtf5 = new JTextField(20); 54 | jp5.add(jl5); 55 | jp5.add(jtf5); 56 | this.add(jp5); 57 | 58 | jp6 = new JPanel(); 59 | jl6 = new JLabel("身高:"); 60 | jtf6 = new JTextField(20); 61 | jp6.add(jl6); 62 | jp6.add(jtf6); 63 | this.add(jp6); 64 | 65 | grade1 = new JPanel(); 66 | g1 = new JLabel("高等数学:"); 67 | f1 = new JTextField(3); 68 | g2 = new JLabel("数据结构:"); 69 | f2 = new JTextField(3); 70 | g3 = new JLabel("JAVA:"); 71 | f3 = new JTextField(3); 72 | g4 = new JLabel("离散数学·:"); 73 | f4 = new JTextField(3); 74 | g5 = new JLabel("编译原理:"); 75 | f5 = new JTextField(3); 76 | grade1.add(g1); 77 | grade1.add(f1); 78 | grade1.add(g2); 79 | grade1.add(f2); 80 | grade1.add(g3); 81 | grade1.add(f3); 82 | grade1.add(g4); 83 | grade1.add(f4); 84 | grade1.add(g5); 85 | grade1.add(f5); 86 | this.add(grade1); 87 | 88 | grade2 = new JPanel(); 89 | g6 = new JLabel("大学物理:"); 90 | f6 = new JTextField(3); 91 | g7 = new JLabel("计算机组成原理:"); 92 | f7 = new JTextField(3); 93 | g8 = new JLabel("操作系统:"); 94 | f8 = new JTextField(3); 95 | g9 = new JLabel("计算机网络·:"); 96 | f9 = new JTextField(3); 97 | g10 = new JLabel("线性代数:"); 98 | f10 = new JTextField(3); 99 | grade2.add(g6); 100 | grade2.add(f6); 101 | grade2.add(g7); 102 | grade2.add(f7); 103 | grade2.add(g8); 104 | grade2.add(f8); 105 | grade2.add(g9); 106 | grade2.add(f9); 107 | grade2.add(g10); 108 | grade2.add(f10); 109 | this.add(grade2); 110 | 111 | jp7 = new JPanel(); 112 | jp7.add(okButton); 113 | jp7.add(cancelButton); 114 | this.add(jp7); 115 | 116 | okButton.addActionListener(new ActionListener() { 117 | @Override 118 | public void actionPerformed(ActionEvent e) { 119 | String[] grade = {f1.getText(),f2.getText(),f3.getText(),f4.getText(),f5.getText(), 120 | f6.getText(),f7.getText(),f8.getText(),f9.getText(),f10.getText()}; 121 | 122 | niubi.addStudent(jtf1.getText(),jtf2.getText(),jtf3.getText(),jtf4.getText(),jtf5.getText(),jtf6.getText(),grade); 123 | dispose(); 124 | } 125 | }); 126 | 127 | 128 | cancelButton.addActionListener(new ActionListener() { 129 | @Override 130 | public void actionPerformed(ActionEvent e) { 131 | dispose(); 132 | } 133 | }); 134 | 135 | this.setTitle("添加学生信息"); 136 | this.setSize(580, 500); 137 | this.setLocationRelativeTo(null); 138 | this.setDefaultCloseOperation(EXIT_ON_CLOSE); 139 | this.setVisible(true); 140 | } 141 | 142 | public static void main(String[] args) { 143 | new addStudent(); 144 | } 145 | 146 | } 147 | -------------------------------------------------------------------------------- /src/com/ceshi/niubi.java: -------------------------------------------------------------------------------- 1 | package com.ceshi; 2 | 3 | 4 | 5 | import javax.swing.*; 6 | import java.io.*; 7 | import java.sql.SQLOutput; 8 | import java.util.*; 9 | import java.text.ParseException; 10 | import java.text.SimpleDateFormat; 11 | import java.util.Scanner; 12 | 13 | //项目分析: 14 | // 1. 学生类 √ 15 | // 2. 实现 以班级号为文件夹 学号为文件 的 增加学生信息 的功能 √ 16 | // 3. 实现 获得所有同学信息 的功能 √ 17 | // 4. 实现 获取随机5个学生信息 的功能 √ 18 | // 5. 实现 查找学生是否在表内 的功能 √ 19 | // 6. update 增加学生的10科成绩 完善函数 √ 20 | // 7. 实现 班级类 √ 21 | // 8. 实现 获取班级信息 的函数 √ 22 | // 9. 实现 增 √ 删 √ 改 √ 23 | // 班级号 学号 姓名 年龄 体重 身高 24 | 25 | public class niubi { 26 | 27 | // 十门课 数组 28 | public static String[] course = {"高等数学","数据结构","JAVA","离散数学","编译原理","大学物理","计算机组成原理", 29 | "操作系统","计算机网络","线性代数"}; 30 | 31 | //添加学生 32 | public static void addStudent(String class_no, String id, String name, String age, String weight, String height, String[] grade) { 33 | 34 | 35 | String folder = "./data/"; 36 | File dir = new File(folder + class_no); 37 | 38 | //如果这个学生的班级号还没有出现过,就新建一个班级号文件夹 39 | if (!dir.exists()) 40 | dir.mkdir(); 41 | 42 | //创建在班级号文件夹下的 以学号为名 的txt文件 43 | File file = new File(folder + class_no + "/" + id + ".txt"); 44 | //创建文件 必须需要 try-catch块 45 | try { 46 | if(!file.exists()) { 47 | file.createNewFile(); 48 | } 49 | } catch (IOException e) { 50 | e.printStackTrace(); 51 | } 52 | 53 | 54 | //写入 学生文件 信息 55 | try { 56 | FileWriter fileWritter = new FileWriter(folder + class_no + "/" + file.getName()); 57 | fileWritter.write(class_no+"\r\n"); 58 | fileWritter.write(id+"\r\n"); 59 | fileWritter.write(name+"\r\n"); 60 | fileWritter.write(age+"\r\n"); 61 | fileWritter.write(weight+"\r\n"); 62 | fileWritter.write(height+"\r\n"); 63 | for (int i = 0; i < 10; i++) { 64 | fileWritter.write(grade[i]+"\r\n"); 65 | } 66 | fileWritter.close(); 67 | }catch (IOException e) { 68 | e.printStackTrace(); 69 | } 70 | } 71 | 72 | //获取所有学生信息 73 | public static ArrayList getAllstudentsInfo() { 74 | String folder_name = "./data/"; 75 | File folder = new File(folder_name); 76 | String[] ClassList = folder.list(); 77 | ArrayList Students = new ArrayList<>(); 78 | 79 | //外循环: 每个班级号文件夹 80 | for (int index = 0; index < ClassList.length; index++) { 81 | // System.out.println(ClassList[index]); 输出文件夹名 82 | // Mac OSX 环境下的 bUG 83 | if (ClassList[index].equals(".DS_Store") ) 84 | continue; 85 | File dir = new File(folder_name + ClassList[index]); 86 | String[] FileList = dir.list(); 87 | // System.out.println(); 88 | //内循环: 每个班级号文件夹中 的 学生txt文件 89 | for (int file_index = 0; file_index < FileList.length; file_index++) { 90 | //Mac OSX 环境下的 bUG 91 | if (FileList[file_index].equals(".DS_Store") ) 92 | continue; 93 | 94 | File file = new File(folder_name + ClassList[index] + "/" + FileList[file_index]); 95 | // System.out.println(FileList[file_index]); 输出文件名 96 | ArrayList arrayList = new ArrayList<>(); 97 | try { 98 | FileReader fr = new FileReader(file); 99 | BufferedReader bf = new BufferedReader(fr); 100 | String str; 101 | while ((str = bf.readLine()) != null) { 102 | arrayList.add(str); 103 | } 104 | } catch (IOException e) { 105 | e.printStackTrace(); 106 | } 107 | 108 | String class_no = arrayList.get(0); 109 | String id = arrayList.get((1)); 110 | String name = arrayList.get(2); 111 | String age = arrayList.get(3); 112 | String weight = arrayList.get(4); 113 | String height = arrayList.get(5); 114 | String[] grade = new String[10]; 115 | for (int i = 0; i < 10; i++) { 116 | grade[i] = arrayList.get(6+i); 117 | } 118 | Student student = new Student(class_no, id, name, age, weight, height, grade); 119 | Students.add(student); 120 | 121 | } 122 | 123 | } 124 | return Students; 125 | } 126 | 127 | //获取所有学生信息 只输出数据 没有返回数组 128 | public static void getAllstudentsInfow_output() { 129 | ArrayList students = getAllstudentsInfo(); 130 | 131 | for (int i = 0; i < students.size(); i++) { 132 | System.out.println(students.get(i)); 133 | } 134 | } 135 | 136 | //获取随机5个学生信息 137 | public static void getRandom5studentsInfo() { 138 | //储存 学生信息文件 的地址 139 | ArrayList arrayList = new ArrayList<>(); 140 | String folder_name = "./data/"; 141 | File folder = new File(folder_name); 142 | String[] ClassList = folder.list(); 143 | 144 | //外循环: 每个班级号文件夹 145 | for (int index = 0; index < ClassList.length; index++) { 146 | // System.out.println(ClassList[index]); 输出文件夹名 147 | if (ClassList[index].equals(".DS_Store") ) 148 | continue; 149 | File dir = new File(folder_name + ClassList[index]); 150 | String[] FileList = dir.list(); 151 | 152 | //内循环: 每个班级号文件夹中 的 学生txt文件 153 | for (int file_index = 0; file_index < FileList.length; file_index++) { 154 | // File file = new File(folder_name + ClassList[index] + "/" + FileList[file_index]); 155 | // System.out.println(FileList[file_index]); 输出文件名 156 | if (FileList[file_index].equals(".DS_Store") ) 157 | continue; 158 | arrayList.add(folder_name + ClassList[index] + "/" + FileList[file_index]); 159 | } 160 | } 161 | 162 | 163 | int random_num = (int) (Math.random() * (arrayList.size() - 5)); 164 | //遍历 数组 165 | for (int index = random_num; index < random_num + 5; index++) { 166 | //储存学生信息 的数组 167 | ArrayList arrayList_student = new ArrayList<>(); 168 | File file = new File(arrayList.get(index)); 169 | try { 170 | FileReader fr = new FileReader(file); 171 | BufferedReader bf = new BufferedReader(fr); 172 | String str; 173 | while ((str = bf.readLine()) != null) { 174 | arrayList_student.add(str); 175 | 176 | } 177 | } catch (IOException e) { 178 | e.printStackTrace(); 179 | } 180 | // 输出 txt文件 信息 181 | System.out.println("班级: " + arrayList_student.get(0)); 182 | System.out.println("学号: " + arrayList_student.get(1)); 183 | System.out.println("姓名: " + arrayList_student.get(2)); 184 | System.out.println("年龄: " + arrayList_student.get(3)); 185 | System.out.println("体重: " + arrayList_student.get(4)); 186 | System.out.println("身高: " + arrayList_student.get(5)); 187 | for (int i = 0; i < 10; i++) { 188 | System.out.print(course[i] +":" + arrayList_student.get(6+i) + " "); 189 | } 190 | System.out.println(""); 191 | System.out.println("--------------------------------------"); 192 | } 193 | } 194 | 195 | //查找学生是否在表内 196 | public static void findStudent() { 197 | System.out.print("输入要查找的学生学号: "); 198 | Scanner scanner = new Scanner(System.in); 199 | String id = scanner.nextLine(); 200 | //获得学生名单 201 | ArrayList list = getAllstudentsInfo(); 202 | //查找 203 | int flag = 1; 204 | for (int index = 0; index < list.size(); index++) { 205 | 206 | if (list.get(index).getId().equals(id)) { // 字符串不能用 == 比较 207 | System.out.println(list.get(index)); 208 | System.out.println("-----------------------------"); 209 | flag = 0; 210 | break; 211 | } 212 | } 213 | if (flag == 1) 214 | System.out.println("查无此人"); 215 | } 216 | 217 | //查询学生是否在表内 返回 boolean 类型 218 | public static boolean findStudent(String id) { 219 | //获得学生名单 220 | ArrayList list = getAllstudentsInfo(); 221 | //查找 222 | for (int index = 0; index < list.size(); index++) { 223 | 224 | if (list.get(index).getId().equals(id)) { // 字符串不能用 == 比较 225 | return true; 226 | 227 | } 228 | } 229 | return false; 230 | } 231 | 232 | //获取一个班级的学生信息 233 | public static void getOneclass() { 234 | System.out.print("输入要查找的班级号: "); 235 | Scanner scanner = new Scanner(System.in); 236 | String class_no = scanner.nextLine(); 237 | 238 | //查找是否有这个班级号文件夹 239 | String folder_name = "./data/"; 240 | File folder = new File(folder_name); 241 | String[] ClassList = folder.list(); 242 | //查找是否成功的标记 243 | int flag = 1; 244 | 245 | for (int index = 0; index < ClassList.length; index++) { 246 | 247 | if (ClassList[index].equals(class_no)) { 248 | Classmates classmates = new Classmates(class_no); 249 | classmates.getAllstudentsInfo(); 250 | flag = 0; 251 | break; 252 | } 253 | 254 | } 255 | if (flag == 1) { 256 | System.out.println("查无此班"); 257 | } 258 | 259 | } 260 | 261 | //删除学生 信息 262 | public static void deleteStudent(String id) { 263 | 264 | if (findStudent(id)) { 265 | String folder_name = "./data/"; 266 | File folder = new File(folder_name); 267 | String[] ClassList = folder.list(); 268 | 269 | for (int index = 0; index < ClassList.length; index++) { 270 | // Mac OSX 环境下的 bUG 271 | if (ClassList[index].equals(".DS_Store") ) 272 | continue; 273 | File dir = new File(folder_name + ClassList[index]); 274 | String[] FileList = dir.list(); 275 | 276 | for (int file_index = 0; file_index < FileList.length; file_index++) { 277 | ; 278 | if (FileList[file_index].equals(id+".txt")) { 279 | File file = new File(folder_name + ClassList[index] + "/" + FileList[file_index]); 280 | file.delete(); 281 | } 282 | 283 | 284 | } 285 | } 286 | } else { 287 | JOptionPane.showMessageDialog(null, "没有这个学生.", "通知", JOptionPane.ERROR_MESSAGE); 288 | } 289 | 290 | } 291 | 292 | //改动学生 信息 293 | public static void change() { 294 | Scanner scanner = new Scanner(System.in); 295 | System.out.print("请输入学生学号: "); 296 | String input_id = scanner.nextLine(); 297 | System.out.print("请问需要改动学生的哪一门成绩: "); 298 | String change_course = scanner.nextLine(); 299 | System.out.print("改为多少分: "); 300 | String new_grade = scanner.nextLine(); 301 | //这门课在course中的索引 302 | int index; 303 | for (index = 0; index < course.length; index++) { 304 | if (course[index].equals(change_course)) 305 | break; 306 | } 307 | 308 | 309 | if (findStudent(input_id)) { 310 | 311 | String folder_name = "./data/"; 312 | File folder = new File(folder_name); 313 | String[] ClassList = folder.list(); 314 | ArrayList arrayList = new ArrayList<>(); 315 | 316 | for (int i = 0; i < ClassList.length; i++) { 317 | // Mac OSX 环境下的 bUG 318 | if (ClassList[i].equals(".DS_Store")) 319 | continue; 320 | File dir = new File(folder_name + ClassList[i]); 321 | String[] FileList = dir.list(); 322 | 323 | for (int file_index = 0; file_index < FileList.length; file_index++) { 324 | 325 | if (FileList[file_index].equals(input_id + ".txt")) { 326 | File file = new File(folder_name + ClassList[i] + "/" + FileList[file_index]); 327 | try { 328 | FileReader fr = new FileReader(file); 329 | BufferedReader bf = new BufferedReader(fr); 330 | String str; 331 | 332 | while ((str = bf.readLine()) != null) { 333 | arrayList.add(str); 334 | } 335 | } catch (IOException e) { 336 | e.printStackTrace(); 337 | } 338 | 339 | //读取要改动的 学生 的信息 340 | String class_no = arrayList.get(0); 341 | String id = arrayList.get((1)); 342 | String name = arrayList.get(2); 343 | String age = arrayList.get(3); 344 | String weight = arrayList.get(4); 345 | String height = arrayList.get(5); 346 | String[] grade = new String[10]; 347 | for (int j = 0; j < 10; j++) { 348 | grade[j] = arrayList.get(6+j); 349 | } 350 | //改动成绩 351 | grade[index] = new_grade; 352 | 353 | //写入 354 | try { 355 | FileWriter fileWritter = new FileWriter(file); 356 | fileWritter.write(class_no+"\r\n"); 357 | fileWritter.write(id+"\r\n"); 358 | fileWritter.write(name+"\r\n"); 359 | fileWritter.write(age+"\r\n"); 360 | fileWritter.write(weight+"\r\n"); 361 | fileWritter.write(height+"\r\n"); 362 | for (int k = 0; k < 10; k++) { 363 | fileWritter.write(grade[k]+"\r\n"); 364 | } 365 | fileWritter.close(); 366 | }catch (IOException e) { 367 | e.printStackTrace(); 368 | } 369 | } 370 | } 371 | } 372 | } else { 373 | System.out.println("没有这个学生"); 374 | } 375 | } 376 | 377 | public static void main(String[] args) { 378 | //ArrayList students = getAllstudentsInfo(); 379 | //getAllstudentsInfow_output(); 380 | //getRandom5studentsInfo(); 381 | //addStudent(); 382 | //findStudent(); 383 | //getOneclass(); 384 | //deleteStudent(); 385 | //change(); 386 | ArrayList students = niubi.getAllstudentsInfo(); 387 | for ( Student student : students) { 388 | System.out.println(student); 389 | } 390 | } 391 | 392 | } 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | /* 402 | Scanner scanner = new Scanner(System.in); 403 | System.out.print("输入学号:"); 404 | String id = scanner.nextLine(); 405 | System.out.print("输入姓名:"); 406 | String name= scanner.nextLine(); 407 | System.out.print("输入年龄:"); 408 | int age = scanner.nextInt(); 409 | System.out.print("输入体重:"); 410 | double weight = scanner.nextDouble(); 411 | System.out.print("输入身高:"); 412 | double height = scanner.nextDouble(); 413 | System.out.print("这学期修的课程有几门:"); 414 | int i = scanner.nextInt(); 415 | 416 | String[] course = new String[i]; 417 | String s = scanner.nextLine(); 418 | for (int n = 0; n < i; n++) { 419 | course[n] = scanner.nextLine(); 420 | 421 | } 422 | Student student = new Student(id,name,age,weight,height,course); 423 | 424 | System.out.println(student.toString()); 425 | */ 426 | 427 | -------------------------------------------------------------------------------- /user.txt: -------------------------------------------------------------------------------- 1 | admin 2 | 123 3 | --------------------------------------------------------------------------------