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

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

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.

Liên kết rút gọn:

https://gokisoft.com/987

Bình luận

avatar
hieuvm0512 [community,C2010L]
2021-07-07 08:25:15



#MAIN

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

import static java.lang.System.exit;
import java.util.Scanner;

/**
 *
 * @author vuive
 */
public class main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        AptechBook Aptech[] = null;
        int n = 0;
        int choose;
        do {
            System.out.println("Menu");
            System.out.println("1. Nhap thong tin sach.");
            System.out.println("2. Hien thi sach.");
            System.out.println("3. Sap xep thong tin giam dan theo nam xuat ban.");
            System.out.println("4. Tim kiem theo ten sach.");
            System.out.println("5. Tim kiem theo ten tac gia");
            System.out.println("6. Thoat");
            choose = input.nextInt();
            input.nextLine();
            switch (choose) {
                case 1:

                    System.out.println("So luong sach muon them vao: ");
                    n = input.nextInt();
                    Aptech = new AptechBook[n];
                    for (int i = 0; i < n; i++) {
                        System.out.format("Nhap thong tin sach %d \n", i+1);
                        Aptech[i] = new AptechBook();
                        Aptech[i].input();
                    }
                    break;
                case 2:
                    for (int i = 0; i < n; i++) {
                        System.out.format("Thong tin sach thu %d \n", i+1);
                        Aptech[i].display();
                    }
                    break;
                case 3:
                    AptechBook temp = Aptech[0];
                    System.out.println("Sap xep theo nam xuat ban: ");
                    for(int i =0; i<n-1;i++){
                        for(int j=1;j<n;j++){
                            if(Aptech[i].getYearPublishing()<Aptech[j].getYearPublishing()){
                                temp = Aptech[j];
                                Aptech[j]=Aptech[i];
                                Aptech[i]=temp;
                            }
                            
                        }
                    }
                    for(int i=0;i<n;i++){
                        System.out.format("Sach so %d \n", i+1);
                        Aptech[i].display();
                    }
                    break;
                case 4:
                    System.out.println("Nhap ten sach can tim: ");

                    String s = input.nextLine();
                    for(int i = 0; i<n;i++){
                        if(s.equals(Aptech[i].getBookName())){
                            Aptech[i].display();
                        }else{
                            System.out.format("Ten %s khong trung khop! Sach %d \n", s,i);
                        }
                    }
                    break;
                case 5:
                    System.out.println("Nhap ten tac gia can tim: \n");

                    String t = input.nextLine();
                    for(int i = 0; i<n;i++){
                        if(t.equals(Aptech[i].getBookAuthor())){
                            Aptech[i].display();
                        }
                        else{
                            System.out.format("Tac gia %s khong trung khop! Sach %d \n", t,i);
                        }
                    }
                    break;
                case 6: 
                    exit(0);

            }
        } while (true);

    }
}

#Class 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 Quanly;

import java.util.Scanner;

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

    private String bookName;

    private String bookAuthor;

    private String producer;

    private int yearPublishing;

    private float price;

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

    public void display() {
        System.out.format("Title :%s \n", bookName);
        System.out.format("Author :%s \n", bookAuthor);
        System.out.format("Manufacturer :%s \n", producer);
        System.out.format("Year :%s \n", yearPublishing);
        System.out.format("Price :%s \n", 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 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;
    }

}

#Class 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 Quanly;

import java.util.Scanner;

/**
 *
 * @author vuive
 */
public class AptechBook extends Book {

    private String language;

    private int semester;
    
    public AptechBook(){
    }

    /**
     *
     * @param bookName
     * @param bookAuthor
     * @param producer
     * @param yearPublishing
     * @param price
     * @param language
     * @param semester
     */
    public AptechBook(String bookName, String bookAuthor, String producer, int yearPublishing, float price, String language, int semester) {
        super(bookName,bookAuthor,producer,yearPublishing,price);
        this.language = language;
        this.semester = semester;
    }
    @Override
    public void input(){
        super.input();
        Scanner input = new Scanner(System.in);
        System.out.println("Nhap ngon ngu: ");
        language = input.nextLine();
        System.out.println("Nhap hoc ki: ");
        semester = input.nextInt();
    }
    @Override
    public void display(){
        super.display();
        System.out.format("Ngon ngu: %s \n", language);
        System.out.format("Hoc ki: %d \n", semester);
    }

    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;
    }
}


avatar
Đào Mạnh Dũng [C2010L]
2021-07-06 16:00:12


#AptechBook.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 test;

import java.util.Scanner;

/**
 *
 * @author inclu
 */
public class AptechBook extends Book{
    
    private String language;

