By GokiSoft.com| 13:34 16/09/2022|
Java Basic

Java Basic- OOP - quản lý sách trong java

Cài đặt lớp Book gồm các thuộc tính:

private String bookName;

private String bookAuthor;

private String producer;

private int yearPublishing;

private float price;

 

Cài đặt 2 constructors, các phương thức set/get cho các thuộc tính của lớp.

Cài đặt 2 phương thức input() và display để nhập và hiển thị các thuộc tính của lớp.

 

Cài đặt lớp AptechBook kế thừa lớp Book và bổ sung thêm vào thuộc tính:

private String language;

private int semester;

 

Cài đặt 2 constructor trong đó sử dụng super để gọi đến constructor của lớp cha.

Cài đặt các phương thức get/set cho các thuộc tính bổ sung

Override các phương thức input() và display().

 

Cài đặt lớp Test trong đó tạo menu và thực hiện theo các chức năng của menu:

1.    Nhập thông tin n cuốn sách của Aptech

2.    Hiển thị thông tin vừa nhập

3.    Sắp xếp thông tin giảm dần theo năm xuất bản và hiển thị

4.    Tìm kiếm theo tên sách

5.    Tìm kiếm theo tên tác giả

6.    Thoát.

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

5

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

Trần Thị Khánh Huyền [T2008A]
Trần Thị Khánh Huyền

2021-02-21 01:39:28



/*
 * 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 QuanLySach;
import java.util.Scanner;
/**
 *
 * @author Admin
 */
public class Book {

    private String bookName;
    private String bookAuthor;
    private String producer;
    private int yearPublishing;
    private float price;
    
    public Book(){
        System.out.println("Book init");
    }

    public Book(String bookName, String bookAuthor, String producer, int yearPublishing, float price) {
        this.bookName = bookName;
        this.bookAuthor = bookAuthor;
        this.producer = producer;
        this.yearPublishing = yearPublishing;
        this.price = price;
    }

    public String getBookName() {
        return bookName;
    }

    public String getBookAuthor() {
        return bookAuthor;
    }

    public String getProducer() {
        return producer;
    }

    public int getYearPublishing() {
        return yearPublishing;
    }

    public float getPrice() {
        return price;
    }

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

    public void setBookAuthor(String bookAuthor) {
        this.bookAuthor = bookAuthor;
    }

    public void setProducer(String producer) {
        this.producer = producer;
    }

    public void setYearPublishing(int yearPublishing) {
        this.yearPublishing = yearPublishing;
    }

    public void setPrice(float price) {
        this.price = price;
    }
    
    public void input(){
        Scanner sc = new Scanner(System.in);
        System.out.println("Nhap ten sach: ");
        bookName = sc.nextLine();
        System.out.println("Nhap ten tac gia: ");
        bookAuthor = sc.nextLine();
        System.out.println("Nhap ten nha san xuat: ");
        producer = sc.nextLine();
        System.out.println("Nhap nam xuat ban: ");
        yearPublishing = sc.nextInt();
        System.out.println("Nhap gia ban: ");
        price = sc.nextFloat();
    }
    
    public void display(){
        System.out.println(this);
    }
    
    
}



Do Trung Duc [T2008A]
Do Trung Duc

2021-02-20 14:49:46

AptechBook


/*
 * 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 BookManagerTinhkethua;

import java.util.Scanner;

/**
 *
 * @author TrungDuc
 */
public class AptechBook extends Book{
    private String language;
    private int semester;
    
 
    public AptechBook() {
    }

    public AptechBook(String bookName, String bookAuthor, String producer, int yearPublishing, float price) {
        super(bookName, bookAuthor, producer, yearPublishing, price);
    }

    public String getLanguage() {
        return language;
    }

    public int getSemester() {
        return semester;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public void setSemester(int semester) {
        this.semester = semester;
    }
    
    @Override
    public void Input(){
        Scanner scan = new Scanner(System.in);
        super.Input();
        System.out.println("Nhap ngon ngu: ");
        language = scan.nextLine();
        
        System.out.println("Nhap hoc ky: ");
        semester = Integer.parseInt(scan.nextLine());
    }
    
    @Override
    public void Display(){
        super.Display();
        System.out.println("Ngon ngu: "+ language);
         System.out.println("Hoc ky: "+ semester);
    }
    
    
    
}







Do Trung Duc [T2008A]
Do Trung Duc

2021-02-20 14:49:25


Book
/*
 * 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 BookManagerTinhkethua;

import java.util.Scanner;

/**
 *
 * @author TrungDuc
 */
public class Book {

