Untitled
unknown
plain_text
2 years ago
4.0 kB
4
Indexable
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Books {
private String name;
private double price;
public Books(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public void displayInfo() {
System.out.println("Book: " + name);
System.out.println("Price: RM" + price);
}
}
class BookStore {
private List<Books> books;
public BookStore() {
this.books = new ArrayList<>();
}
public void addBook(Books book) {
books.add(book);
}
public void displayBooks() {
System.out.println("Books in the Bookstore:");
for (Books book : books) {
book.displayInfo();
if (book instanceof Textbook) {
System.out.println("Subject: " + ((Textbook) book).getSubject());
} else if (book instanceof Novel) {
System.out.println("Author: " + ((Novel) book).getAuthor());
}
System.out.println();
}
}
}
class Textbook extends Books {
private String subject;
public Textbook(String name, double price, String subject) {
super(name, price);
this.subject = subject;
}
public String getSubject() {
return subject;
}
@Override
public void displayInfo() {
super.displayInfo();
System.out.println("Subject: " + subject);
}
}
class Novel extends Books {
private String author;
public Novel(String name, double price, String author) {
super(name, price);
this.author = author;
}
public String getAuthor() {
return author;
}
@Override
public void displayInfo() {
super.displayInfo();
System.out.println("Author: " + author);
}
}
public class TestBookApp {
public static void main(String[] args) {
TestBookApp app = new TestBookApp();
app.run();
}
public void run() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of books to add: ");
int numBooks = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
BookStore bookstore = new BookStore();
for (int i = 1; i <= numBooks; i++) {
System.out.println("Book " + i);
System.out.print("Enter book name: ");
String bookName = scanner.nextLine();
System.out.print("Enter book price: ");
double bookPrice = scanner.nextDouble();
scanner.nextLine(); // Consume the newline character
System.out.println("Choose the type of book:");
System.out.println("1. Textbook");
System.out.println("2. Novel");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
if (choice == 1) {
System.out.print("Enter textbook subject: ");
String subjectTxt = scanner.nextLine();
Textbook textbook = new Textbook(bookName, bookPrice, subjectTxt);
bookstore.addBook(textbook);
} else if (choice == 2) {
System.out.print("Enter novel author: ");
String authorNovel = scanner.nextLine();
Novel novel = new Novel(bookName, bookPrice, authorNovel);
bookstore.addBook(novel);
}
System.out.println("Book added: " + bookName);
}
// Displaying the bookstore inventory
bookstore.displayBooks();
// Close the scanner
scanner.close();
}
}Editor is loading...
Leave a Comment