Advertisement
Guest User

LendingLibrary

a guest
Oct 22nd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Library
  4. {
  5.  
  6. public static void main(String[] args)
  7. {
  8. Scanner input = new Scanner(System.in);
  9.  
  10. final int SIZE = 100;
  11. MediaItem[] media = new MediaItem[SIZE];
  12. int choice = 0;
  13. choice = displayMenu();
  14. int numberOfItems = 0;
  15.  
  16. while (choice != 5)
  17. {
  18. int i = 0;
  19. switch (choice)
  20.  
  21. {
  22. case 1: {
  23. numberOfItems = addItem(media, numberOfItems);
  24. break;
  25. }
  26.  
  27. case 2: {
  28. markItemOnLoan(media, numberOfItems);
  29. break;
  30. }
  31.  
  32. case 3: {
  33. listAllItems(numberOfItems, media);
  34. break;
  35. }
  36.  
  37. case 4: {
  38. System.out.println("Which item? ");
  39. String title = input.nextLine();
  40. markItemReturned(title, numberOfItems, media);
  41. break;
  42. }
  43. }
  44. choice = displayMenu();
  45. }
  46. }
  47.  
  48. public static int displayMenu()
  49. {
  50. int choice = 0;
  51.  
  52. Scanner input = new Scanner(System.in);
  53.  
  54.  
  55. System.out.println("1. Add new item");
  56. System.out.println("2. Mark and item as on loan");
  57. System.out.println("3. List all items");
  58. System.out.println("4. Mark an item as returned");
  59. System.out.println("5. Quit");
  60. System.out.println("What would you like to do?\n");
  61.  
  62. choice = input.nextInt();
  63. input.nextLine();
  64. return (choice);
  65. }
  66.  
  67. public static int addItem(MediaItem media[], int numberOfItems)
  68. {
  69. String title;
  70. String format;
  71.  
  72. Scanner input = new Scanner(System.in);
  73.  
  74. System.out.print("What is the title? ");
  75. title = input.nextLine();
  76. System.out.print("What is the format? ");
  77. format = input.nextLine();
  78.  
  79. MediaItem item = new MediaItem(title, format);
  80. media[numberOfItems] = item;
  81. numberOfItems++;
  82.  
  83. return (numberOfItems);
  84.  
  85. }
  86.  
  87. public static void markItemOnLoan(MediaItem media[], int numberOfItems)
  88. {
  89. Scanner input = new Scanner(System.in);
  90. String title;
  91. String name;
  92. String date;
  93. int called = 0;
  94. System.out.print("Which item do you want to loan? ");
  95. title = input.nextLine();
  96.  
  97.  
  98. if (called == 0)
  99. System.out.println("I'm sorry, I couldn't find " + title + " in the library.");
  100. else {
  101.  
  102.  
  103. called = 0;
  104. System.out.print("Who are you loaning it to? ");
  105. name = input.nextLine();
  106. System.out.print("What date did you loan it? ");
  107. date = input.nextLine();
  108.  
  109. for (int i = 0; i < numberOfItems; i++)
  110. {
  111. if (title.equals(media[i].title))
  112. {
  113. media[i].markItemOnLoan(title, name, date);
  114. called = 1;
  115. }
  116. }
  117. }
  118. //if (called == 0)
  119. // System.out.println("I'm sorry, I couldn't find " + title + " in the library.");
  120. //called = 0;
  121. }
  122.  
  123. public static void listAllItems(int numberOfItems, MediaItem media[])
  124. {
  125. String[] str = new String[100];
  126. for (int i = 0; i < numberOfItems; i++) {
  127. if (media[i].onLoan)
  128. str[i] = "\n" + media[i].title + " " + media[i].format + " loaned to " + media[i].loanedTo + " on "
  129. + media[i].dateLoaned;
  130. else
  131. str[i] = "\n" + media[i].title + " " + media[i].format;
  132. System.out.println(str[i] + "\n");
  133. }
  134. }
  135. public static void markItemReturned(String title, int numberOfItems, MediaItem media[])
  136. {
  137. int check = 0;
  138.  
  139. for(int i = 0; i < numberOfItems; i++)
  140. {
  141. if(title.equals(media[i].title))
  142. {
  143. media[i].markItemReturned(title);
  144. check = 1;
  145. }
  146. }
  147. if(check == 0)
  148. System.out.println("I'm sorry, I couldn't find " + title + " in the library.");
  149. check = 0;
  150. }
  151. }
  152.  
  153.  
  154.  
  155. *******************************************************************************************************
  156. *******************************************************************************************************
  157.  
  158. import java.util.Scanner;
  159.  
  160. public class MediaItem
  161. {
  162.  
  163. String title;
  164. String format;
  165. boolean onLoan;
  166. String loanedTo;
  167. String dateLoaned;
  168.  
  169. MediaItem(String title, String format)
  170. {
  171. onLoan = false;
  172. this.title = title;
  173. this.format = format;
  174. }
  175.  
  176. public void markItemOnLoan(String title, String name, String date)
  177. {
  178. if(onLoan == true)
  179. System.out.println( title + " is already on loan to " + loanedTo);
  180. else
  181. {
  182. onLoan = true;
  183. loanedTo = name;
  184. dateLoaned = date;
  185. }
  186. }
  187.  
  188.  
  189. public void markItemReturned(String title)
  190. {
  191. if(onLoan == false)
  192. System.out.println(this.title + " is not currently on loan");
  193. else
  194. onLoan = false;
  195.  
  196.  
  197. }
  198.  
  199.  
  200. public String getDateLoaned()
  201. {
  202. return dateLoaned;
  203. }
  204. public void setDateLoaned(String dateLoaned)
  205. {
  206. this.dateLoaned = dateLoaned;
  207. }
  208.  
  209.  
  210. public String getFormat()
  211. {
  212. return format;
  213. }
  214. public void setFormat(String format)
  215. {
  216. this.format = format;
  217. }
  218.  
  219.  
  220. public boolean isOnLoan()
  221. {
  222. return onLoan;
  223. }
  224. public void setOnLoan(boolean onLoan)
  225. {
  226. this.onLoan = onLoan;
  227. }
  228.  
  229.  
  230. public String getLoanedTo()
  231. {
  232. return loanedTo;
  233. }
  234. public void setLoanedTo(String loanedTo)
  235. {
  236. this.loanedTo = loanedTo;
  237. }
  238.  
  239.  
  240. public String getTitle()
  241. {
  242. return title;
  243. }
  244. public void setTitle(String title)
  245. {
  246. this.title = title;
  247. }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement