By GokiSoft.com| 14:54 28/09/2022|
Java Basic

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

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


#Author.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 bt1136;

import java.util.Scanner;

/**
 *
 * @author diepvan
 */
public class Author {
    String name;
    String birthday;
    String nickname;
    String address;

    public Author() {
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap ten: ");
        name = scan.nextLine();
        System.out.println("Nhap ngay sinh: ");
        birthday = scan.nextLine();
        System.out.println("Nhap but danh: ");
        nickname = scan.nextLine();
        System.out.println("Nhap dia chi: ");
        address = scan.nextLine();
    }
    
    public void inputIgnoreNickname() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap ten: ");
        name = scan.nextLine();
        System.out.println("Nhap ngay sinh: ");
        birthday = scan.nextLine();
        System.out.println("Nhap dia chi: ");
        address = scan.nextLine();
    }
    
    public void display() {
        System.out.println(this);
    }

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


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

import java.util.Scanner;

/**
 *
 * @author diepvan
 */
public class Book {
    String title;
    String publishDate;
    String nickname;

    public Book() {
    }

    public String getNickname() {
        return nickname;
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap ten sach: ");
        title = scan.nextLine();
        
        System.out.println("Nhap ngay XB: ");
        publishDate = scan.nextLine();
        
        System.out.println("Nhap but danh: ");
        nickname = scan.nextLine();
        
        Test01.checkNickname(nickname);
    }
    
    public void display() {
        System.out.println(this);
    }

    @Override
    public String toString() {
        return "title=" + title + ", publishDate=" + publishDate + ", nickname=" + nickname;
    }
    
    
}


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

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author diepvan
 */
public class Main {
    public static void main(String[] args) {
        //B1. Xu ly 1 object
//        Author author = new Author();
//        
//        author.input();
//        
//        author.display();

        //Tao mang quan ly author
        List<Author> authorList = new ArrayList<>();
        
        for (int i = 0; i < 5; i++) {
            Author author = new Author();
            author.input();
            
            authorList.add(author);
        }
        
        //Hien thi danh sach author
        for (int i = 0; i < 5; i++) {
            authorList.get(i).display();
        }
        
        for (Author author : authorList) {
            author.display();
        }
    }
}


#Test.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 bt1136;

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

/**
 *
 * @author diepvan
 */
public class Test {
    public static List<Author> authorList = new ArrayList<>();
    public static List<Book> bookList = new ArrayList<>();
    public static Scanner scan = new Scanner(System.in);
    
    public static void main(String[] args) {
        int choose;
        int n;
        
        do {
            showMenu();
            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++) {
                        Book b = new Book();
                        b.input();
                        
                        bookList.add(b);
                    }
                    break;
                case 2:
                    System.out.println("Thong tin sach: ");
                    for (Book b : bookList) {
                        b.display();
                    }
                    break;
                case 3:
                    System.out.println("Nhap so tac gia can them: ");
                    n = Integer.parseInt(scan.nextLine());
                    
                    for (int i = 0; i < n; i++) {
                        Author author = new Author();
                        author.input();
                        
                        authorList.add(author);
                    }
                    break;
                case 4:
                    System.out.println("Nhap but danh can tim: ");
                    String nickname = scan.nextLine();
                    
                    for (Book book : bookList) {
                        if(book.getNickname().equalsIgnoreCase(nickname)) {
                            book.display();
                        }
                    }
                    break;
                case 5:
                    System.out.println("Thoat!!!");
                    break;
                default:
                    System.out.println("Nhap sai!!!");
            }
        } while(choose != 5);
    }
    
    static void showMenu() {
        System.out.println("1. Nhap sach");
        System.out.println("2. Hien thi sach");
        System.out.println("3. Nhap tac gia");
        System.out.println("4. Tim kiem sach theo tac gia");
        System.out.println("5. Thoat");
        System.out.println("Chon: ");
    }
}


#Test01.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 bt1136;

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

/**
 *
 * @author diepvan
 */
public class Test01 {

    public static List<Author> authorList = new ArrayList<>();
    public static List<Book> bookList = new ArrayList<>();
    public 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:
                    inputBook();
                    break;
                case 2:
                    displayBook();
                    break;
                case 3:
                    inputAuthor();
                    break;
                case 4:
                    searchByNickname();
                    break;
                case 5:
                    System.out.println("Thoat!!!");
                    break;
                default:
                    System.out.println("Nhap sai!!!");
            }
        } while (choose != 5);
    }

    static void inputBook() {
        System.out.println("Nhap so sach can them: ");
        int n = Integer.parseInt(scan.nextLine());

        for (int i = 0; i < n; i++) {
            Book b = new Book();
            b.input();

            bookList.add(b);
        }
    }

    static void displayBook() {
        System.out.println("Thong tin sach: ");
        for (Book b : bookList) {
            b.display();
        }
    }

    static void inputAuthor() {
        System.out.println("Nhap so tac gia can them: ");
        int n = Integer.parseInt(scan.nextLine());

        for (int i = 0; i < n; i++) {
            Author author = new Author();
            author.input();

            authorList.add(author);
        }
    }

    static void searchByNickname() {
        System.out.println("Nhap but danh can tim: ");
        String nickname = scan.nextLine();

        for (Book book : bookList) {
            if (book.getNickname().equalsIgnoreCase(nickname)) {
                book.display();
            }
        }
    }
    
    public static void checkNickname(String nickname) {
        for (Author author : authorList) {
            if(author.getNickname().equalsIgnoreCase(nickname)) {
                return;
            }
        }
        
        System.out.println("====== Author does not exist ======");
        Author author = new Author();
        author.setNickname(nickname);
        
        author.inputIgnoreNickname();
        
        authorList.add(author);
        System.out.println("====================================");
    }

    static void showMenu() {
        System.out.println("1. Nhap sach");
        System.out.println("2. Hien thi sach");
        System.out.println("3. Nhap tac gia");
        System.out.println("4. Tim kiem sach theo tac gia");
        System.out.println("5. Thoat");
        System.out.println("Chon: ");
    }
}


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 đó