    private int semester;

    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 void setLanguage(String language) {
        this.language = language;
    }

    public int getSemester() {
        return semester;
    }

    public void setSemester(int semester) {
        this.semester = semester;
    }
    
    @Override
    public void input(){
        
        super.input();

    Scanner scan = new Scanner(System.in);    
        
        System.out.print("Nhap ngon ngu : ");
        language = scan.nextLine();
        System.out.print("Nhap semester : ");
        semester = scan.nextInt();
        
    }
    
    
    @Override
    public void display(){
        
        super.display();
        
            System.out.println("ngon ngu : " + language);

            System.out.println("semester : " + semester);
        
        
    }
    
    
    
    
}


#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 test;

import java.util.Scanner;

/**
 *
 * @author inclu
 */
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 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;
    }
    
    public void input(){
       
    Scanner scan = new Scanner(System.in);    
        
        System.out.print("Nhap ten sanh : ");
        bookName = scan.nextLine();
        System.out.print("Nhap ten tac gia : ");
        bookAuthor = scan.nextLine();
        System.out.print("Nhap ten NXB : ");
        producer = scan.nextLine();
        System.out.print("Nhap nam suat ban : ");
        yearPublishing = scan.nextInt();
        System.out.print("Nhap Gia : ");
        price = scan.nextFloat();

    }
    
    public void display(){
        
        System.out.println("ten sanh : "+bookName);
        System.out.println("ten tac gia : "+bookAuthor);
        System.out.println("ten NXB : "+producer);
        System.out.println("nam suat ban : "+yearPublishing);
        System.out.println("Gia : "+price);
         
        
    }
    
}


#menu.txt


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.


#MyLib.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 test;
import java.io.*;
import java.util.Scanner;
/**
 *
 * @author inclu
 */
class MyLib {
    private final static String FILE_URL = "D:\\_Project_Java\\test\\src\\test\\menu.txt";
    
    static String choose;
    static int chooses;
    public static int menu() throws IOException, InterruptedException {
        
        cls();
        
        File file = new File(FILE_URL);
        InputStream inputStream = new FileInputStream(file);
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader reader = new BufferedReader(inputStreamReader);
 
        String line = "";
        while((line = reader.readLine()) != null){
            System.out.println(line);
        }
       
        
        Scanner scan = new Scanner(System.in);    
            
        choose = scan.next();
            
try {
        chooses = Integer.parseInt(choose);
} catch (NumberFormatException e) {
        System.out.println("ban phai nhap mot so");
}    
            
        return chooses;
    }
 public static void cls()
{
	try
	{	
		new ProcessBuilder("cmd","/c","cls").inheritIO().start().waitFor();
	}catch(Exception E)
		{
			System.out.println(E);
		}
}
	
}


#Test.java


package test;

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



public class Test {

int i;
static Scanner scan = new Scanner(System.in);

    public static ArrayList<AptechBook> listbook = new ArrayList<>();
    
    public static void main(String[] args) throws IOException, InterruptedException {
        
        
        
        while(true){
            
            switch(MyLib.menu()) {
            case 1 -> {
                    AptechBook book = new AptechBook();
                    book.input();
                    listbook.add(book);
                }
            
            case 2 -> {
                    if(listbook.size()-1<0) System.out.println("chua co 1 quyen sach nao ca");
                    else listbook.get(listbook.size()-1).display();
                }
            case 3 -> {
                if(listbook.size()-1<0) System.out.println("chua co 1 quyen sach nao ca");
                else{
                    Collections.sort(listbook, Collections.reverseOrder()); 
                    for (int i = 0; i < listbook.size(); i++) {
                        
                        listbook.get(i).display();
                }
                    
                }
                }
            case 4 -> {
                if(listbook.size()-1<0) System.out.println("chua co 1 quyen sach nao ca");
                else{
                    System.out.print("nhap ten sach can tim : ");
                    String name = scan.nextLine();
                    System.out.println("");
                for (int i = 0; i < listbook.size(); i++) {

                    if(listbook.get(i).getBookName().indexOf(name)!=-1){
                        listbook.get(i).display();
                        System.out.println("");
                    }
                }
                }
                }
            case 5 -> {
                if(listbook.size()-1<0) System.out.println("chua co 1 quyen sach nao ca");
                else{
                    System.out.print("nhap ten tac gia can tim : ");
                    String name = scan.nextLine();
                    System.out.println("");
                for (int i = 0; i < listbook.size(); i++) {

                    if(listbook.get(i).getBookAuthor().indexOf(name)!=-1){
                        listbook.get(i).display();
                        System.out.println("");
                    }
                }
                }
                }
            case 6 -> {
                    System.exit(0);
                }
              
            default -> System.out.println("Lua chon ko hop le");
          }
            String name = scan.nextLine();
       }
         
    }
}
    
 


