-1

This is my code

Book.java :

public class Book {
    private String id;
    private String name;
    private int quantity;
    private String publisher;
    private double price;
    private double amount;
    
    public Book(String id, String name, int quantity, String publisher, double price) {
        super();
        this.id = id;
        this.name = name;
        this.quantity = quantity;
        this.publisher = publisher;
        this.price = price;
    }
    public Book() {
        this.id="";
        this.name="";
        this.publisher="";
        // TODO Auto-generated constructor stub
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getQuantity() {
        return quantity;
    }
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
    public String getPublisher() {
        return publisher;
    }
    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public double getAmount() {
        return price*quantity;
    }
    public void setAmount(double amount) {
        this.amount = amount;
    }
    
    
}

TextBook.java :

public class TextBook extends Book {

    private boolean status;

    public TextBook(String id, String name, int quantity, String publisher, double price, boolean status) {
        super(id, name, quantity, publisher, price);
        this.status = status;
    }

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    @Override
    public double getAmount() {
        // TODO Auto-generated method stub
        if (status)
        {
            return super.getAmount();
        } else {
            return super.getAmount() * 0.5;
        }
    }

    @Override
    public String toString() {
        return "TextBook [status=" + status + ", getId()=" + getId() + ", getName()=" + getName() + ", getQuantity()="
                + getQuantity() + ", getPublisher()=" + getPublisher() + ", getPrice()=" + getPrice() + ", getAmount()="
                + getAmount() + "]";
    }
}

ReferenceBook.java:

class ReferenceBook extends Book
{
    private double tax;
    public ReferenceBook(String id, String name, int quantity, String publisher, double price) {
        
        // TODO Auto-generated constructor stub
    }
    
    public ReferenceBook(String id, String name, int quantity, String publisher, double price,
            double tax) {
        super(id, name, quantity, publisher, price);
        this.tax=tax;
    }

    @Override
    public double getAmount() {
        // TODO Auto-generated method stub
        return super.getAmount() + tax;
    }

    @Override
    public String toString() {
        return "ReferenceBook [tax=" + tax + ", getId()=" + getId() + ", getName()=" + getName() + ", getQuantity()="
                + getQuantity() + ", getPublisher()=" + getPublisher() + ", getPrice()=" + getPrice()+ ", getAmount()=" + getAmount() +  "]";
    }
}

BookManagement.java:

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

public class BookManagement {
    public Book[] readData(Scanner sc) {
        int n = sc.nextInt();
        Book[] books = new Book[n];
        for (int i=0; i < n; i++)
        {
            int a = Integer.parseInt(sc.nextLine());
            String id = sc.nextLine();
            String name = sc.nextLine();
            int quantity = Integer.parseInt(sc.nextLine());
            String publisher = sc.nextLine();
            double price = Double.parseDouble(sc.nextLine());
            if (a==1) {
                boolean status = Boolean.parseBoolean(sc.nextLine());
                books[i] = new TextBook(id, name, quantity, publisher, price, status);
            }
            if (a==0) {
                double tax = Double.parseDouble(sc.nextLine());
                books[i] = new ReferenceBook(id, name, quantity, publisher, price, tax);
            }
        }
        return books;
    }

    public void printBooks(Book[] books) {
        for (Book book : books)
        {
            System.out.println(book.toString());
        }
    }
    public ArrayList<Book> findBooks(Book[] books, String publisher) {
        ArrayList<Book> results = new ArrayList<Book>();
        for (Book book : books)
        {
            if (book.getPublisher().equals(publisher))
            {
                results.add(book);
            }
        }
        return results;

    }

    public double computeAverage(Book[] books, String typeOfBooks) {
        double sum = 0;
        int count = 0;
        for (Book book : books)
        {
            if (book.getClass().getName().equals(typeOfBooks))
            {
                sum += book.getAmount();
                count++;
            }
        }
        return sum/count;
    }

    public void sortByPrice(Book[] books) {
        for (int i=0; i<books.length-1; i++)
        {
            for (int j=i+1; j<books.length; j++)
            {
                if (books[i].getPrice() > books[j].getPrice())
                {
                    Book temp = new Book();
                    temp = books[i];
                    books[i] =books[j];
                    books[j] = temp;
                }
            }
        }
    }

    public static void main(String[] args) {
        BookManagement bookManagement = new BookManagement();
        Scanner input = new Scanner(System.in);
        int c = Integer.parseInt(input.nextLine());
        if (c==0)
        {
            Book[] books = bookManagement.readData(input);
            bookManagement.printBooks(books);
        }
        if (c==1)
        {
            Book[] books = bookManagement.readData(input);
            ArrayList<Book> bookResults = bookManagement.findBooks(books, "Nhà xuất bản giáo dục");
            Book[] books1 = bookResults.toArray(new Book[bookResults.size()]);
            bookManagement.printBooks(books1);
        }
        if (c==2)
        {
            Book[] books = bookManagement.readData(input);
            System.out.println(bookManagement.computeAverage(books, "TextBook"));
        }
        if (c==3)
        {
            Book[] books = bookManagement.readData(input);
            bookManagement.sortByPrice(books);
            bookManagement.printBooks(books);
        }
    }
}

This is my input:

3
3
1
001
Dstt
3
NXBGD
10000
true
0
002
BTGT
4
NXBDHQG
20000
5000
1
003
GT
3
NXBGD
12000
false

And IntelliJ show:

Exception in thread "main" java.lang.NumberFormatException: For input string: "" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67) at java.base/java.lang.Integer.parseInt(Integer.java:670) at java.base/java.lang.Integer.parseInt(Integer.java:778) at labweek_10/Book.BookManagement.readData(BookManagement.java:22) at labweek_10/Book.BookManagement.main(BookManagement.java:112)

dd000
  • 21
  • 2

0 Answers0