当前位置: 首页 > 工具软件 > news > 使用案例 >

Java-新闻news案例

樊杰
2023-12-01
一共有三个类 News NewsService, NewsTest

/**
* 新闻,实体类,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;
}
}

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());
}
}
}
}

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;
}
}
}

}

 类似资料: