package ljt.client.controller;
import ljt.client.service.ClientService;
import ljt.client.tcp.ClientTcp;
public class ClientController {
private ClientService service = new ClientService();
private ClientTcp client = new ClientTcp();
private static final String SUCCESS = "success";
private static final String NOT_FOUND = "查无此人";
private static final String EMPTY_SET = "无数据";
public void login() {
String s = service.loginMessage();
client.sendMessage("login" + s);
String s1 = client.receiveMessage();
if (SUCCESS.equals(s1)) {
System.out.println("登陆成功");
} else {
System.out.println("登陆失败");
logout();
}
}
public void logout() {
client.sendMessage("close:");
System.exit(0);
}
public void addStudent() {
String studentJsonString = service.getStudent();
String message = "addStudent:" + studentJsonString;
client.sendMessage(message);
String s = client.receiveMessage();
System.out.println(s);
}
public void removeStudnet() {
Integer id = findOne();
if (id != -1 && service.confirm()) {
client.sendMessage("deleteStu:" + id);
System.out.println(client.receiveMessage());
} else {
System.out.println("删除操作撤销");
}
}
public void modifyStudent() {
Integer id = service.getStringId();
client.sendMessage("findOne:" + id);
String s = client.receiveMessage();
if (NOT_FOUND.equals(s)) {
System.out.println(s);
return;
}
String jsonString = service.modifyStudent(s);
client.sendMessage("modifyStu:" + jsonString);
String s1 = client.receiveMessage();
System.out.println(s1);
}
public Integer findOne() {
Integer id = service.getStringId();
client.sendMessage("finOne:" + id);
String s = client.receiveMessage();
if (NOT_FOUND.equals(s)) {
System.out.println(s);
return -1;
}
service.showStudent(s);
return id;
}
public void findAll() {
client.sendMessage("findAll:");
String s = client.receiveMessage();
if (EMPTY_SET.equals(s)) {
System.out.println(s);
} else {
service.showStudentList(s);
}
}
public void sortStu() {
Integer choose = service.sortChoose();
if (11 == choose) {
return;
}
client.sendMessage("sortStu:" + choose);
String s = client.receiveMessage();
if (EMPTY_SET.equals(s)) {
System.out.println(s);
} else {
service.showStudentList(s);
}
}
public void filterStu() {
String s = service.ageFilter();
client.sendMessage("filterStu:" + s);
String s1 = client.receiveMessage();
if (EMPTY_SET.equals(s)) {
System.out.println(s);
} else {
service.showStudentList(s1);
}
}
}
2.entity包
package ljt.client.entity;
public class Student {
private Integer id;
private String name;
private Boolean gender;
private Integer age;
private Integer mathScore;
private Integer chnScore;
private Integer engScore;
private Integer totalScore;
private Integer rank;
public Student() {
}
public Student(Integer id, String name, Boolean gender, Integer age, Integer mathScore, Integer chnScore, Integer engScore, Integer totalScore, Integer rank) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.mathScore = mathScore;
this.chnScore = chnScore;
this.engScore = engScore;
this.totalScore = totalScore;
this.rank = rank;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getGender() {
return gender;
}
public void setGender(Boolean gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getMathScore() {
return mathScore;
}
public void setMathScore(Integer mathScore) {
this.mathScore = mathScore;
}
public Integer getChnScore() {
return chnScore;
}
public void setChnScore(Integer chnScore) {
this.chnScore = chnScore;
}
public Integer getEngScore() {
return engScore;
}
public void setEngScore(Integer engScore) {
this.engScore = engScore;
}
public Integer getTotalScore() {
return totalScore;
}
public void setTotalScore(Integer totalScore) {
this.totalScore = totalScore;
}
public Integer getRank() {
return rank;
}
public void setRank(Integer rank) {
this.rank = rank;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", gender=" + gender +
", age=" + age +
", mathScore=" + mathScore +
", chnScore=" + chnScore +
", engScore=" + engScore +
", totalScore=" + totalScore +
", rank=" + rank +
'}';
}
}
3.mainproject包
package ljt.client.mainproject;
import ljt.client.controller.ClientController;
import ljt.client.util.ClientOperation;
import java.lang.reflect.InvocationTargetException;
import java.util.Scanner;
public class ClientMain {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
ClientController controller = new ClientController();
Class cls = ClientController.class;
controller.login();
int choose = 0;
while (true) {
System.out.println("用户您好,骚磊学生系统");
System.out.println("1. 查看所有学生");
System.out.println("2. 查看指定学生");
System.out.println("3. 增加学生信息");
System.out.println("4. 修改指定学生");
System.out.println("5. 删除指定学生");
System.out.println("6. 指定信息排序");
System.out.println("7. 指定信息筛选");
System.out.println("8. 退出");
choose = sc.nextInt();
ClientOperation[] values = ClientOperation.values();
for (ClientOperation value : values) {
if (choose == value.getId()) {
cls.getMethod(value.getMethodName()).invoke(controller);
break;
}
}
}
}
}
4.service包
package ljt.client.service;
import ljt.client.entity.Student;
import ljt.client.util.JsonUtil;
import java.util.List;
import java.util.Scanner;
public class ClientService {
private static Scanner sc = new Scanner(System.in);
public String getStudent() {
System.out.println("请输入学生的姓名:");
String name = sc.nextLine();
System.out.println("请输入学生的年龄:");
Integer age = sc.nextInt();
System.out.println("请输入学生的性别 男 或者 女:");
Boolean gender = sc.nextLine().charAt(0) != '男';
System.out.println("请输入学生的语文成绩:");
Integer chnScore = sc.nextInt();
System.out.println("请输入学生的数学成绩:");
Integer mathScore = sc.nextInt();
System.out.println("请输入学生的英语成绩:");
Integer engScore = sc.nextInt();
Integer totalScore = chnScore + mathScore + engScore;
Student student = new Student(0, name, gender, age, mathScore, chnScore, engScore, totalScore, 0);
return JsonUtil.studentToJson(student);
}
public void showStudentList(String jsonString) {
List students = JsonUtil.jsonToStudentList(jsonString);
for (Student student : students) {
System.out.println(student);
}
}
public void showStudent(String jsonString) {
Student student = JsonUtil.jsonToStudent(jsonString);
System.out.println(student);
}
public String modifyStudent(String jsonString) {
Student student = JsonUtil.jsonToStudent(jsonString);
int choose = 0;
boolean flag = false;
while (true) {
System.out.println("ID: " + student.getId() + " Name: " + student.getName());
System.out.println("Age: " + student.getAge() + " Gender: " + (student.getGender() ? "女" : "男"));
System.out.println("chnScore: " + student.getChnScore() + " mathScore: " + student.getMathScore());
System.out.println("engScore: " + student.getEngScore() + " TotalScore: " + student.getTotalScore());
System.out.println("Rank: " + student.getRank());
System.out.println("1. 修改学生姓名:");
System.out.println("2. 修改学生年龄:");
System.out.println("3. 修改学生性别:");
System.out.println("4. 修改学生语文成绩:");
System.out.println("5. 修改学生数学成绩:");
System.out.println("6. 修改学生英语成绩:");
System.out.println("7. 退出");
choose = sc.nextInt();
sc.nextLine();
switch (choose) {
case 1:
System.out.println("请输入学生的名字:");
String name = sc.nextLine();
student.setName(name);
break;
case 2:
System.out.println("请输入学生的年龄:");
Integer age = sc.nextInt();
student.setAge(age);
break;
case 3:
System.out.println("请输入学生的性别 男 或者 女:");
Boolean gender = sc.nextLine().charAt(0) != '男';
student.setGender(gender);
break;
case 4:
System.out.println("请输入学生的语文成绩:");
Integer chnScore = sc.nextInt();
student.setTotalScore(student.getTotalScore() + chnScore - student.getChnScore());
student.setChnScore(chnScore);
break;
case 5:
System.out.println("请输入学生的数学成绩:");
Integer mathScore = sc.nextInt();
student.setTotalScore(student.getTotalScore() + mathScore - student.getMathScore());
student.setMathScore(mathScore);
break;
case 6:
System.out.println("请输入学生的英语成绩:");
Integer engScore = sc.nextInt();
student.setTotalScore(student.getTotalScore() + engScore - student.getEngScore());
student.setEngScore(engScore);
break;
case 7:
flag = true;
break;
default:
System.out.println("选择错误");
break;
}
if (flag) {
break;
}
}
return JsonUtil.studentToJson(student);
}
public Integer getStringId() {
System.out.println("请输入学生ID号:");
return sc.nextInt();
}
public boolean confirm() {
System.out.println("确认当前操作吗? Y or N");
return "Y".equalsIgnoreCase(sc.next());
}
public Integer sortChoose() {
System.out.println("1. 年龄升序");
System.out.println("2. 年龄降序");
System.out.println("3. 数学成绩降序");
System.out.println("4. 数学成绩升序");
System.out.println("5. 语文成绩降序");
System.out.println("6. 语文成绩升序");
System.out.println("7. 英语成绩降序");
System.out.println("8. 英语成绩升序");
System.out.println("9. 总成绩降序");
System.out.println("10. 总成绩升序");
System.out.println("11. 排序取消");
int choose = sc.nextInt();
return Math.min(choose, 11);
}
public String ageFilter() {
System.out.println("请输入需要展示年龄的范围 最小值和最大值");
int min = sc.nextInt();
int max = sc.nextInt();
if (min > max) {
return max + "," + min;
} else {
return min + "," + max;
}
}
public String loginMessage() {
System.out.println("欢迎来到涛涛学生系统:");
System.out.println("请输入用户名:");
String userName = sc.next();
System.out.println("请输入密码:");
String password = sc.next();
return userName + ":" + password;
}
}
5.tcp包
package ljt.client.tcp;
import ljt.client.util.CloseUtil;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
public class ClientTcp {
private SocketChannel socket = null;
private static String host = "192.168.1.101";
private static int port = 8848;
public ClientTcp() {
try {
socket = SocketChannel.open();
socket.configureBlocking(false);
InetSocketAddress address = new InetSocketAddress(host, port);
if (!socket.connect(address)) {
while (!socket.finishConnect()) {
Thread.sleep(2000);
}
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
public String receiveMessage() {
int length = -1;
String str = null;
ByteBuffer buffer = ByteBuffer.allocate(1024);
try {
StringBuilder stb = new StringBuilder();
while ((length = socket.read(buffer)) == 0) {}
stb.append(new String(buffer.array(), 0, length));
while ((length = socket.read(buffer)) > 0) {
stb.append(new String(buffer.array()), 0, length);
}
str = stb.toString();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public void sendMessage(String message) {
ByteBuffer buffer = ByteBuffer.wrap(message.getBytes());
try {
socket.write(buffer);
} catch (IOException e) {
e.printStackTrace();
} finally {
if ("close".equals(message)) {
CloseUtil.closrAll(socket);
}
}
}
}
util包先不放了