    private String bookName;
    private String bookAuthor;
    private String producer;
    private int yearPublishing;
    private float price;

    public Book() {
    }

    ;

    public Book(String bookName, String bookAuthor, String producer, int yearPublishing, float price) {
        this.bookName = bookName;
        this.bookAuthor = bookAuthor;
        this.producer = producer;
        this.yearPublishing = yearPublishing;
        this.price = price;
    }

    public String getBookName() {
        return bookName;
    }

    public String getBookAuthor() {
        return bookAuthor;
    }

    public String getProducer() {
        return producer;
    }

    public int getYearPublishing() {
        return yearPublishing;
    }

    public float getPrice() {
        return price;
    }

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

    public void setBookAuthor(String bookAuthor) {
        this.bookAuthor = bookAuthor;
    }

    public void setProducer(String producer) {
        this.producer = producer;
    }

    public void setYearPublishing(int yearPublishing) {
        this.yearPublishing = yearPublishing;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    public void Input() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap ten sach: ");
        bookName = scan.nextLine();

        System.out.println("Nhap ten tac gia: ");
        bookAuthor = scan.nextLine();

        System.out.println("Nhap ten nha xuat ban: ");
        producer = scan.nextLine();

        System.out.println("Nhap nam xuat ban: ");
        yearPublishing = Integer.parseInt(scan.nextLine());

        System.out.println("Nhap gia ban: ");
        price = Integer.parseInt(scan.nextLine());
    }
    
    public void Display(){
        System.out.println("Ten: " + bookName);
        System.out.println("Tac gia: " + bookAuthor);
        System.out.println("Nha xuat ban: " + producer);
        System.out.println("Nam xuat ban: " + yearPublishing);
        System.out.println("Gia: " + price);
    }
    
//    @Override
//    public String toString(){
//        return("Ten: " + bookName + "Tac gia: " + bookAuthor + "Nha xuat ban: " +  producer + "Nam xuat ban" + yearPublishing +"Gia: " + price);
//    }
}



Do Trung Duc [T2008A]
Do Trung Duc

2021-02-20 14:48:53

Test


/*
 * 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 BookManagerTinhkethua;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;
import java.util.Comparator;

public class Test {

    public static void main(String[] args) {
        int choose, n;
        Scanner scan = new Scanner(System.in);
        ArrayList<AptechBook> listAptechBook = new ArrayList<>();

        do {
            displayMenu();

            System.out.println(" Nhập lua chon choose = ");
            choose = Integer.parseInt(scan.nextLine());

            switch (choose) {
                case 1:
                    System.out.println(" Nhập so luong cuốn sách của Aptech n = ");
                    n = Integer.parseInt(scan.nextLine());

                    for (int i = 0; i < n; i++) {
                        AptechBook book = new AptechBook();
                        book.Input();
                        listAptechBook.add(book);
                    }
                    break;

                case 2:
                    for (int i = 0; i < listAptechBook.size(); i++) {
                        listAptechBook.get(i).Display();
                    }
                    break;

                case 3:
                    Collections.sort(listAptechBook, new Comparator<AptechBook>() {
                        @Override
                        public int compare(AptechBook o1, AptechBook o2) {
                            if (o1.getYearPublishing() < o2.getYearPublishing()) {
                                return 1;
                            }
                            return -1;
                        }
                    });

                    for (int i = 0; i < listAptechBook.size(); i++) {
                        listAptechBook.get(i).Display();
                    }
                    break;

                case 4:
                    String tenSachCanTim;
                    int count = 0;
                    System.out.println("Nhap ten sach ");
                    tenSachCanTim = scan.nextLine();

                    for (int i = 0; i < listAptechBook.size(); i++) {
                        if (listAptechBook.get(i).getBookName().equals(tenSachCanTim)) {
                            listAptechBook.get(i).Display();
                            count++;
                        }
                    }

                    if (count == 0) {
                        System.out.println("Khong co sach nao co ten nhu yeu cau:");
                    }

                    break;

                case 5:
                    String tenTacGiaCanTim;
                    int countTacGia = 0;
                    System.out.println("Nhap ten tac gia ");
                    tenTacGiaCanTim = scan.nextLine();

                    for (int i = 0; i < listAptechBook.size(); i++) {
                        if (listAptechBook.get(i).getBookAuthor().equals(tenTacGiaCanTim)) {
                            listAptechBook.get(i).Display();
                            countTacGia++;
                        }
                    }

                    if (countTacGia == 0) {
                        System.out.println("Khong co sach nao co tac gia nhu yeu cau:");
                    }

                    break;

                case 6:
                    System.out.println("Thoat");
                    break;

                default:
                    System.out.println("Chon lai choose = ");
                    break;
            }

        } while (choose != 6);

    }

    static void displayMenu() {
        System.out.println("1. Nhập thông tin n cuốn sách của Aptech");
        System.out.println("2. Hiển thị thông tin vừa nhập");
        System.out.println("3. Sắp xếp thông tin giảm dần theo năm xuất bản và hiển thị");
        System.out.println("4. Tìm kiếm theo tên sách");
        System.out.println("5. Tìm kiếm theo tên tác giả ");
        System.out.println("6. Thoat ");
    }
}



vuong huu phu [T2008A]
vuong huu phu

2021-01-31 02:35:59


Test
/*
 * 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 buoi_3;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

/**
 *
 * @author Admin
 */
public class Test {

