In: Computer Science
Using Java language:
Overall Situation:
You work for a company that sells cars and services cars. You have been instructed to write a program to help keep track of these operations for the store manager. Your company is moving to an object oriented programming system for all of its software, so you will have to create a car class and use it in your program. Besides creating the cars when the user chooses, the user can also change information about a car they choose. You will need to incorporate error handling to make sure that incorrect values are not stored if the user enters them for a change. There are also menu options for specific reports you should show on the screen when the user selects those options.
Car Class:
Your system works with cars. Each car has the following information it keeps: vin (a string), make (a string), model (a string), year (a number greater than 1970), mileage (a number not less than zero), price (a floating point number greater than 1000.)
Menu:
You need to have a menu system for this program. The user should be allowed to do several different things and each time returning to the main menu when they are done.
Main System Features/Processes:
Here are the main functional features your system has to have for the user:
(Hint: Have it in a separate method you are calling and print a message and ask them what they want to change and number them so you can do a separate If/Else or Switch. Then ask them for the value to change it to, and do it there. …don’t try to do it in the main menu.)
Screenshot
Program
Car.java
/**
* Class Car with given attributes
* With 2 constructors,getters ,setters and overrided methods
* @author deept
*
*/
public class Car {
//Attributes
private String vin;
private String make;
private String model;
private int year;
private int mileage;
private float price;
//Default constructor
public Car() {
vin="19UYA31581L000000";
make="Hundai";
model="I20";
year=2009;
mileage=18;
price=1100;
}
//Parameterized constructor
public Car(String vin,String make,String model,int
year,int mileage,float price) {
this.vin=vin;
this.make=make;
this.model=model;
this.year=year;
this.mileage=mileage;
this.price=price;
}
//Setters
public void setVin(String vin) {
this.vin = vin;
}
public void setMake(String make) {
this.make = make;
}
public void setModel(String model) {
this.model = model;
}
public void setYear(int year) {
this.year = year;
}
public void setMileage(int mileage) {
this.mileage = mileage;
}
public void setPrice(float price) {
this.price = price;
}
//Getters
public String getVin() {
return vin;
}
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public int getYear() {
return year;
}
public int getMileage() {
return mileage;
}
public float getPrice() {
return price;
}
@Override
public String toString() {
return "Vin: "+vin+", Make:
"+make+", Model: "+model+", Year: "+year+", Mileage: "+mileage+",
Price: $"+String.format("%.2f", price);
}
@Override
//Check 2 cars are equal or not
public boolean equals(Object obj) {
if ( obj instanceof Car )
{
Car car = (Car) obj ;
return
car.vin.equals(vin);
}
else
{
return false ;
}
}
}
CarShop.java
/**
* Implementation of class Car shop
*/
import java.util.ArrayList;
import java.util.Scanner;
public class CarShop {
public static void main(String[] args) {
//Array for Cars storage
ArrayList<Car>cars=new
ArrayList<Car>();
//Get menu
int opt=getMenu();
//If menu not exit
while(opt!=7) {
//Add a new
car
if(opt==1)
{
getCar(cars);
}
//Change a car
feature
else if(opt==2)
{
getChange(cars);
}
//Display a car
details
//Prompt for vin
match with cars in array then display
else if(opt==3)
{
Boolean check=false;
Scanner sc=new Scanner(System.in);
System.out.println("\nEnter car vin to change:
");
String vin=sc.nextLine();
for(int i=0;i<cars.size();i++) {
if(cars.get(i).getVin().equals(vin)) {
System.out.println(cars.get(i));
check=true;
break;
}
}
if(!check) {
System.out.println("Not
found!!!");
}
}
//Display all
cars in the shop
else if(opt==4)
{
System.out.println("\nCar details:");
for(int i=0;i<cars.size();i++) {
System.out.println(cars.get(i));
}
}
//Display
average of cars shop
else if(opt==5)
{
double mileage=0;
System.out.println("\nCar details:");
for(int i=0;i<cars.size();i++) {
mileage+=cars.get(i).getMileage();
}
System.out.println("Average Mileage =
"+String.format("%.2f",(mileage/cars.size())));
}
//Display all
cars in lowest price
else if(opt==6)
{
System.out.println();
float lowest=getLowest(cars);
for(int i=0;i<cars.size();i++) {
if(lowest==cars.get(i).getPrice()) {
System.out.println((cars.get(i)));
}
}
}
opt=getMenu();
}
//End
System.out.println("\nExiting
application....");
}
/*
* Display user menu
* Check error entry in option and return correct
option
*/
public static int getMenu() {
Scanner sc=new
Scanner(System.in);
System.out.println("User Options:
");
System.out.println("1. Add a new
car\n2. Change existing car feauture\n3. Display a car\n"
+ "4. Display data of all cars\n5. Display
average mileage of all cras\n6. Display cars using lowest price\n7.
Exit");
System.out.print("Enter option:
");
int opt=sc.nextInt();
while(opt<1 || opt>7) {
System.out.println("Error!!!Enter correct option 1-7");
System.out.print("Enter option: ");
opt=sc.nextInt();
}
return opt;
}
/*
* Method to add a new car into shop
* Display message
* Error check in user input
*/
public static void getCar(ArrayList<Car>cars)
{
Scanner sc=new
Scanner(System.in);
System.out.println("\nEnter Vin:
");
String vin=sc.nextLine();
while(vin.equals("") ||
vin.length()!=17){
System.out.println("Error!!Worng vin format.Please
re-enter");
System.out.println("Enter Vin: ");
vin=sc.nextLine();
}
System.out.println("Enter make:
");
String make=sc.nextLine();
while(make.equals("") ){
System.out.println("Error!!Worng make");
System.out.println("Enter make: ");
make=sc.nextLine();
}
System.out.println("Enter model:
");
String model=sc.nextLine();
while(model.equals("") ){
System.out.println("Error!!Worng model");
System.out.println("Enter model: ");
model=sc.nextLine();
}
System.out.println("Enter year:
");
int year=sc.nextInt();
while(year<=1970){
System.out.println("Error!!Year must be greater than 1970");
System.out.println("Enter year: ");
year=sc.nextInt();
}
System.out.println("Enter mileage:
");
int mileage=sc.nextInt();
while(mileage<=0){
System.out.println("Error!!Mileage must be positive");
System.out.println("Enter mileage: ");
mileage=sc.nextInt();
}
System.out.println("Enter price:
");
float price=sc.nextFloat();
while(price<=1000){
System.out.println("Error!!Price should be greater tahn
1000");
System.out.println("Enter price: ");
price=sc.nextFloat();
}
Car car=new
Car(vin,make,model,year,mileage,price);
//Check already present in
shop
for(int i=0;i<cars.size();i++)
{
if(cars.get(i).equals(car)) {
System.out.println("Car already present in the
shop!!!");
return;
}
}
//Otherwise add
cars.add(car);
System.out.println("New Car
successfully Added!!!");
}
/*
* Method to change a car feature
* Prompt for vin
* Check if it is present in array
* If then only ask for change property
*/
public static void getChange(ArrayList<Car>cars)
{
Scanner sc=new
Scanner(System.in);
System.out.println("\nEnter car vin
to change: ");
String vin=sc.nextLine();
for(int i=0;i<cars.size();i++)
{
if(cars.get(i).getVin().equals(vin)) {
while(true) {
System.out.println("\nWhich
feaature you want to change, Options: ");
System.out.println("1. change
make\n2. Change model\n3. Change year\n4. Change mileage\n5. Change
price\n6. Exit");
System.out.print("Enter
choice: ");
int opt=sc.nextInt();
while(opt<1 || opt>6)
{
System.out.println("Error!!!Option should be 1-6");
System.out.print("Enter choice: ");
opt=sc.nextInt();
}
sc.nextLine();
if(opt==1) {
System.out.print("Enter make: ");
String
make=sc.nextLine();
while(make.equals("") ){
System.out.println("Error!!Worng make");
System.out.println("Enter make: ");
make=sc.nextLine();
}
cars.get(i).setMake(make);
}
else if(opt==2) {
System.out.print("Enter model: ");
String
model=sc.nextLine();
while(model.equals("") ){
System.out.println("Error!!Worng model");
System.out.println("Enter make: ");
model=sc.nextLine();
}
cars.get(i).setMake(model);
}
else if(opt==3) {
System.out.println("Enter year: ");
int
year=sc.nextInt();
while(year<=1970){
System.out.println("Error!!Year must be greater
than 1970");
System.out.println("Enter year: ");
year=sc.nextInt();
}
cars.get(i).setYear(year);
}
else if(opt==4) {
System.out.println("Enter mileage: ");
int
mileage=sc.nextInt();
while(mileage<=0){
System.out.println("Error!!Mileage must be
positive");
System.out.println("Enter mileage: ");
mileage=sc.nextInt();
}
cars.get(i).setMileage(mileage);
}
else if(opt==5) {
System.out.println("Enter price: ");
float
price=sc.nextFloat();
while(price<=1000){
System.out.println("Error!!Price should be
greater tahn 1000");
System.out.println("Enter price: ");
price=sc.nextFloat();
}
cars.get(i).setPrice(price);
}
else if(opt==6) {
break;
}
}
return;
}
}
System.out.println("That car not
present inb Shop!!!");
}
/*
* Method to get lowest price
*/
public static float
getLowest(ArrayList<Car>cars) {
float lowest=0;
for(int i=0;i<cars.size();i++)
{
if(lowest<cars.get(i).getPrice()) {
lowest=cars.get(i).getPrice();
}
}
return lowest;
}
}
-------------------------------------------------
Output
User Options:
1. Add a new car
2. Change existing car feauture
3. Display a car
4. Display data of all cars
5. Display average mileage of all cras
6. Display cars using lowest price
7. Exit
Enter option: 1
Enter Vin:
19UYA31581L000000
Enter make:
Hundai
Enter model:
i10
Enter year:
2009
Enter mileage:
18
Enter price:
1100
New Car successfully Added!!!
User Options:
1. Add a new car
2. Change existing car feauture
3. Display a car
4. Display data of all cars
5. Display average mileage of all cras
6. Display cars using lowest price
7. Exit
Enter option: 4
Car details:
Vin: 19UYA31581L000000, Make: Hundai, Model: i10, Year: 2009,
Mileage: 18, Price: $1100.00
User Options:
1. Add a new car
2. Change existing car feauture
3. Display a car
4. Display data of all cars
5. Display average mileage of all cras
6. Display cars using lowest price
7. Exit
Enter option: 3
Enter car vin to change:
19UYA31581L000000
Vin: 19UYA31581L000000, Make: Hundai, Model: i10, Year: 2009,
Mileage: 18, Price: $1100.00
User Options:
1. Add a new car
2. Change existing car feauture
3. Display a car
4. Display data of all cars
5. Display average mileage of all cras
6. Display cars using lowest price
7. Exit
Enter option: 2
Enter car vin to change:
19UYA31581L000000
Which feaature you want to change, Options:
1. change make
2. Change model
3. Change year
4. Change mileage
5. Change price
6. Exit
Enter choice: 3
Enter year:
2008
Which feaature you want to change, Options:
1. change make
2. Change model
3. Change year
4. Change mileage
5. Change price
6. Exit
Enter choice: 6
User Options:
1. Add a new car
2. Change existing car feauture
3. Display a car
4. Display data of all cars
5. Display average mileage of all cras
6. Display cars using lowest price
7. Exit
Enter option: 4
Car details:
Vin: 19UYA31581L000000, Make: Hundai, Model: i10, Year: 2008,
Mileage: 18, Price: $1100.00
User Options:
1. Add a new car
2. Change existing car feauture
3. Display a car
4. Display data of all cars
5. Display average mileage of all cras
6. Display cars using lowest price
7. Exit
Enter option: 5
Car details:
Average Mileage = 18.00
User Options:
1. Add a new car
2. Change existing car feauture
3. Display a car
4. Display data of all cars
5. Display average mileage of all cras
6. Display cars using lowest price
7. Exit
Enter option: 6
Vin: 19UYA31581L000000, Make: Hundai, Model: i10, Year: 2008,
Mileage: 18, Price: $1100.00
User Options:
1. Add a new car
2. Change existing car feauture
3. Display a car
4. Display data of all cars
5. Display average mileage of all cras
6. Display cars using lowest price
7. Exit
Enter option: 1500
Error!!!Enter correct option 1-7
Enter option: 6
Vin: 19UYA31581L000000, Make: Hundai, Model: i10, Year: 2008,
Mileage: 18, Price: $1100.00
User Options:
1. Add a new car
2. Change existing car feauture
3. Display a car
4. Display data of all cars
5. Display average mileage of all cras
6. Display cars using lowest price
7. Exit
Enter option: 7
Exiting application....
---------------------------------------
Note:-
I assume you are expecting this way.