avatar
Triệu Văn Lăng [T2008A]
2021-03-02 14:23:03



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

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

/**
 *
 * @author MTLS
 */
public class main {

    public static void main(String[] args) {
        ArrayList<aptechbook> aptechBook = new ArrayList<>();
        Scanner scan = new Scanner(System.in);

        int choose;

        do {
            showMenu();
            choose = Integer.parseInt(scan.nextLine());

            switch (choose) {
                case 1:
                    System.out.print("Nhap so sach can them: ");
                    int n = Integer.parseInt(scan.nextLine());

                    for (int i = 0; i < n; i++) {
                        aptechbook book = new aptechbook();
                        book.input();

                        aptechBook.add(book);
                    }
                    break;
                case 2:
                    System.out.print("Nhap thong tin cuon sach: ");

                    for (aptechbook book : aptechBook) {
                        book.display();
                    }
                    break;
                case 3:
                    Collections.sort(aptechBook, new Comparator<aptechbook>() {
                        @Override
                        public int compare(aptechbook o1, aptechbook o2) {
                            if (o1.getYearPublishing() < o2.getYearPublishing()) {
                                return 1;
                            }
                            return -1;
                        }
                    });
                    System.out.println("sap xep thanh cong");
                    for (aptechbook book : aptechBook) {
                        book.display();
                    }
                    break;
                case 4:
                    System.out.println("Nhap ten sach can tim: ");
                    String bookName = scan.nextLine();
                    for (aptechbook book : aptechBook) {
                        if (book.getBookName().equalsIgnoreCase(bookName)) {
                            book.display();
                        }
                    }
                    break;
                case 5:
                    System.out.println("Nhap ten tac gia can tim: ");
                    String bookAthor = scan.nextLine();
                    for (aptechbook book : aptechBook) {
                        if (book.getBookAuthor().equalsIgnoreCase(bookAthor)) {
                            book.display();
                        }
                    }
                    break;
                case 6:
                    System.out.print("Thoat");
                    break;
                default:
                    System.out.print("Nhap sai");
                    break;
            }
        } while (choose != 6);
    }

    private static void showMenu() {
        System.out.println("\n1. Nhap n cuon sach: ");
        System.out.println("2. Hien thi thong tin");
        System.out.println("3. Sap xep thong tin");
        System.out.println("4. Tim sach theo ten");
        System.out.println("5. Tim theo ten tac gia");
        System.out.println("6. Thoat");
    }

    private static class input {

        public input() {
        }
    }
}


avatar
Triệu Văn Lăng [T2008A]
2021-03-02 14:22:34



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

import java.util.Scanner;

/**
 *
 * @author MTLS
 */
public class aptechbook extends book {

    private String language;

    private int semester;

    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 input() {
        super.input();

        Scanner scan = new Scanner(System.in);

        System.out.print("Nhap ki hoc: ");
        semester = Integer.parseInt(scan.nextLine());

        System.out.print("Nhap ngon ngu: ");
        language = scan.nextLine();
    }

    @Override
    public void display() {
        super.display();
        System.out.format("ngon ngu: %s, ki hoc: %d", language, semester);
    }

    @Override
    public String toString() {
        return super.toString() + "aptechbook{" + "language=" + language + ", semester=" + semester + '}';
    }

}


avatar
Triệu Văn Lăng [T2008A]
2021-03-02 14:22:07



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

import java.util.Scanner;

/**
 *
 * @author MTLS
 */
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.print("Nhap ten sach: ");
        bookName = scan.nextLine();

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

        System.out.print("Nhap ten NSX: ");
        producer = scan.nextLine();

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

        System.out.print("Nhap gia ban: ");
        price = Float.parseFloat(scan.nextLine());

    }

    public void display() {
        System.out.print(this);
    }

    @Override
    public String toString() {
        return "book{" + "bookName=" + bookName + ", bookAuthor=" + bookAuthor + ", producer=" + producer + ", yearPublishing=" + yearPublishing + ", price=" + price + '}';
    }

}


avatar
Nguyễn Anh Vũ [T2008A]
2021-02-24 09:25:14


#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 QLS.java;
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);
    }
    
    
}


avatar
Nguyễn Anh Vũ [T2008A]
2021-02-24 09:23:20



/*
 * 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 QLS.java;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
/**
 *
 * @author Admin
 */
public class Test {
    public static void main (String[] args){
        ArrayList<Book> bookList = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        int choose,n;
        
        do {
            showMenu();
            choose= Integer.parseInt(sc.nextLine());
            switch (choose){
                case 1:
                    System.out.println("Nhap tong so quyen sach: ");
                    n = Integer.parseInt(sc.nextLine());
                    for(int i =0;i<n;i++){
                        System.out.println("Nhap thong tin quyen sach thu "+(i+1)+":");
                        Book book = new Book();
                        book.input();
                        bookList.add(book);
                    }
                    break;
                case 2:
                    for(int i=0; i<bookList.size();i++){
                        bookList.get(i).display();
                    }
                    break;
                case 3:
                    Collections.sort(bookList, new Comparator<Book>() {
                    @Override
                        public int compare(Book o1, Book o2) {
                            if(o1.getYearPublishing() < o2.getYearPublishing()) {
                                return 1;
                            }
                            return -1;
                        }
                    });
                    for (int i =0; i<bookList.size();i++){
                        bookList.get(i).display();
                    }
                    break;
                case 4:
                     System.out.println("Nhập tên sách cần tìm:");
                    String bookName;
                    bookName=sc.nextLine();
                    int count=0;
                    for (Book book : bookList) {
                        if(book.getBookName().equalsIgnoreCase(bookName)){
                            book.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=sc.nextLine();
                    int countt=0;
                    for (Book book : bookList) {
                        if(book.getBookAuthor().equalsIgnoreCase(nameAuthor)){
                            book.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("Nhap sai!!!");
                    break;
            }
        }while (choose !=6);
    }
    static void showMenu(){
        System.out.println("1. Nhap thong tin n cuon sach Aptech");
        System.out.println("2. Hien thi thong tin vua nhap");
        System.out.println("3. Sap xep thong tin giam dan theo nam xuat ban va hien thi");
        System.out.println("4. Tim kiem theo ten sach");
        System.out.println("5. Tim kiem theo ten tac gia");
        System.out.println("6. Thoat");
        
    }
}


avatar
TRẦN VĂN ĐIỆP [Teacher]
2021-02-22 07:42:41


#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 aptech.lesson05;

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

/**
 *
 * @author Diep.Tran
 */
public class Main {

    public static void main(String[] args) {
        ArrayList<AptechBook> aptechBooks = new ArrayList<>();

        Scanner input = new Scanner(System.in);
        int choose;

        do {
            showMenu();
            choose = Integer.parseInt(input.nextLine());

            switch (choose) {
                case 1:
                    System.out.println("Nhap so sach can them N = ");
                    int n = Integer.parseInt(input.nextLine());

                    for (int i = 0; i < n; i++) {
                        AptechBook book = new AptechBook();
                        book.input();

                        aptechBooks.add(book);
                    }
                    break;
                case 2:
                    System.out.println("Thong tin quan sach:");
//                    for (int i = 0; i < aptechBooks.size(); i++) {
//                        AptechBook book = aptechBooks.get(i);
//                        book.display();
//                    }

                    //foreach
                    for (AptechBook book : aptechBooks) {
                        book.display();
                    }
                    break;
                case 3:
                    //sap xep
                    Collections.sort(aptechBooks, new Comparator<AptechBook>() {
                        @Override
                        public int compare(AptechBook o1, AptechBook o2) {
                            if(o1.getYearPublishing() < o2.getYearPublishing()) {
                                return 1;
                            }
//                            if(o1.getBookName().compareToIgnoreCase(o2.getBookName()) < 0) {
//                                return 1;
//                            }
                            return -1;
                        }
                    });
                    System.out.println("Sap xep thanh cong!!!");
                    
                    //foreach
                    for (AptechBook book : aptechBooks) {
                        book.display();
                    }
                    break;
                case 4:
                    System.out.println("Nhap ten sach can tim: ");
                    String bookName = input.nextLine();
                    
                    //foreach
                    for (AptechBook book : aptechBooks) {
                        if(book.getBookName().equalsIgnoreCase(bookName)) {
                            book.display();
                        }
                    }
                    break;
                case 5:
                    System.out.println("Nhap ten tac gia can tim: ");
                    String bookAuthor = input.nextLine();
                    
                    //foreach
                    for (AptechBook book : aptechBooks) {
                        if(book.getBookAuthor().equalsIgnoreCase(bookAuthor)) {
                            book.display();
                        }
                    }
                    break;
                case 6:
                    System.out.println("Goodbye!!!");
                    break;
                default:
                    System.out.println("Nhap sai!!!");
                    break;
            }
        } while (choose != 6);
    }

    public static void showMenu() {
        System.out.println("\n1. Nhap N quan sach");
        System.out.println("2. Hien thi");
        System.out.println("3. Sap xep");
        System.out.println("4. Tim kiem theo ten sach");
        System.out.println("5. Tim kiem theo tac gia");
        System.out.println("6. Thoat");
        System.out.println("Chon: ");
    }
}


#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 aptech.lesson05;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Book {
    private String bookName;

    private String bookAuthor;

    private String producer;

    private int yearPublishing;

    private float price;
    
//    public static void main(String[] args) {
//        Book book = new Book();
//        book.input();
//        
//        book.display();
//    }

    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 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;
    }

    public void input() {
        //Khai bao ham nhap
        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("Nha sx: ");
        producer = scan.nextLine();
        
        System.out.println("Nhap nam sx: ");
        yearPublishing = Integer.parseInt(scan.nextLine());
        
        System.out.println("Nhap gia ban: ");
        price = Float.parseFloat(scan.nextLine());
    }
    
    public void display() {
        System.out.println("");
        System.out.format("Ten quan sach: %s, tac gia: %s, nha sx: %s, nam sx: %d, gia: %f", 
                bookName, bookAuthor, producer, yearPublishing, price);
//        System.out.println(this);
    }

    @Override
    public String toString() {
        return "Book{" + "bookName=" + bookName + ", bookAuthor=" + bookAuthor + ", producer=" + producer + ", yearPublishing=" + yearPublishing + ", price=" + price + '}';
    }
}


#AptechBook.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 aptech.lesson05;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class AptechBook extends Book {
    private String language;

    private int semester;

    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 void setLanguage(String language) {
        this.language = language;
    }

    public int getSemester() {
        return semester;
    }

    public void setSemester(int semester) {
        this.semester = semester;
    }
    
    @Override
    public void input() {
        super.input();
        
        //Khai bao ham nhap
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap ky hoc: ");
        semester = Integer.parseInt(scan.nextLine());
        
        System.out.println("Nhap ngon ngu lap trinh: ");
        language = scan.nextLine();
    }
    
    @Override
    public void display() {
        super.display();
        
//        System.out.println(this);
        System.out.format(", ngon ngu lap trinh: %s, ky hoc: %d", language, semester);
    }

    @Override
    public String toString() {
        return super.toString() + " - AptechBook{" + "language=" + language + ", semester=" + semester + '}';
    }
}


avatar
Trần Thị Khánh Huyền [T2008A]
2021-02-21 01:41:35



/*
 * 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.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
/**
 *
 * @author Admin
 */
public class Test {
    public static void main (String[] args){
        ArrayList<Book> bookList = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        int choose,n;
        
        do {
            showMenu();
            choose= Integer.parseInt(sc.nextLine());
            switch (choose){
                case 1:
                    System.out.println("Nhap tong so quyen sach: ");
                    n = Integer.parseInt(sc.nextLine());
                    for(int i =0;i<n;i++){
                        System.out.println("Nhap thong tin quyen sach thu "+(i+1)+":");
                        Book book = new Book();
                        book.input();
                        bookList.add(book);
                    }
                    break;
                case 2:
                    for(int i=0; i<bookList.size();i++){
                        bookList.get(i).display();
                    }
                    break;
                case 3:
                    Collections.sort(bookList, new Comparator<Book>() {
                    @Override
                        public int compare(Book o1, Book o2) {
                            if(o1.getYearPublishing() < o2.getYearPublishing()) {
                                return 1;
                            }
                            return -1;
                        }
                    });
                    for (int i =0; i<bookList.size();i++){
                        bookList.get(i).display();
                    }
                    break;
                case 4:
                     System.out.println("Nhập tên sách cần tìm:");
                    String bookName;
                    bookName=sc.nextLine();
                    int count=0;
                    for (Book book : bookList) {
                        if(book.getBookName().equalsIgnoreCase(bookName)){
                            book.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=sc.nextLine();
                    int countt=0;
                    for (Book book : bookList) {
                        if(book.getBookAuthor().equalsIgnoreCase(nameAuthor)){
                            book.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("Nhap sai!!!");
                    break;
            }
        }while (choose !=6);
    }
    static void showMenu(){
        System.out.println("1. Nhap thong tin n cuon sach Aptech");
        System.out.println("2. Hien thi thong tin vua nhap");
        System.out.println("3. Sap xep thong tin giam dan theo nam xuat ban va hien thi");
        System.out.println("4. Tim kiem theo ten sach");
        System.out.println("5. Tim kiem theo ten tac gia");
        System.out.println("6. Thoat");
        
    }
}


avatar
Trần Thị Khánh Huyền [T2008A]
2021-02-21 01:41:05



/*
 * 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);
    }
    
    
}