    public static void main(String[] args) {
        String[] sach = null;
        String tac_gia, ten_sach;
        int n, lua_chon = 0;
        Scanner scan = new Scanner(System.in);
        ArrayList<Book> Book_list = new ArrayList<>();
        do {
            menu();
            System.out.println("Nhap su lua chon: ");
            lua_chon = Integer.parseInt(scan.nextLine());
            switch (lua_chon) {
                case 1:
                    System.out.print("Nhap so cuon sach:  ");
                    n = Integer.parseInt(scan.nextLine());
                    for (int i = 0; i < n; i++) {
                        AptechBook book = new AptechBook();
                        book.nhap();
                        Book_list.add(book);
                    }

                    break;
                case 2:
                    for (int i = 0; i < Book_list.size(); i++) {
                        Book_list.get(i).hien_thi();
                    }
                    break;
                case 3:
                    Collections.sort(Book_list, (Book book1, Book book2) -> {
                        if (book1.getYearPublishing() < book2.getYearPublishing()) {
                            return 1;
                        }
                        return -1;
                    });
                    for (int i = 0; i < Book_list.size(); i++) {
                        Book_list.get(i).hien_thi();
                    }
                    break;
                case 4:
                    int kt = 0;
                    System.out.print("Nhap ten cuan sach can tim: ");
                    ten_sach = scan.nextLine();
                    for (Book book : Book_list) {
                        if (book.getBookAuthor().equalsIgnoreCase(ten_sach)) {
                            book.hien_thi();
                            kt++;
                        }
                    }
                    if (kt == 0) {
                        System.out.print("khong co cuon sach nao");
                    }
                    break;
                case 5:
                    int kt1 = 0;
                    System.out.print("Nhap ten tac gia: ");
                    tac_gia = scan.nextLine();
                    for (Book book : Book_list) {
                        if (book.getBookAuthor().equalsIgnoreCase(tac_gia)) {
                            book.hien_thi();
                            kt1++;
                        }
                    }
                    if (kt1 == 0) {
                        System.out.print("khong co cuon sach nao");
                    }
//                   
                    break;
                case 6:
                    System.out.print(" Thoat ");
                    break;
                default:
                    System.out.print(" Lua chon lai");
                    break;

            }
        } while (lua_chon != 7);
    }

    static void menu() {
        System.out.println();
        System.out.println(" 1 Nhap thong tin n cuan sach: ");
        System.out.println(" 2 Hien thi thong tin vua nhap: ");
        System.out.println(" 3 Sap xep giam dan theo nam xuat ban: ");
        System.out.println(" 4 Tim kiem theo ten: ");
        System.out.println(" 5 Tim kiem theo ten tac gia: ");
        System.out.println(" 6 Thoat:");
    }
}



vuong huu phu [T2008A]
vuong huu phu

2021-01-31 02:34:14


AptechBook
/*
 * 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 buoi_3;

import java.util.Scanner;

/**
 *
 * @author Admin
 */
public class AptechBook extends Book {
private String language;
private int semester;
Scanner scan = new Scanner(System.in);
    public AptechBook() {
    }

