TCP服务器和客户端学生管理系统(第一天)

本文介绍了一个基于TCP的服务器和客户端学生管理系统,包括服务器端的资源管理和Json处理工具类,以及客户端的控制层、TCP通信和操作服务。系统实现了增删改查功能,使用ArrayList存储Student信息。
摘要由CSDN通过智能技术生成

TCP服务器和客户端学生管理系统(第一天)

一,工具类(util包中)第二天有补充

1.1 资源管理close工具类

package com.qfedu.student.system.client.util;

import java.io.Closeable;
import java.io.IOException;

/**
 * 资源管理Close工具类
 *
 * @author Anonymous 2020/3/17 14:33
 */
public class CloseUtil {

    /**
     * 可以调用close方法的类都可以使用
     *
     * @param res 不定长参数Closeable接口实现类,处理资源关闭问题
     */
    public static void closeAll(Closeable... res) {
        for (Closeable re : res) {
            try {
                if (re != null) {
                    re.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

1.2 Json数据格式操作工具类

package com.qfedu.student.system.client.util;

import com.alibaba.fastjson.JSON;

import com.qfedu.student.system.client.entity.Student;

import java.util.List;

/**
 * Json数据格式操作工具类
 *
 *
 *      服务器和客户端直接的数据交互方式
 *          3. 解析Json格式数据 ==> Object
 *          4. Object ==> 转换为Json格式数据
 *          5. 解析Json格式数据 ==> List<Object>
 *          6. List<Object> ==> 转换为Json格式数据
 *
 * @author Anonymous 2020/3/17 11:31
 */
public class JsonUtil {
    /**
     * Json字符串转换成对应Student类对象
     *
     * @param jsonString Json格式字符串
     * @return Student类对象
     */
    public static Student jsonToStudent(String jsonString) {
        return JSON.parseObject(jsonString, Student.class);
    }

    /**
     * Student类对象转换成Json格式字符串
     *
     * @param student Student类对象数据
     * @return 符合Json格式的String类型字符串
     */
    public static String studentToJson(Student student) {
        return JSON.toJSONString(student);
    }

    /**
     * Json格式字符串转换成保存Student类型的List集合
     *
     * @param jsonString Json格式字符串
     * @return 保存Student类型List集合
     */
    public static List<Student> jsonToStudentList(String jsonString) {
        return JSON.parseArray(jsonString, Student.class);
    }

    /**
     * 保存Student类型的List集合转换成一个Json格式字符串
     *
     * @param list 保存Student类型的List集合
     * @return Json格式字符串
     */
    public static String studentListToJson(List<Student> list) {
        return JSON.toJSONString(list);
    }
}

二,Student实体类(entity包中)

严格按照JavaBean规范实现
1. 所有成员变量全部私有化
2. 必须提供一个无参数构造方法
3. 完成对应成员变量的setter和getter
4. 这里建议使用【包装类】

代码演示

package com.qfedu.student.system.client.entity;

/**
 * Student实体类,要求符合JavaBean规范
 * 对象关系映射(Object Relational Mapping) ORM
 *          Object Java中的任何一个对象 保存映射到对应的 数据保存 ==> 文件 XML JSON Database
 *
 * @author Anonymous 2020/3/17 10:00
 */
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;
    }

 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值