学员信息管理系统
学员信息管理系统
客户端类
View Code
1 package jbit.client; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.Scanner; 6 7 import jbit.entity.Datas; 8 import jbit.entity.Serverserver; 9 import jbit.entity.Student; 10 import jbit.entity.Teacher; 11 12 public class Socket_test { 13 14 /** 15 * @param args 16 */ 17 public static Datas datas = new Datas(); 18 public static Socket_data data = new Socket_data(); 19 public static Scanner scanner = new Scanner(System.in); 20 public static void main(String[] args) { 21 22 23 log(); 24 System.out.println("欢迎使用! 程序结束!..."); 25 26 27 } 28 /** 29 * 登陆 30 */ 31 public static void log() { 32 System.out.println("========================欢迎使用学员信息管理系统========================"); 33 System.out.println("1、登陆"); 34 System.out.println("2、退出"); 35 System.out.println("========================请输入您要选择的操作(1、2)========================"); 36 int len = scanner.nextInt(); 37 if(len == 1) { 38 System.out.print("请输入用户名:"); 39 String username = scanner.next(); 40 System.out.print("请输入密码:"); 41 String password = scanner.next(); 42 Teacher teacher = data.Log(username, password); 43 if(teacher != null) { 44 System.out.println("登陆成功!"); 45 Verification(teacher); 46 }else { 47 System.out.println("用户名或密码错误!登陆失败.."); 48 } 49 50 } 51 } 52 53 /** 54 * 管理视图 55 * @param teacher 56 */ 57 public static void Verification(Teacher teacher) { 58 System.out.println("========================学员信息管理系统========================"); 59 System.out.println("1、查看学员信息\n2、查找学员信息\n3、修改学员信息\n4、增加学员信息\n5、删除学员信息\n6、注销"); 60 System.out.println("========================请输入(1、2、3、4)========================"); 61 int Y = scanner.nextInt(); 62 /** 63 * 1、查看学员信息 64 * 2、查找学员信息 65 * 3、修改学员信息 66 * 4、增加学员信息 67 * 5、删除学员信息 68 * 6、注销 69 */ 70 if (Y == 1) { 71 List<Student> list = new ArrayList<Student>(); 72 list = data.Seek(); 73 System.out.println("学员编号\t" + "学员姓名\t" +"学员年龄\t" + "学员性别\t" + "学员教师\t" + "学员评语\t"); 74 for(Student student : list) { 75 System.out.println(student.getStuid() + "\t" + student.getStuname() + "\t" + student.getStuage() 76 + "\t" + student.getStusex() + "\t" + teacher.getNickname() + "\t" + student.getComments()); 77 } 78 Verification(teacher); 79 }else if (Y == 2) { 80 System.out.print("请输入您想查找的学员编号:"); 81 Student student = data.Query(scanner.nextInt()); 82 System.out.println("学员编号\t" + "学员姓名\t" +"学员年龄\t" + "学员性别\t" + "学员教师\t" + "学员评语\t"); 83 System.out.println(student.getStuid() + "\t" + student.getStuname() + "\t" + student.getStuage() 84 + "\t" + student.getStusex() + "\t" + teacher.getNickname() + "\t" + student.getComments()); 85 Verification(teacher); 86 }else if (Y == 3) { 87 List<Student> list1 = new ArrayList<Student>(); 88 list1 = data.Seek(); 89 System.out.println("学员编号\t" + "学员姓名\t" +"学员年龄\t" + "学员性别\t" + "学员教师\t" + "学员评语\t"); 90 for(Student student1 : list1) { 91 System.out.println(student1.getStuid() + "\t" + student1.getStuname() + "\t" + student1.getStuage()+ "\t" + student1.getStusex() + "\t" + teacher.getNickname() + "\t" + student1.getComments()); 92 } 93 System.out.print("请输入您想修改的学员:"); 94 Student student2 = new Student(); 95 student2.setStuid(scanner.nextInt()); 96 System.out.print("输入您要修改的评语:"); 97 student2.setComments(scanner.next()); 98 datas = (Datas) data.update(student2); 99 if (datas.getFlag().equals(Serverserver.SYS_OK)) { 100 System.out.println("修改成功!"); 101 }else { 102 System.out.println("修改失败"); 103 } 104 Verification(teacher); 105 }else if (Y == 4) { 106 Student student3 = new Student(); 107 System.out.print("请输入学员编号:"); 108 student3.setStuid(scanner.nextInt()); 109 System.out.print("请输入学员姓名:"); 110 student3.setStuname(scanner.next()); 111 System.out.print("请输入学员年龄:"); 112 student3.setStuage(scanner.nextInt()); 113 System.out.print("请输入学员性别:"); 114 String sex = scanner.next(); 115 if(sex.equals("男") || sex.equals("女")) { 116 student3.setStusex(sex); 117 }else { 118 System.out.println("输入错误,重新录入..."); 119 Verification(teacher); 120 } 121 System.out.print("请输入学员评语:"); 122 student3.setComments(scanner.next()); 123 datas = data.plus(student3); 124 if (datas.getFlag().equals(Serverserver.SYS_OK)) { 125 System.out.println("增加成功!"); 126 }else { 127 System.out.println("增加失败"); 128 } 129 Verification(teacher); 130 131 } else if (Y == 5) { 132 List<Student> list2 = new ArrayList<Student>(); 133 list2 = data.Seek(); 134 System.out.println("学员编号\t" + "学员姓名\t" +"学员年龄\t" + "学员性别\t" + "学员教师\t" + "学员评语\t"); 135 for(Student student5 : list2) { 136 System.out.println(student5.getStuid() + "\t" + student5.getStuname() + "\t" + student5.getStuage()+ "\t" + student5.getStusex() + "\t" + teacher.getNickname() + "\t" + student5.getComments()); 137 } 138 System.out.print("请输入您想删除的学员编号:"); 139 Student student4 = new Student(); 140 student4.setStuid(scanner.nextInt()); 141 datas = data.dell(student4); 142 System.out.println(datas.getFlag()); 143 if (datas.getFlag().equals(Serverserver.SYS_OK)) { 144 System.out.println("删除成功!"); 145 }else { 146 System.out.println("删除失败"); 147 } 148 Verification(teacher); 149 } else if (Y == 6) { 150 log(); 151 } 152 else { 153 System.out.println("输入错误! 请重新选择!!!"); 154 Verification(teacher); 155 } 156 } 157 } 158
客户端功能类
View Code
1 package jbit.client; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.Scanner; 6 7 import jbit.entity.Datas; 8 import jbit.entity.Serverserver; 9 import jbit.entity.Student; 10 import jbit.entity.Teacher; 11 12 public class Socket_test { 13 14 /** 15 * @param args 16 */ 17 public static Datas datas = new Datas(); 18 public static Socket_data data = new Socket_data(); 19 public static Scanner scanner = new Scanner(System.in); 20 public static void main(String[] args) { 21 22 23 log(); 24 System.out.println("欢迎使用! 程序结束!..."); 25 26 27 } 28 /** 29 * 登陆 30 */ 31 public static void log() { 32 System.out.println("========================欢迎使用学员信息管理系统========================"); 33 System.out.println("1、登陆"); 34 System.out.println("2、退出"); 35 System.out.println("========================请输入您要选择的操作(1、2)========================"); 36 int len = scanner.nextInt(); 37 if(len == 1) { 38 System.out.print("请输入用户名:"); 39 String username = scanner.next(); 40 System.out.print("请输入密码:"); 41 String password = scanner.next(); 42 Teacher teacher = data.Log(username, password); 43 if(teacher != null) { 44 System.out.println("登陆成功!"); 45 Verification(teacher); 46 }else { 47 System.out.println("用户名或密码错误!登陆失败.."); 48 } 49 50 } 51 } 52 53 /** 54 * 管理视图 55 * @param teacher 56 */ 57 public static void Verification(Teacher teacher) { 58 System.out.println("========================学员信息管理系统========================"); 59 System.out.println("1、查看学员信息\n2、查找学员信息\n3、修改学员信息\n4、增加学员信息\n5、删除学员信息\n6、注销"); 60 System.out.println("========================请输入(1、2、3、4)========================"); 61 int Y = scanner.nextInt(); 62 /** 63 * 1、查看学员信息 64 * 2、查找学员信息 65 * 3、修改学员信息 66 * 4、增加学员信息 67 * 5、删除学员信息 68 * 6、注销 69 */ 70 if (Y == 1) { 71 List<Student> list = new ArrayList<Student>(); 72 list = data.Seek(); 73 System.out.println("学员编号\t" + "学员姓名\t" +"学员年龄\t" + "学员性别\t" + "学员教师\t" + "学员评语\t"); 74 for(Student student : list) { 75 System.out.println(student.getStuid() + "\t" + student.getStuname() + "\t" + student.getStuage() 76 + "\t" + student.getStusex() + "\t" + teacher.getNickname() + "\t" + student.getComments()); 77 } 78 Verification(teacher); 79 }else if (Y == 2) { 80 System.out.print("请输入您想查找的学员编号:"); 81 Student student = data.Query(scanner.nextInt()); 82 System.out.println("学员编号\t" + "学员姓名\t" +"学员年龄\t" + "学员性别\t" + "学员教师\t" + "学员评语\t"); 83 System.out.println(student.getStuid() + "\t" + student.getStuname() + "\t" + student.getStuage() 84 + "\t" + student.getStusex() + "\t" + teacher.getNickname() + "\t" + student.getComments()); 85 Verification(teacher); 86 }else if (Y == 3) { 87 List<Student> list1 = new ArrayList<Student>(); 88 list1 = data.Seek(); 89 System.out.println("学员编号\t" + "学员姓名\t" +"学员年龄\t" + "学员性别\t" + "学员教师\t" + "学员评语\t"); 90 for(Student student1 : list1) { 91 System.out.println(student1.getStuid() + "\t" + student1.getStuname() + "\t" + student1.getStuage() 92 + "\t" + student1.getStusex() + "\t" + teacher.getNickname() + "\t" + student1.getComments()); 93 } 94 System.out.print("请输入您想修改的学员:"); 95 Student student2 = new Student(); 96 student2.setStuid(scanner.nextInt()); 97 System.out.print("输入您要修改的评语:"); 98 student2.setComments(scanner.next()); 99 datas = (Datas) data.update(student2); 100 if (datas.getFlag().equals(Serverserver.SYS_OK)) { 101 System.out.println("修改成功!"); 102 }else { 103 System.out.println("修改失败"); 104 } 105 Verification(teacher); 106 }else if (Y == 4) { 107 Student student3 = new Student(); 108 System.out.print("请输入学员编号:"); 109 student3.setStuid(scanner.nextInt()); 110 System.out.print("请输入学员姓名:"); 111 student3.setStuname(scanner.next()); 112 System.out.print("请输入学员年龄:"); 113 student3.setStuage(scanner.nextInt()); 114 System.out.print("请输入学员性别:"); 115 String sex = scanner.next(); 116 if(sex.equals("男") || sex.equals("女")) { 117 student3.setStusex(sex); 118 }else { 119 System.out.println("输入错误,重新录入..."); 120 Verification(teacher); 121 } 122 System.out.print("请输入学员评语:"); 123 student3.setComments(scanner.next()); 124 datas = data.plus(student3); 125 if (datas.getFlag().equals(Serverserver.SYS_OK)) { 126 System.out.println("增加成功!"); 127 }else { 128 System.out.println("增加失败"); 129 } 130 Verification(teacher); 131 132 } else if (Y == 5) { 133 List<Student> list2 = new ArrayList<Student>(); 134 list2 = data.Seek(); 135 System.out.println("学员编号\t" + "学员姓名\t" +"学员年龄\t" + "学员性别\t" + "学员教师\t" + "学员评语\t"); 136 for(Student student5 : list2) { 137 System.out.println(student5.getStuid() + "\t" + student5.getStuname() + "\t" + student5.getStuage() 138 + "\t" + student5.getStusex() + "\t" + teacher.getNickname() + "\t" + student5.getComments()); 139 } 140 System.out.print("请输入您想删除的学员编号:"); 141 Student student4 = new Student(); 142 student4.setStuid(scanner.nextInt()); 143 datas = data.dell(student4); 144 System.out.println(datas.getFlag()); 145 if (datas.getFlag().equals(Serverserver.SYS_OK)) { 146 System.out.println("删除成功!"); 147 }else { 148 System.out.println("删除失败"); 149 } 150 Verification(teacher); 151 } else if (Y == 6) { 152 log(); 153 } 154 else { 155 System.out.println("输入错误! 请重新选择!!!"); 156 Verification(teacher); 157 } 158 } 159 } 160
类封装传输类
View Code
1 package jbit.entity; 2 3 import java.io.Serializable; 4 import java.util.List; 5 6 7 8 /** 9 * 序列化传输对象 10 * 在客户端与服务器端之间传递的数据对象 11 * 12 * @author xu.sun 13 * 14 */ 15 public class Datas implements Serializable { 16 private String flag;// 传递的标识 17 private Teacher teacher;// 老师对象 18 private Student student;// 学员对象 19 private List<Student> studentList;//学员集合 20 public Datas() { 21 } 22 public Student getStudent() { 23 return student; 24 } 25 26 public void setStudent(Student student) { 27 this.student = student; 28 } 29 30 public String getFlag() { 31 return flag; 32 } 33 34 public void setFlag(String flag) { 35 this.flag = flag; 36 } 37 38 public Teacher getTeacher() { 39 return teacher; 40 } 41 42 public void setTeacher(Teacher teacher) { 43 this.teacher = teacher; 44 } 45 46 public List<Student> getStudentList() { 47 return studentList; 48 } 49 50 public void setStudentList(List<Student> studentList) { 51 this.studentList = studentList; 52 } 53 54 }
服务器数据和方法类
View Code
1 package jbit.entity; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 public class Serverserver { 7 private static Teacher teacher = null; 8 private static List<Student> stulist = null; 9 public static final String SYS_LOGIN = "LOGIN"; //登陸 10 public static final String SYS_SEEK = "SEEK"; //查看 11 public static final String SYS_FIND = "FIND"; //查找 12 public static final String SYS_UPDATE = "UPDATE"; //修改 13 public static final String SYS_PLUS = "PLUS"; //增加 14 public static final String SYS_DELETE = "DELETE"; //刪除 15 public static final String SYS_OK = "OK"; //ok 16 public static final String SYS_NO = "NO"; //no 17 18 19 /** 20 * 初始化数据 21 */ 22 static { 23 teacher = new Teacher(); 24 stulist = new ArrayList<Student>(); 25 26 teacher.setId(1); 27 teacher.setUsername("root"); 28 teacher.setPassword("toor"); 29 teacher.setNickname("刘老师"); 30 31 stulist.add(new Student(1, "何清", 1, 24, "男", "还好!")); 32 stulist.add(new Student(2, "周东", 1, 23, "男", "还可以!")); 33 stulist.add(new Student(3, "刘欢", 1, 25, "女", "比较好!")); 34 stulist.add(new Student(4, "周维", 1, 23, "男", "混日子呀!")); 35 36 } 37 38 39 40 /** 41 * 验证登陆 42 * @param username 43 * @param password 44 * @return 45 */ 46 public static Teacher doLogin(String username,String password){ 47 //验证用户名和密码 48 if(username.equals(teacher.getUsername())&& password.equals(teacher.getPassword())){ 49 return teacher; 50 }else{ 51 return null; 52 } 53 } 54 55 /** 56 * 查找学员 57 * @param len 58 * @return 59 */ 60 public static Student Query(int id) { 61 Student student = stulist.get(id-1); 62 return student; 63 64 } 65 66 /** 67 * 查看说有学员信息 68 * @return 69 */ 70 public static List<Student> Seek() { 71 return stulist; 72 } 73 74 /** 75 * 修改学员信息 76 * @return 77 * @return 78 */ 79 public static void update (Student student) { 80 81 for(int i = 0; i < stulist.size(); i ++) { 82 if(stulist.get(i).getStuid() == student.getStuid()) { 83 stulist.get(i).setComments(student.getComments()); 84 } 85 } 86 } 87 88 /** 89 * 增加学员信息 90 * @param student 91 */ 92 public static void plus(Student student) { 93 stulist.add(student); 94 } 95 96 /** 97 * 删除学员信息 98 * @param student 99 */ 100 public static void dele(Student student) { 101 for(int i = 0; i < stulist.size(); i ++) { 102 if(stulist.get(i).getStuid() == student.getStuid() - 1) { 103 stulist.remove(student.getStuid() - 1); 104 } 105 } 106 } 107 108 }
学生类、 老师类
View Code
1 package jbit.entity; 2 3 import java.io.Serializable; 4 5 public class Student implements Serializable { 6 7 private int stuid; //学员编号 8 private String stuname; //学员姓名 9 private int teacherid; //教员编号 10 private int stuage; //学员年龄 11 private String stusex; //学员性别 12 private String comments; //学员评语 13 14 public Student(int stuid, String stuname, int teacherid, int stuage, 15 String stusex, String comments) { 16 this.stuid = stuid; 17 this.stuname = stuname; 18 this.teacherid = teacherid; 19 this.stuage = stuage; 20 this.stusex = stusex; 21 this.comments = comments; 22 23 } 24 public Student() { 25 } 26 27 public int getStuid() { 28 return stuid; 29 } 30 public void setStuid(int stuid) { 31 this.stuid = stuid; 32 } 33 public void setStuname(String stuname) { 34 this.stuname = stuname; 35 } 36 public String getStuname() { 37 return stuname; 38 } 39 public int getTeacherid() { 40 return teacherid; 41 } 42 public void setTeacherid(int teacherid) { 43 this.teacherid = teacherid; 44 } 45 public int getStuage() { 46 return stuage; 47 } 48 public void setStuage(int stuage) { 49 this.stuage = stuage; 50 } 51 public String getStusex() { 52 return stusex; 53 } 54 public void setStusex(String stusex) { 55 this.stusex = stusex; 56 } 57 public String getComments() { 58 return comments; 59 } 60 public void setComments(String comments) { 61 this.comments = comments; 62 } 63 64 65 66 67 68 }
View Code
1 package jbit.entity; 2 3 import java.io.Serializable; 4 5 public class Teacher implements Serializable { 6 7 private int id; //教员编号 8 private String username; //登陆用户名 9 private String password; //登陆密码 10 private String nickname; //教员姓名 11 12 public Teacher() { 13 14 } 15 16 public Teacher(String username, String password){ 17 this.username = username; 18 this.password = password; 19 } 20 21 public int getId() { 22 return id; 23 } 24 public void setId(int id) { 25 this.id = id; 26 } 27 public String getUsername() { 28 return username; 29 } 30 public void setUsername(String username) { 31 this.username = username; 32 } 33 public String getPassword() { 34 return password; 35 } 36 public void setPassword(String password) { 37 this.password = password; 38 } 39 public String getNickname() { 40 return nickname; 41 } 42 public void setNickname(String nickname) { 43 this.nickname = nickname; 44 } 45 46 47 48 }
服务器类
View Code
1 package jbit.server; 2 3 import java.io.IOException; 4 import java.io.ObjectInputStream; 5 import java.io.ObjectOutputStream; 6 import java.net.ServerSocket; 7 import java.net.Socket; 8 import java.util.ArrayList; 9 import java.util.List; 10 11 import jbit.entity.Datas; 12 import jbit.entity.Serverserver; 13 import jbit.entity.Student; 14 import jbit.entity.Teacher; 15 16 public class Server_test { 17 18 /** 19 * @param args 20 * @throws IOException 21 * @throws ClassNotFoundException 22 */ 23 public static void main(String[] args) throws IOException, ClassNotFoundException { 24 ServerSocket ss = null; 25 Socket s = null; 26 ObjectInputStream objin = null; 27 ObjectOutputStream objout = null; 28 try {//创建服务端点 29 ss = new ServerSocket(1314); 30 31 while(true) { 32 33 //获取客户端对象 34 s = ss.accept(); 35 36 System.out.println(s.getInetAddress().getHostAddress()); 37 38 //创建输入流 接受客服端数据 39 objin = new ObjectInputStream(s.getInputStream()); 40 objout = new ObjectOutputStream(s.getOutputStream()); 41 Serverserver server = new Serverserver(); 42 Datas datas = new Datas(); 43 44 datas = (Datas) objin.readObject(); 45 if (datas.getFlag().equals(Serverserver.SYS_LOGIN)) { 46 Teacher teacher = server.doLogin(datas.getTeacher().getUsername(), datas.getTeacher().getPassword()); 47 objout.writeObject(teacher); 48 } else if (datas.getFlag().equals(Serverserver.SYS_SEEK)) { 49 List<Student> list = new ArrayList<Student>(); 50 list = Serverserver.Seek(); 51 objout.writeObject(list); 52 53 } else if(datas.getFlag().equals(Serverserver.SYS_FIND)) { 54 Student student = new Student(); 55 student = server.Query(datas.getStudent().getStuid()); 56 objout.writeObject(student); 57 }else if (datas.getFlag().equals(Serverserver.SYS_UPDATE)) { 58 Serverserver.update(datas.getStudent()); 59 datas.setFlag(Serverserver.SYS_OK); 60 objout.writeObject(datas); 61 }else if (datas.getFlag().equals(Serverserver.SYS_PLUS)) { 62 Serverserver.plus(datas.getStudent()); 63 datas.setFlag(Serverserver.SYS_OK); 64 objout.writeObject(datas); 65 }else if (datas.getFlag().equals(Serverserver.SYS_DELETE)) { 66 Serverserver.dele(datas.getStudent()); 67 datas.setFlag(Serverserver.SYS_OK); 68 objout.writeObject(datas); 69 } 70 71 } 72 } finally { 73 // 关闭 74 objin.close(); 75 objout.close(); 76 s.close(); 77 ss.close(); 78 System.out.println("服务器终止"); 79 } 80 } 81 }