    public AptechBook(String language, int semester, String bookName, String bookAuthor, String producer, int yearPublishing, float price) {
        super(bookName, bookAuthor, producer, yearPublishing, price);
        this.language = language;
        this.semester = semester;
    }

    public String getLanguage() {
        return language;
    }

    public int getSemester() {
        return semester;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public void setSemester(int semester) {
        this.semester = semester;
    }

    @Override
    public void nhap() {
        super.nhap(); //To change body of generated methods, choose Tools | Templates.
        System.out.print("Nhap Ngon ngu: ");
        language = scan.nextLine();
        System.out.print("Nhap ki hoc: ");
        semester = scan.nextInt();
    }

    @Override
    public void hien_thi() {
     super.hien_thi(); //To change body of generated methods, choose Tools | Templates.
    System.out.print("Ngon ngu: "+language);
    System.out.print("Hoc ki: "+semester);
    }
    
    
    
}



vuong huu phu [T2008A]
vuong huu phu

2021-01-31 02:33:34

Book


/*
 * 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 buoi_3;

import java.util.Scanner;

/**
 *
 * @author Admin
 */
public class Book {

    private String bookName;

    private String bookAuthor;

    private String producer;

    private int yearPublishing;

    private float price;

    public String getBookName() {
        return bookName;
    }

    public String getBookAuthor() {
        return bookAuthor;
    }

    public String getProducer() {
        return producer;
    }

    public int getYearPublishing() {
        return yearPublishing;
    }

    public float getPrice() {
        return price;
    }

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

    public void setBookAuthor(String bookAuthor) {
        this.bookAuthor = bookAuthor;
    }

    public void setProducer(String producer) {
        this.producer = producer;
    }

    public void setYearPublishing(int yearPublishing) {
        this.yearPublishing = yearPublishing;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    public Book() {
    }

    public Book(String bookName, String bookAuthor, String producer, int yearPublishing, float price) {
        this.bookName = bookName;
        this.bookAuthor = bookAuthor;
        this.producer = producer;
        this.yearPublishing = yearPublishing;
        this.price = price;
    }

    public void nhap() {
        
        Scanner scan = new Scanner(System.in);
        System.out.println();
        System.out.print("Nhap ten : ");
        bookName = scan.nextLine();
        System.out.print("Nhap ten tac gia : ");
        bookAuthor = scan.nextLine();
        System.out.print("Nhap ten nha san xuat : ");
        producer = scan.nextLine();
        System.out.print("Nhap nam xuat ban : ");
        yearPublishing = Integer.parseInt(scan.nextLine());
        System.out.print("Nhap gia ban : ");
        price = Integer.parseInt(scan.nextLine());
    }

    public void hien_thi() {
        System.out.println(this);
    }

    @Override
    public String toString() {
        System.out.println();
        return " Ten sach: " + bookName + " ,Tac gia: " + bookAuthor + " ,Nha san xuat: " + producer + " ,Nam xuat ban: " + yearPublishing + " ,Gia ban: " + price;
    }


}



Nguyễn Tiến Đạt [T2008A]
Nguyễn Tiến Đạt

2021-01-30 04:29:54


#Test
/*
 * 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 lesson4.QuanLiSach;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

/**
 *
 * @author MyPC
 */
public class Test {
    public static void main(String[] args) {
        ArrayList<AptechBook> bookList = new ArrayList<>();
        Scanner scan = new Scanner(System.in);
        int choose, n;
        do{
            Menu();
            System.out.println("Lua chon chuong trinh:");
            choose = Integer.parseInt(scan.nextLine());
            switch(choose){
                case 1:
                    System.out.println("Nhap so sach can them: ");
                    n = Integer.parseInt(scan.nextLine());
                    for (int i = 0; i < n; i++) {
                        System.out.println("Nhap thong tin quyen sach thu " +(i+1)+":");
                        AptechBook book = new AptechBook();
                        book.input();
                        bookList.add(book);
                        System.out.println("");
                    }
                break;
                
                case 2:
                    for (int i = 0; i < bookList.size(); i++) {
                        System.out.println("Thong tin quyen sach thu " +(i+1)+":");
                        bookList.get(i).display();
                        System.out.println("");
                    }
                break;
                
                case 3:
                    Collections.sort(bookList, new Comparator<AptechBook>() {
                        @Override
                        public int compare(AptechBook o1, AptechBook o2) {
                            if(o1.getYearPublishing() < o2.getYearPublishing()) {
                                return 1;
                            }
                            return -1;
                        }
                    });
                    for (int i = 0; i < bookList.size(); i++) {
                        System.out.println("Thong tin quyen sach thu " +(i+1)+":");
                        bookList.get(i).display();
                        System.out.println("");
                    }
                break;
                
                case 4:
                    String bookSearch;
                    int check = 0;
                    System.out.println("Nhap ten sach can tim kiem:");
                    bookSearch = scan.nextLine();
                    for (int i = 0; i < bookList.size(); i++) {
                        if(bookList.get(i).getBookName().equals(bookSearch)){
                            System.out.println("Thong tin quyen sach thu " +(i+1)+":");
                            bookList.get(i).display();
                            System.out.println("");
                            check++;
                        }
                    }
                    if(check == 0){
                        System.out.println("Khong co sach theo yeu cau!!");
                        System.out.println("");
                    }
                break;
                
                case 5:
                    String authorSearch;
                    int check1 = 0;
                    System.out.println("Nhap ten tac gia can tim kiem:");
                    authorSearch = scan.nextLine();
                    for (int i = 0; i < bookList.size(); i++) {
                        if(bookList.get(i).getBookAuthor().equals(authorSearch)){
                            bookList.get(i).display();
                            check1++;
                        }
                    }
                    if(check1 == 0){
                        System.out.println("Khong co ten tac gia theo yeu cau!!");
                        System.out.println("");
                    }
                break;
                
                case 6:
                    System.out.println("Tam biet!!");
                break;
                
                default:
                    System.err.println("Nhap sai!!!");
                    System.out.println("");
                break;
            }
        }while(choose != 6);
    }
    static void Menu(){
        System.out.println("1.Nhập thông tin n cuốn sách của Aptech");
        System.out.println("2.Hiển thị thông tin vừa nhập");
        System.out.println("3.Sắp xếp thông tin giảm dần theo năm xuất bản và hiển thị");
        System.out.println("4.Tìm kiếm theo tên sách");
        System.out.println("5.Tìm kiếm theo tên tác giả");
        System.out.println("6.Thoát.");
    }
}
#Book


/*
 * 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 lesson4.QuanLiSach;
import java.util.Scanner;

/**
 *
 * @author MyPC
 */
public class Book {
    private String bookName, bookAuthor, producer;
    private int yearPublishing;
    private float price;
    
    //Construct
    public Book(){
    }
    public Book(String bookName, String bookAuthor, String producer, int yearPublishing, float price) {
        this.bookName = bookName;
        this.bookAuthor = bookAuthor;
        this.producer = producer;
        this.yearPublishing = yearPublishing;
        this.price = price;
    }
    
    //set, get
    public String getBookName() {
        return bookName;
    }

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

    public String getBookAuthor() {
        return bookAuthor;
    }

    public void setBookAuthor(String bookAuthor) {
        this.bookAuthor = bookAuthor;
    }

    public String getProducer() {
        return producer;
    }

    public void setProducer(String producer) {
        this.producer = producer;
    }

    public int getYearPublishing() {
        return yearPublishing;
    }

    public void setYearPublishing(int yearPublishing) {
        this.yearPublishing = yearPublishing;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }
    
    //Input
    public void input(){
        Scanner scan = new Scanner(System.in);
        System.out.print("Ten sach: ");
        bookName = scan.nextLine();
        System.out.print("Ten tac gia: ");
        bookAuthor = scan.nextLine();
        System.out.print("Nha san xuat: ");
        producer = scan.nextLine();
        System.out.print("Nam xuat ban: ");
        yearPublishing = scan.nextInt();
        System.out.print("Gia tien: ");
        price = scan.nextFloat();
    }
    
