Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

1. 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.

2. The following requirements apply to the IllegalPurchaseArgumentException class.

a. Make this a checked exception by extending the Exception class

b. 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.

image text in transcribed

3. The following requirements apply to the Purchase class.

a. Here is the class diagram: Purchase Class Diagram

image text in transcribed.

b. 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.

c. Create a public get and set method for each private variable.

d. Create a single constructor that has four parameters, one for each private variable.

e. 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.

f. 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]

4. The following requirements apply to the ManagePurchase application class.

a. This class will have the main method. Create an ArrayList to store Purchase objects.

b. 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.

c. 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.

d. When the user chooses menu option 2, display all the Purchase objects in the ArrayList using the toString method of the Purchase class.

e. 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.

f. 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.

g. Remember to write the Purchase data as text data to the file. I recommend the following to write the data.

i. 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.

h. To read the file I recommend the following.

i. 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.

5. Use a Scanner object for all user input. Use System.out for all user output. (This doesn't include the file input and output.)

Chapter 12 IllegalPurchaseArgumentExceptionClass Diagram IllegalPurchaseArgumentException + IllegalPurchaseArgumentException() + IllegalPurchaseArgumentException(message: String) Chapter 12 IllegalPurchaseArgumentExceptionClass Diagram IllegalPurchaseArgumentException + IllegalPurchaseArgumentException() + IllegalPurchaseArgumentException(message: String)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions