Question: Step 3 : Allow a member to put back items A member might put back an item because they changed their mind about that item
Step : Allow a member to "put back" items
A member might put back an item because they changed their mind about that item or because they have insufficient money to pay when they go to the checkout.
We could write code to deal with this in the Store class or we could write code to deal with this in the Member class, but it does seem more straightforward to check for this before you get to the actual checkout ie in the Member class, so that is the approach we shall adopt.
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. Hint: Worksheet includes the following in Worksheet Exercise : Section and Section which explains the pitfalls of removing elements whilst in a loop: read this carefully and then complete Exercise
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 Similar to the basket field you added in Step a 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 a format identical to the example shown below.
The members of this store are
Andy with id SGT
Imran with id GGA
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 private method convertPenceToString that returns a String and call this new method from getPriceString
You can copy and reuse the method convertPenceToStringin other classes where it is needed.Having "duplicate code" is not an ideal situation and later we will discuss how to avoid it but for now do not worry
Hint: After this when the checkout method in the Store class is called a message to the terminal window can now be of the form:
Salford Thrifty Store: Serving Andy
Your basket contains items:
Fresh Whole Chicken at p
Milk at p
The total cost is p
You have tendered p
Your change is p
Hand in details
You will need to upload three separate java files Storejava, Member.java and Item.java to separate upload areas on BB
The deadline to do this is Wednesday th December.
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