    //Display
    public void display(){
        System.out.println("Ten sach: " + bookName);
        System.out.println("Ten tac gia: " + bookAuthor);
        System.out.println("Nha san xuat: " + producer);
        System.out.println("Nam xuat ban: " + yearPublishing);
        System.out.println("Gia tien: " + price);
    }
    
}


#AptechBook
/*
 * 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 lesson4.QuanLiSach;
import java.util.Scanner;

/**
 *
 * @author MyPC
 */
public class AptechBook extends Book{
    private String language;
    private int semester;
    
    //Construct
    public AptechBook(){
    }
    public AptechBook(String language, int semester, String bookName, String bookAuthor, String producer, int yearPublishing, float price) {
        super(bookName, bookAuthor, producer, yearPublishing, price);
        this.language = language;
        this.semester = semester;
    }
    
    //Get and Set
    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public int getSemester() {
        return semester;
    }

    public void setSemester(int semester) {
        this.semester = semester;
    }
    
    //Input
    @Override
    public void input() {
        Scanner scan = new Scanner(System.in);
        super.input(); //To change body of generated methods, choose Tools | Templates.
        System.out.print("Ngon ngu: ");
        language = scan.nextLine();
        System.out.print("Hoc ki: ");
        semester = scan.nextInt();
    }
    
    //Display
    @Override
    public void display() {
        super.display(); //To change body of generated methods, choose Tools | Templates.
        System.out.println("Ngon ngu: " + language);
        System.out.println("Hoc ki: " + semester);
    }
    
    
    
}



nguyễn văn huy [T1907A]
nguyễn văn huy

2020-12-22 07:30:24



/*
 * 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 QLSach;

import java.util.Scanner;

/**
 *
 * @author ASUS
 */
public class Book {
    private String bookName;
    private String bookAuthor;
    private String producer;
    private int year;
    private float price;

    public Book() {
    }

    public Book(String bookName, String bookAuthor, String producer, int year, float price) {
        this.bookName = bookName;
        this.bookAuthor = bookAuthor;
        this.producer = producer;
        this.year = year;
        this.price = price;
    }

    public String getBookName() {
        return bookName;
    }

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

    public String getBookAuthor() {
        return bookAuthor;
    }

    public void setBookAuthor(String bookAuthor) {
        this.bookAuthor = bookAuthor;
    }

    public String getProducer() {
        return producer;
    }

    public void setProducer(String producer) {
        this.producer = producer;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }
    
    public void input(){
        Scanner scan=new Scanner(System.in);
        System.out.println("Nhập tên sách:");
        bookName=scan.nextLine();
        System.out.println("Nhập tên tác giả:");
        bookAuthor=scan.nextLine();
        System.out.println("Nhập người sản xuất:");
        producer=scan.nextLine();
        System.out.println("Nhập năm sx:");
        year=Integer.parseInt(scan.nextLine());
        System.out.println("Nhập giá:");
        price=Float.parseFloat(scan.nextLine());
    }

    @Override
    public String toString() {
        return "Book{" + "bookName=" + bookName + ", bookAuthor=" + bookAuthor + ", producer=" + producer + ", year=" + year + ", price=" + price + '}';
    }
    
    public void display(){
        System.out.println(toString());
    }
}
>>>>>>>>>
/*
 * 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 QLSach;

import java.util.Scanner;

/**
 *
 * @author ASUS
 */
public class AptechBook extends Book{
    private String language;
    private int semester;

    public AptechBook() {
    }

    public AptechBook(String language, int semester) {
        this.language = language;
        this.semester = semester;
    }

    @Override
    public void input() {
        super.input(); //To change body of generated methods, choose Tools | Templates.
        Scanner scan=new Scanner(System.in);
        System.out.println("Nhập ngôn ngữ:");
        language=scan.nextLine();
        System.out.println("Nhập học kỳ:");
        semester=Integer.parseInt(scan.nextLine());
    }

   

