📄 studentscore.java
字号:
package edu.hit.software;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class StudentScore
{
public static void main(String[] args)
{
ManageScoreFrame frame=new ManageScoreFrame();
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screenSize=kit.getScreenSize();
int screenWidth=screenSize.width;
int screenHeigth=screenSize.height;
int frameWidth=frame.getWidth();
int frameHeigth=frame.getHeight();
frame.setLocation((screenWidth-frameWidth)/2, (screenHeigth-frameHeigth)/2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
}
}
class ManageScoreFrame extends JFrame
{
private static final long serialVersionUID = 1L;
public ManageScoreFrame()
{
setTitle("ManageScore2.0");
ManageScorePanel panel=new ManageScorePanel();
add(panel);
pack();
}
}
class ManageScorePanel extends JPanel
{
private static final long serialVersionUID = 1L;
public ManageScorePanel()
{
setLayout(new BorderLayout());
JLabel InNameLabel=new JLabel("姓名");
JLabel InCourseLabel=new JLabel("课程名");
JLabel InScoreLabel=new JLabel("分数");
JLabel SearchNameLabel=new JLabel("姓名");
JLabel SearchCourseLabel=new JLabel("课程名");
InNameField=new JTextField(5);
InCourseField=new JTextField(5);
InScoreField=new JTextField(5);
SearchNameField=new JTextField(5);
SearchCourseField=new JTextField(5);
InputButton=new JButton("录入");
SearchButton=new JButton("查找");
TextArea=new JTextArea(10,35);
InputButton.addActionListener(new InputAction());
SearchButton.addActionListener(new SearchAction());
TextArea.setEditable(false);
InputPanel=new JPanel(new FlowLayout(FlowLayout.LEFT));
InputPanel.add(InputButton);
InputPanel.add(InNameLabel);
InputPanel.add(InNameField);
InputPanel.add(InCourseLabel);
InputPanel.add(InCourseField);
InputPanel.add(InScoreLabel);
InputPanel.add(InScoreField);
SearchPanel=new JPanel(new FlowLayout(FlowLayout.LEFT));
SearchPanel.add(SearchButton);
SearchPanel.add(SearchNameLabel);
SearchPanel.add(SearchNameField);
SearchPanel.add(SearchCourseLabel);
SearchPanel.add(SearchCourseField);
TextPanel=new JPanel();
TextPanel.add(TextArea);
add(InputPanel,BorderLayout.NORTH);
add(SearchPanel,BorderLayout.CENTER);
add(TextPanel,BorderLayout.SOUTH);
}
public class InputAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
try{
String s1,s2,s3;
s1=InNameField.getText();
s2=InCourseField.getText();
s3=InScoreField.getText();
if(s1.equals(""))throw new IOException("请输入学生姓名");
if(s2.equals(""))throw new IOException("请输入课程名");
if(s3.equals(""))throw new IOException("请输入分数");
int score=Integer.parseInt(s3);
if(score<0||score>100)throw new IOException("请输入0~100的分数");
data=new Data(s1,s2,score);
database.addData(data);
InNameField.setText("");
InCourseField.setText("");
InScoreField.setText("");
TextArea.setText(" 姓名 课程名 得分\n" +
" -------------------------\n"
+" "+s1+" "+s2+" "+score+"\n录入成功");
}
catch(IOException e)
{
TextArea.setText("\n"+e.getMessage());
}
catch(NumberFormatException e)
{
TextArea.setText("\n"+"输入分数时请用数字");
}
}
}
public class SearchAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String student,course,result="";
student=SearchNameField.getText();
course=SearchCourseField.getText();
try
{
if(student.equals("")&&course.equals(""))
throw new IOException("请输入查询的条件");
if(student.equals("")&&!course.equals(""))
result=database.searchData_Course(course);
if(!student.equals("")&&course.equals(""))
result=database.searchData_Student(student);
if(!student.equals("")&&!course.equals(""))
result=database.searchData_Both(student,course);
TextArea.setText(" 姓名 课程名 得分\n" +
" -------------------------\n"
+result);
SearchNameField.setText("");
SearchCourseField.setText("");
}
catch(IOException e)
{
TextArea.setText("\n"+e.getMessage());
}
}
}
private JPanel InputPanel;
private JPanel SearchPanel;
private JPanel TextPanel;
private JTextField InNameField;
private JTextField InCourseField;
private JTextField InScoreField;
private JTextField SearchNameField;
private JTextField SearchCourseField;
private JButton InputButton;
private JButton SearchButton;
private JTextArea TextArea;
private Data data;
private DataBase database=new DataBase();
}
class DataBase
{
public DataBase()
{
}
public void addData(Data d) throws IOException
{
if(!searchStudent(d.getStudentName()))throw new IOException("学生不存在");
if(!searchCourse(d.getCourseName()))throw new IOException("课程不存在");
data=new ArrayList<Data>(100);
try
{
BufferedReader in=new BufferedReader(new FileReader("data.dat"));
String line;
while((line=in.readLine())!=null)
{
StringTokenizer t = new StringTokenizer(line, "|");
String s1=t.nextToken();
String s2=t.nextToken();
String s3=t.nextToken();
data.add(new Data(s1,s2,Integer.parseInt(s3)));
}
in.close();
}
catch(IOException e)
{
e.printStackTrace();
}
boolean isExist=false;
int i=0;
for(Data e:data)
{
i++;
if(e.getStudentName().equals(d.getStudentName())
&&e.getCourseName().equals(d.getCourseName()))
{
e.setScore(d.getScore());
isExist=true;
}
}
System.out.println(isExist+" i="+i);
if(!isExist)data.add(d);
try
{
PrintWriter out = new PrintWriter(new FileWriter("data.dat"));
for(Data e:data)
{
out.append(e.getStudentName()+"|"
+e.getCourseName()+"|"
+e.getScore()+"\n");
}
out.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public boolean searchStudent(String s)
{
boolean judge=false;
try
{
BufferedReader in=new BufferedReader(new FileReader("student.dat"));
String line;
while((line=in.readLine())!=null)
{
if(line.equals(s))
judge=true;
}
in.close();
}
catch(IOException e)
{
e.printStackTrace();
}
return judge;
}
public boolean searchCourse(String c)
{
boolean judge=false;
try
{
BufferedReader in=new BufferedReader(new FileReader("course.dat"));
String line;
while((line=in.readLine())!=null)
{
if(line.equals(c))
judge=true;
}
in.close();
}
catch(IOException e)
{
e.printStackTrace();
}
return judge;
}
public String searchData_Student(String s) throws IOException
{
String result="";
if(!searchStudent(s))throw new IOException("学生不存在");
try
{
BufferedReader in=new BufferedReader(new FileReader("data.dat"));
String line;
while((line=in.readLine())!=null)
{
StringTokenizer t = new StringTokenizer(line, "|");
String s1=t.nextToken();
String s2=t.nextToken();
String s3=t.nextToken();
if(s1.equals(s))
{
result+=" "+s+" "+s2+" "+s3+"\n";
}
}
in.close();
}
catch(IOException e)
{
e.printStackTrace();
}
return result;
}
public String searchData_Course(String c) throws IOException
{
String result="";
if(!searchCourse(c))throw new IOException("课程不存在");
try
{
BufferedReader in=new BufferedReader(new FileReader("data.dat"));
String line;
while((line=in.readLine())!=null)
{
StringTokenizer t = new StringTokenizer(line, "|");
String s1=t.nextToken();
String s2=t.nextToken();
String s3=t.nextToken();
if(s2.equals(c))
{
result+=" "+s1+" "+c+" "+s3+"\n";
}
}
in.close();
}
catch(IOException e)
{
e.printStackTrace();
}
return result;
}
public String searchData_Both(String s,String c) throws IOException
{
if(!searchStudent(s))throw new IOException("学生不存在");
if(!searchCourse(c))throw new IOException("课程不存在");
String result="";
try
{
BufferedReader in=new BufferedReader(new FileReader("data.dat"));
String line;
while((line=in.readLine())!=null)
{
StringTokenizer t = new StringTokenizer(line, "|");
String s1=t.nextToken();
String s2=t.nextToken();
String s3=t.nextToken();
if(s1.equals(s)&&s2.equals(c))
{
result+=" "+s+" "+c+" "+s3+"\n";
}
}
in.close();
}
catch(IOException e)
{
e.printStackTrace();
}
return result;
}
private ArrayList<Data> data;
}
class Data
{
public Data(String student,String course,int score)
{
studentName=student;
courseName=course;
this.score=score;
}
public String getStudentName()
{
return studentName;
}
public String getCourseName()
{
return courseName;
}
public int getScore()
{
return score;
}
public void setScore(int s)
{
score=s;
}
private String studentName;
private String courseName;
private int score;
}
class Student
{
public Student(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
private String name;
}
class Course
{
public Course(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
private String name;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -