Question: When you have successfully completed each step, save your project ( using the Save As option ) at the end of each subsequent step so

When you have successfully completed each step, save your project (using the Save As option) at the end of each subsequent step so that you have a backup and can always refer back to.
For this homework, you should also consider for appropriate methods what happens if the input to that method is not as expected. There are several ways of dealing with this and the way that we want you to deal with this for the moment is to output an error message explaining what the error was with a relevant message. This error message must start with the text "Error: ".
As mentioned, there are other ways of dealing with errors that you will meet in Semester 2.
Read the Steps carefully and do not skip instructions as we may come back to previous instructions in later steps.
Step 1: Allow a member to select multiple items before going to the checkout
a) Introduce basket as a field in the Member class which will be used to store those items selected by a member. It should be declared to be an ArrayList collection class object and it should have a declaration similar to that of animalCollection in the Zoo class. You will also need an import statement for the ArrayList. The field basket will eventually replace the item field but do not remove the item field at this point as otherwise your code will not compile.
b) Modify the Member class's selectItem() so that the item that is selected is added to the basket.
c) Add a toString() method to the Item class which returns an item's details in a format identical to the example below:
milk at 89p
where milk is the name of the item and 89 is the cost of that item.
See the Animal class for similar methods.
d) Using a while loop with a counter, write a new method showBasket() for the Member class. It should print out the details of each item in the shopping basket using the item's toString() method. These details should be in a format identical to the example shown below:
Your basket contains 2 items:
Fresh Whole Chicken at 304p
Milk at 89p
e) Using a while loop with a counter write a new method calculateCostOfBasket() which checks the basket for each item and its price and calculates the total cost of all the items in the basket before finally returning the cost as a whole number.
Test your code this point by making sure that a member can select items and show the basket and also check the total cost of the basket. But do not proceed to the checkout -- the Store class also needs modifying J.
Step 2: In the below you will amend the checkout() method so that all the items in a member's basket are processed
You should again re-compile after each of these parts.
a) Introduce a getBasket() method in the Member class -- it will eventually replace the getItem() method. The method header should be
public ArrayList getBasket()
which means that this method returns an ArrayList object which contains Item objects.
b) Modify the checkout() method in the Store class so that a call to the member's getBasket() method is made inside the method body. You will also need to create a local variable to store whatever you are getting from the getBasket() method call, name it basket. Do not remove the original call to the getItem() method at the moment or your code will not compile but eventually it will be removed.
c) The checkout() method will now need to process the items in the basket. For this you will need a loop and a running total to calculate the cost of the items stored in the basket as you loop through. Each time round the loop you will "retrieve" an item from the basket so use a for-each loop to do this.
The output from Andy's visit to the checkout should now look like this:
Salford Thrifty Store: Serving Andy
Your basket contains 2 items:
Fresh Whole Chicken at 304p
Milk at 89p
The total cost is 393p
You have tendered 400p
Your change is 7p
d) If you haven't already done so, make sure that the shopping basket of a member is emptied after successfully buying the items. Look in the JDK documentation for the ArrayList in order to find a suitable method which can be used to empty the ArrayList.
e) Finally remove the field item and the method getItem() from the Member class. The project should still compile and execute correctly -- if it does not then you need to re-examine and modify your code.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!