By GokiSoft.com| 15:14 19/09/2022|
Java Basic

[Souce Code] Java Basic- OOP - quản lý sách trong java - C2109I

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


#AptechBook.java


/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.lesson05.bt987;

import java.util.Scanner;

/**
 *
 * @author diepvan
 */
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;
    }
    
    public void input() {
        super.input();
        
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap ngon ngu lap trinh: ");
        language = scan.nextLine();
        System.out.println("Nhap ky hoc: ");
        semester = Integer.parseInt(scan.nextLine());
    }

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


#Book.java


/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.lesson05.bt987;

import java.util.Scanner;

/**
 *
 * @author diepvan
 */
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.println("Nhap ten sach: ");
        bookName = scan.nextLine();
        System.out.println("Nhap ten tac gia: ");
        bookAuthor = scan.nextLine();
        System.out.println("Nhap nxb: ");
        producer = scan.nextLine();
        System.out.println("Nhap nam xuat ban: ");
        yearPublishing = Integer.parseInt(scan.nextLine());
        System.out.println("Nhap gia: ");
        price = Float.parseFloat(scan.nextLine());
    }

    @Override
    public String toString() {
        return "bookName=" + bookName + ", bookAuthor=" + bookAuthor + ", producer=" + producer + ", yearPublishing=" + yearPublishing + ", price=" + price;
    }
    
    public void display() {
        System.out.println(this);
    }
}


#Main.java


/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.lesson05.bt987;

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

/**
 *
 * @author diepvan
 */
public class Main {
    static List<AptechBook> dataList = new ArrayList<>();
    static Scanner scan = new Scanner(System.in);
    
    public static void main(String[] args) {
        int choose;
        
        do {
            showMenu();
            choose = Integer.parseInt(scan.nextLine());
            
            switch (choose) {
                case 1:
                    input();
                    break;
                case 2:
                    display();
                    break;
                case 3:
                    sortByYear();
                    break;
                case 4:
                    searchByNam();
                    break;
                case 5:
                    searchByAuthor();
                    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 N sach");
        System.out.println("2. Hien thi");
        System.out.println("3. Sap xep theo nam");
        System.out.println("4. Tim kiem theo ten");
        System.out.println("5. Tim kiem theo tac gia");
        System.out.println("6. Thoat");
        System.out.println("Chon: ");
    }

    private static void input() {
        System.out.println("Nhap so sach can them: ");
        int n = Integer.parseInt(scan.nextLine());
        
        for (int i = 0; i < n; i++) {
            AptechBook book = new AptechBook();
            book.input();
            
            dataList.add(book);
        }
    }

    private static void display() {
        System.out.println("Danh sach sach: ");
        for (AptechBook aptechBook : dataList) {
            aptechBook.display();
        }
    }

    private static void sortByYear() {
        Collections.sort(dataList, new Comparator<AptechBook>() {
            @Override
            public int compare(AptechBook o1, AptechBook o2) {
                if(o1.getYearPublishing() >= o2.getYearPublishing()) {
                    return -1;
                }
                return 1;
            }
        });
        
        display();
    }

    private static void searchByNam() {
        System.out.println("Nhap ten can tim: ");
        String s = scan.nextLine();
        
        for (AptechBook aptechBook : dataList) {
            if(aptechBook.getBookName().equalsIgnoreCase(s)) {
                aptechBook.display();
            }
        }
    }

    private static void searchByAuthor() {
        System.out.println("Nhap tac gia can tim: ");
        String s = scan.nextLine();
        
        for (AptechBook aptechBook : dataList) {
            if(aptechBook.getBookAuthor().equalsIgnoreCase(s)) {
                aptechBook.display();
            }
        }
    }
}


Tags:



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

5

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

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó