java学生管理系统(简易)(二)---学生对象建立与登录

学生登录与查看成绩+验证是否是注册的用户所绑定的信息


将一些登录查看成绩的代码写在前面了。。。学生基本信息的获取在后面直接生成了。。

 
 
package StudentMajor;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

// 学生要实现Comparable接口--->以便后期可以使用TreeSet排序(因为涉及到成绩嘛)
public class StudentUser implements Comparable<StudentUser> {

	private JFrame jf;
	private JButton jb1,jb2;
	private JTextField jtf2,jtf3;
	private JPanel jp1,jp2,jp3;
	private JLabel jlb2,jlb3;//标签
	final private File file = new File("e:\\abc\\file\\UserName.txt");
	private String name;// 姓名
	private String num;// 学号
	private int score1,score2,score3;// 成绩
	private int sum;// 总成绩
	private JTable jtable;
	private JScrollPane jsp;
	private int countAStudentShow;
	private String UserName;
	
	public StudentUser(String name,String num,int a,int b,int c) {
		this.name = name;
		this.num = num;
		this.score1 = a;
		this.score2 = b;
		this.score3 = c;
		this.sum = a+b+c;
	}
	public StudentUser() {
		
	}
	public StudentUser(String name,String num) {
		this.name = name;
		this.num = num;
	}

	public void init(String UserName) {
		if(!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.UserName = UserName;
		jf = new JFrame("学生信息绑定");
		jf.setSize(300,150);
		jf.setLocationRelativeTo(null);
		jf.setLayout(new FlowLayout());
		jf.setResizable(false);
		
		jb1 = new JButton("确定");
		jb2 = new JButton("返回");
		

		
		jlb2 = new JLabel("请绑定学号:");
		jtf2 = new JTextField(10);
		jlb3 = new JLabel("请绑定姓名:");
		jtf3 = new JTextField(10);
		
		
		jp1 = new JPanel();
		jp2 = new JPanel();
		jp3 = new JPanel();
	
		jp1.add(jlb3);
		jp1.add(jtf3);
		
		jp2.add(jlb2);
		jp2.add(jtf2);
		
		jp3.add(jb1);
		jp3.add(jb2);
		
		
		
		
		jf.add(jp2);
		jf.add(jp1);
		jf.add(jp3);
		
		myEvent();
		
		jf.setVisible(true);
		
	}
	public void myEvent() {
		//窗体关闭
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jb1.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
			//	jtf1.getText();
				int option;
				if(countAStudentShow==0) {
					StudentUser stu = StudentTools.searchForStudentByName(jtf2.getText(),jtf3.getText());
					
					if(stu==null)
						JOptionPane.showMessageDialog(null, "姓名和学号输入错误!", "提示信息", JOptionPane.WARNING_MESSAGE);
					else{
						boolean flag = false;
						flag = WriteToFile(file,UserName,jtf2.getText());
						if(!flag)
							return;
						option = JOptionPane.showConfirmDialog(null, "请核实您的信息    学号:"+jtf2.getText()+"姓名是"+
								jtf3.getText());
						if(option==JOptionPane.YES_OPTION) {
							jlb2.setVisible(false);
							jtf2.setVisible(false);
							jtf3.setVisible(false);
							jp1.setVisible(false);
							jb1.setVisible(false);
							jsp = jspOfAStudent(stu);
							jf.add(jsp);
							jf.pack();
							countAStudentShow++;
						}
						
					}
					
				}else {
					jf.remove(jsp);
					StudentUser stu = StudentTools.searchForStudentByName(jtf2.getText(),jtf3.getText());
					if(stu==null)
						JOptionPane.showMessageDialog(null, "没有该学号的学生!", "提示信息", JOptionPane.WARNING_MESSAGE);
					else {
						option = JOptionPane.showConfirmDialog(null, "请核实您的信息:  学号:"+jtf2.getText()+"姓名是"+
								jtf3.getText());
						if(option==JOptionPane.YES_NO_OPTION) {
							jtf3.getText();
							jsp = jspOfAStudent(stu);
							jf.add(jsp);
							jf.pack();
						}
						
					}
					
				}

			}
		
		});
		
		jb2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new Login().init();
				jf.dispose();
				
			}
		});
		
		
	}
	
	
	// 学生输入都正确,证明已经注册过或者新注册的用户--》可查看成绩
	public JScrollPane jspOfAStudent(StudentUser s) {
		if(s==null)
			return null;
		// 定义二维数组作为表格数据
		 Object[][] tableData =
		 {
		   new Object[]{"","","","","",""},
		 };
		 // 定义一维数据作为列标题
		 Object[] columnTitle = {"姓名", "学号" , "数学","语文","英语","总分"};
		 
		 //给表格中填入数据
		
		  tableData[0][0] = s.getName();
		  tableData[0][1] = s.getNum();
		  tableData[0][2] = s.getScore1();
		  tableData[0][3] = s.getScore2();
		  tableData[0][4] = s.getScore3();
		  tableData[0][5] = s.getSum();
		 
		 
		 jtable = new JTable(tableData,columnTitle);
		 jtable.enable(false);//让这个表格不可编辑
		 return new JScrollPane(jtable);
		
	}
	
	
	
	public boolean WriteToFile(File file,String name,String num)  {
		if(file==null) {
			System.out.println("没有文件");
			return false;
		}
		BufferedWriter bufw = null;
		try {
			 bufw = new BufferedWriter(new FileWriter(file,true));
			 int temp = readFileOfStudent(file,name,num);
			 System.out.println(temp);
			if(temp==-1) {
				
				JOptionPane.showMessageDialog(null, "学号输入错误!", "提示信息", JOptionPane.ERROR_MESSAGE);
				//jtf.setText("");
				//jpf.setText("");
				return false;
		
			}else if(temp==1){
				return true;
			}
			
			// temp=0,该学生没有注册过,则写到注册信息的文件里面
			 bufw.write(name+"    "+num);
			 
			 bufw.newLine();
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			 try {
				 if(bufw!=null)
					 bufw.close();
			} catch (IOException e) {
				
				System.out.println("文件关闭失败");
			}
		}
		return true;
	}
	
	
	
	// 学生里面有个读文件方法
	
	public int readFileOfStudent(File file,String UserName,String num) {
		BufferedReader bufr = null;
		int flag = -1;
		try {
			bufr = new BufferedReader(new FileReader(file));
		} catch (FileNotFoundException e) {
			new RuntimeException("该文件不存在!");
		}
		int count=0;
		// 此处用到泛型
		String len = null;
		String infoReg = " +";
		String[]s = null;
		try {
			
			while((len = bufr.readLine())!=null) {
				count++;//计数-->因为你不仅一个学生去登录。文件里面存了好多学生
						//      如果这个学生没有注册过的话(判断时要将文件便利完)
				// 取出姓名和学号
				s = len.split(infoReg);
				// System.out.println(s[0]+"  "+s[1]);
				
				// 用户输入的姓名和学号都存在
				if(s[0].equals(UserName)&&s[1].equals(num)) {
					flag=1;
					return flag;
					
				// 用户输入的	姓名学号都不存在(此用户没用绑定过)-->就要将此信息写入到文件
				}else if((!s[0].equals(UserName))&&(!s[1].equals(num))) {
					flag = 0;
					// System.out.println(UserName+num);
					
				// 用户已经绑定过了。但是有些信息输入错误
				}else {
					return -1;
				}
				
			}
		} catch (IOException e) {
			System.out.println("读取失败!");
			e.printStackTrace();
		}finally {
			try {
				if(bufr!=null)
					bufr.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(count==0)
			return 0;
		return flag;
	}

	
	// 以下为自动生成的代码
	public int getScore1() {
		return score1;
	}


	public void setScore1(int score1) {
		this.score1 = score1;
	}


	public int getScore2() {
		return score2;
	}


	public void setScore2(int score2) {
		this.score2 = score2;
	}


	public int getScore3() {
		return score3;
	}


	public void setScore3(int score3) {
		this.score3 = score3;
	}


	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNum() {
		return num;
	}

	public void setNum(String num) {
		this.num = num;
	}

	
	public int getSum() {
		return sum;
	}

	public void setSum(int sum) {
		this.sum = sum;
	}

	@Override	// 万一要是采用HashSet的话,就可以用上了,顺便就复写掉方法
	public int hashCode() {
		return this.hashCode() + this.num.hashCode();
	}

	@Override	// 要用TreeSet肯定要复写equals方法啦
	public boolean equals(Object obj) {
		if (!(obj instanceof StudentUser))
			return false;
		StudentUser stu = (StudentUser) obj;
		// 此处认为姓名跟学号相同的视为同一个学生
		return this.getName().equals(stu.getName()) && this.getNum().equals(stu.getNum());
	}

	@Override // 按照成绩降序比较(最后逆转比较器就ok了成绩就升序了)
	public int compareTo(StudentUser o) {
		int num = new Integer(this.sum).compareTo(new Integer(o.sum));
		if (num == 0)
			return this.name.compareTo(o.name);
		return num;
	}

}




猜你喜欢

转载自blog.csdn.net/qq_41044665/article/details/80556324
今日推荐