Question

Java Programming --- complete the given classes DeLand Space Car Dealer needs a new program for...

Java Programming --- complete the given classes

DeLand Space Car Dealer needs a new program for their inventory management system. The program needs the following classes:

  • A main class (SpaceCarDealer.java) that includes the main method and GUI.

Two instantiable classes:

  • CarDealer.java

o Variables to store the dealer name, and the carsOnLot array in Car type. o A constructor to initialize the name variable with the given dealer name. o An accessor method to return the name of the dealer.

o An accessor method to return the cars on lot.

o A mutator method to buy a car and add to the car dealer inventory.

o A mutator method to sell the car with the given sale price and remove the car from dealer’s inventory.

o A mutator method that makes the necessary changes to update the sale price of the car.

  • Car.java

o Variables to store makeAndModel, purchasePrice, and salePrice. o A constructor to initialize the variables with the given parameters. o An accessor method to return the purchase price.

o A mutator method to update the sale price with the given new price.

SpaceCarDealer.java

import javax.swing.JOptionPane;
public class SpaceCarDealer {
public static void main(String[] args) {
String input;
int response = 0;
double newPrice;
Car selectedCar;
CarDealer delandCarDealer = new CarDealer("DeLand", 20);
String[] option = {
"Exit",
"Sell",
"Buy",
"UpdatePrice"
};
do {
response = JOptionPane.showOptionDialog(null,
delandCarDealer.getCarsOnLot(),
"DeLand Car Dealer",
0,
JOptionPane.INFORMATION_MESSAGE,
null,
option,
option[2]);
switch (response) {
case 0:
break;
case 1:
selectedCar = (Car) JOptionPane.showInputDialog(null,
"Select: ",
"Pick Car to Sell: ",
JOptionPane.QUESTION_MESSAGE,
null,
delandCarDealer.getCarsOnLot(),
delandCarDealer.getCarsOnLot()[0]
);
delandCarDealer.sellACar(selectedCar);
break;
case 2:
input = JOptionPane.showInputDialog(null, "Make and Model:
");

newPrice =
Double.parseDouble(JOptionPane.showInputDialog("Purchase Price: "));
//sale price with 20 percent profit: newPrice*1.2
delandCarDealer.buyACar(new Car(input, newPrice,
newPrice * 1.2));
break;
case 3:
selectedCar = (Car) JOptionPane.showInputDialog(null,
"Select: ",
"Update the sale price",
JOptionPane.QUESTION_MESSAGE,
null,
delandCarDealer.getCarsOnLot(),
delandCarDealer.getCarsOnLot()[0]
); newPrice =

Double.parseDouble(JOptionPane.showInputDialog("Purchase Price: ")); delandCarDealer.UpdateSalePrice(selectedCar, newPrice);
break;
}
}
while (response != 0);
}

}

CarDealer.java

import java.util.Arrays;
public class CarDealer {
private String dealerName;
private Car[] carsOnLot;
public CarDealer(String dealerName, int num) {
this.dealerName = dealerName;
this.carsOnLot = new Car[num];
}
@Override
public String toString() {
return "CarDealer [dealerName=" + dealerName + ", carsOnLot=" +
Arrays.toString(carsOnLot) + "]";
}
}

Car.java

public class Car {
private String makeAndModel;
private double purchasePrice;
private double salePrice;
public Car() {
this.makeAndModel = null;
this.purchasePrice = 0;
this.salePrice = 0;
}
@Override
public String toString() {
return String.format("%20s $%12.2f", makeAndModel, salePrice);
}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hello,

Below is the code required for above problem :

There are no changes in the SpaceCarDealer class. It's the same as before. I have made the changes that are required in the CarDealer and Car class. I have also added comments that you can reference for better understanding.

SpaceCarDealer.java :

import javax.swing.JOptionPane;
public class SpaceCarDealer {
    public static void main(String[] args) {
        String input;
        int response = 0;
        double newPrice;
        Car selectedCar;
        CarDealer delandCarDealer = new CarDealer("DeLand", 20);
        String[] option = {
                "Exit",
                "Sell",
                "Buy",
                "UpdatePrice"
        };
        do {
            response = JOptionPane.showOptionDialog(null,
                    delandCarDealer.getCarsOnLot(),
                    "DeLand Car Dealer",
                    0,
                    JOptionPane.INFORMATION_MESSAGE,
                    null,
                    option,
                    option[2]);
            switch (response) {
                case 0:
                    break;
                case 1:
                    selectedCar = (Car) JOptionPane.showInputDialog(null,
                            "Select: ",
                            "Pick Car to Sell: ",
                            JOptionPane.QUESTION_MESSAGE,
                            null,
                            delandCarDealer.getCarsOnLot(),
                            delandCarDealer.getCarsOnLot()[0]
                    );
                    delandCarDealer.sellACar(selectedCar);
                    JOptionPane.showMessageDialog(null,"Buying Price :  "+
                    selectedCar.getPurchasePrice()+"\nSelling Price : " + selectedCar.getPurchasePrice()*1.2+
                    "\nProfit : " +(selectedCar.getPurchasePrice()*1.2 - selectedCar.getPurchasePrice()));
                    break;
                case 2:
                    input = JOptionPane.showInputDialog(null, "Make and Model:");
                    newPrice =Double.parseDouble(JOptionPane.showInputDialog("Purchase Price: "));
                    //sale price with 20 percent profit: newPrice*1.2
                    delandCarDealer.buyACar(new Car(input, newPrice,newPrice * 1.2));
                    break;
                case 3:
                    selectedCar = (Car) JOptionPane.showInputDialog(null,
                            "Select: ",
                            "Update the sale price",
                            JOptionPane.QUESTION_MESSAGE,
                            null,
                            delandCarDealer.getCarsOnLot(),
                            delandCarDealer.getCarsOnLot()[0]
                    );
                    newPrice = Double.parseDouble(JOptionPane.showInputDialog("Purchase Price: ")); delandCarDealer.updateSalePrice(selectedCar, newPrice);
                    break;
            }
        }
        while (response != 0);
    }

}

CarDealer.java :

import java.util.Arrays;
public class CarDealer {
    //instance variables for the class CarDealer
    private String dealerName;
    private Car[] carsOnLot;

    //Parameterized constructor to create a CarDealer object
    public CarDealer(String dealerName, int num) {
        this.dealerName = dealerName;
        this.carsOnLot = new Car[num];
    }
    //Overriding toString method to provide our own implementation of the method
    @Override
    public String toString() {
        return "CarDealer [dealerName=" + dealerName + ", carsOnLot=" +
                Arrays.toString(carsOnLot) + "]";
    }

    //Accessor method to return the array carsOnLot
    public Car[] getCarsOnLot() {
        return carsOnLot;
    }

    //method to sellACar. What we are doing is we are running an array from 0 to the length of the carsOnLot array.
    //After that we get the currentCar from the array, and compare it to the car that we want to sell. If it matches,
    //we delete that car from the array by shifting each car present in the array to one position backwards.
    public void sellACar(Car selectedCar) {
        for(int i=0;i<carsOnLot.length;i++){
            Car car = carsOnLot[i];
            if(selectedCar==car){
                for(int j=i;j<carsOnLot.length - 1; j++){
                    carsOnLot[j] = carsOnLot[j+1];
                }
                break;
            }
        }
    }

    //method to buyACar, what we are doing is we are running the loop from 1 to the length of the array. After that we
    //are checking for the position which is null, that is where there is no car object present, and in that position
    //we are assiging our car object and adding our car object to the inventory.
    public void buyACar(Car car) {
        for(int i=0;i<carsOnLot.length;i++){
            Car currentCar = carsOnLot[i];
            if(null == currentCar){
                carsOnLot[i]=car;
                break;
            }
        }
    }

