⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 curriculum.java

📁 J2ME程序设计实例教程的源码
💻 JAVA
字号:
import java.io.*;

/**
 * 该类描述了课表中课程的信息,同时提供了课程对象序列化和反序列化的方法
 */
public class Curriculum {
    private int id = -1;        //
    private String name;        //课程名
    private String teacher;     //任课老师
    private String classroom;   //教室
    private int time;           //上课时间
    private String remark;      //备注信息
    
    public static final String[] WEEKS_LABEL = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
    public static final String[] JIE_LABEL = {"第1~2节", "第3~4节", "第5~6节", "第7~8节", "第9~10节"};
    
    public Curriculum(String name) {
        this.name = name;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getTeacher() {
        return teacher;
    }
    
    public void setTeacher(String teacher) {
        this.teacher = teacher;
    }
    
    public String getClassroom() {
        return classroom;
    }
    
    public void setClassroom(String classroom) {
        this.classroom = classroom;
    }
    
    //获取上课的日期,0:星期一, 1:星期二, ..., 7:星期日
    public int getDay() {
        return time/10;
    }
    
    //获取上课时间,0:第1~2节, 1:第3~4节, ..., 4:第5~6节
    public int getTime() {
        return time%10;
    }
    
    //设置上课日期,时间
    public void setDate(int day, int t) {
        time = day*10 + t;
    }
    
    public String getRemark() {
        return remark;
    }
    
    public void setRemark(String remark) {
        this.remark = remark;
    }
    
    public int getID() {
        return id;
    }
    
    public void setID(int id) {
        this.id = id;
    }
    
    public String toString() {
        return "课程: " + name + "\n任课教师:" + teacher 
                + "\n教室:" + classroom + "\n上课时间: " + WEEKS_LABEL[getDay()] + "   " + JIE_LABEL[getTime()] 
                + "\n备注信息:\n   " + remark;
    }
    
    //使用字节数组data中的数据更新当前课程对象
    public void setData(byte[] data) throws IOException {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        DataInputStream dis = new DataInputStream(bais);
        
        this.setName(dis.readUTF());
        this.setTeacher(dis.readUTF());
        this.setClassroom(dis.readUTF());
        int temptime = dis.readInt();
        this.setDate(temptime/10, temptime%10);
        this.setRemark(dis.readUTF());
    }
    
    //将当前课程对象序列化为字节数组
    public byte[] sequence() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        dos.writeUTF(name);
        dos.writeUTF(teacher);
        dos.writeUTF(classroom);
        dos.writeInt(time);
        dos.writeUTF(remark);
        dos.close();
        baos.close();
        
        return baos.toByteArray();
    }
    
    //反序列化,使用字节数组中的数据生成一个新的课程对象
    public static Curriculum unSequence(byte[] data) throws IOException {
        Curriculum curriculum = new Curriculum(null);
        curriculum.setData(data);
        
        return curriculum;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -