Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Finish the menu program ( in the photo ) connecting the methods in CarLot to the correct menu option. CartLot Program: import java.io . *

Finish the menu program(in the photo) connecting the methods in CarLot to the correct menu option.
CartLot Program:
import java.io.*;
import java.util.*;
public class CarLot {
private ArrayList inventory;
public CarLot(){
inventory = new ArrayList>();
}
public ArrayList getInventory(){
return inventory;
}
public void setInventory(ArrayList inventory){
this.inventory = inventory;
}
public Car findCarByIdentifier(String identifier){
for (Car car : inventory){
if (car.getId().equals(identifier)){
return car;
}
}
return null;
}
public ArrayList getCarsInOrderOfEntry(){
return new ArrayList>(inventory);
}
public ArrayList getCarsSortedByMPG(){
ArrayList sortedByMPG = new ArrayList>(inventory);
selectionSort(sortedByMPG);
return sortedByMPG;
}
private void selectionSort(ArrayList sortedByMPG){
for (int i =0; i sortedByMPG.size()-1; i++){
int minIndex = i;
for (int j = i +1; j sortedByMPG.size(); j++){
if (sortedByMPG.get(j).getMPG() sortedByMPG.get(minIndex).getMPG()){
minIndex = j;
}
}
Car temp = sortedByMPG.get(minIndex);
sortedByMPG.set(minIndex, sortedByMPG.get(i));
sortedByMPG.set(i, temp);
}
}
public Car getCarWithBestMPG(){
if (inventory.isEmpty()){
return null;
}
Car bestMPGCar = inventory.get(0);
for (Car car : inventory){
if (car.getMPG()> bestMPGCar.getMPG()){
bestMPGCar = car;
}
}
return bestMPGCar;
}
public Car getCarWithHighestMileage(){
if (inventory.isEmpty()){
return null;
}
Car highestMileageCar = inventory.get(0);
for (Car car : inventory){
if (car.getMileage()> highestMileageCar.getMileage()){
highestMileageCar = car;
}
}
return highestMileageCar;
}
public double getAverageMpg(){
if (inventory.isEmpty()){
return 0;
}
double totalMpg =0;
for (Car car : inventory){
totalMpg += car.getMPG();
}
return totalMpg / inventory.size();
}
public double getTotalProfit(){
double totalProfit =0;
for (Car car : inventory){
totalProfit += car.getSalesPrice()- car.getCost();
}
return totalProfit;
}
public void addCar(String id, int mileage, int mpg, double cost, double salesPrice){
inventory.add(new Car(id, mileage, mpg, cost, salesPrice));
}
public void sellCar(String identifier, double priceSold){
Car car = findCarByIdentifier(identifier);
if (car != null){
car.setSalesPrice(priceSold);
} else {
System.out.println("Car with identifier "+ identifier +" not found.");
}
}
public void saveToDisk() throws IOException {
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("carlot.dat"))){
out.writeObject(inventory);
}
}
@SuppressWarnings("unchecked")
public ArrayList loadFromDisk() throws IOException, ClassNotFoundException {
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("carlot.dat"))){
inventory =(ArrayList) in.readObject();
}
return inventory;
}
}
image text in transcribed

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

SQL Server T-SQL Recipes

Authors: David Dye, Jason Brimhall

4th Edition

1484200616, 9781484200612

More Books

Students also viewed these Databases questions

Question

Define Management or What is Management?

Answered: 1 week ago

Question

What do you understand by MBO?

Answered: 1 week ago

Question

What is meant by planning or define planning?

Answered: 1 week ago