Advertisement
Cs_java987

Untitled

Mar 7th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. //testbook
  2. import java.util.Scanner;
  3.  
  4. public class TestBook {
  5. public static Scanner reader = new Scanner(System.in);
  6.  
  7. public static void main(String[] args) {
  8. Book b1 = new Book("Yuval Noah Harari", "Sapiens", 70, 20);
  9. Book b2 = new Book("Andy Weir", "THe Martian", 50, 100);
  10. Book b3 = new Book("Suzanne Collins", "The Hunger Games", 60, 10);
  11. System.out.println(b1 + "\n" + b2 + "\n" + b3);
  12. Book[] books = new Book[20];
  13. booksDetails(books);
  14. BookShop bs1 = new BookShop("Book Depository", books);
  15. showBookDetails(bs1.getBooksInStore());
  16. System.out.println(valueOfTheBooks(bs1.getBooksInStore()));
  17. showBooksLessThan5(bs1.getBooksInStore());
  18. }
  19.  
  20. public static void booksDetails(Book[] books) {
  21. String author = "", name = "";
  22. int price = 0, stock = 0;
  23. for (int i = 0; i < books.length; i++) {
  24. System.out.println("Enter the author of the " + (i + 1) + " book");
  25. author = reader.nextLine();
  26. System.out.println("Enter the name of the " + (i + 1) + " book");
  27. name = reader.nextLine();
  28. System.out.println("Enter the price of the " + (i + 1) + " book");
  29. price = reader.nextInt();
  30. System.out.println("How many books are in stock?");
  31. stock = reader.nextInt();
  32. reader.nextLine();
  33. books[i] = new Book(author, name, price, stock);
  34. }
  35. }
  36.  
  37. public static void showBookDetails(Book[] books) {
  38. for (int i = 0; i < books.length; i++) {
  39. System.out.println(books[i]);
  40. }
  41. }
  42.  
  43. public static int valueOfTheBooks(Book[] books) {
  44. int sum1book = 0, sumofall = 0;
  45. for (int i = 0; i < books.length; i++) {
  46. sum1book = books[i].getPrice() * books[i].getStock();
  47. sumofall += sum1book;
  48. }
  49. return sumofall;
  50. }
  51.  
  52. public static void showBooksLessThan5(Book[] books) {
  53. String all = "", one = "";
  54. for (int i = 0; i < books.length; i++) {
  55. if (books[i].getStock() < 5) {
  56. one = books[i].getName() + " ";
  57. all += one;
  58. }
  59. }
  60. System.out.println(all);
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement