Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Library {
  4. private Book[] arrOfBooks;
  5. int numOfDiffBooks = 0;
  6. final int sizeOfLibrary;
  7. Scanner input = new Scanner(System.in);
  8. public Library(int size)
  9. {
  10. sizeOfLibrary = size;
  11. arrOfBooks = new Book[size];
  12. }
  13. public boolean putBook(Book someBook)
  14. {
  15. for (int i = numOfDiffBooks-1; i>=0;i--)
  16. {
  17. if (someBook.equals (arrOfBooks[i]))
  18. {
  19. arrOfBooks[i].setCopys(arrOfBooks[i].getCopys()+1);
  20. return true;
  21. }
  22. }
  23. if (numOfDiffBooks + 1 <= sizeOfLibrary )
  24. {
  25. numOfDiffBooks++;
  26. arrOfBooks[numOfDiffBooks-1] = someBook;
  27. return true;
  28. }
  29. return false;
  30. }
  31. public int numberOfDifBooks()
  32. {
  33. return numOfDiffBooks;
  34. }
  35. public int numOfCopies(Book someBook)
  36. {
  37. for (Book Books : arrOfBooks)
  38. {
  39. if (someBook.equals(Books))
  40. return Books.getCopys();
  41. }
  42. return 0 ;
  43. }
  44. public int numOfBooks()
  45. {
  46. int sum = 0;
  47. for (int i = 0; i<numOfDiffBooks;i++)
  48. sum+= arrOfBooks[i].getCopys();
  49. return sum;
  50. }
  51.  
  52. }
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60. import java.lang.String;
  61. public class Book {
  62. private String nameOfBook;
  63. private int copys;
  64.  
  65. public Book(String title)
  66. {
  67. nameOfBook = title;
  68. copys = 1;
  69. }
  70. public void setName(String name)
  71. {
  72. nameOfBook = name;
  73. }
  74. public String getTitle()
  75. {
  76. return nameOfBook;
  77. }
  78. public boolean equals(Book other)
  79. {
  80. if (nameOfBook.equals(other.getTitle()))
  81. return true;
  82. else return false;
  83. }
  84. public int getCopys()
  85. {
  86. return copys;
  87. }
  88. public void setCopys(int num)
  89. {
  90. copys = num;
  91. }
  92. }
  93.  
  94.  
  95.  
  96. import java.util.Scanner;
  97.  
  98. public class LibTest {
  99.  
  100. public static void main(String[] args) {
  101.  
  102. final int size;
  103. int tmp,tmpCpy;
  104. Library lib = null;
  105. Book someBook = null;
  106.  
  107. System.out.println("Enter library size: ");
  108. Scanner input = new Scanner(System.in);
  109. size = input.nextInt();
  110. lib = new Library(size);
  111.  
  112. //Menu with options (put book,checking library size, and number of copies for a specific book
  113. do {
  114. System.out.println("\nWhat would you like to do next?\n\n1 - Put book\n2 - Show the amount of diffrent books the library can hold\n3 - Show number of copies for a specific book\n4 - Total number of books in the library\n5 - Exit");
  115. tmp = input.nextInt();
  116. input.nextLine();
  117.  
  118. switch(tmp) {
  119.  
  120. case 1:
  121. System.out.println("Enter book name: ");
  122. someBook = new Book(input.nextLine());
  123. if (lib.putBook(someBook)) {
  124. System.out.println("The book '"+someBook.getTitle()+"' was entered!");
  125. }
  126. else {
  127. System.out.println("Library is full!");
  128. }
  129. break;
  130.  
  131. case 2:
  132. System.out.println("The library can hold a maximum of " + lib.sizeOfLibrary + " different books");
  133. break;
  134.  
  135. case 3:
  136. System.out.println("Enter book name: ");
  137. someBook= new Book(input.nextLine());
  138. tmpCpy = lib.numOfCopies(someBook);
  139. if (tmpCpy>0) {
  140. System.out.println("The library has " + tmpCpy + " copies of the book: " + someBook.getTitle());
  141. }
  142. else {
  143. System.out.println("Book doesn't exist");
  144. }
  145. break;
  146. case 4:
  147. System.out.println("The total is: " + lib.numOfBooks());
  148. break;
  149. case 5:
  150. System.out.println("Good bye!");
  151. input.close();
  152. return;
  153. default:
  154. System.out.println("Not valid, try again.");
  155.  
  156.  
  157. }
  158. }while(tmp != 0);
  159.  
  160. input.close();
  161. }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement