Java: Hệ thống quản lý tin tức (news)
Giải phóng thời gian, khai phóng năng lực
Solution cho bài tập: Bài tập 2 - Bài tập phần Abstract class & Interface
1. Video hướng dẫn cho Hệ thống quản lý tin tức:
2. Bài viết hướng dẫn cho Hệ thống quản lý tin tức:
Interface INews (file INews.java):
package practical_news_management; /** * * @author LongDT - v1study.com */ public interface INews { void Display(); }
Class News (file News.java):
package practical_news_management; /** * * @author LongDT - v1study.com */ public class News implements INews { int id; String title; String publishDate; String author; String content; float averageRate; public News() {//Hàm tạo 0 tham số id=1; title="News"; publishDate="2020-07-13"; author="Đặng Trần Long"; content="Tin tức công nghệ"; averageRate=4.5f; } public News(int id, String title, String publishDate, String author, String content, float averageRate, int[] rateList) { this.id = id; this.title = title; this.publishDate = publishDate; this.author = author; this.content = content; this.averageRate = averageRate; RateList = rateList; } public int getId() { return id; } public String getTitle() { return title; } public String getPublishDate() { return publishDate; } public String getAuthor() { return author; } public String getContent() { return content; } public float getAverageRate() { return averageRate; } //Read only nên chỉ có get public void setId(int id) { this.id = id; } public void setTitle(String title) { this.title = title; } public void setPublishDate(String publishDate) { this.publishDate = publishDate; } public void setAuthor(String author) { this.author = author; } public void setContent(String content) { this.content = content; } @Override public void display() { System.out.println("Title: " + title); System.out.println("Publish date: " + publishDate); System.out.println("Author: " + author); System.out.println("Content: " + content); System.out.println("Average rate: " + averageRate); } int[] RateList = new int[3]; void calculate() { averageRate = (float) (RateList[0] + RateList[1] + RateList[2]) / RateList.length; } }
Class Practical_News_Management (file Practical_News_Management.java):
Class này dùng để thực thi chương trình.
package practical_news_management; import java.util.ArrayList; import java.util.Scanner; /** * @author LongDT - v1study.com */ public class Practical_News_Management { import java.util.ArrayList; import java.util.Scanner; public class TestNews { static void menu() { System.out.println("1. Insert news"); System.out.println("2. View list news"); System.out.println("3. Average rate"); System.out.println("4. Exit"); } static void inputNews(News news) { Scanner scanner = new Scanner(System.in); System.out.print("Nhập title: "); news.title = scanner.nextLine(); System.out.print("Nhập publish date: "); news.publishDate = scanner.nextLine(); System.out.print("Nhập author: "); news.author = scanner.nextLine(); System.out.print("Nhập content: "); news.content = scanner.nextLine(); System.out.println("Nhập các đánh giá:"); for (int i = 0; i < news.RateList.length; i++) { do { System.out.format("Đánh giá %d: ", i + 1); news.RateList[i] = scanner.nextInt(); } while (!(1 <= news.RateList[i] && news.RateList[i] <= 5)); } } public static void main(String[] args) { int option; Scanner scanner = new Scanner(System.in); ArrayList<News> NewsList = new ArrayList<>(); menu(); while (true) { while (true) { try { System.out.print("Mời bạn nhập 1 lựa chọn: "); option = scanner.nextInt(); break; } catch (Exception ex) { System.out.println("Lựa chọn phải là 1 số nguyên"); } } switch (option) { case 1: News news = new News(); inputNews(news); news.id = NewsList.size() + 1; NewsList.add(news); System.out.println("Đã thêm tin có mã " + news.id + " vào danh sách!"); break; case 2: System.out.println("Danh sách tin:"); for (News news1 : NewsList) { news1.display(); } break; case 3: System.out.println("Danh sách tin sau khi tính trung bình rate:"); for (News news1 : NewsList) { news1.calculate(); news1.display(); } break; case 4: return; } } } }
Giải phóng thời gian, khai phóng năng lực