(Java) Tracking Car Dealerships sales performance. A large network of car dealer
ID: 3768129 • Letter: #
Question
(Java) Tracking Car Dealerships sales performance.
A large network of car dealerships has asked that you develop a solution to keep track of the sales for multiple dealerships for a single time period.
Each dealership is described by the following pieces of data:
• Dealership Name
• Dealership ID (unique)
• total number of sold cars for single dealership
• most common model of car sold
• average cost of sold cars
• The list of all cars sold in the time period
Each dealership when created should be created with a custom constructor that creates a dealership with a specified name. The values for each dealership should be initialized in the constructor.
The dealership should have five methods:
1. sellACar - this will create a random car and add it to the ArrayList carsSold
2. getTotalSales – this will return the total value of the sold cars.
3. getMostCommonModel – this determines the most common model and returns the number of the model.
4. toStringSoldCars – this returns a string representation of all the cars
5. toString – this returns a string representation of the object.
Each car can be described by the following pieces of data:
• model type (integer)
• cost – the cost should be within the range 5,000.00 to 30,000.00.
The car class should have the following methods:
1. Getter methods for each variable
2. Setter methods for each variable
3. toString method – This will return the string representation of the object.
The file Car.java contains an incomplete defintion for the car class. Save it to your directory and complete the class definition as follows:
• Declare the instance data (model type and cost)
• Add the missing method headers (aka signatures)
• Add the missing method bodies
The file Dealership.java contains an incomplete definition for the dealership class. Save it to your directory and complete the class definition as follows:
• Declare the instance data (dealership name, dealership id, and average cost of cars sold)
• Add the missing method headers (aka signatures)
• Add the missing method bodies
Car.java
import java.text.NumberFormat;
import java.util.Random;
public class Car {
static Random gen = new Random(System.currentTimeMillis());
//private variables
//Constructor with a given type as a parameter
//getters
//setters
//Override the toString method
}//end class
Dealership.java
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Random;
public class Dealership {
final int numModels = 5;
int [] modelsCount = new int[numModels];
ArrayList carsSold = new ArrayList();
Random gen = new Random(System.currentTimeMillis());
//private variables
//Constructor with a name as a parameter
/* Sell Car - This method will create a random car object and * add it to the ArrayList using the add method. */
public void sellACar(){ int type = gen.nextInt(numModels); //0 to 5
carsSold.add(new Car(type));
modelsCount[type]++; }
//Get the total cost of all the cars by add the cost of each car
//Add code here
public double getTotalSales(){
double total = 0.0; return total; }
//Get the average cost of each car by getting the totalSales and dividing by the size() of the arrayList carsSold
//Add code here
//Get the most common model
//Add code here
//Print all the sold cars
public String toStringSoldCars(){
StringBuilder result = new StringBuilder(); for(int i = 0; i < this.carsSold.size(); i++){
result.append(this.carsSold.get(i)); }
return result.toString(); }
//override toString method
//Add code here
}
(Don't worry about the Driver class, I already have the Driver class to run the program)
Explanation / Answer
Here is the code for Car.java:
import java.text.NumberFormat;
import java.util.Random;
public class Car
{
static Random gen = new Random(System.currentTimeMillis());
//private variables
int modelType;
double cost;
//Constructor with a given type as a parameter
Car(int m)
{
this.setModelType(m);
}
//getters
int getModelType()
{
return modelType;
}
double getCost()
{
return cost;
}
//setters
void setModelType(int m)
{
modelType = m;
}
void setCost(double c)
{
if(c < 5000 || c > 30000)
throw new IllegalArgumentException("Cost out of range. Should be within the range 5000 - 30000");
else
cost = c;
}
//Override the toString method
public String toString()
{
return String.format("ModelType: "+modelType+" Cost: "+cost);
}
}//end class
And this is the code for Dealership.java:
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Random;
public class Dealership
{
final int numModels = 5;
int [] modelsCount = new int[numModels];
ArrayList carsSold = new ArrayList();
Random gen = new Random(System.currentTimeMillis());
//private variables
String dealershipName;
int dealershipID;
double averageCostOfCarsSold;
//Constructor with a name as a parameter
Dealership(String name)
{
dealershipName = name;
}
/* Sell Car - This method will create a random car object and * add it to the ArrayList using the add method. */
public void sellACar()
{
int type = gen.nextInt(numModels);//0 to 5
carsSold.add(new Car(type));
System.out.println("Check: "+carsSold.get(0));
modelsCount[type]++;
}
//Get the total cost of all the cars by add the cost of each car
public double getTotalSales()
{
Car c;
double total = 0.0;
for(int i = 0; i < carsSold.size(); i++)
{
c = (Car)carsSold.get(i);
total += c.getCost();
}
return total;
}
//Get the average cost of each car by getting the totalSales
//and dividing by the size() of the arrayList carsSold
public double getAverageCost()
{
return getTotalSales() / carsSold.size();
}
//Get the most common model
public int getCommonModel()
{
int common = 0;
for(int i = 0; i < numModels; i++)
if(modelsCount[i] > modelsCount[common])
common = i;
return common;
}
//Print all the sold cars
public String toStringSoldCars()
{
StringBuilder result = new StringBuilder();
for(int i = 0; i < this.carsSold.size(); i++)
{
result.append(this.carsSold.get(i));
}
return result.toString();
}
//override toString method
public String toString()
{
return String.format("Dealership Name: "+dealershipName+" Dealership ID: "+dealershipID);
}
//Add code here
}
If you need any further refinements, just get back to me.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.