Question: Step 1 : Allow a member to select multiple items before going to the checkout a ) Introduce basket as a field in the Member
Step : 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.
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 p
where milk is the name of the item and 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.
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.
In the below you will amend the checkout method so that all the items in a member's
basket are processed
You should again recompile after each of these parts.
Step:
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.
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.
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 foreach loop to do this.
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.
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 : Allow a member to "put back" items
a Write a method putItemBackfor the Member class which removes an item from the
shopping basket. It is passed an int parameter indicating the position in the list of the item
you wish to remove.
b Write a second method putItemBackfor the Member class that is passed a String
parameter indicating the name of the item to be removed. Note that this method has the
same name as the method in Part a but has a different signature so this is allowed
c Write a method putExpensiveItemsBackfor the Member class which removes
items note the plural with a price greater than or equal to a specified amount. This latter
value is passed to the method as a parameter.
d Next add a field to the Store class called maxMumberOfItems that stores the maximum
number of items that a basket should hold. Then, write a method basketOverFull
also for the Store class, that is passed a basket object as the method argument This method should be called from
checkout and if the basket does contain more than the allowed limit
maxMumberOfItems then it returns true, otherwise it returns false. Depending on
this value, the member is allowed or not to pay for their items
Step with code enhancements:
a Write a method showMembers in the Store class that outputs details of all the members
registered to shop at a store
In order for this method to work correctly, you will need to add a new feature to the Store
class ie and ArrayList of members and a new method in MembertoString
One obvious enhancement is to change the output so that it produces the outputs using
pound signs eg and p To do this, you should first return to the
getPriceString method of the Item class and make sure that this method returns
the price of an item in the new format. Once you have done this, it is helpful to perform a
little reorganisation. Move the code that actually does the conversion into a second method
convertPenceToString that returns a String and call this new method from
getPriceString
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
