初学java--利用面向对象概念做一个简单的新闻系统

系统属性部分:
package com.jredu.teach;
/**
 * 新闻,实体类,javabean,pojo,domain对象
 * @author Administrator
 */
public class News {
	
	private int id;//新闻编号
	private String title;//新闻标题
	private String author;//作者
	private String pubDate;//发布时间
	private String type;//分类
	private String remark;//概要
	private String img;//图片
	private boolean isShow;
	
	public News() {
		
	}
	
	public News(int id, String title, String author, String pubDate) {
		super();
		this.id = id;
		this.title = title;
		this.author = author;
		this.pubDate = pubDate;
	}

	public News(int id, String title, String author, String pubDate, String type, String remark, String img,
			boolean isShow) {
		super();
		this.id = id;
		this.title = title;
		this.author = author;
		this.pubDate = pubDate;
		this.type = type;
		this.remark = remark;
		this.img = img;
		this.isShow = isShow;
	}

	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 getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getPubDate() {
		return pubDate;
	}

	public void setPubDate(String pubDate) {
		this.pubDate = pubDate;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getImg() {
		return img;
	}

	public void setImg(String img) {
		this.img = img;
	}

	public boolean isShow() {
		return isShow;
	}

	public void setShow(boolean isShow) {
		this.isShow = isShow;
	}
	
	
	
	
	
}


系统操作部分:

package com.jredu.teach;

public class NewsService {
	
	private News[] newslist=new News[200];
	private int index;//下标
	
	public boolean isFind(int id) {
		for(int i=0;i<newslist.length;i++) {
			if(newslist[i]!=null&&newslist[i].getId()==id) {
				return true;
			}
		}
		return false;
	}
	
	public void insert(News news) {
		newslist[index]=news;
		index++;
		System.out.println("成功添加一条新闻");
	}
	
	public void delete(int id) {
		//找到该条数据
		for(int i=0;i<newslist.length;i++) {
			if(newslist[i]!=null&&newslist[i].getId()==id) {
				//删除
				newslist[i]=null;
				System.out.println("成功删除一条新闻");
				return;
			}
		}
		System.out.println("没有该条新闻");
	}
	
	public void update(News news) {
		//找到该条数据
		for(int i=0;i<newslist.length;i++) {
			if(newslist[i]!=null&&newslist[i].getId()==news.getId()) {
				//更新
				newslist[i]=news;
				System.out.println("成功更新一条新闻");
				return;
			}
		}
		System.out.println("没有该条新闻");
	}
	
	public void selectAll() {
		System.out.println("编号\t标题\t作者\t发布日期");
		for (int i = 0; i < newslist.length; i++) {
			// 每次拿到一条新闻数据
			News news = newslist[i];
			if (news != null) {
				// 打印输出新闻内容
				System.out.println(
						news.getId() + "\t" + 
						news.getTitle() + "\t" + 
						news.getAuthor() + "\t" + 
						news.getPubDate());
			}
			
		}
	}
}

系统测试部分:

package com.jredu.teach;

import java.util.Scanner;

public class NewsTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		NewsService service=new NewsService();
		while(true) {
			//提示信息
			System.out.println("1.添加新闻 2.删除新闻 3.更新新闻 4.查看新闻 0.退出");
			int num=sc.nextInt();
			switch (num) {
			case 0:
				//退出
				System.out.println("谢谢使用,再见");
				return;
			case 1:
				//添加
				System.out.print("请输入编号:");
				int id=sc.nextInt();
				if(service.isFind(id)) {
					System.out.println("新闻已存在,请重新录入");
					continue;
				}
				System.out.print("请输入标题:");
				String title=sc.next();
				System.out.print("请输入作者");
				String author=sc.next();
				System.out.print("请输入发布日期");
				String pubDate=sc.next();
				News news=new News(id, title, author, pubDate);
				service.insert(news);
				break;
			case 2:
				//删除
				System.out.print("请输入新闻编号:");
				int id2=sc.nextInt();
				service.delete(id2);
				break;
			case 3:
				//修改
				System.out.print("请输入编号:");
				int id3=sc.nextInt();
				if(!service.isFind(id3)) {
					System.out.println("新闻不存在,请重新选择");
					continue;
				}
				System.out.print("请输入标题:");
				String title3=sc.next();
				System.out.print("请输入作者");
				String author3=sc.next();
				System.out.print("请输入发布日期");
				String pubDate3=sc.next();
				News news3=new News(id3, title3, author3, pubDate3);
				service.update(news3);
				break;
			case 4:
				//查看
				service.selectAll();
				break;
			}
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值