Question

In: Computer Science

Using Java language: Overall Situation: You work for a company that sells cars and services cars....

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:

  • Ability to add a car to the array and set all of the values for it with input from the user
  • Ability to ask the user which car in the array they wish to change in some way, and then make that change.

(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.)

  • Display a message with all the data for a car the user chooses.
  • Display the data for all the cars (currently in the array) for the user.
  • Display the average mileage for all of the cars on the lot.
  • Display the lowest price for all of the cars on the lot.

Solutions

Expert Solution

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.


Related Solutions

The Kaufman car company sells cars with a warranty that they will work properly. based on...
The Kaufman car company sells cars with a warranty that they will work properly. based on its historic experience, it expects the cost of honoring this warranty to be about 1% of sales. In 2014, it makes $500 million of sales. Assume all the warranties for these cars expire at the end of 2016. The actual costs to fix the cars was $1,700,000 in 2014, $2,000,000 in 2015, and $1,200,000 in 2016. What expense would the company record: A. In...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
Programing Language: Java The Problem: You are writing a program that encrypts or decrypts messages using...
Programing Language: Java The Problem: You are writing a program that encrypts or decrypts messages using a simple substitution cipher. Your program will use two constant strings. One will represent the code for encryption: going from the original message (called the plaintext) to the encrypted version of the message. The other will be “abcdefghijklmnopqrstuvwxyz” (the lowercase alphabet. Your program will ask the user whether they want to 1) encrypt a message, 2) decrypt a message, or 3) quit. If they...
Using Java language (in program NetBeans). 1) Using a 2 dimensional array Your company has 4...
Using Java language (in program NetBeans). 1) Using a 2 dimensional array Your company has 4 grocery stores. Each store has 3 departments where product presentation affects sales (produce, meat, frozen). Every so often a department in a store gets a bonus for doing a really good job. You need to create a program that keeps a table of bonuses in the system for departments. Create a program that has a two dimensional array for these bonuses. The stores can...
Use JAVA language. Using the switch method concept create a program in which you have an...
Use JAVA language. Using the switch method concept create a program in which you have an artist (singer) and the list of his or her songs. Add 18 songs. Then alter the script to achieve the following in each test run: a) Print all the songs. b) Print only song 15. c) Print only song 19.
You work for a small insurance company. Your company is currently insuring 900 different cars for...
You work for a small insurance company. Your company is currently insuring 900 different cars for $10,000. If a car you are insuring gets in a crash you have to pay $10,000. Each car you insure has a 5% chance of crashing each year. If 50 cars you insure crash in a given year, you have to pay 50×$10, 000 = $500, 000 in insurance payouts that year. (a) What is the expected value of the amount your company would...
Relate a situation where you know of an unethical situation going on at work or at...
Relate a situation where you know of an unethical situation going on at work or at home that you are concerned about. It might be something that you cannot become directly involved in, but that you know about. Tell as much as you can, but again, it is confidential, and no one except the Professor will see it.
create scientific calculator using java language with OOP rule and interfaces.
create scientific calculator using java language with OOP rule and interfaces.
create calculator standard using java language with OOP rule and interfaces.
create calculator standard using java language with OOP rule and interfaces.
What is the primary goal in using a scripting language for programming work?
What is the primary goal in using a scripting language for programming work?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT