Question
Chapter 12 IllegalPurchaseArgumentException Class Diagram IllegalPurchaseArgumentException +IllegalPurchaseArgumentException() +IllegalPurchaseArgumentException(message:String) Chapter 12 Purchase Class Diagram Purchase -productName:String -storeName:String -purchaseDate:LocalDate -cost:double +Purchase(newProduct:String, newStore:String, newDate:LocalDate, newCost:double) +toString():String +getProductName():String +setProductName(newProduct:String):void
Chapter 12
IllegalPurchaseArgumentException Class Diagram
IllegalPurchaseArgumentException
+IllegalPurchaseArgumentException()
+IllegalPurchaseArgumentException(message:String) Chapter 12 Purchase Class Diagram
Purchase -productName:String -storeName:String -purchaseDate:LocalDate -cost:double +Purchase(newProduct:String, newStore:String, newDate:LocalDate, newCost:double) +toString():String +getProductName():String +setProductName(newProduct:String):void +getStoreName():String +setStoreName(newStore:String):void +getPurchaseDate():LocalDate +setPurchaseDate(newDate:LocalDate):void +getCost():String +setCost(newCost:double):void
The purpose of this assignment is to use exceptions, objects, and text file input and file output. The application will allow the user to enter data about purchases and store that data in a text file.
- Create an Eclipse project named ManagePurchases. Use a package name of edu.seminolestate.managepurchases. (You can substitute your domain name for edu.seminolestate if you wish.) This project will have 3 classes Purchases, ManagePurchases, and IllegalPurchaseArgumentException. The ManagePurchases class will have the main method. Each class must be public and in its .java own file.
- The following requirements apply to the IllegalPurchaseArgumentException class.
- Make this a checked exception by extending the Exception class
- Create two constructors as indicated in the UML class diagram (IllegalPurchaseArgumentException Class Diagram). Each constructor must call the superclass constructor passing the appropriate error message.
- The following requirements apply to the Purchase class.
- Here is the class diagram: Purchase Class Diagram.
- Create four private instance variables. Use a String to represent the product name. Use a String to represent the store where the product was purchased. Use a LocalDate to represent the date of the purchase. Use a double to represent the product's cost.
- Create a public get and set method for each private variable.
- Create a single constructor that has four parameters, one for each private variable.
- Implement the following edits: the product name cannot be null nor can it have a length less than one; the store name cannot be null nor can it have a length less than one; the purchase date cannot be null; the cost must be greater than or equal to zero. Throw an IllegalPurchaseArgumentException if any of the values are invalid.
- Create a toString method that displays the class name and the values of each of the four instance variables. Below is sample output from the toString method. (Note: Your browser may display this output on one or more lines. Code it without line breaks.) class edu.seminolestate.managepurchases.Purchase [productName=Bread, storeName=Publix Store 111, purchaseDate=2014-03-13, cost=1.99]
- The following requirements apply to the ManagePurchase application class.
- This class will have the main method. Create an ArrayList to store Purchase objects.
- Display a menu that has choices for: 1 - Add a purchase; 2 - Display all purchases; 3 - Exit. Display an error message and repeat the menu if some other value is entered. Catch an exception if letters instead of numbers are entered.
- When the user chooses menu option 1, prompt for a product name, a store name, purchase date, and a cost. Edit these values such that: the product name cannot have a length less than one; the store name cannot have a length less than one; the purchase date cannot be null; the cost cannot be less than zero. If the data is valid, create a Purchase object and store it in the ArrayList. If the data is invalid prompt the user to re-enter valid data. You must edit the data the user enters to make sure it is in the proper format. Use the material on the "Dates and Times" page in Chapter 9 online notes. Also use the DateTimeParseException to determine when an invalid date is entered.
- When the user chooses menu option 2, display all the Purchase objects in the ArrayList using the toString method of the Purchase class.
- When the user chooses menu option 3, check to see if the ArrayList has any Purchase objects in it. If it does, write the data from each object to a text file. Then display the message "Thank you for using the Purchase Tracker" and end the application. The data must be written to a relative file location using the file name purchases.txt. Make this file name a class constant and use the constant in your code wherever necessary (instead of purchases.txt). Use try with resources when writing the file.
- When the application first starts check to see if the purchases.txt file exists. If it does, read each piece of data for an object, create the Purchase object, and store the Purchase object into the ArrayList. If the file doesn't exist just continue with an empty ArrayList. Use try with resources when reading the file.
- Remember to write the Purchase data as text data to the file. I recommend the following to write the data.
- For each Purchase object in the ArrayList, call the get method for the product name and write this as a line to the file; call the get method for store name and write this as a line to the file; call the get method for the purchase date, convert it to a string and write this as a line to the file; call the get method for the cost, convert this to a String, and write this as a line to the file. In this design, each object will create four lines of text data in the output file.
- To read the file I recommend the following.
- Each set of four lines in the file are required to create a Purchase object. As long as the file has more data to read, read a line and consider this the product name; read the next line and consider this the store name; read the next line and consider this the purchase date; convert this string version of the purchase date to a LocalDate object; read the next line and consider this the cost; convert this String to a double. Use these four pieces of data to instantiate a Purchase object. Put the Purchase object into the ArrayList. Continue processing the next four lines of data from the file, if any.
- Use a Scanner object for all user input. Use System.out for all user output. (This doesn't include the file input and output.)
Transcribed image text
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started