Question: Task 1. UML Class diagram Before you begin coding, you must draw a UML class diagram showing the structure of the AutoShowroom system as it
Task 1. UML Class diagram
Before you begin coding, you must draw a UML class diagram showing the structure of the AutoShowroom system as it will stand at the end of this lab, i.e. after you have implemented all tasks. Your diagram must show all classes and the relationships between them: associations and dependencies. You dont have to show attributes and methods.
Put your class diagram in your git repository.
Task 2. Refactor AutoShowroom to use ArrayList
Native Java arrays have some significant limitations - not least, they are statically allocated. That means that their maximum size is fixed at the time of their creation you cant easily resize an array. We would like to change AutoShowroom to make it easier to add new cars. Fortunately, the Java class libraries provide a wide variety of collection classes in the Java Collections Framework. We will use one called the ArrayList class.
Modify the AutoShowroom class to use an ArrayList rather than an array to store the collection of Cars.
Task 3. Refactor Cars method names using your IDEs refactoring tools
In Week 2, you created a Car class with a method called getCarDescription() that returns a string consisting of the car maker and the car model. This is not really a good name. By convention in Java, methods starting with get return the value of an attribute, and thats not what this method does. Also, since it is a part of the Car class, we really dont need the Car part of the method name. We can just call it description().
Making this sort of change using a simple text editor can be a lot of work. We need to change the method name not only in the Car class but also in every client of Car that uses that method. We don't want to change any method that has the same name but belongs to a different class. Fortunately, Most modern IDEs provide refactoring tools to help.
Go to the Class class and find the method getCarDescription(). Select the text getCarDescription() and then right-click on it to bring up a menu. Choose Refactor Rename. Edit the method name to be just description(). The IDE will analyse the code for the system and change the method name everywhere it occurs. Now open the AutoShowroom class and check that the method name has changed where it is used in the method displayCars().
Once you have made the changes, informally test your program to ensure that the functionality is unchanged. Refactoring should not change what your program does, only how it does it. In a larger project, you would use automated unit tests and possibly system tests to ensure that your refactoring has not affected any functionality.
Task 4. Create a Buyer and a Bid class
We need to add a Buyer class to our system. You will need to use some of the new Java language features we have seen in lectures to implement Buyer, including private attributes and constructors.
The Buyer class needs to have private attributes to store:
- the buyer's ID (buyerId)
- the buyer's given name ( givenName)
- family name ( familyName)
Note that we have not specified the type of buyertId. You can choose to use a numeric integer type, or a Java string. This kind of decision is actually a design decision. Create a text file called Justifications.txt and add it to the top-level directory of your project. In it, briefly list the advantages and disadvantages of using both, and justify your final decision. I dont know how to use Java integers very well is not in itself sufficient justification.
Additionally, add a comment explaining why you should use givenName and familyName rather than firstName and lastName.
Implement two constructors for Buyer, with these signatures:
- Buyer(*type* newBuyerId)
- Buyer(*type* newBuyerId, String newGivenName, String newFamilyName)
Replace *type* with the name of the type you have used as the attribute.
These features provide two different ways of creating and initialising a Buyer object. Next, implement two mutators (methods that change attribute values):
- setGivenName(...)
- setFamilyName(...)
These should both take String arguments.
Finally, implement an accessor method called description() that returns a String containing the Buyer ID, given name, and family name concatenated together, in that order and separated by spaces.
Similar to the Buyer class, add a new class that represents a bid on a car.
The Bid class needs to have private attributes to store:
- the bid's ID
- an instance of the Buyer class
- the price of the bid
- the date of the bid (you can use Date class from Util package or a simple string)
Add setters and getters for all the four attributes mentioned above.
Add a constructor that accepts as input values for the four attributes.
Task 5. Adding Bids to Cars
First, add an ArrayList attribute called bids to Car to store a collection of Bids objects. Again, justify your choice in Justifications.txt.
Add a method addBid(Buyer newBuyer, price, date) to Car to add a new bid in a car.
In the AutoShowroom class, modify createCars() so that it also creates some Buyers and Bids objects and add them to some cars. Make sure you test both Bid and Buyer constructors and the mutators. Modify displayCars() so that, as well as displaying the description for each car, it also displays a list of the bids and their buyers.
You will need to decide how to access the bids and buyers in Car from the AutoShowroom class. There should be no I/O in any class other than AutoShowroom, and the mechanism should not allow the bids array stored in Car to be modified by any other class.
Task 6. Add console I/O
Many programs that you will write, including the assignments in this subject, will have a user interface of some kind, to allow humans to interact directly with them. These days, most such programs use some kind of graphical user interface, which is often web-based.
However, in certain circumstances, a console-based user interface is used. Youll see a console-based user interface if you open the command prompt (or PowerShell) in Windows) or a Terminal on a Mac or a Linux machine.
A console user interface is a bit like having a two-way conversation with an old typewriter (which in fact how they originally worked). Youd type something at the keyboard, hit enter, and then the computer would respond with some text output of its own.
Console-based user interfaces are usually less attractive and harder to learn, but they:
- can be as fast or faster to use than a GUI for experienced users,
- can be used over a slow or heavily loaded network connection,
- can be easily scripted with another computer program taking the place of the human typing the input, and importantly,
- are much easier to write than a GUI or web interface!
There are several ways to implement a console user interface in Java. Unfortunately, the simplest method is the Console class2, is incompatible with your IDEs console where the output displays when you run from within the IDE.
Modify AutoShowroom so that when Bid and Buyer objects are created, the user is prompted to enter the buyer's first name, last name, buyer ID, bid price, and date.
The following recording demonstrates how to build a console menu.
Console Menu: How does it work?
Task 7. Design a buyer list display method
Once youve finished implementing the current functionality, its time to think about how the system can be further extended. This will help you think about how the design of the system affects its maintainability.
Write down a description of a method to print a list of all buyers, without any duplicates. Assume that refactoring will be expensive as much as possible, avoid modifying existing code, changing existing system structure, or changing attributes in the existing system. You will need to decide for yourself which class should hold this method.
You may use any combination of pseudocode, plain English text, and UML diagrams to describe your method. You do not have to implement your method, but you do need enough detail that you could give it to a classmate and they could implement it without having to consider anything but the details of Java.
This design must be written in your own words and pushed to your git repository. (You may wish to get a head start on next weeks work by using Markdown.) No marks will be granted for ideas youve copied from the internet or other sources. We want to see how clearly you can express and justify your design ideas, and how well you have understood the design principles covered in lectures and notes.
Do not implement your design yet! Doing so might make it hard to complete future Bootcamp worksheets.
/////Copy the code below before doing the task/////
public class AutoShowroomDriver { public static void main(String[] args){ AutoShowroom showRoom = new AutoShowroom(); showRoom.printStatus(); } }
public class AutoShowroom { Car[] ar = new Car[3]; public void printStatus(){ System.out.println("Welcome to FIT2099 Showroom"); createCars(); displayCars(); System.out.println("Thank you for visiting FIT2099 Showroom"); } //change to ArrayList to make it easier to add new car. public void createCars(){ ar[0] = new Car("BMW","X7"); ar[1] = new Car("Audi","A8"); ar[2] = new Car("Mercedes","GLS"); } public void displayCars(){ int count = 1; for(Car ob:ar) { System.out.println("Car (" + count + ") " + ob.getCarDescription()); count++; } } }
public class Car { private String maker; private String model; public Car(String maker, String model){ this.maker = maker; this.model = model; } public String getMaker(){ return maker; } public String getModel(){ return model; } public String getCarDescription(){ return "Maker:" + maker + " and Model:" + model; } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
