Individual Assessed Coursework
November 2022
Scenario: Wine Direct is an attempt by a cooperative to sell their wine reserves directly to the public. Wine is only sold by the case. People who are interested in buying the wine have to submit their details online and, once accepted, are then given a unique ID which they can later use to log in and then to buy wine. For the purposes of this assessment, you may assume that only one person can log onto the web site at any one time.
For this homework, you must use the names of fields (and all other variables and methods) as they are exactly specified in this document.
Noting also for method names that if you have a variable called numberOfTracks, then a mutator for this field is called setNumberOfTracks and the corresponding accessor is called getNumberOfTracks.
Overall specification: to write a simple Java class, WineDirect, that models a wine selling cooperative.
Step 1: A simple Browser class
Write a basic Browser class with three private fields, yearOfBirth, email (a String) and iD (which you can assume consists of a unique sequence of digits).
There are going to be two kinds of browsers: those who already have an ID (and so can purchase the wine straightaway) and those who do not. Hence, you will need to write two constructors: one for each type of browser. One constructor will be passed three arguments (you may choose the order of these arguments) and the other will be passed two arguments (again, you may choose the order).
Now introduce a new field isBuyer and a method setBuyerStatus(), which is passed a boolean value as an argument. This method will be used whenever the potential buyer logs into the Wine Direct website.
The class should include suitable accessor & mutator methods.
Step 2: A simple Website class
Write a method for the class, called buyerLogin(), which allows a buyer to log in to the site. This method is passed a Browser object as a parameter and which for the moment simply outputs a welcome message to the terminal window in the format (assuming your chosen site name is Wine Direct) shown here:
Wine Direct welcomes buyer 6732, you are now logged in.
Anyhow, if the potential buyer is indeed old enough then this method sets the buyer’s iD field (but see the paragraph below) and then this method calls the method buyerLogin().
To set the browser’s iD field, you have a choice: either set it to 999 or, for more marks, set it to a random value. To do the latter, you need to use an object from the Random class as follows. Firstly, write the code import java.util.Random before the class header of Website. Then use the BlueJ Help menu to find out about the Random class and which method from that class it is most appropriate to use here.
Step 3: Add a basic WineCase class and allow a buyer to select a case of wine to buy.
Buyer with ID 6732 has selected wine case with reference
number LO786, a case of 2018 Pinot from Italy of 12 bottles
at £120
Step 4: Allow a buyer to pay for the case of wine.
And here we have a problem: at the moment the Website object does not record which browser has entered the store and, conversely, the Browser object also keeps no details of which site it has entered. Let’s deal with that now.
Think about what this value could be:
So, setWebsite() needs to be passed, as an argument, a pointer to the Website object that you are currently browsing. This will enable the Browser object to record which website it is logged into.
In fact, the Website object needs to be able to tell the Browser object “point to me”. That is, the Website object needs a pointer to itself.
And a pointer that points to the object itself is called a self-referencing pointer and has value this.
Amend your code to do this.
Next, complete payForWine() by adding a call to the method checkout() of the Website class. What parameters do you think checkout() will need? Try working this out for yourself and then to check, read c) below which details the parameters needed.