Advertisement
Guest User

Assignment 8

a guest
Oct 31st, 2013
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.59 KB | None | 0 0
  1. Assignment #8
  2.  
  3. Due Date
  4.  
  5. Friday, November 1st, at 7pm.
  6.  
  7. Important: This is an individual assignment. Please do not collaborate.
  8.  
  9. Minimal Submitted Files
  10.  
  11. You are required, but not limited, to turn in the following source files:
  12.  
  13. Assignment8.java(More code need to be added)
  14. Customer.java(given by the instructor, it needs to be modified for this assignment)
  15. CustomerParser.java(given by the instructor)
  16. Bank.java(from assignment 4, it needs to be modified for this assignment)
  17. BankParser.java(given by the instructor)
  18. Address.java(from assignment 4, it needs to be modified for this assignment)
  19. Sorts.java
  20. BankSystem.java
  21.  
  22. Requirements to get full credits in Documentation
  23.  
  24. The assignment number, your name, StudentID, Lecture number/time, and a class description need to be included at the top of each file/class.
  25. A description of each method is also needed.
  26. Some additional comments inside of methods(especially for the "main" method) to explain code that are hard to follow should be written.
  27. You can look at the Java programs in the text book to see how comments are added to programs.
  28.  
  29. Skills to be Applied
  30.  
  31. In addition to what has been covered in previous assignments, the use of the following items, discussed in class, will probably be needed:
  32.  
  33. Vector/ArrayList
  34. Sorting
  35. Searching
  36. Aggregate Objects
  37. Interfaces
  38. Serialization
  39. File read/write
  40.  
  41. Program Description
  42.  
  43. Class Diagram (download .ppt file of the figure):
  44.  
  45.  
  46.  
  47. Customer
  48.  
  49. The Customer class implements the "Comparable" interface. (The Comparable interface is defined in the "java.lang" package.) In addition to the attributes and methods constructed already in the given file, the following method must be defined:
  50.  
  51. public int compareTo(Object other)
  52.  
  53. If the current object has the last name lexicographically less than that of the argument, an int less than zero is returned. If the current object has the last name greater than that of the argument, an int greater than zero is returned. If the both objects have the same last Name, their first names should be compared.
  54.  
  55. The Customer class also implements the "Serializable" interface so that its object can be stored. (The Serializable interface is defined in the "java.io" pakcage.)
  56.  
  57. Address
  58.  
  59. The Address class implements the "Comparable" interface. In addition to the attributes and methods constructed from the previous assignment, the following method must be defined:
  60.  
  61. public int compareTo(Object other)
  62.  
  63. If the current object has the state lexicographically less than that of the argument, an int less than zero is returned. If the current object has the state greater than that of the argument, an int greater than zero is returned. If the both states are same, their cities should be compared lexicographically. If the current city is larger, it should return a positive integer. If the current city is smaller, it should return a negative integer. If the cities are same (and the states are same), then it should return 0.
  64.  
  65. The Address class also implements the "Serializable" interface so that its object can be stored.
  66.  
  67. Bank
  68.  
  69. The Bank class implements the "Comparable" interface. In addition to the attributes and methods constructed from the previous assignment, the following method must be defined:
  70.  
  71. public int compareTo(Object other)
  72.  
  73. If the current object has the bank name lexicographically less than that of the argument, an int less than zero is returned. If the current object has the bank name greater than that of the argument, an int greater than zero is returned. If the both objects have the same bank name, their bank id should be compared. should be compared using the compareTo method of the Address class. If the both objects have the same bank name and id, then their address should be compared using the compareTo method of the Address class.
  74.  
  75. The Bank class also implements the "Serializable" interface so that its object can be stored.
  76.  
  77. Sorts
  78.  
  79. The Sorts class is a utility class that will be used to sort the customer list and the bank list. Sorting algorithms are described in the algorithm note posted under Notes section of the course web site. These algorithms sort numbers stored in an array. It is your job to modify it to sort objects stored in an array list (or a vector).
  80. The Sorts class object will never be instantiated. It must have the following methods:
  81.  
  82. public static void sort(ArrayList objects)
  83. Your sort method utilizes the compareTo method of objects to be sorted. You can use one of Insertion Sort, Merge Sort, or Quick Sort.
  84.  
  85. BankSystem
  86.  
  87. The BankSystem class has a list of customers and a list of banks that can be organized at the bank system. The bank system will be a fully encapsulated object. The BankSystem class implements the Serializable interface.
  88. It has the following attributes:
  89. Attribute name Attribute type Description
  90. customerList ArrayList or Vector A list of customers in the bank system
  91. bankList ArrayList or Vector A list of banks in the bank system
  92. The following public methods should be provided to interact with the bank system:
  93.  
  94. Method Description
  95. BankSystem() A Constructor of the BankSystem class. The ArrayLists/Vectors of customerList and bankList are instantiated.
  96. int customerExists(String) Search for a customer by customer ID and return the index of the object if found. Return -1 if not found. The parameter is the customer ID of a customer.
  97. boolean addCustomer(String) Add a customer to the customer list. Return true if the customer was added successfully. Return false if the customer with the same customer ID already exists (the customer is not added). The parameter is the string to be parsed by the CustomerParser.
  98. boolean removeCustomer(String) Remove a customer from the customer list. Return true if the customer was removed successfully. Return false if the customer with the given customer ID does not exist. The parameter is the customer ID of a customer.
  99. void sortCustomers() Sort the list of customers by last name from A to Z. If more than one customer have the same last name, compare their first names. This method calls the sort method defined in the Sorts class.
  100. String listCustomers() List all customers in the customer list. The returned string is the concatenation of each customer object information in the list. Hint: you can utilize customer's toString method to help you complete this method. If there is no customer in the list, This method should return the string containing "no customer\n".
  101. int bankExists(String bankName, int bankID, String city, String state) Search for a bank in the bank list. Return the index of the bank object if the bank object of the same bank name, id, city, and state is found. Return -1 if not found. The parameters are the bank name (String), the id (int), the city (String), and the state (String) of the bank to be searched.
  102. boolean addBank(String) Add a bank to the bank list. Return true if the bank was added successfully. Return false if the bank already exists based on the criteria in the bankExists method (the bank is not added in this case). The parameter is the string to be parsed by the BankParser.
  103. boolean removeBank(tring bankName, int bankID, String city, String state) Remove a bank from the bank list. Return true if the bank of the given bank name, id, city and state was removed successfully. Return false if the bank does not exist. The parameters are the bank name (String), the id (int), the city (String), and the state (String) of the bank to be removed.
  104. void sortBanks() Sort the list of banks. If more than one bank have the same bank name, compare their id. If they have the same id, compare address. This method calls the sort method defined in the Sorts class.
  105. String listBanks() List all banks in the bank list. The returned string is the concatenation of each bank object information in the list. Hint: you can utilize bank's toString method to help you complete this method. If there is no bank in the list, it should return the string containing "no bank\n"
  106. void closeBankSystem() Closes the bank system by making all lists empty. This can be done by using clear() method of the ArrayList.
  107. No input/output should occur in the bank system. User interaction should be handled only by the driver class. Customers will be uniquely identified by their customer ID, banks will be uniquely identified by the combination of their bank name, id, city, and state. This might not a realistic assumption, but it will make the project easier to implement.
  108.  
  109. You may add other methods to the class in order to make your life easier.
  110.  
  111. Assignment8
  112.  
  113. All input and output should be handled in Assignment8 class. The main method should start by displaying this updated menu in this exact format:
  114.  
  115. Choice\t\tAction\n
  116. ------\t\t------\n
  117. A\t\tAdd Customer\n
  118. B\t\tAdd Bank\n
  119. D\t\tSearch for Customer\n
  120. E\t\tSearch for Bank\n
  121. L\t\tList Customers\n
  122. M\t\tList Banks\n
  123. O\t\tSort Customers\n
  124. P\t\tSort Banks\n
  125. Q\t\tQuit\n
  126. R\t\tRemove Customer\n
  127. S\t\tRemove Bank\n
  128. T\t\tClose BankSystem\n
  129. U\t\tWrite Text to File\n
  130. V\t\tRead Text from File\n
  131. W\t\tSerialize BankSystem to File\n
  132. X\t\tDeserialize BankSystem from File\n
  133. ?\t\tDisplay Help\n\n
  134.  
  135. Next, the following prompt should be displayed:
  136.  
  137. What action would you like to perform?\n
  138.  
  139. Read in the user input and execute the appropriate command. After the execution of each command, redisplay the prompt. Commands should be accepted in both lowercase and uppercase. The following commands are modified or new.
  140.  
  141. Add Customer
  142.  
  143. Your program should display the following prompt:
  144.  
  145. Please enter the customer information to add:\n
  146.  
  147. Read in customer information and parse it using the CustomerParser class, if the customer object with the same customer ID is not in the customer list, then add it into the customer list and display:
  148.  
  149. customer added\n
  150.  
  151. Otherwise, display:
  152.  
  153. customer exists\n
  154.  
  155. Add Bank
  156.  
  157. Your program should display the following prompt:
  158.  
  159. Please enter the bank information to add:\n
  160.  
  161. Read in the information and parse it using bank parser. If the bank already exists, display the following message:
  162.  
  163. bank exists\n
  164.  
  165. Otherwise, add the object to the bank list and display the following:
  166.  
  167. bank added\n
  168.  
  169. Search for Customer
  170.  
  171. Your program should display the following prompt:
  172.  
  173. Please enter the customerID of a customer to search:\n
  174.  
  175. Read in the string and look up the customer list, if there exists a customer object with the same customer ID, then display the following:
  176.  
  177. customer found\n
  178.  
  179. Otherwise, display this:
  180.  
  181. customer not found\n
  182.  
  183. Search for Bank
  184.  
  185. Your program should display the following prompt:
  186.  
  187. Please enter the bank's name to search:\n
  188.  
  189. Read in the bank name, then display the following prompt:
  190.  
  191. Please enter the bank's id to search:\n
  192.  
  193. Read in the id, then display the following prompt:
  194. Please enter the bank's city to search:\n
  195.  
  196. Read in the city, then display the following prompt:
  197. Please enter the bank's state to search:\n
  198.  
  199. Read in the state, and search the bank list based on these information. If there exists a bank with the bank name, the id, the city, and the state, then display the following:
  200. bank found\n
  201.  
  202. Otherwise, display this:
  203.  
  204. bank not found\n
  205.  
  206. Sort Customers
  207.  
  208. Your program should sort the customers list alphabetically by the last name (A to Z) and output the following:
  209.  
  210. customers sorted\n
  211.  
  212. If there are multiple customer objects with the same last name in the customer list, they should be sorted by the first name. If both last name and first name are same, their order after sorting should be the same as the order before sorting.
  213.  
  214. Sort Banks
  215.  
  216. Your program should sort the bank list alphabetically by bank name (A to Z and 0 - 9) and output the following:
  217.  
  218. banks sorted\n
  219.  
  220. If there are multiple bank objects with the same bank name in the bank list, they should be sorted by their id. If they have the same bank name and the id, then they should be sorted by their address.
  221.  
  222. Remove Customer
  223.  
  224. Your program should display the following prompt:
  225.  
  226. Please enter the customerID to remove:\n
  227.  
  228. Read in the string. If the customer object can be found in the customer list, then remove it from the list, and display the following:
  229.  
  230. customer removed\n
  231.  
  232. If there is no such customer object in the customer list, display:
  233.  
  234. customer not found\n
  235.  
  236. Reminder:
  237. Once again, customers will be uniquely identified by the customer ID. In another words, there does NOT exist more than one customer object in the customer list, that have the same customer ID.
  238.  
  239. Remove Bank
  240.  
  241. Your program should display the following prompt:
  242.  
  243. Please enter the bank's bank name to remove:\n
  244.  
  245. Read in the bank name and display the following prompt:
  246.  
  247. Please enter the bank's id to remove:\n
  248.  
  249. Read in the id, and display the following prompt:
  250.  
  251. Please enter the bank's city to remove:\n
  252.  
  253. Read in the city, and display the following prompt:
  254.  
  255. Please enter the bank's state to remove:\n
  256.  
  257. Read in the state, and use those information to remove that the bank object from the bank list. If the bank cannot be found, then display the following:
  258.  
  259. bank not found\n
  260.  
  261. Otherwise, remove successfully, then display:
  262.  
  263. bank removed\n
  264.  
  265. List Customers
  266.  
  267. Each customer information in the customer list should be displayed in the following format:
  268.  
  269. \nFirst name:\t\tBarack\n
  270. Last name:\t\tObama\n
  271. Customer ID:\t\t000-00-0000\n
  272. Cash amount:\t\t$2,000.00\n\n
  273.  
  274. If there is no customer object in the customer list, display:
  275.  
  276. no customer\n
  277.  
  278. A real example is looked like this (suppose there are only 2 customer objects in the customer list):
  279.  
  280. What action would you like to perform?
  281.  
  282. First name: Barack
  283. Last name: Obama
  284. Customer ID: 234-56-7890
  285. Cash amount: $20.00
  286.  
  287. First name: George
  288. Last name: Bush
  289. Customer ID: 000-00-0000
  290. Cash amount: $30.00
  291.  
  292. List Banks
  293.  
  294. Each bank information in the bank list should be displayed in the following format:
  295.  
  296. \nBank name:\t\tBank of Arizona\n
  297. Bank ID:\t\t1005\n
  298. Bank address:\t\tTempe,AZ\n\n
  299.  
  300.  
  301. If there is no bank object in the bank list, display:
  302.  
  303. no bank\n
  304.  
  305. A real example is looked like this (suppose there are only 2 bank objects in the bank list):
  306.  
  307. What action would you like to perform?
  308.  
  309. Bank name: Bank Arizona
  310. Bank ID: 1
  311. Bank address: Phoenix,AZ
  312.  
  313.  
  314. Bank name: Bank Nevada
  315. Bank ID: 5
  316. Bank address: Las Vegas,NV
  317.  
  318.  
  319. Close BankSystem
  320. Delete all customers and banks. Then, display the following:
  321.  
  322. bank system closed\n
  323.  
  324. Write Text to File
  325. Your program should display the following prompt:
  326.  
  327. Please enter a file name to write:\n
  328.  
  329. Read in the filename and create an appropriate object to get ready to read from the file. Then it should display the following prompts:
  330.  
  331. Please enter a string to write in the file:\n
  332.  
  333. Read in the string that a user types, say "input", then attach "\n" at the end of the string, and write it to the file. (i.e. input+"\n" string will be written in the file.)
  334.  
  335. If the operation is successful, display the following:
  336.  
  337. FILENAME was written\n
  338.  
  339. Replace FILENAME with the actual name of the file.
  340.  
  341. Use try and catch statements to catch IOException. The file should be closed in a finally statement.
  342.  
  343. Read Text from File
  344. Your program should display the following prompt:
  345.  
  346. Please enter a file name to read:\n
  347.  
  348. Read in the file name create appropriate objects to get ready to read from the file. If the operation is successful, display the following (replace FILENAME with the actual name of the file):
  349.  
  350. FILENAME was read\n
  351.  
  352. Then read only the first line in the file, and display:
  353.  
  354. The first line of the file is:\n
  355.  
  356. CONTENT\n
  357.  
  358. where CONTENT should be replaced by the actual first line in the file.
  359.  
  360. Your program should catch the exceptions if there are. (Use try and catch statement to catch, FileNotFoundException, and the rest of IOException.)
  361.  
  362. If the file name cannot be found, display
  363.  
  364. FILENAME was not found\n
  365.  
  366. where FILENAME is replaced by the actual file name.
  367.  
  368. Serialize BankSystem to File
  369.  
  370. Your program should display the following prompt:
  371.  
  372. Please enter a file name to write:\n
  373.  
  374. Read in the filename and write the serialized BankSystem object out to it. Note that any objects to be stored must implement Serializable interface. The Serializable interface is defined in java.io.* package. If the operation is successful, display the following:
  375.  
  376. FILENAME was written\n
  377.  
  378. Replace FILENAME with the actual name of the file.
  379.  
  380. Use try and catch statements to catch NotSerializableExeption and IOException.
  381.  
  382. Deserialize BankSystem from File
  383. Your program should display the following prompt:
  384.  
  385. Please enter a file name to read:\n
  386.  
  387. Read in the file name and attempt to load the BankSystem object from that file. Note that there is only one BankSystem object in the Assignment8 class, and the first object read from the file should be assigned to the BankSystem object. If the operation is successful, display the following (replace FILENAME with the actual name of the file):
  388.  
  389. FILENAME was read\n
  390.  
  391. Your program should catch the exceptions if there are.
  392.  
  393. (Use try and catch statement to catch ClassNotFoundException, FileNotFoundException, and the rest of IOException.)
  394. See the output files for exception handling.
  395.  
  396. Test cases:
  397.  
  398.  
  399. hw8testcases.jar All test case files are in this file. To extract each file:
  400. jar xf hw8testcases.jar
  401.  
  402. OR, you can download each file individually:
  403. Input
  404.  
  405. The following files are the test cases that will be used as input for your program (Right-click and use "Save As"):
  406.  
  407. Test Case #1
  408. Test Case #2
  409. Test Case #3
  410. Test Case #4
  411.  
  412.  
  413. Output
  414.  
  415. The following files are the expected outputs of the corresponding input files from the previous section (Right-click and use "Save As"):
  416.  
  417. Test Case #1
  418. Test Case #2
  419. Test Case #3
  420. Test Case #4
  421.  
  422.  
  423. Error Handling
  424.  
  425. Your program is expected to be robust to pass all test cases.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement