Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Make a new Java class called Dealership.java that keeps track of a vehicle inventory. The Dealership class has a method with the following method signature

Make a new Java class called Dealership.java that keeps track of a vehicle inventory. The Dealership class has a method with the following method signature that loads the inventory from a file:

void loadInventoryFromFile(String file)

Using the loadInventoryFromFile method, download the vehicles.csv file that describes the current inventory. Each line in the csv file describes the make, model, year, price and whether it is a convertible or not (information such as mpg is not included).

Vehicles.csv

Chevrolet Corvette 1963 200000 FALSE
Chevrolet Corvette 1957 130000 TRUE
Bugatti Chiron 2022 3500000 FALSE
Genesis G90 2022 495666 FALSE
Shelby Cobra 427 1965 2000000 TRUE
Chevrolet Camaro 1969 150000 TRUE
Ford Mustang Bullit 1968 180000 FALSE
Ferrari Testarossa 1992 180000 FALSE
Porche 911 2022 200000 TRUE
Bentley Bacalar 2022 1900000 TRUE

You will need to parse this data and load it to the inventory of the dealership. After loading the inventory with the data, you will verify that the inventory has been loaded correctly by using the following methods in the Inventory class.

Vehicle findCheapestVehicle()

Vehicle findMostExpensiveVehicle()

double getAveragePriceOfAllVehicles()

The most expensive vehicle is the Bugatti Chiron, cheapest vehicle is the Chevy Corvette 1957 and the average price for all vehicles is $893,566.60.

Write JUnit tests to verify this result in the DealershipTest.java class.

Submit the Dealership.java and DealershipTest.java when done.

Dealership.java

// import io exceptions

public class Dealership { // declare inventory object and any necessary CONSTANTS

// Dealership constructor public Dealership(){ // new the inventory object }

public void loadInventoryFromFile( /* declare file param */ ) throws /* throw appropriate file IO error method */ { // try catch statement // declare variables to open stream for the csv file // split each field from the line, separated by commas

// loop until end of csv file

// split/substring the different fields in each line

// verify you have values for all 5 fields // otherwise, throw an exception

// Declare variables for all 5 fields and save the values

// use the inventory add method to add the new car (no trucks) } // catch exception for File IO read error // throw appropriate method } }

public Inventory getInventory(){ // insert code for getter } }

DealershipTest.java

/ import org.junit.* libraries

// import library to open and read files

public class DealershipTest { private Dealership dealership;

/* Use the annotation @Before for a method that loads the data from a csv file. Create a setUp() method that throws an exception in case there is a problem reading the file. This method must create a new dealership object and import the data from a csv file. Place the file on your c: drive so the path is as follows "c:\dealership.csv" */

/* Use the annotation @Test for a method that tests for the cheapest vehicle at the dealership defined as follows: - Create a testCheapest() method. - Initialize the dealership inventory list - call method to find cheapest vehicle - use assertEquals to check for the cheapest vehicle given in specs */

/* Use the annotation @Test for a method that tests for the most expensive vehicle at the dealership defined as follows: - Create a testMostExpensive() method. - Initialize the dealership inventory list - call method to find most expensive vehicle - use assertEquals to check for the most expensive vehicle given in specs */

/* Use the annotation @Test for a method that tests for the average price for all of the vehicles at the dealership defined as follows: - Create a testAveragePrice() method. - Initialize the dealership inventory list - call method to find the average vehicle price - use assertEquals to check for the average vehicle price given in specs */

}

Inventory.java:

import java.util.ArrayList;

import java.util.List;

public class Inventory {

private List vehicleList;

public Inventory() {

vehicleList = new ArrayList();

}

public void add(Vehicle v) {

vehicleList.add(v);

}

public void remove(Vehicle v) {

for (int i = 0; i < vehicleList.size(); i++) {

Vehicle current = vehicleList.get(i);

if (v.getMake().equalsIgnoreCase(current.getMake())

&& v.getModel().equalsIgnoreCase(current.getModel())

&& v.getYear() == current.getYear()) {

// found, removing and breaking loop

vehicleList.remove(i);

break;

}

}

}

public Vehicle findCheapestVehicle() {

Vehicle cheapest = null;// initializing a Vehicle instance to null

for (Vehicle v : vehicleList) {

if (cheapest == null || cheapest.getPrice() > v.getPrice()) {

cheapest = v;

}

}

return cheapest;

}

public Vehicle findMostExpensiveVehicle() {

Vehicle expensive = null;

for (Vehicle v : vehicleList) {

if (expensive == null || expensive.getPrice() < v.getPrice()) {

expensive = v;

}

}

return expensive;

}

public void printAveragePriceOfAllVehicles() {

double sum = 0;

for (Vehicle v : vehicleList) {

sum += v.getPrice();

}

double avg = 0;

if (vehicleList.size() > 0) {

avg = (double) sum / vehicleList.size();

}

System.out.printf("Average Price of Vehicles: %.2f ", avg);

}

public List getVehicleList() {

return vehicleList;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Real Time Database Systems Architecture And Techniques

Authors: Kam-Yiu Lam ,Tei-Wei Kuo

1st Edition

1475784023, 978-1475784022

More Books

Students also viewed these Databases questions

Question

(c) What is the resolution of this design? Pg45

Answered: 1 week ago