    @Override
    public void display() {
        super.display(); //To change body of generated methods, choose Tools | Templates.
        System.out.println(",Language:"+language+",Sếmter:"+semester);
        System.err.println(" ");
    }
    
    
}
>>>>>>
/*
 * 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 QLSach;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

/**
 *
 * @author ASUS
 */
public class Main {
    public static void main(String[] args) {
        ArrayList<AptechBook>aptechList=new ArrayList<>();
        Scanner scan=new Scanner(System.in);
        int choise,n;
        do {            
            ShowMenu();
            choise=Integer.parseInt(scan.nextLine());
            switch(choise){
                case 1:
                    System.out.println("Nhập thông tin n cuốn sách:");
                    n=Integer.parseInt(scan.nextLine());
                    for (int i = 0; i < n; i++) {
                        AptechBook aptech=new AptechBook();
                        aptech.input();
                        aptechList.add(aptech);
                    }
                    break;
                case 2:
                    for (int i = 0; i <aptechList.size(); i++) {
                        aptechList.get(i).display();
                    }
                    break;
                case 3:
                    Collections.sort(aptechList, new Comparator<AptechBook>() {
                @Override
                public int compare(AptechBook o1, AptechBook o2) {
                   int thtrue=o1.getBookName().compareTo(o2.getBookName());
                   if(thtrue>=0){
                       return -1;
                   }
                   return 1;
                }
            });
                    for (int i = 0; i <aptechList.size(); i++) {
                        aptechList.get(i).display();
                    }
                    break;
                case 4:
                    System.out.println("Nhập tên sách cần tìm:");
                    String bookName;
                    bookName=scan.nextLine();
                    int count=0;
                    for (AptechBook aptechBook : aptechList) {
                        if(aptechBook.getBookName().equalsIgnoreCase(bookName)){
                            aptechBook.display();
                            count++;
                        }if(count==0){
                            System.out.println("Ko tìm thấy....");
                        }
                    }
                    break;
                case 5:
                    System.out.println("Nhập tên tác giả cần tìm");
                    String nameAuthor;
                    nameAuthor=scan.nextLine();
                    int countt=0;
                    for (AptechBook aptechBook : aptechList) {
                        if(aptechBook.getBookAuthor().equalsIgnoreCase(nameAuthor)){
                            aptechBook.display();
                            countt++;
                        }if(countt==0){
                            System.out.println("Ko tìm thấy");
                        }
                    }
                    break;
                case 6:
                    System.out.println("Thoat!!!");
                    break;
                    default:
                        System.out.println("Nhập lại đê");
                        break;
            }
        } while (choise!=7);
    }
    static void ShowMenu(){
        System.out.println("1.Nhập thông tin n cuốn sách của Aptech");
        System.out.println("2.Hiển thị thông tin vừa nhập");
        System.out.println("3.Sắp xếp thông tin giảm dần theo năm xuất bản và hiển thị");
        System.out.println("4.Tìm kiếm theo tên sách");
        System.out.println("5.Tìm kiếm theo tên tác giả");
        System.out.println("6.Thoát!!!");
    }
}




Thành Lâm [T1907A]
Thành Lâm

2020-04-28 09:06:21



/*
 * 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 BaiTap2;

import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author Thannh Lam
 */
public class Main {
    public static void main(String[] args) {
            int choose, n;
            //Quản lý sach tác giả
            ArrayList<Author> authorList = new ArrayList<>();
            
            //Quản lý danh sách sách
            ArrayList<Book> bookList = new ArrayList<>();
            Scanner input = new Scanner(System.in);
            do{
                showMenu();
                choose = Integer.parseInt(input.nextLine());
                
                switch(choose){
                    case 1:
                        System.out.println("Nhập số sách cần thêm: ");
                        n = Integer.parseInt(input.nextLine());
                        for(int i = 0; i<n; i++){
                            Book book = new Book();
                            book.input();
                            boolean isFind = false;
                            for(int j = 0; j < authorList.size(); j++) {
                                if(authorList.get(j).getNickname().equalsIgnoreCase(book.getNickname())) {
                                    isFind = true;
                                    break;
                                }
                            }
                            if(!isFind) {
                            Author  author = new Author(book.getNickname());
                            author.input();
                            
                            //Luu thong tin tac gia
                            authorList.add(author);
                        }
                            
                            //Lưu đối tượng book vào mảng BookList
                            bookList.add(book);
                        
                        }
                        break;
                    case 2:
                        for(Book book : bookList){
                            book.display();
                        }
                        break;
                    case 3: 
                        
                        //Nhập thông tin tác giả
                        
                        System.out.println("Nhập số tác giả cần thêm");
                        n = Integer.parseInt(input.nextLine());
                        
                        for(int i = 0; i < n; i++){
                            Author author = new Author ();
                            author.input(authorList);
                            
                            //lưu đối tượng author vào mảng authorList
                        }
                        break;
                    case 4:
                        System.out.println("Nhập tên Bút danh cần tìm kiếm: ");
                        String nickname = input.nextLine();
                        
                        for(int i = 0; i < bookList.size(); i++) {
                            if(bookList.get(i).getNickname().equalsIgnoreCase(nickname)) {
                                bookList.get(i).display();
                            }
                        }
                        break;
                    case 5:
                        System.out.println("Tạm biệt");
                        break;
                    default:
                        System.err.println("nhập sai");
                        break;
                }
            }while(choose !=5);
    }
    
