By GokiSoft.com| 19:55 13/02/2023|
Java Basic

Bài tập - Quản lý sách - Lập trình Java căn bản

Xây dựng menu chương trình như sau.

Menu :

1. Nhập thông tin sách

2. Hiển thị tất cả sách ra màn hình

3. Nhập thông tin tác giả

4. Tìm kiếm tất cả sách được viết bởi tác giả

5. Thoát

Choose : ???

Yêu cầu :

Thiết kế lớp đối tượng tác giả gồm các thuộc tính sau (Tên, tuổi, bút danh, ngày sinh, quê quán) 

- Chú ý : cài đặt tất cả các thuộc tính chỉ có thuộc tính đọc dữ liệu

- Tạo các hàm tạo ko đối và đầy đủ đối số

- Tạo phương thức nhập thông tin tác giả, và xem thông tin tác giả

- Chú ý : Mỗi tác giả có bút danh duy nhất, ko được phép nhập trùng lặp tên bút danh của tác giả

Thiết kế lớp book gồm các thuộc tín : Tên sách, ngày xuất bản, but danh

- Cài đặt get/set cho các thuộc tính, hàm tạo ko đối và đầy đủ tham số

- Tạo hàm nhập và xem thông tin sách

- Chú ý : Khi nhập tên bút danh của tác giả, nếu chưa tồn tại thì -> Xuất hiện màn hình nhập thông tin cho tác giả đó luôn.

Xây dựng từng chức năng chương trình

Khi người dùng chọn 1 : Hỏi người dùng nhập số sách cần thêm -> sau đó nhập thông tin cho từng quấn sách -> và lưa vào 1 list.

Khi người dùng chọn 2 : In tất cả thông tin quấn sách.

Khi người dùng chon 3 : Hỏi người dùng nhập số tác giả -> Nhập thông tin tác giả.

Khi người dùng chọn 4 : Hiển thị màn hình nhập bút danh cần tìm -> in ra quấn sach của tác giả đó

Khi người dùng chọn 5 : Thoát chương trình.

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

GokiSoft.com [Teacher]
GokiSoft.com

2021-07-28 02:35:43


#Utility.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson11;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Diep.Tran
 */
public class Utility {
    public static String convertDateToString(Date datetime) {
        SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
        
        return format.format(datetime);
    }
    
    public static Date convertStringToDate(String str) {
        SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
        
        try {
            return format.parse(str);
        } catch (ParseException ex) {
            Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}


#Main.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson11;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static Scanner scan;
    
    public static void main(String[] args) {
        scan = new Scanner(System.in);
        
        int choose;

        do {
            showMenu();
            choose = Integer.parseInt(scan.nextLine());
            
            switch(choose) {
                case 1:
                    DataMgr.getInstance().inputBook();
                    break;
                case 2:
                    DataMgr.getInstance().displayBook();
                    break;
                case 3:
                    DataMgr.getInstance().inputAuthor();
                    break;
                case 4:
                    DataMgr.getInstance().findBookByAuthor();
                    break;
                case 5:
                    System.out.println("Thoat!!!");
                    break;
                default:
                    System.out.println("Nhap sai!!!");
                    break;
            }
        } while(choose != 5);
    }
    
    static void showMenu() {
        System.out.println("1. Nhap thong tin sach");
        System.out.println("2. Hien thi");
        System.out.println("3. Nhap thong tin tac gia");
        System.out.println("4. Tim kiem theo tac gia");
        System.out.println("5. Thoat");
        System.out.println("Chon: ");
    }
}


#DataMgr.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson11;

import java.util.ArrayList;
import java.util.List;
import static lesson11.Main.scan;

/**
 * DataMgr -> chi can tao duy nhat 1 object -> tu class object DataMgr
 * DataMgr -> singleton
 * @author Diep.Tran
 */
public class DataMgr {
    List<Book> bookList;
    List<Author> authorList;
    
    static DataMgr instance = null;
    
    private DataMgr() {
        bookList = new ArrayList<>();
        authorList = new ArrayList<>();
    }
    
    public static DataMgr getInstance() {
        if(instance == null) {
            instance = new DataMgr();
        }
        return instance;
    }

    public List<Book> getBookList() {
        return bookList;
    }

    public void setBookList(List<Book> bookList) {
        this.bookList = bookList;
    }

    public List<Author> getAuthorList() {
        return authorList;
    }

    public void setAuthorList(List<Author> authorList) {
        this.authorList = authorList;
    }
    
