- 需求分析
程序设计的任务是实现对学生信息的管理。用户名和密码都默认设置为0,用户名或密码输入错误会弹出“用户名或密码输入不正确”的对话框。在用户名和密码输入正确后进入学生信息管理系统,然后进行添加、修改、删除、统计等操作。添加学生记录:输入学号、姓名、高等数学、英语。点击保存则自动跳转显示学生记录列表的页面。来分页展示学生记录列表。这个页面有查询,删除,修改,退出显示等按钮。
- 概要设计
1、类之间的调用关系
2、学生信息E-R图
- 详细设计
- 主程序Starter的代码
主要实现了信息管理窗口,还有对各个服务的依赖掉用等功能。
package com.paper.view;
import com.paper.entity.Statistics;
import com.paper.entity.Student;
import com.paper.service.StudentService;
import com.paper.table.MyJTable;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Starter {
public static void main(String args[])throws Exception{
new Starter().init();
}
private JFrame jf=new JFrame("学生信息管理系统");
Dimension faceSize=new Dimension(800,600);
private Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
//按学号查询
private JPanel pSelect=new JPanel();
private JButton bNoSelect=new JButton("按学号序显示");
private JButton bNameSelect=new JButton("按姓名序显示");
private JButton bScoreSelect=new JButton("按成绩序显示");
private JButton bLogout=new JButton("退出系统");
//查询结果放在一个JTable
private MyJTable table;
private DefaultTableModel tableModel;
private JScrollPane tableScrollPane;
private Object[] tableTitle={"学号","姓名","计算机","高等数学","英语"};
private Object[][]tableData={new Object[]{""}};
//对学生信息进行管理的添加、删除、修改按钮
private JPanel buttonPanel=new JPanel();
private JButton insert=new JButton("添加");
private JButton delete=new JButton("删除");
private JButton update=new JButton("修改");
private JButton statistics=new JButton("统计");
//单机添加、修改时弹出的对话框
private JDialog dialog=new JDialog(jf,"学生管理");
private Box box=Box.createVerticalBox();
private JPanel pPhoto=new JPanel(new FlowLayout(FlowLayout.LEFT));
private JPanel pId=new JPanel(new FlowLayout(FlowLayout.LEFT));
private JPanel pName=new JPanel(new FlowLayout(FlowLayout.LEFT));
private JPanel pComputer=new JPanel(new FlowLayout(FlowLayout.LEFT));
private JPanel pAdvancedMath=new JPanel(new FlowLayout(FlowLayout.LEFT));
private JPanel pEnglish=new JPanel(new FlowLayout(FlowLayout.LEFT));
private JLabel lId=new JLabel("学 号");
private JLabel lName=new JLabel("姓 名");
private JLabel lComputer=new JLabel("计算机");
private JLabel lAdvancedMath=new JLabel("高等数学");
private JLabel lEnglish=new JLabel("英 语");
private JTextField tId=new JTextField(15);
private JTextField tName=new JTextField(15);
private JTextField tComputer= new JTextField(15);
private JTextField tAdvancedMath=new JTextField(15);
private JTextField tEnglish=new JTextField(15);
private JPanel pButton=new JPanel();
private JButton confirm=new JButton("确认");
private JButton cancel=new JButton("取消");
private StudentService service=new StudentService();
//用于标记是添加还是修改
private String id;
public void init(){
pSelect.add(bNoSelect);
pSelect.add(bNameSelect);
pSelect.add(bScoreSelect);
pSelect.add(bLogout);
//查询按钮的监听器
bNoSelect.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
java.util.List<Student> student = service.selectAll(1);
clearTable();
for(Student s:student){
insertTable(s);
}
}
});
//查询按钮的监听器
bNameSelect.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
java.util.List<Student> student = service.selectAll(2);
clearTable();
for(Student s:student){
insertTable(s);
}
}
});
//查询按钮的监听器
bScoreSelect.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
java.util.List<Student> student = service.selectAll(3);
clearTable();
for(Student s:student){
insertTable(s);
}
}
});
//退出系统
bLogout.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
//table
tableModel=new DefaultTableModel(tableData,tableTitle);
table=new MyJTable(tableModel);
tableScrollPane=new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED );
//button
buttonPanel.add(insert);
buttonPanel.add(delete);
buttonPanel.add(update);
buttonPanel.add(statistics);
//添加按钮的监听器
insert.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
//如果是添加,则将id=null;
id=null;
tId.setText("");
tId.setEditable(true);
tName.setText("");
tComputer.setText("");
tAdvancedMath.setText("");
tEnglish.setText("");
dialog.setVisible(true);
}
});
//删除按钮的监听器
delete.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
//获得选择删除的行号数组
int[] selected=table.getSelectedRows();
//如果selected的长度为0,说明没有选择要删除的
if(selected.length==0){
JOptionPane.showMessageDialog(jf, "请选择要删除的信息!","提示",JOptionPane.WARNING_MESSAGE );
}else{
//提示是否要进行删除
int flag=JOptionPane.showConfirmDialog(jf, "确认删除吗?","提示",JOptionPane.WARNING_MESSAGE );
//如果选择是,则进行删除
if(flag==JOptionPane.OK_OPTION ){
for(int i=selected.length-1;i>=0;i--){
service.delete((String)tableModel.getValueAt(selected[i], 0));
tableModel.removeRow(selected[i]);
}
}
}
}
});
//统计按钮的监听器
statistics.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
Statistics statistics = service.statistics();
new StatisticsView().init(statistics);
}
});
//修改按钮的监听器
update.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
int row=table.getSelectedRow();
//如果要进行修改,就将id=要修改的学号
id=String.valueOf(table.getValueAt(row, 0));
//设置tId的内容
tId.setText(id);
//设置tId不可修改
tId.setEditable(false);
tName.setText(String.valueOf(table.getValueAt(row, 1)));
tComputer.setText(String.valueOf(table.getValueAt(row, 2)));
tAdvancedMath.setText(String.valueOf(table.getValueAt(row, 3)));
tEnglish.setText(String.valueOf(table.getValueAt(row, 4)));
//设置dialog可见
dialog.setVisible(true);
}
});
jf.setLayout(new BorderLayout());
//设置pSelect在jf的北面
jf.add(pSelect,BorderLayout.NORTH);
//设置pSelect在jf的中心
jf.add(tableScrollPane,BorderLayout.CENTER );
//设置pSelelct在jf的南面
jf.add(buttonPanel,BorderLayout.SOUTH);
jf.pack();
jf.setSize(faceSize);
jf.setLocation((int)(screenSize.width-faceSize.getWidth())/2,(int)(screenSize.height-faceSize.getHeight())/2);
jf.setResizable(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
pId.add(lId);
pId.add(tId);
pName.add(lName);
pName.add(tName);
pComputer.add(lComputer);
pComputer.add(tComputer);
pAdvancedMath.add(lAdvancedMath);
pAdvancedMath.add(tAdvancedMath);
pEnglish.add(lEnglish);
pEnglish.add(tEnglish);
pButton.add(confirm);
pButton.add(cancel);
//确定按钮的监听器
confirm.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
Student student=new Student();
student.setUserId(tId.getText());
student.setPassword(tId.getText());
student.setId(tId.getText());
student.setName(tName.getText());
student.setComputer(toDouble(tComputer.getText()));
student.setAdvancedMath(toDouble(tAdvancedMath.getText()));
student.setEnglish(toDouble(tEnglish.getText()));
if(id!=null){
service.update(student);
}else{
service.insert(student);
}
dialog.dispose();
}
});
//取消按钮的监听器
cancel.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
dialog.dispose();
}
});
box.add(pPhoto);
box.add(pId);
box.add(pName);
box.add(pComputer);
box.add(pAdvancedMath);
box.add(pEnglish);
box.add(pButton);
box.add(pButton);
dialog.add(box);
dialog.setBounds((int)(screenSize.width-280)/2,(int)(screenSize.height-300)/2,280,350);
}
public void insertTable(Student student){
if(student!=null){
String[]newCell=new String[7];
newCell[0]=student.getId();
newCell[1]=student.getName();
newCell[2]=student.getComputer().toString();
newCell[3]=student.getAdvancedMath().toString();
newCell[4]=student.getEnglish().toString();
tableModel.addRow(newCell);
}
}
public void clearTable(){
int rows=tableModel.getRowCount();
for(int i=0 ;i<rows;i++){
tableModel.removeRow(0);
}
}
public Double toDouble(String doubleStr) {
try {
if(doubleStr == null || "".equals(doubleStr)){
return 0D;
}
return Double.valueOf(doubleStr);
} catch (Exception e) {
System.err.println("数据转换错误,分数不是实数");
return 0d;
}
}
}
- StudentService服务类代码
实现了具体的业务,对数据的增删改查统计等功能
package com.paper.service;
import com.paper.entity.Statistics;
import com.paper.entity.Student;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
public class StudentService{
private Map<String, Student> students= new ConcurrentHashMap<String, Student>();
//添加学生信息
public void insert(Student s){
if(! students.containsKey(s.getId())){
students.put(s.getId(),s);
System.out.println("添加成功!");
}else{
System.err.println("学号重复了");
}
}
//删除学生信息
public void delete(String userId){
students.remove(userId);
System.out.print("删除成功!");
}
//修改学生信息
public void update(Student s){
String userId=s.getUserId();
students.remove(userId);
students.put(userId,s);
}
//按学号查询
public Student selectById(String userId){
return students.get(userId);
}
//查询所有学生信息
public List<Student> selectAll(int sorted){
List<Student> studentList = new ArrayList<Student>(students.values());
switch (sorted){
case 1:
studentList = studentList.stream().sorted(Comparator.comparing(Student::getId)).collect(Collectors.toList());
break;
case 2:
studentList = studentList.stream().sorted(Comparator.comparing(Student::getName)).collect(Collectors.toList());
break;
case 3:
studentList = studentList.stream().sorted(Comparator.comparing(student -> {
Double computer = student.getComputer();
Double advancedMath = student.getAdvancedMath();
Double english = student.getEnglish();
Double sum = computer+advancedMath+english;
return sum;
})).collect(Collectors.toList());
break;
default:
break;
}
return studentList;
}
/**
* 统计学生信息
* @return
*/
public Statistics statistics(){
List<Student> studentList = new ArrayList<Student>(students.values());
Integer excellentQty = 0;
Integer goodQty= 0;
Integer averageQty= 0;
Integer passQty= 0;
Integer failQty= 0;
BigDecimal highestScoreD= new BigDecimal(0);
BigDecimal minimumScoreD= new BigDecimal(0);
BigDecimal totalScore= new BigDecimal(0);
for (Student student : studentList) {
//60分以下为不及格
int i = checkScore(student);
switch (i){
case 1:
excellentQty++;
break;
case 2:
goodQty++;
break;
case 3:
averageQty++;
break;
case 4:
passQty++;
break;
case 5:
failQty++;
break;
}
Double computer = student.getComputer();
Double advancedMath = student.getAdvancedMath();
Double english = student.getEnglish();
BigDecimal computerD = new BigDecimal(computer);
BigDecimal advancedMathD = new BigDecimal(advancedMath);
BigDecimal englishD = new BigDecimal(english);
BigDecimal sumDecimal = computerD.add(advancedMathD).add(englishD);
totalScore.add(sumDecimal);
if(highestScoreD.compareTo(sumDecimal) < 0){
highestScoreD = sumDecimal;
}else if(highestScoreD.compareTo(sumDecimal) > 0){
minimumScoreD = sumDecimal;
}
}
BigDecimal excellentQtyD = new BigDecimal(excellentQty);
BigDecimal goodQtyD = new BigDecimal(goodQty);
BigDecimal averageQtyD = new BigDecimal(averageQty);
BigDecimal passQtyD = new BigDecimal(passQty);
BigDecimal failQtyD = new BigDecimal(failQty);
BigDecimal sumD = excellentQtyD.add(goodQtyD).add(averageQtyD).add(passQtyD).add(failQtyD);
Double passRate = sumD.subtract(failQtyD).divide(sumD).doubleValue();
Integer totalQty= sumD.intValue();
Double excellentRatio = excellentQtyD.divide(sumD, 1, BigDecimal.ROUND_HALF_UP).doubleValue();
Double goodRatio = goodQtyD.divide(sumD,1,BigDecimal.ROUND_HALF_UP).doubleValue();
Double averageRatio = averageQtyD.divide(sumD,1,BigDecimal.ROUND_HALF_UP).doubleValue();
Double passRatio = passQtyD.divide(sumD,1,BigDecimal.ROUND_HALF_UP).doubleValue();
Double failRatio = failQtyD.divide(sumD,1,BigDecimal.ROUND_HALF_UP).doubleValue();
Double averageScore = totalScore.divide(sumD).doubleValue();
Statistics statistics = new Statistics();
statistics.setExcellentQty(excellentQty);
statistics.setExcellentRatio(excellentRatio);
statistics.setGoodQty(goodQty);
statistics.setGoodRatio(goodRatio);
statistics.setAverageQty(averageQty);
statistics.setAverageRatio(averageRatio);
statistics.setPassQty(passQty);
statistics.setPassRatio(passRatio);
statistics.setFailQty(failQty);
statistics.setFailRatio(failRatio);
statistics.setTotalQty(totalQty);
statistics.setPassRate(passRate);
statistics.setHighestScore(highestScoreD.doubleValue());
statistics.setMinimumScore(minimumScoreD.doubleValue());
statistics.setAverageScore(averageScore);
return statistics;
}
/**
* 校验学生是分数
* 100分满分
* 总分大于等于270分为优秀 返回1
* 总分大于等于240分且小于270分为良好 返回2
* 总分大于等于210分且小于240分为中等 返回3
* 总分大于等于180分且小于210分为及格 返回4
* 有一科分数小于60分为不及格 返回5
* @param student
*/
private int checkScore(Student student) {
Double computer = student.getComputer();
Double advancedMath = student.getAdvancedMath();
Double english = student.getEnglish();
if(computer<60 || advancedMath<60 || english<60){
return 5;
}
BigDecimal computerD = new BigDecimal(computer);
BigDecimal advancedMathD = new BigDecimal(advancedMath);
BigDecimal englishD = new BigDecimal(english);
BigDecimal sumDecimal = computerD.add(advancedMathD).add(englishD);
if(sumDecimal.compareTo(new BigDecimal(270)) >=0){
return 1;
}else if(sumDecimal.compareTo(new BigDecimal(240)) >=0){
return 2;
}if(sumDecimal.compareTo(new BigDecimal(210)) >=0){
return 3;
}else{
return 4;
}
}
}
- Student类
学生类,具体学生信息
package com.paper.entity;
public class Student extends User{
private String id;
private String name;
private Double computer;
private Double advancedMath;
private Double english;
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 Double getComputer() {
return computer;
}
public void setComputer(Double computer) {
this.computer = computer;
}
public Double getAdvancedMath() {
return advancedMath;
}
public void setAdvancedMath(Double advancedMath) {
this.advancedMath = advancedMath;
}
public Double getEnglish() {
return english;
}
public void setEnglish(Double english) {
this.english = english;
}
}
- 统计表
package com.paper.entity;
public class Statistics {
private Integer excellentQty;
private Double excellentRatio;
private Integer goodQty;
private Double goodRatio;
private Integer averageQty;
private Double averageRatio;
private Integer passQty;
private Double passRatio;
private Integer failQty;
private Double failRatio;
private Integer totalQty;
private Double passRate;
private Double highestScore;
private Double minimumScore;
private Double averageScore;
public Integer getExcellentQty() {
return excellentQty;
}
public void setExcellentQty(Integer excellentQty) {
this.excellentQty = excellentQty;
}
public Double getExcellentRatio() {
return excellentRatio;
}
public void setExcellentRatio(Double excellentRatio) {
this.excellentRatio = excellentRatio;
}
public Integer getGoodQty() {
return goodQty;
}
public void setGoodQty(Integer goodQty) {
this.goodQty = goodQty;
}
public Double getGoodRatio() {
return goodRatio;
}
public void setGoodRatio(Double goodRatio) {
this.goodRatio = goodRatio;
}
public Integer getAverageQty() {
return averageQty;
}
public void setAverageQty(Integer averageQty) {
this.averageQty = averageQty;
}
public Double getAverageRatio() {
return averageRatio;
}
public void setAverageRatio(Double averageRatio) {
this.averageRatio = averageRatio;
}
public Integer getPassQty() {
return passQty;
}
public void setPassQty(Integer passQty) {
this.passQty = passQty;
}
public Double getPassRatio() {
return passRatio;
}
public void setPassRatio(Double passRatio) {
this.passRatio = passRatio;
}
public Integer getFailQty() {
return failQty;
}
public void setFailQty(Integer failQty) {
this.failQty = failQty;
}
public Double getFailRatio() {
return failRatio;
}
public void setFailRatio(Double failRatio) {
this.failRatio = failRatio;
}
public Integer getTotalQty() {
return totalQty;
}
public void setTotalQty(Integer totalQty) {
this.totalQty = totalQty;
}
public Double getPassRate() {
return passRate;
}
public void setPassRate(Double passRate) {
this.passRate = passRate;
}
public Double getHighestScore() {
return highestScore;
}
public void setHighestScore(Double highestScore) {
this.highestScore = highestScore;
}
public Double getMinimumScore() {
return minimumScore;
}
public void setMinimumScore(Double minimumScore) {
this.minimumScore = minimumScore;
}
public Double getAverageScore() {
return averageScore;
}
public void setAverageScore(Double averageScore) {
this.averageScore = averageScore;
}
}
- User类
用户类。如果登录用于登录操作
package com.paper.entity;
public class User {
private String userId;
private String password;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
- StatisticsView类
用于展示统计信息
package com.paper.view;
import com.paper.entity.Statistics;
import com.paper.table.MyJTable;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
public class StatisticsView{
private JFrame jf=new JFrame("学生信息管理系统");
Dimension faceSize=new Dimension(800,600);
private Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
private DefaultTableModel tableModel;
private Object[] tableTitle={"统计学生记录"};
private Object[][]tableData={new Object[]{""}};
private MyJTable table;
private JScrollPane tableScrollPane;
private Box box=Box.createVerticalBox();
public void init(Statistics statistics) {
jf.setResizable(false);
jf.setVisible( true);
jf.pack();
//用户单击窗口的关闭按钮时程序执行的操作
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLocation((int)(screenSize.width-faceSize.getWidth())/2,(int)(screenSize.height-faceSize.getHeight())/2);
//table
tableModel=new DefaultTableModel(tableData,tableTitle);
table=new MyJTable(tableModel);
tableScrollPane=new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED );
//设置pSelect在jf的中心
jf.add(tableScrollPane,BorderLayout.CENTER );
jf.setSize(faceSize);
//封装表格
showStatisticsTable(statistics);
}
private void showStatisticsTable(Statistics statistics) {
clearTable();
insertTable("优秀学生人数:"+ integerToString(statistics.getExcellentQty()));
insertTable("优秀学生占比:"+ doubleToString(statistics.getExcellentRatio()));
insertTable("良好学生人数:"+ integerToString(statistics.getGoodQty()));
insertTable("良好学生占比:"+ doubleToString(statistics.getGoodRatio()));
insertTable("中等学生人数:"+ integerToString(statistics.getAverageQty()));
insertTable("中等学生占比:"+ doubleToString(statistics.getAverageRatio()));
insertTable("及格学生人数:"+ integerToString(statistics.getPassQty()));
insertTable("及格学生人数:"+ doubleToString(statistics.getPassRatio()));
insertTable("不及格学生人数:"+ integerToString(statistics.getFailQty()));
insertTable("不及格学生人数:"+ doubleToString(statistics.getFailRatio()));
insertTable("总学生人数:"+ integerToString(statistics.getTotalQty()));
insertTable("学生及格率:"+ doubleToString(statistics.getPassRate()));
insertTable("学生成绩最高分:"+ doubleToString(statistics.getHighestScore()) +" 分");
insertTable("学生成绩最低分:"+ doubleToString(statistics.getMinimumScore()) +" 分");
insertTable("学生成绩平均分:"+ doubleToString(statistics.getAverageScore()) +" 分");
}
private String doubleToString(Double value) {
if(value == null){
return "";
}
return String.valueOf(value);
}
private String integerToString(Integer value) {
if(value == null){
return "";
}
return String.valueOf(value)+" 人";
}
public void insertTable(String value){
System.out.println(11111);
if(value!=null && ! "".equals(value)){
String[]newCell=new String[1];
newCell[0]=value;
tableModel.addRow(newCell);
}
}
public void clearTable(){
int rows=tableModel.getRowCount();
for(int i=0 ;i<rows;i++){
tableModel.removeRow(0);
}
}
public static void main(String[] args) {
Statistics statistics = new Statistics();
new StatisticsView().init(statistics);
}
}
- MyJTable类
自定义的table类,继承自JTable
package com.paper.table;
import javax.swing.*;
import javax.swing.table.TableModel;
public class MyJTable extends JTable {
/**
*
*/
private static final long serialVersionUID = -3083638370004874364L;
public MyJTable(TableModel dm){
super(dm);
}
//设置表格不可编辑
@Override
public boolean isCellEditable(int rowIndex, int columnIndex){
return false;
}
}
- 调试结果展示
- 主页面
- 添加学生记录
- 显示学生记录
- 删除学生记录
- 查询学生记录
同显示学生记录
- 统计学生记录
- 退出系统
点击关闭退出系统或直接退出