Question: Hi, I am currently finishing up a project for Java. I have the car.java and carlot.java classes finished, I just need the main class to

Hi, I am currently finishing up a project for Java. I have the car.java and carlot.java classes finished, I just need the main class to implement into the GUI that sorts and does the math. If it could be finished before 11:59 pm on the 30th, that would be amazing.

The project should:

Be a WidgetViewer program that allows GUI control of the Car Lot and individual Car information

Use event handlers to indicate a user clicking a button to add or sell cars

Allow a user to add a car with a given identifier, initial mileage and fuel consumption in miles per gallon (MPG), the cost of acquiring the new car, and the price the new car will be sold for. All new cars have a status of is not sold

Be implemented as multiple classes with multiple instances of some classes

Remove or sell a car from the inventory using its identifier, changing its status from is not sold to is sold and adding a field for profit where profit made is displayed.

Display the lots inventory both in the order that the cars were entered and in the order of best-MPG to worst-MPG; In descending order.

Display the car with the best MPG and the highest mileage.

Display the average miles per gallon for the entire fleet

Display profit (price - cost) from sold cars

The files are as follows:

Car.java

public class Car {

private int cost;

private String identifier;

private boolean isSold = false;

private int milage;

private double mpg;

private int price;

public Car() {

identifier = "";

milage = 0;

mpg = 0;

cost = 0;

price = 0;

}

public Car(String identifier) {

this.identifier = identifier;

milage = 0;

mpg = 0;

cost = 0;

price = 0;

}

public Car(String identifier, int milage, double mpg, int cost, int price) {

this.identifier = identifier;

this.milage = milage;

this.mpg = mpg;

this.cost = cost;

this.price = price;

}

public void addMiles(int miles) {

milage += miles;

}

public int compareId(Car o) {

return identifier.compareTo(o.identifier);

}

public int compareMPG(Car otherCar) {

double myMPG = getMPG();

double otherMPG = otherCar.getMPG();

if (myMPG == otherMPG) return 0;

if (myMPG - otherMPG > 0) return 1;

return -1;

}

public int comparePrice(Car o) {

return price - o.price;

}

public int getCost() {

return cost;

}

public String getIdentifier() {

return identifier;

}

public int getMiles() {

return milage;

}

public double getMPG() {

return mpg;

}

public int getPrice() {

return price;

}

public int getProfit() {

if (isSold) return price - cost;

return 0;

}

public boolean isSold() {

return isSold;

}

public void setCost(int cost) {

this.cost = cost;

}

public void setIdentifier(String identifier) {

this.identifier = identifier;

}

public void setMiles(int milage) {

this.milage = milage;

}

public void setMPG(double mpg) {

this.mpg = mpg;

}

public void setPrice(int price) {

this.price = price;

}

public void setSold(boolean isSold) {

this.isSold = isSold;

}

@Override

public String toString() {

String soldProfit = "is sold, profit=" + getProfit();

if (!isSold()) soldProfit = "is not sold";

return "Car id=" + identifier + ", milage=" + milage + ", mpg=" + mpg

+ ", cost=" + cost + ", price=" + price + ", " + soldProfit + " ";

}

}

CarLot.java

import java.util.ArrayList;

public class CarLot extends ArrayList {

public CarLot() {

}

public Car find(String identifier) {

for (Car c : this) {

if (c.getIdentifier().equals(identifier)) {

return c;

}

}

return null;

}

public double getCarAverageMPG() {

if (isEmpty()) return -1;

double totMPG = 0;

for (Car c: this) {

totMPG += c.getMPG();

}

return totMPG / size();

}

public Car getCarBestMPG() {

if (isEmpty())

return null;

Car car = get(0);

for (Car c : this) {

if (c.getMPG() > car.getMPG())

car = c;

}

return car;

}

public Car getCarHighestMilage() {

if (isEmpty())

return null;

Car car = get(0);

for (Car c : this) {

if (c.getMiles() > car.getMiles())

car = c;

}

return car;

}

public int getProfit() {

int profit = 0;

for (Car c: this) {

profit += c.getProfit();

}

return profit;

}

public ArrayList getSortedByMPG() {

ArrayList tmp = new ArrayList<>(this);

selectionSort(tmp);

return tmp;

}

public int getTotalMiles() {

int totalMiles = 0;

for (Car c : this) {

totalMiles += c.getMiles();

}

return totalMiles;

}

// from Liang Ch 07

private void selectionSort(ArrayList list) {

for (int i = 0; i < list.size() - 1; i++) {

// Find the minimum in the list[i..list.length-1]

Car currentMin = list.get(i);

int currentMinIndex = i;

for (int j = i + 1; j < list.size(); j++) {

if (currentMin.getMPG() < list.get(j).getMPG()) {

currentMin = list.get(j);

currentMinIndex = j;

}

}

// Swap list[i] with list[currentMinIndex] if necessary;

if (currentMinIndex != i) {

list.set(currentMinIndex, list.get(i));

list.set(i, currentMin);

}

}

}

@Override

public String toString() {

String s = "CarLot [ ";

for (Car c : this) {

s += c.toString();

}

s += "]";

return s;

}

}

CarLotGUI.java

import javax.swing.JLabel; import javax.swing.JTextField;

import java.util.ArrayList; import java.util.Scanner;

import javax.swing.JButton;

public class CarLotGUI {

public static void main(String[] args) { WidgetViewer wv = new WidgetViewer(); JLabel idmain = new JLabel("new car id"); JLabel mileage = new JLabel("new car mileage"); JLabel mpgmain = new JLabel("MPG"); JLabel cost = new JLabel("new car cost"); JLabel price = new JLabel("new car price"); JLabel soldID = new JLabel("sold car id"); JLabel bestMPG = new JLabel("best mpg: "); JLabel higest = new JLabel("highest mileage: "); JLabel fleet = new JLabel("fleet avg mpg: "); JLabel profit = new JLabel("profit: "); JLabel lotmain = new JLabel("Car lot as entered"); JLabel mpgLot = new JLabel("Car lot by MPG"); JButton newcar = new JButton("make a new car"); JButton sale = new JButton("Press to record sale"); JTextField a = new JTextField(); JTextField b = new JTextField(); JTextField c = new JTextField(); JTextField d = new JTextField(); JTextField e = new JTextField(); JTextField f = new JTextField(); wv.add(a, 850, 10, 60, 20); wv.add(b, 80, 10, 60, 20); wv.add(c, 285, 10, 60, 20); wv.add(d, 450, 10, 60, 20); wv.add(e, 95, 50, 60, 20); wv.add(f, 270, 50, 60, 20); wv.add(idmain, 10, 10, 200, 20); wv.add(mileage, 180, 10, 200, 20); wv.add(mpgmain, 400, 10, 200, 20); wv.add(cost, 10, 50, 200, 20); wv.add(price, 170, 50, 200, 20); wv.add(soldID, 780, 10, 200, 20); wv.add(bestMPG, 10, 100, 200, 20); wv.add(higest, 200, 100, 200, 20); wv.add(fleet, 500, 100, 200, 20); wv.add(profit, 780, 70, 150, 20); wv.add(lotmain, 10, 150, 150, 20); wv.add(mpgLot, 10, 400, 150, 20); wv.add(newcar, 10, 80, 160, 20); wv.add(sale, 770, 50, 150, 20); newcar.setActionCommand("new car"); newcar.addActionListener(new CarListener()); sale.setActionCommand("sale"); sale.addActionListener(new CarListener()); } }

Please, this is my third time asking for help on this. I have done most of the hard work, I just need help on the main class. It is similar to the other car lot programs on this website. I know it can be done on time. Thank you.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!