    public void inputBook() {
        System.out.println("Nhap so sach can them: ");
        int n = Integer.parseInt(scan.nextLine());
        
        for (int i = 0; i < n; i++) {
            Book book = new Book();
            book.input();
            
            bookList.add(book);
        }
    }

    public void displayBook() {
        System.out.println("============= Thong tin sach =============");
        
        bookList.forEach(book -> {
            book.display();
        });
    }

    public void inputAuthor() {
        System.out.println("Nhap so tac gia can them: ");
        int n = Integer.parseInt(scan.nextLine());
        
        for (int i = 0; i < n; i++) {
            Author author = new Author();
            author.input();
            
            authorList.add(author);
        }
    }

    public void findBookByAuthor() {
        System.out.println("Tim quan sach cua tac gia: ");
        String nickname = scan.nextLine();
        
        for (Book book : bookList) {
            if(book.getNickname().equalsIgnoreCase(nickname)) {
                book.display();
            }
        }
    }
    
    public boolean checkExistAuthor(String nickname) {
        if(authorList.isEmpty()) return false;
        
        for (Author author : authorList) {
            if(author.getNickname().equalsIgnoreCase(nickname)) {
                return true;
            }
        }
        return false;
    }
}


#Book.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson11;

import java.util.Date;
import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Book {
    String bookName, nickname;
    Date publishDate;

    public Book() {
    }

    public Book(String bookName, String nickname, Date publishDate) {
        this.bookName = bookName;
        this.nickname = nickname;
        this.publishDate = publishDate;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public Date getPublishDate() {
        return publishDate;
    }

    public void setPublishDate(Date publishDate) {
        this.publishDate = publishDate;
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Book -> Nhap ten sach: ");
        bookName = scan.nextLine();
        
        //Khi nhập tên bút danh của tác giả, nếu chưa tồn tại thì -> Xuất hiện màn hình nhập thông tin cho tác giả đó luôn.
        System.out.println("Book -> Nhap nickname: ");
        nickname = scan.nextLine();
        
        if(!DataMgr.getInstance().checkExistAuthor(nickname)) {
            //Nhap thong tin moi cho tac gia
            Author author = new Author(nickname);
            author.inputWithoutNickname();
            
            DataMgr.getInstance().authorList.add(author);
        }
        
        System.out.println("Book -> Nhap ngay xuat ban (dd/mm/YYYY): ");
        publishDate = Utility.convertStringToDate(scan.nextLine());
    }
    
    public void display() {
        System.out.println(this);
    }

    @Override
    public String toString() {
        return "Book{" + "bookName=" + bookName + ", nickname=" + nickname + ", publishDate=" + Utility.convertDateToString(publishDate) + '}';
    }
}


#Author.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson11;

import java.util.Date;
import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Author {
    String fullname, nickname,address;
    Date birthday;

    public Author() {
    }

    public Author(String fullname, String nickname, String address, Date birthday) {
        this.fullname = fullname;
        this.nickname = nickname;
        this.address = address;
        this.birthday = birthday;
    }

    public Author(String nickname) {
        this.nickname = nickname;
    }
    
    public String getFullname() {
        return fullname;
    }

    public String getNickname() {
        return nickname;
    }

    public String getAddress() {
        return address;
    }

    public Date getBirthday() {
        return birthday;
    }
    
    public void inputWithoutNickname() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Author -> Nhap ten: ");
        fullname = scan.nextLine();
        
        System.out.println("Author -> Nhap dia chi: ");
        address = scan.nextLine();
        
        System.out.println("Author -> Nhap ngay sinh (dd/mm/YYYY): ");
        birthday = Utility.convertStringToDate(scan.nextLine());
    }
    
    public void input() {
        inputWithoutNickname();
        
        Scanner scan = new Scanner(System.in);
        
        //Mỗi tác giả có bút danh duy nhất, ko được phép nhập trùng lặp tên bút danh của tác giả
        System.out.println("Author -> Nhap nickname: ");
        while(true) {
            nickname = scan.nextLine();
            boolean exist = DataMgr.getInstance().checkExistAuthor(nickname);
            if(exist) {
                System.out.println("Author -> Nickname da ton tai -> vui long nhap moi: ");
            } else {
                break;
            }
        }
    }
    
    public void display() {
        System.out.println(this);
    }

    @Override
    public String toString() {
        return "Author{" + "fullname=" + fullname + ", nickname=" + nickname + ", address=" + address + ", birthday=" + Utility.convertDateToString(birthday) + '}';
    }
}