Java: Hệ thống quản lý tin tức (news)

Các khóa học qua video:
Python SQL Server PHP C# Lập trình C Java HTML5-CSS3-JavaScript
Học trên YouTube <76K/tháng. Đăng ký Hội viên
Viết nhanh hơn - Học tốt hơn
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 to 0 tham s    id=1;
    title="News";
    publishDate="2020-07-13";
    author=ng Trn Long";
    content="Tin tc 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("Nhp title: ");
    news.title = scanner.nextLine();
    System.out.print("Nhp publish date: ");
    news.publishDate = scanner.nextLine();
    System.out.print("Nhp author: ");
    news.author = scanner.nextLine();
    System.out.print("Nhp content: ");
    news.content = scanner.nextLine();
    System.out.println("Nhp 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("Mi bn nhp 1 la chn: ");
          option = scanner.nextInt();
          break;
        } catch (Exception ex) {
          System.out.println("La chn phi 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;
      }
    }
  }
}
» Tiếp: Solution câu hỏi và bài tập phần Generics
« Trước: Hệ thống quản lý số điện thoại
Các khóa học qua video:
Python SQL Server PHP C# Lập trình C Java HTML5-CSS3-JavaScript
Học trên YouTube <76K/tháng. Đăng ký Hội viên
Viết nhanh hơn - Học tốt hơn
Giải phóng thời gian, khai phóng năng lực
Copied !!!