    static void showMenu(){
        System.out.println("1.Nhập thông tin sách: ");
        System.out.println("2. Hiển thị tất cả sách ra màn hình: ");
        System.out.println("3.Nhập thông tin tác giả: ");
        System.out.println("4.Tìm kiếm sách theo bút danh: ");
        System.out.println("5.Thoát");
    }
}



/*
 * 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 BaiTap2;

import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author Thannh Lam
 */
public class Author {
    private String name;
    private int old;
    private String nickname;
    private String birthday;
    private String address;

    public Author() {
    }

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

    public Author(String name, int old, String nickname, String birthday, String address) {
        this.name = name;
        this.old = old;
        this.nickname = nickname;
        this.birthday = birthday;
        this.address = address;
    }

    public void input(ArrayList<Author> authorList) {
        input();
        Scanner input = new Scanner(System.in);
        
        System.out.println("Nhập bút danh: ");
        while(true){
            nickname = input.nextLine();
            boolean isFind = false;
            for(int i = 0; i<authorList.size(); i++){
               if(authorList.get(i).getNickname().equalsIgnoreCase(nickname)){
                   isFind = true;
               } 
            }
            if(!isFind){
                break;
            }else {
                System.out.println("Nhập bút danh khác: ");
            }
        }
        

    }
    
    
     public void input() {
        System.out.println("--------------------------------");
        Scanner input = new Scanner(System.in);
        System.out.println("Nhập tên tác giả: ");
        name = input.nextLine();
        System.out.println("Nhập tuổi: ");
        old = Integer.parseInt(input.nextLine());
        

        
        System.out.println("Ngày sinh: ");
        birthday = input.nextLine();
        
        
        System.out.println("Nhập địa chỉ: ");
        address = input.nextLine();
    }
        public void display(){
            System.out.println(toString());
        }

    @Override
    public String toString() {
        return "Author{" + "name=" + name + ", old=" + old + ", nickname=" + nickname + ", birthday=" + birthday + ", address=" + address + '}';
    }
        
    
    public String getName() {
        return name;
    }

    public int getOld() {
        return old;
    }

    public String getNickname() {
        return nickname;
    }

    public String getBirthday() {
        return birthday;
    }

    public String getAddress() {
        return address;
    }
    
    
    
}



/*
 * 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 BaiTap2;

import java.util.Scanner;

/**
 *
 * @author Thannh Lam
 */
public class Book {
    String bookName;
    String createdAt;
    String nickname;

    public Book() {
    }

    public Book(String bookName, String createdA, String nickname) {
        this.bookName = bookName;
        this.createdAt = createdA;
        this.nickname = nickname;
    }
    
    public void input() {
        Scanner input = new Scanner(System.in);
        System.out.println("Nhập tên sách: ");
        bookName = input.nextLine();
        
        System.out.println("Ngày xuất bản: ");
        createdAt = input.nextLine();
        
        System.out.println("Nhập bút danh: ");
        nickname = input.nextLine();
        
    }
    
    public void display() {
        
    }
    
    
    @Override
    public String toString() {
        return "Book{" + "bookName=" + bookName + ", createdAt=" + createdAt + ", nickname=" + nickname + '}';
    }
    
    
    

    public String getBookName() {
        return bookName;
    }

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

    public String getCreatedAt() {
        return createdAt;
    }

    public void setCreatedA(String createdA) {
        this.createdAt = createdA;
    }

    public String getNickname() {
        return nickname;
    }

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