    //method to updateSalePrice of the car. We are running a loop on the carsOnLot array and getting the current car on the position i
    //If it matches the car provided whose sale price we want to update, we call the setSalePrice method of Car object on that car.
    public void updateSalePrice(Car selectedCar, double newPrice) {
        for(int i=0; i< carsOnLot.length;i++){
            Car car = carsOnLot[i];
            if(selectedCar==car){
                selectedCar.setSalePrice(newPrice);
                break;
            }
        }
    }
}

Car.java :

public class Car {
    private String makeAndModel;
    private double purchasePrice;
    private double salePrice;

    //default constructor.
    public Car() {
        this.makeAndModel = null;
        this.purchasePrice = 0;
        this.salePrice = 0;
    }

    //parameterized constructor to create a car object provided on user information
    public Car(String makeAndModel,double purchasePrice, double salePrice) {
        this.makeAndModel = makeAndModel;
        this.purchasePrice = purchasePrice;
        this.salePrice = salePrice;
    }
    @Override
    public String toString() {
        return String.format("%20s $%12.2f", makeAndModel, salePrice);
    }

    //method to set the sale price
    public void setSalePrice(double salePrice){
        this.salePrice = salePrice;
    }

    //method to get the purchase price
    public double getPurchasePrice(){
        return purchasePrice;
    }
}

Sample Output :

DeLand Car Dealer Exit S ell B uy UpdatePrice

Buying a car :

Input Make and Model: ABC OK Cancel

Input Purchase Price: 80 OK Cancel

Deland Car Dealer 96.00 ABC $ Sell Exit B uy UpdatePrice

Updating price :

Update the sale price Select: ABC $ 96.00 OK Cancel

Input Purchase Price: OK Cancel

DeLand Car Dealer ABC $ 60.00 Exit Sell B uy UpdatePrice

Selling A Car :

Pick Car to Sell: Select: ABC $ 60.00 OK Cancel

Sample Output of Profit :

DeLand Car Dealer ABC $ 108.00 Exit Sell B uy UpdatePrice

Pick Car to Sell: Select ABC $ 108.00 OK Cancel

Message Buying Price: 90.0 Selling Price: 108.0 Profit: 18.0 Tok|

DeLand Car Dealer Exit S ell B uy UpdatePrice

Input Make and Model: ABC OK Cancel

Input Purchase Price: 80 OK Cancel

Deland Car Dealer 96.00 ABC $ Sell Exit B uy UpdatePrice

Update the sale price Select: ABC $ 96.00 OK Cancel

Input Purchase Price: OK Cancel

DeLand Car Dealer ABC $ 60.00 Exit Sell B uy UpdatePrice

Pick Car to Sell: Select: ABC $ 60.00 OK Cancel

DeLand Car Dealer Exit S ell B uy UpdatePrice

Input Make and Model: ABC OK Cancel

Input Purchase Price: 80 OK Cancel

Deland Car Dealer 96.00 ABC $ Sell Exit B uy UpdatePrice

Update the sale price Select: ABC $ 96.00 OK Cancel

Input Purchase Price: OK Cancel

DeLand Car Dealer ABC $ 60.00 Exit Sell B uy UpdatePrice

Pick Car to Sell: Select: ABC $ 60.00 OK Cancel

Add a comment
Know the answer?
Add Answer to:
Java Programming --- complete the given classes DeLand Space Car Dealer needs a new program for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the...

