Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //testbook
- import java.util.Scanner;
- public class TestBook {
- public static Scanner reader = new Scanner(System.in);
- public static void main(String[] args) {
- Book b1 = new Book("Yuval Noah Harari", "Sapiens", 70, 20);
- Book b2 = new Book("Andy Weir", "THe Martian", 50, 100);
- Book b3 = new Book("Suzanne Collins", "The Hunger Games", 60, 10);
- System.out.println(b1 + "\n" + b2 + "\n" + b3);
- Book[] books = new Book[20];
- booksDetails(books);
- BookShop bs1 = new BookShop("Book Depository", books);
- showBookDetails(bs1.getBooksInStore());
- System.out.println(valueOfTheBooks(bs1.getBooksInStore()));
- showBooksLessThan5(bs1.getBooksInStore());
- }
- public static void booksDetails(Book[] books) {
- String author = "", name = "";
- int price = 0, stock = 0;
- for (int i = 0; i < books.length; i++) {
- System.out.println("Enter the author of the " + (i + 1) + " book");
- author = reader.nextLine();
- System.out.println("Enter the name of the " + (i + 1) + " book");
- name = reader.nextLine();
- System.out.println("Enter the price of the " + (i + 1) + " book");
- price = reader.nextInt();
- System.out.println("How many books are in stock?");
- stock = reader.nextInt();
- reader.nextLine();
- books[i] = new Book(author, name, price, stock);
- }
- }
- public static void showBookDetails(Book[] books) {
- for (int i = 0; i < books.length; i++) {
- System.out.println(books[i]);
- }
- }
- public static int valueOfTheBooks(Book[] books) {
- int sum1book = 0, sumofall = 0;
- for (int i = 0; i < books.length; i++) {
- sum1book = books[i].getPrice() * books[i].getStock();
- sumofall += sum1book;
- }
- return sumofall;
- }
- public static void showBooksLessThan5(Book[] books) {
- String all = "", one = "";
- for (int i = 0; i < books.length; i++) {
- if (books[i].getStock() < 5) {
- one = books[i].getName() + " ";
- all += one;
- }
- }
- System.out.println(all);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement