Java写一个新闻类

  
1
package com.jredu.ch10; 2 3 import java.util.Scanner; 4 5 /** 6 * 新闻类 7 * @author Administrator 8 * 9 */ 10 public class News { 11 12 private int id;//编号 13 private String title;//标题 14 private String content;//内容 15 private String addUser;//添加人 16 private String imgPath;//图片 17 private String pubDate;//发布时间 18 private boolean isShow;//是否显示 19 20 /** 21 * 无参构造 22 */ 23 public News(Scanner sc) { 24 super(); 25 System.out.print("请输入新闻编号"); 26 this.id=sc.nextInt(); 27 // TODO Auto-generated constructor stub 28 init(sc); 29 } 30 31 /** 32 * 带编号的有参构造 33 * @param id 34 */ 35 public News(Scanner sc,int id) { 36 super(); 37 // TODO Auto-generated constructor stub 38 this.id=id; 39 // TODO Auto-generated constructor stub 40 init(sc); 41 } 42 43 public News(int id, String title, String content, String addUser, String imgPath, String pubDate, boolean isShow) { 44 super(); 45 this.id = id; 46 this.title = title; 47 this.content = content; 48 this.addUser = addUser; 49 this.imgPath = imgPath; 50 this.pubDate = pubDate; 51 this.isShow = isShow; 52 } 53 54 public void init(Scanner sc) { 55 System.out.print("请输入标题"); 56 this.title=sc.next(); 57 System.out.print("请输入内容"); 58 this.content=sc.next(); 59 System.out.print("请输入发布者"); 60 this.addUser=sc.next(); 61 System.out.print("请输入发布时间"); 62 this.pubDate=sc.next(); 63 System.out.print("请输入图片路径"); 64 this.imgPath=sc.next(); 65 System.out.print("是否显示(y/n)"); 66 this.isShow=sc.next().equals("y")?true:false; 67 } 68 69 public int getId() { 70 return id; 71 } 72 73 public void setId(int id) { 74 this.id = id; 75 } 76 77 public String getTitle() { 78 return title; 79 } 80 81 public void setTitle(String title) { 82 this.title = title; 83 } 84 85 public String getContent() { 86 return content; 87 } 88 89 public void setContent(String content) { 90 this.content = content; 91 } 92 93 public String getAddUser() { 94 return addUser; 95 } 96 97 public void setAddUser(String addUser) { 98 this.addUser = addUser; 99 } 100 101 public String getImgPath() { 102 return imgPath; 103 } 104 105 public void setImgPath(String imgPath) { 106 this.imgPath = imgPath; 107 } 108 109 public String getPubDate() { 110 return pubDate; 111 } 112 113 public void setPubDate(String pubDate) { 114 this.pubDate = pubDate; 115 } 116 117 public boolean isShow() { 118 return isShow; 119 } 120 121 public void setShow(boolean isShow) { 122 this.isShow = isShow; 123 } 124 125 }
package com.jredu.ch10;
/**
 * 新闻业务类
 * @author Administrator
 *
 */
public class NewsService {
    //所有的新闻
    private News[] news1=new News[100];
    private int index;//新闻的下标
    /**
     * 添加新闻
     * @param news
     * @return 添加新闻成功时,返回true,添加失败时返回false
     */
    public boolean addNews(News news) {
        if(index<news1.length) {
            for(int i=0;i<index;i++) {
                if(news1[i].getId()==news.getId()) {
                    System.out.println("该新闻编号已经存在,无法添加");
                    return false;
                }
            }
            news1[index]=news;
            index++;
            return true;
        }
        return false;
    }
    
    /**
     * 删除新闻
     * @param id
     * @return 当新闻删除成功时,返回true,删除失败时,返回false
     */
    public boolean deleteNews(int id) {
        //循环遍历所有的新闻
        for(int i=0;i<index;i++) {
            //遍历每一条新闻不为空,并且id等于传入的id时
            if(news1[i]!=null&&news1[i].getId()==id) {
                //删除这条新闻
                news1[i]=null;
                return true;
            }
        }
        return false;
    }
    
    /**
     * 更新新闻
     * @param news
     */
    public boolean updateNews(News news) {
        //循环遍历所有的新闻
        for(int i=0;i<index;i++) {
            //遍历每一条新闻不为空,并且id等于传入的id时
            if(news1[i]!=null&&news1[i].getId()==news.getId()) {
                //更新这条新闻
                news1[i]=news;
                return true;
            }
        }
        return false;
    }
    
    /**
     * 显示新闻
     */
    public void showNews() {
        News news=null;
        for(int i=0;i<index;i++) {
            if(news1[i]!=null&&news1[i].isShow()) {
                news=news1[i];
                System.out.println("编号:"+news.getId()+"\t标题:"+news.getTitle());
                System.out.println("作者:"+news.getAddUser()+"\t发布时间:"+news.getPubDate());
                System.out.println("\t"+news.getContent());
                System.out.println("图片地址:"+news.getImgPath());
            }
        }
    }
    
}
 1 package com.jredu.ch10;
 2 
 3 import java.util.Scanner;
 4 
 5 public class NewsTest {
 6 
 7     public static void main(String[] args) {
 8         NewsService service=new NewsService();
 9         Scanner sc=new Scanner(System.in);
10         //进入系统,让用户选择功能
11         System.out.println("*********欢迎进入新闻管理系统*********");
12         while(true) {
13             System.out.println("1.添加新闻\t2.删除新闻\t3.更新新闻\t4.查看新闻\tn.退出系统");
14             System.out.println("请选择功能");
15             String code=sc.next();
16             boolean isReturn=false;
17             switch(code) {
18                 //添加新闻
19                 case "1":
20                     News news=new News(sc);
21                     boolean isAdd=service.addNews(news);
22                     if(!isAdd) {
23                         System.out.println("添加新闻失败");
24                     }
25                     break;
26                 //删除新闻
27                 case "2":
28                     System.out.print("请输入编号");
29                     boolean isDelete=service.deleteNews(sc.nextInt());
30                     if(!isDelete) {
31                         System.out.println("删除新闻失败");
32                     }
33                     break;
34                 //更新新闻
35                 case "3":
36                     System.out.print("请输入编号");
37                     News news1=new News(sc,sc.nextInt());
38                     boolean isUpdate=service.updateNews(news1);
39                     if(!isUpdate) {
40                         System.out.println("更新新闻失败");
41                     }
42                     break;
43                 //查看新闻
44                 case "4":
45                     service.showNews();
46                     break;
47                 //退出系统
48                 default:
49                     isReturn=true;
50                     break;
51             }
52             if(isReturn) {
53                 break;
54             }
55         }
56         System.out.println("谢谢使用,再见");
57         sc.close();
58     }
59     
60 }

 

posted @ 2017-03-29 12:23  天冷吃冰棍儿  阅读(1111)  评论(0编辑  收藏  举报