Write a program in C++ . Using vehicle as a parent (base) class, cars and motorc
ID: 3664193 • Letter: W
Question
Write a program in C++ . Using vehicle as a parent (base) class, cars and motorcycles as offspring (derived) classes to maintain and display a list of vehicles , should calculate the sales price and monthly payments for a designated vehicle.
The class should have data elements for the following info: Make,Model,color,mileage,fuelconsumption and list price. The following will be derived classes from the vehicle: Automobile,motorcycle. Program will need to have functions to : add miles to the mileage from test drives or employee use and another separate function to subtract/decrease the mileage. Program will also need a List class. The List class will use a dynamic array to store Vehicle objects. Program will have one list for Automobiles and one for Motorcycles. The List class will use a dynamic array to store Vehicle objects. Program will have one list for Automobiles and one for Motorcycles. Program will NEVER have an object of class Vehicle. As each vehicle is entered an object of the appropriate type must be created and added to the correct array.Double the size each time you increase capacity. Program must perform the following activities: create a list, add items, search for a vehicle, calculate the price and payments for a vehicle, remove a vehicle, and display the inventory. Some of these should be member functions of the List class, and which will be functions in your program. The user should be able to search for any attribute (data member) of the Vehicle, Automobile, or Motorcycle classes. To add an item you should prompt the user to enter the required information.
The display should show: Menu of choices (display all vehicles in the inventory, adjust mileage for a vehicle, search for a vehicle, add a vehicle, calculate purchase price for a vehicle, and exit. The calculate price option should remove the vehicle if it was sold. The search should display the information for each vehicle matching the criteria.
Explanation / Answer
Vehicle.java
/**
*
* @author prmsh
*/
public class Vehicle {
//class variables declared
String make;
String model;
String color;
double mileage;
double fuelConsumption;
double listPrice;
//constructor
public Vehicle(String make, String model, String color, double mileage, double fuelConsumption, double listPrice) {
this.make = make;
this.model = model;
this.color = color;
this.mileage = mileage;
this.fuelConsumption = fuelConsumption;
this.listPrice = listPrice;
}
//getter setter methods
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getMileage() {
return mileage;
}
public void setMileage(double mileage) {
this.mileage = mileage;
}
public double getFuelConsumption() {
return fuelConsumption;
}
public void setFuelConsumption(double fuelConsumption) {
this.fuelConsumption = fuelConsumption;
}
public double getListPrice() {
return listPrice;
}
public void setListPrice(double listPrice) {
this.listPrice = listPrice;
}
//toString method
@Override
public String toString() {
return "Vehicle{" + "make=" + make + ", model=" + model + ", color=" + color + ", mileage=" + mileage + ", fuelConsumption=" + fuelConsumption + ", listPrice=" + listPrice + '}';
}
}
------------------------------------------------------------------------------------------------------------------------------
Automobile.java
public class Automobile extends Vehicle{
public Automobile(String make, String model, String color, double mileage, double fuelConsumption, double listPrice) {
super(make, model, color, mileage, fuelConsumption, listPrice);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------
Motorcycles.java
public class Motorcycles extends Vehicle{
public Motorcycles(String make, String model, String color, double mileage, double fuelConsumption, double listPrice) {
super(make, model, color, mileage, fuelConsumption, listPrice);
}
}
----------------------------------------------------------------------------------------------------------------------------------
inventory.java
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author prmsh
*/
public class Inventory {
public static void main(String args[]){
//arraylist declared for both automobiles and motorcycles
ArrayList<Automobile> automobiles = new ArrayList<Automobile>();
ArrayList<Motorcycles> motorcycles = new ArrayList<Motorcycles>();
Scanner sc = new Scanner(System.in);
double totalPrice = 0;
while(true){
//displaying menu
System.out.println("1. add item 2.search vehicle 3.calculate price 4.remove vehicle 5.display inventory 6.exit");
//reading choice from user
int choice = sc.nextInt();
if(choice == 1){
//adding vehicle
System.out.println("1.automobile 2.motorcycle");
int type = sc.nextInt();
System.out.println("Enter make: ");
String make = sc.nextLine();
System.out.println("Enter model: ");
String model = sc.nextLine();
System.out.println("Enter color: ");
String color = sc.nextLine();
System.out.println("Enter mileage: ");
double mileage = sc.nextDouble();
System.out.println("Enter fuelConsumption: ");
double consumption = sc.nextDouble();
System.out.println("Enter list price: ");
double price = sc.nextDouble();
//updating total price
totalPrice = totalPrice +price;
if(type == 1){
Automobile obj = new Automobile(make,model,color,mileage,consumption,price);
automobiles.add(obj);
}
else{
Motorcycles obj = new Motorcycles(make,model,color,mileage,consumption,price);
motorcycles.add(obj);
}
}
//search
else if(choice == 2){
//reading model name from user
System.out.println("Enter model: ");
String model = sc.nextLine();
//searching in automobiles
for(int i=0;i<automobiles.size();i++){
if(automobiles.get(i).model.equals(model)){
System.out.println(automobiles.get(i));
}
}
for(int i=0;i<motorcycles.size();i++){
if(motorcycles.get(i).model.equals(model)){
System.out.println(motorcycles.get(i));
}
}
}
else if(choice == 3){
System.out.println("Total Price: "+totalPrice);
}
//delete vehicle
else if(choice == 4){
//reading model name from user
System.out.println("Enter model to delete: ");
String model = sc.nextLine();
//searching in automobiles
for(int i=0;i<automobiles.size();i++){
if(automobiles.get(i).model.equals(model)){
automobiles.remove(i);
}
}
for(int i=0;i<motorcycles.size();i++){
if(motorcycles.get(i).model.equals(model)){
motorcycles.remove(i);
}
}
}
//disaply inventory
else if(choice == 4){
//searching in automobiles
for(int i=0;i<automobiles.size();i++){
System.out.println(automobiles.get(i));
}
for(int i=0;i<motorcycles.size();i++){
System.out.println(motorcycles.get(i));
}
}else{
//exiting
break;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.