Advertisement
NB52053

dsdsdd

Jun 20th, 2017
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class BookStore {
  4.  
  5. private Book[] b = new Book[10]; int len = 0;
  6.  
  7. public BookStore() {
  8.  
  9.  
  10. b[0] = new Book("Head First Java", "O Railey", "JAVAISBN", 3);
  11. b[1] = new Book("C Programming", "Herbert Schield", "CPRGRM", 4);
  12. b[2] = new Book("Linear Algebra", "Kenith Bleh", "MATH183", 5);
  13.  
  14. len = 3;
  15.  
  16. }
  17.  
  18. public void display() {
  19.  
  20. System.out.println("\n");
  21.  
  22.  
  23. for (int i = 0; i < 10 && b[i] != null; i++) {
  24.  
  25. System.out.printf("%d %s\t%s\t%s\t%d\n", i+1, b[i].bookTitle, b[i].author, b[i].ISBN, b[i].numCopies);
  26. }
  27.  
  28. if (len == 0) System.out.println("There are no books to display."); System.out.println("\n\n");
  29.  
  30. }
  31.  
  32. public void order(String isbn, int nums) {
  33.  
  34. for ( int index = 0; index < len && b[index] != null; index++) {
  35.  
  36. if (b[index].ISBN.equals(isbn) ) { b[index].numCopies += nums; return;
  37.  
  38. }
  39. }
  40.  
  41. Scanner scan = new Scanner(System.in);
  42.  
  43. System.out.println("New Book Name: ");
  44. String name = scan.nextLine();
  45.  
  46. System.out.println("New Book's Author: ");
  47. String auth = scan.nextLine()
  48. b[index] = new Book(name, auth, isbn, nums); len++;
  49.  
  50. }
  51.  
  52. public void sell(String name, int nums) {
  53. for (int index = 0; index < len && b[index] != null; index++) {
  54.  
  55. if (b[index].bookTitle.equals(name) ) {
  56.  
  57. if (b[index].numCopies >= nums) {
  58.  
  59. b[index].numCopies -= nums;
  60.  
  61. System.out.printf("You have just sold %d copies of %s\n\n",
  62. nums, name);
  63.  
  64. System.out.printf("You now have %d of %s left.\n\n", b[index].numCopies, b[index].bookTitle);
  65.  
  66. return;
  67. }
  68. }
  69. }
  70. System.out.println("The book is not available now... Do check back another time!");
  71. }
  72.  
  73. }
  74.  
  75.  
  76. import java.util.Scanner;
  77.  
  78. class BookStoreApp {
  79. public static void main(String[] args) {
  80. Scanner scan = new Scanner(System.in);
  81.  
  82.  
  83. BookStore books = new BookStore();
  84.  
  85. System.out.println("\n---------- Welcome to your Book Store App ----------\n\n");
  86.  
  87. while (true) {
  88.  
  89. System.out.println("Enter <1> for Displaying all books.");
  90. System.out.println("Enter <2> to order a new book.");
  91. System.out.println("Enter <3> for selling a book.");
  92. System.out.println("Enter <4> for Exiting Out.\n\n");
  93.  
  94. System.out.printf("Enter your choice: ");
  95.  
  96. int choice;
  97. choice = scan.nextInt();
  98.  
  99. String nameOrIsbn; int nums;
  100.  
  101. switch (choice)
  102. {
  103. case 1:
  104. books.display(); break;
  105. case 2:
  106. System.out.printf("Enter Book's ISBN: "); nameOrIsbn =
  107. scan.next();
  108.  
  109. System.out.printf("How many? "); nums = scan.nextInt(); books.order(nameOrIsbn, nums);
  110.  
  111. break;
  112. case 3:
  113.  
  114. System.out.printf("Enter Book's Name: "); nameOrIsbn =
  115. scan.next();
  116.  
  117. System.out.printf("How many? "); nums = scan.nextInt(); books.sell(nameOrIsbn, nums);
  118.  
  119. break;
  120. case 4:
  121.  
  122. System.out.println("Bye Bye!\nSee you later!"); scan.close(); System.exit(1);
  123.  
  124. break;
  125.  
  126. default:
  127. System.out.println("Only numbers between 1 and 4 (inclusive) are
  128. accepted.\n\n");
  129. }
  130. }
  131.  
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement