java学习5:公告管理案例

xiaoxiao2021-02-28  108

公告管理案例 需求: --公告的添加和显示 --在指定位置处插入公告 --删除公告 --修改公告 类: --公告属性:编号id,标题 tile,创建人 creator , 创建时间 create Time --公告类方法:构造方法,获取属性值方法,set方法   代码如下: Notice类: package com.imooc.set; import java.util.Date; public class Notice {     //属性:     private  int id;     private  String title;     private  String creator;     private Date createTime;     public Notice(int id, String title, String creator, Date createTime) {         this.id = id;         this.title = title;         this.creator = creator;         this.createTime = createTime;     }     public int getId() {         return id;     }     public void setId(int id) {         this.id = id;     }     public String getTitle() {         return title;     }     public void setTitle(String title) {         this.title = title;     }     public String getCreator() {         return creator;     }     public void setCreator(String creator) {         this.creator = creator;     }     public Date getCreateTime() {         return createTime;     }     public void setCreateTime(Date createTime) {         this.createTime = createTime;     } } 主类: package com.imooc.set; import java.util.ArrayList; import java.util.Date; public class NoticeTest {     public static void main(String[] args) {         //创建notice类对象,生成3条公告         Notice notice1 = new Notice(1, "欢迎来到慕课网!", "管理员", new Date());         Notice notice2 = new Notice(2, "请同学们按时提交作业!", "老师", new Date());         Notice notice3 = new Notice(3, "考情通知!", "老师", new Date());         //添加公告         ArrayList noticeList = new ArrayList();         noticeList.add(notice1);         noticeList.add(notice2);         noticeList.add(notice3);         //显示公告         System.out.println("公告的内容为:");         for (int i = 0; i < noticeList.size(); i++) {             System.out.println((i + 1) + ":" + ((Notice) (noticeList.get(i))).getTitle());         }         System.out.println("***********************");         //在第一条公告后添加一条新公告         Notice notice4 = new Notice(4, "在线编辑器可以使用啦!", "管理员", new Date());         noticeList.add(1, notice4);         System.out.println("添加完成后公告的内容为:");         for (int i = 0; i < noticeList.size(); i++) {             System.out.println((i + 1) + ":" + ((Notice) (noticeList.get(i))).getTitle());         }         System.out.println("***********************");         //删除按时完成作业         noticeList.remove(2);         System.out.println("删除完成后公告的内容为:");         for (int i = 0; i < noticeList.size(); i++) {             System.out.println((i + 1) + ":" + ((Notice) (noticeList.get(i))).getTitle());         }         System.out.println("***********************");         //将第二条公告改为:JAVA在线编辑器可以使用啦!         notice4.setTitle("JAVA在线编辑器可以使用啦!");         noticeList.set(1,notice4);         System.out.println("修改完成后公告的内容为:");         for (int i = 0; i < noticeList.size(); i++) {             System.out.println((i + 1) + ":" + ((Notice) (noticeList.get(i))).getTitle());         }     }

}

感谢慕课网!

转载请注明原文地址: https://www.6miu.com/read-20949.html

最新回复(0)