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.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class StudentTools {
	
	private JFrame jf;//窗口
	private JPanel jp1,jp2,jp3,jp4,jp5,jp6,jp7;//面板
	private JLabel jlb1,jlb2,jlb3,jlb4,jlb5,jlb6;//标签
	private JButton jb1,jb2,jb3;//按钮
	private JTextField jtf1,jtf2,jtf3,jtf4,jtf5,jtf6;//文本
	public static Set<StudentUser> set = new TreeSet<StudentUser>();
	
	
	private  File file = new File("e:\\abc\\file\\StudentInfo.txt");
//	public static void main(String[] args) {
//		new StudentTools().init();
//		showStudents();
//	}
	public StudentTools() {
		
	}
	
	public void init() {
		if(!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//<1>设置主页面
		jf = new JFrame("学生管理系统");
		jf.setSize(300, 300);//窗体大小
		jf.setLocationRelativeTo(null);//居中显示
		jf.setLayout(new FlowLayout());//设置布局
		jf.setResizable(false); //设置窗口不能改变大小
		//一初始化就有面板
		jp1 = new JPanel();
		jp2 = new JPanel();
		jp3 = new JPanel();
		jp4 = new JPanel();
		jp5 = new JPanel();
		jp6 = new JPanel();
		//(1)设置标签
		jlb1 = new JLabel("姓   名");
		jlb2 = new JLabel("学   号");
		jlb3 = new JLabel("数   学");
		jlb4 = new JLabel("语   文");
		jlb5 = new JLabel("英   语");
		//(2)设置按钮
		jb1 = new JButton("确定");
		jb2 = new JButton("重置");
		jb3 = new JButton("返回");
		
		//(3)创建文本框
		jtf1 = new JTextField(10);
		jtf2 = new JTextField(10);
		jtf3 = new JTextField(10);
		jtf4 = new JTextField(10);
		jtf5 = new JTextField(10);
		
		//<2>添加部件(添加到面板)
		
		jp1.add(jlb1);
		jp1.add(jtf1);
		
		jp2.add(jlb2);
		jp2.add(jtf2);
		
		jp3.add(jlb3);
		jp3.add(jtf3);
		
		jp4.add(jlb4);
		jp4.add(jtf4);
		
		jp5.add(jlb5);
		jp5.add(jtf5);
		
		jp6.add(jb1);
		jp6.add(jb2);
		jp6.add(jb3);
		
		//(1)将组件都添加到窗体上
		
		jf.add(jp1);
		jf.add(jp2);
		jf.add(jp3);
		jf.add(jp4);
		jf.add(jp5);
		jf.add(jp6);
		//<3>添加窗体事件
		myEvent();
		
		//<4>设置窗体可见
		jf.setVisible(true);
	}
	
	
	
	
	
	// 基本事件
	public void myEvent() {
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jb1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(jtf1.getText().isEmpty()||jtf2.getText().isEmpty()||jtf3.getText().isEmpty()||
						jtf4.getText().isEmpty()||jtf5.getText().isEmpty()) {
					JOptionPane.showMessageDialog(null,"请输入完整的信息" ,"提示信息", JOptionPane.WARNING_MESSAGE);
					return;
				}
				else {
					
					
					String s1 = jtf1.getText();
					String s1Reg = ".{2,5}";
					if(!s1.matches(s1Reg)) {
						JOptionPane.showMessageDialog(null,"姓名最多5位,最少两位哦" ,"提示信息", JOptionPane.WARNING_MESSAGE);
						return;
					}
					String s2 = jtf2.getText();
					String s2Reg = "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]";
					if(!s2.matches(s2Reg)) {
						JOptionPane.showMessageDialog(null,"学号只能是八位纯数字哦" ,"提示信息", JOptionPane.WARNING_MESSAGE);
						return;
					}
					String s3 = jtf3.getText();
					String s4 = jtf4.getText();
					String s5 = jtf5.getText();
					String jtfReg = "[1-9][0-9]?";
					if(!(s3.matches(jtfReg)&&s4.matches(jtfReg)&&s5.matches(jtfReg))) {
						JOptionPane.showMessageDialog(null,"成绩有不合法输入(0-99分)" ,"提示信息", JOptionPane.WARNING_MESSAGE);
						return;
					}
					
				}
				int a = Integer.parseInt(jtf3.getText());
				int b = Integer.parseInt(jtf4.getText());
				int c = Integer.parseInt(jtf5.getText());
				boolean flag = false;
				StudentUser stu = new StudentUser(jtf1.getText(),jtf2.getText(),a,b,c);
				stu.setSum(a+b+c);	
				StudentTools.set.add(stu);
				//flag = writeToStuFile(file,set);
				flag = writeToStuFile(file,stu);
				if(flag)
					JOptionPane.showMessageDialog(null,"成绩保存成功" ,"提示信息", JOptionPane.WARNING_MESSAGE);
				else
					JOptionPane.showMessageDialog(null,"该学生已存在" ,"提示信息", JOptionPane.WARNING_MESSAGE);
				
			}
	
		});
		jb2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				jtf1.setText("");
				jtf2.setText("");
				jtf3.setText("");
				jtf4.setText("");
				jtf5.setText("");
				
			}

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

			@Override
			public void actionPerformed(ActionEvent e) {
				
				new TeacherUser().init();
				jf.dispose();
			}
			
		});
	}
	
	
	
	//写进文件
	public boolean writeToStuFile(File file,StudentUser stu) {
	
		if(readFileOfStudent(file,stu.getName(),stu.getNum())){
			
			return false;
		}
		if(file==null) {
			System.out.println("没有文件");
			return false;
		}
		BufferedWriter bufw = null;
		try {
			 bufw = new BufferedWriter(new FileWriter(file,true));
			
			 bufw.write(stu.getName()+"  "+stu.getNum()+"  "+stu.getScore1()+"  "+stu.getScore2()+"  "+stu.getScore3()+"  "+stu.getSum());
			 
			 bufw.newLine();
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			 try {
				 if(bufw!=null)
					 bufw.close();
			} catch (IOException e) {
				
				System.out.println("文件关闭失败");
			}
		}
		return true;
		
		
	}
	
	
	// 从文件中读取
	public boolean readFileOfStudent(File file,String name,String num) {
		BufferedReader bufr = null;
		//System.out.println(name+num);
		boolean flag = false;
		try {
			bufr = new BufferedReader(new FileReader(file));
		} catch (FileNotFoundException e) {
			new RuntimeException("该文件不存在!");
		}
		
		String len = null;
		String infoReg = " +";
		String[]s = null;
		try {
			
			while((len = bufr.readLine())!=null) {
				s = len.split(infoReg);
				//System.out.println(s[0]+"  "+s[1]);
				if(s[1].equals(num)) {
					return true;
				}
			}
		} catch (IOException e) {
			System.out.println("读取失败!");
			e.printStackTrace();
		}
		
		return false;
	}
	
	//展示所有学生
	public static void showStudents() {

		Set<StudentUser> set = getStudentsFromFile();
		StudentTools.set = set;
		for(StudentUser s : set ) {
			System.out.println(s.getName()+s.getNum()+s.getScore1()+s.getScore2()+s.getScore3()+s.getSum());
		}
		
	}
	// 设置学生属性
	public static void setStudents(StudentUser stu,int score1,int score2,int score3) {

		stu.setScore1(score1);
		stu.setScore2(score2);
		stu.setScore3(score3);
		stu.setSum(stu.getScore1()+stu.getScore2()+stu.getScore3());
		
	}
	
	// 返回所有学生的集合
	public static Set<StudentUser> getStudentsFromFile(){
		Comparator<StudentUser>cmp = Collections.reverseOrder();
		 return getStudentsFromFile(cmp);
	}
	
	// 返回所有学生的集合
	public static Set<StudentUser> getStudentsFromFile(Comparator<StudentUser>cmp){
		
		File file = new File("e:\\abc\\file\\StudentInfo.txt");
		if(!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		set = new TreeSet<StudentUser>(cmp);
		BufferedReader bufr = null;
		try {
			bufr = new BufferedReader(new FileReader(file));
		} catch (FileNotFoundException e) {
			new RuntimeException("该文件不存在!");
		}
		
		String len = null;
		String infoReg = " +";
		String[]s = null;
		try {
			
			while((len = bufr.readLine())!=null) {
				s = len.split(infoReg);
				int a = Integer.parseInt(s[2]);
				int b = Integer.parseInt(s[3]);
				int c = Integer.parseInt(s[4]);
				StudentUser stu = new StudentUser(s[0],s[1],a,b,c);
				stu.setSum(a+b+c);	
				set.add(stu);
			}
			
		} catch (IOException e) {
			System.out.println("读取失败!");
			e.printStackTrace();
		}finally {
			try {
				if(bufr!=null)
					bufr.close();
			} catch (IOException e) {
			
				e.printStackTrace();
			}
		}
		
		return set;
		
	}
	
	
	//查找---学号
	public static StudentUser searchForStudent(String num) {
		Set<StudentUser> set = getStudentsFromFile();
		for(StudentUser stu : set) {
			if(stu.getNum().equals(num)) {
				return stu;
			}
		}
		return null;
	}
	
	//查找--姓名/学号
	
	public static StudentUser searchForStudentByName(String num,String name) {
		Set<StudentUser> set = getStudentsFromFile();
		for(StudentUser stu : set) {
			if(stu.getName().equals(name)&&stu.getNum().equals(num)) {
				return stu;
			}
		}
		return null;
	}
	
	//姓名学号查找
	public static StudentUser showAStudentToPanel(String num,String name) {
		return searchForStudentByName(num,name);
	}
	//学号
	public static StudentUser showAStudentToPanel(String num) {
		return searchForStudent(num);
	}
	
	public static boolean showAStudent(String num) {
		StudentUser s = searchForStudent(num);
		System.out.println("姓名:   "+s.getName()+"学号:   "+s.getNum()+
				"数学:   "+s.getScore1()+"语文:   "+s.getScore2()+
				"英语:   "+s.getScore3()+"总分:   "+s.getSum());
		return true;
	}
	// 删除
	
	public static boolean deleteStudent(String num) {
		boolean flag = false;
		Set<StudentUser> set = getStudentsFromFile();
		Iterator<StudentUser> it = set.iterator();
		while(it.hasNext()) {
			StudentUser stu = it.next();
			if(stu.getNum().equals(num)) {
				flag = true;
				it.remove();
			}
		}
		
		StudentTools.set = set;
		return flag;
	}
	
	//修改
	
	public static boolean modificationStudent(String name,String num,int score1,int score2,int score3) {
		boolean flag = false;
		Set<StudentUser> set = getStudentsFromFile();
		StudentUser tempStu = null;
		Iterator<StudentUser> it = set.iterator();
		while(it.hasNext()) {
			StudentUser stu = it.next();
			if(stu.getNum().equals(num)) {
				flag = true;
				tempStu = stu;
			}
		}
	
		if(flag) {

			StudentUser newStu = new StudentUser(name,num,score1,score2,score3);
			newStu.setSum(score1+score2+score3);	
			StudentTools.set.add(newStu);
			
		}else {
			return false;
		}
		
		StudentTools.set = set;
		return true;
	}
	
	
	
	
	//写进文件--set
	public static boolean writeToStuFile(File file,Set<StudentUser> set) {
		if(file==null) {
			System.out.println("没有文件");
			try {
				file.createNewFile();
			} catch (IOException e) {
				System.out.println("文件创建失败!");
				return false;
			}
		}
		BufferedWriter bufw = null;
		try {
			 bufw = new BufferedWriter(new FileWriter(file,true));
			 for(StudentUser stu : set) {
				 bufw.write(stu.getName()+"  "+stu.getNum()+"  "+stu.getScore1()+"  "+stu.getScore2()+"  "+stu.getScore3()+"  "+stu.getSum());
				 bufw.newLine();
			 }
			 
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			 try {
				 if(bufw!=null)
					 bufw.close();
			} catch (IOException e) {
				
				System.out.println("文件关闭失败");
			}
		}
		return true;
		
		
	}
	
	// 读文件 --set
	public boolean readFileOfStudent(File file,Set<StudentUser> set) {
		BufferedReader bufr = null;
		try {
			
			if(file==null)
				file.createNewFile();
			bufr = new BufferedReader(new FileReader(file));
		} catch (IOException e) {
			e.getStackTrace();
			return false;
		}
		
		String len = null;
		String infoReg = " +";
		String[]s = null;
		try {
			
			while((len = bufr.readLine())!=null) {
				s = len.split(infoReg);
				
				int a = Integer.parseInt(s[0]);
				int b = Integer.parseInt(s[1]);
				int c = Integer.parseInt(s[2]);
				StudentUser stu = new StudentUser(s[0],s[1],a,b,c);
				stu.setSum(a+b+c);	
				StudentTools.set.add(stu);
		
			}
		} catch (IOException e) {
			System.out.println("读取失败!");
			e.printStackTrace();
		}
		
		return true;
	}


}
	

猜你喜欢

转载自blog.csdn.net/qq_41044665/article/details/80556479