    java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the below classes: public class Date {    private String month;    private String day;    private String year;    public Date(String month, String day, String year) {       this.month = month;       this.day = day;       this.year = year;    }    public String getMonth() {       return month;    }    public String getDay() {       return day;    }    public String...

  • Java - Car Dealership Hey, so i am having a little trouble with my code, so...

    Java - Car Dealership Hey, so i am having a little trouble with my code, so in my dealer class each of the setName for the salesman have errors and im not sure why. Any and all help is greatly appreciated. package app5; import java.util.Scanner; class Salesman { private int ID; private String name; private double commRate; private double totalComm; private int numberOfSales; } class Car { static int count = 0; public String year; public String model; public String...

  • Writing 3 Java Classes for Student registration /** * A class which maintains basic information about...

    Writing 3 Java Classes for Student registration /** * A class which maintains basic information about an academic course. */ public class Course {    /** * Attributes. */ private String code; private String title; private String dept; // name of department offering the course private int credits; /** * Constructor. */ public Course(String code, String title, int credits) { // TODO : initialize instance variables, use the static method defined in // Registrar to initialize the dept name variable...

  • Write a program in Java that prompts a user for Name and id number. and then...

    Write a program in Java that prompts a user for Name and id number. and then the program outputs students GPA MAIN import java.util.StringTokenizer; import javax.swing.JOptionPane; public class Main {    public static void main(String[] args) {                       String thedata = JOptionPane.showInputDialog(null, "Please type in Student Name. ", "Student OOP Program", JOptionPane.INFORMATION_MESSAGE);        String name = thedata;        Student pupil = new Student(name);                   //add code here       ...

  • Help! Not sure how to create this java program to run efficiently. Current code and assignment...

    Help! Not sure how to create this java program to run efficiently. Current code and assignment attached. Assignment :Write a class Movies.java that reads in a movie list file (movies.txt). The file must contain the following fields: name, genre, and time. Provide the user with the options to sort on each field. Allow the user to continue to sort the list until they enter the exit option. ---------------------------------------------------------- import java.util.*; import java.io.*; public class Movies { String movieTitle; String movieGenre;...

  • solve this Q in java languege Write a method that return DoublyLinkedList as reversed string For...

    solve this Q in java languege Write a method that return DoublyLinkedList as reversed string For example: If the elements of a list is 1, 2, 3, 4, 5, 6 the reverse string should be 6, 5, 4, 3, 2, 1 implement reverse method you have two steps: 1- you should start traversing from the last element of DoublyLinkedList (the previous of the trailer) 2- you should add the element inside each node to string don't forget the space in...

  • Needs Help In Java Programming language Exceptional Cars Purpose To review interfaces and exception usage. Directions...

    Needs Help In Java Programming language Exceptional Cars Purpose To review interfaces and exception usage. Directions Your task is to write a class called Car. Your class should have the following fields and methods: private int position private boolean headlightsOn public Car() - a constructor which initializes position to 0 and headlightsOn to false public Car(int position) - a constructor which initializes position to the passed in value and headlightsOn to false, and it should throw a NegativeNumberException with a...

  • I've previously completed a Java assignment where I wrote a program that reads a given text...

    I've previously completed a Java assignment where I wrote a program that reads a given text file and creates an index that stores the line numbers for where individual words occur. I've been given a new assignment where I need to modify some of my old code. I need to replace the indexer in my Index class with a NavigableMap<String, Word> and update my Word class with NavigableSet<Integer> lines. The instantiated objects should be TreeMap() and TreeSet(). I have below...

  • Use inheritance to create a new class AudioRecording based on Recording class that: it will retain...

    Use inheritance to create a new class AudioRecording based on Recording class that: it will retain all the members of the Recording class, it will have an additional field bitrate (non-integer numerical; it cannot be modified once set), its constructors (non-parametrized and parametrized) will set all the attributes / fields of this class (reuse code by utilizing superclass / parent class constructors): Non-parametrized constructor should set bitrate to zero, If arguments for the parametrized constructor are illegal or null, it...

  • Solve in C++ for Car.h and Car.cpp 8.21 LAB: Car value (classes) Given main, complete the...

    Solve in C++ for Car.h and Car.cpp 8.21 LAB: Car value (classes) Given main, complete the Car class (in files Car hand Car.cpp) with member functions to set and get the purchase price of a car (SetPurchase Price().GetPurchase Price)and to output the car's information (Printinfo). Ex If the input is 2011 18000 2018 where 2011 is the car's model year, 18000 is the purchase price, and 2018 is the current year, the output is Car's information Model year: 2011 Purchase...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT