package come.itheima.hashSet.baidu_work;
import java.util.*;
public class demo {
public static void main(String[] args) {
//由于是把值放入的对象中 所以创建一个单列集合即可
TreeSet<News> ht = new TreeSet<>();
//存入所有热搜
ht.add(new News("备孕者可以接种疫苗", 480));
ht.add(new News("土耳其召见中国,中方回应", 463));
ht.add(new News("日本大阪将取消奥运会火炬传递活动", 447));
ht.add(new News("杀人犯纸面服刑15年 84人担责", 431));
ht.add(new News("好利来辟谣卷入西藏那冒险王事件", 416));
ht.add(new News("湖北全球特别推介活动4月12日亮相", 402));
ht.add(new News("2名中国旷工在尼日利亚遭绑架", 388));
ht.add(new News("一季度北京平均月薪达11187元", 374));
ht.add(new News("巴西侨领张伟遭枪击遇害", 361));
ht.add(new News("浦东车管所称特斯拉目前上牌正常", 348));
while (true) {
//使用while循环展示打印热搜
System.out.println("==================百度热搜===================");
//重新创建一个集合来进行实时排序
TreeSet<News> news = new TreeSet<>(new Comparator<News>() {//重写Comepartor方法使用比较器排序
@Override
public int compare(News o1, News o2) { //o1表示当前要存的数据,o2表示已经存入的数据
int result = o2.getCount() - o1.getCount();//重新制定排序规则 返回当前数据是小的存左边 则表示当前数据往下排
return result; //返回一个值 正数存右边负数存左边 0则不存 则对标右为上左为下
}
});
//使用增强for来从新遍历一遍就得集合,
for (News news1 : ht) {
news.add(news1);//把旧的数据放到新的集合中
}
for (News news1 : news) {
System.out.println(news1); //重新打印遍历所有数据
}
System.out.println("请输入要查询的新闻"); //输入提示信息
Scanner sc = new Scanner(System.in); //键盘录入
String newsName = sc.next();
if (getNews(ht, newsName)) {
System.out.println("请输入要做的操作:1.点赞 2.评论 3.收藏 4.结束程序");
String num = sc.next();
switch (num) { //输入要做的操作
case "1": //输入1调用点赞的方法
GiveLike(ht, newsName);
break;
case "2": //输入2 调用评论的方法
comment(ht, newsName);
break;
case "3": //输入3调用收藏的方法
collect(ht, newsName);
break;
case "4": //输入4 退出程序
System.exit(0);
}
} else {
System.out.println("输入错误,请重新输入");
}
}
}
public static void comment(TreeSet<News> ht, String newsName) { //评论的方法
int count = 0; //定义变量
for (News o : ht) { //使用增强for 来加如热度值
if (o.getTitle().contains(newsName)) { //通过判断是否为输入的热搜值 如果是就增加热度值 否则就不做处理 继续执行switch
count = o.getCount() + 5;//增加5热度值
o.setCount(count); //把新的值加到旧的集合中
}
}
}
public static void collect(TreeSet<News> ht, String newsName) { //收藏的方法
int count = 0; //定义变量
for (News o : ht) {//使用增强for 来加如热度值
if (o.getTitle().contains(newsName)) { //通过判断是否为输入的热搜值 如果是就增加热度值 否则就不做处理 继续执行switch
count = o.getCount() + 8;//增加8热度值
o.setCount(count);//把新的值加到旧的集合中
}
}
}
public static void GiveLike(TreeSet<News> ht, String newsName) {//收藏的方法
int count = 0;//收藏的方法
for (News o : ht) {//使用增强for 来加如热度值
if (o.getTitle().contains(newsName)) {//通过判断是否为输入的热搜值 如果是就增加热度值 否则就不做处理 继续执行switch
count = o.getCount() + 1;//增加8热度值
o.setCount(count);//把新的值加到旧的集合中
}
}
}
public static boolean getNews(TreeSet<News> ht, String newsName) { //判断输入的热搜是否真实存在
for (News o : ht) { //使用增强for 来拿到所有对象
if (o.getTitle().contains(newsName)) { //进行模糊匹配
return true; //匹配到返回true
}
}
return false; //匹配不到返回false
}
}
package come.itheima.hashSet.baidu_work;
public class News implements Comparable<News >{
String title;
int count;
@Override
public String toString() {
return "News{" +
"title='" + title + '\'' +
", count=" + count +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
News hotBot = (News) o;
if (count != hotBot.count) return false;
return title != null ? title.equals(hotBot.title) : hotBot.title == null;
}
@Override
public int hashCode() {
int result = title != null ? title.hashCode() : 0;
result = 31 * result + count;
return result;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public News(String title, int count) {
this.title = title;
this.count = count;
}
public News() {
}
@Override
public int compareTo(News o) {
int result = o.count - this.count;
return result;
}
}