Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Have Dealershi p but need DealershipTest.java Make a new Java class called Dealership.java that keeps track of a vehicle inventory. The Dealership class has a

Have Dealership but need DealershipTest.java

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

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

public class Dealership {

private Inventory inventory;

public Dealership() {

this.inventory = new Inventory();

}

public void loadInventoryFromFile(String file) throws IOException {

BufferedReader reader = new BufferedReader(new FileReader(file));

String line;

while ((line = reader.readLine()) != null) {

String[] fields = line.split(",");

if (fields.length != 5) {

throw new IOException("Invalid data format in file.");

}

String make = fields[0];

String model = fields[1];

int year = Integer.parseInt(fields[2]);

double price = Double.parseDouble(fields[3]);

boolean convertible = Boolean.parseBoolean(fields[4]);

Vehicle vehicle = new Vehicle(make, model, year, price, convertible);

inventory.add(vehicle);

}

reader.close();

}

public Inventory getInventory() {

return inventory;

}

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

Step: 3

blur-text-image

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

MySQL/PHP Database Applications

Authors: Jay Greenspan, Brad Bulger

1st Edition

978-0764535376

More Books

Students also viewed these Databases questions

Question

1. What are the sources of foreign-exchange demand and supply

Answered: 1 week ago