Question

Use your Car class from the previous Programming Assignment. For this assignment, create a new class...

Use your Car class from the previous Programming Assignment. For this assignment, create a new class that represents a Used Car Dealership. Your Java program will simulate an inventory system for the dealership, where the employees can manage the car information. Start by creating an array of 10 cars, with an "ID" of 0 through 9, and use the default constructor to create each car. For example, the third car in an array called cars would have ID 2. You would access it by using:

cars[2]

(Note: This is not a complete Java statement)

To display this car's information, you would use:

cars[2].toString();

Hint: Use a loop to help you initialize each car.

The system should first prompt the user for an ID, and ensure that it is valid (0 through 9). Once the id is accepted, the menu should be displayed:

  1. Set Make - Get the new Make from the user, and set it to this car's Make field
  2. Set Model - Get the new Model from the user, and set it to this car's Model field
  3. Set Year - Get the new Year from the user, and set it to this car's Year field
  4. Set Cost - Get the new Cost from the user, and set it to this car's Cost field
  5. Set Color - Get the new Color from the user, and set it to this car's Color field
  6. Display Car Information
  7. Exit, and choose new car ID

Any other menu option should give the user a message letting them know it was invalid. The program should run continuously. The "Exit" option will prompt the user to select a new ID and begin entering/displaying information for a different car.

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

// Car.java

public class Car {

      

       private String make;

       private String model;

       private int year;

       private double cost;

       private String color;

      

       public Car()

       {

             make="";

             model="";

             year = 0;

             color = "";

             cost = 0;

       }

      

       public Car(String make, String model, int year, double cost, String color)

       {

             this.make = make;

             this.model = model;

             this.year = year;

             this.cost = cost;

             this.color = color;

       }

      

       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 setCost(double cost)

       {

             this.cost = cost;

       }

      

       public void setColor(String color)

       {

             this.color = color;

       }

      

       public String getMake()

       {

             return make;

       }

      

       public String getModel()

       {

             return model;

       }

      

       public double getCost()

       {

             return cost;

       }

      

       public int getYear()

       {

             return year;

       }

      

       public String getColor()

       {

             return color;

       }

      

       public String toString()

       {

             return("Make : "+make+" Model : "+model+" Year : "+year+" Color : "+color);

       }

}

//end of Car.java

// CarDealership.java: Java program to implement a Used Car Dealership

import java.util.Scanner;

public class CarDealership {

      

       public static void main(String[] args) {

             Car cars[] = new Car[10]; // create an array of 10 cars

            

             int id, choice;

             String make,model,color;

             int year;

             double cost;

            

             Scanner scan = new Scanner(System.in);

             // initialize the 10 cars

             for(int i=0;i<10;i++)

                    cars[i] = new Car();

             // loop that continues till the user exits

             while(true)

             {

                    // input of id of the car

                    System.out.print("Enter id of the car(-1 to exit) : ");

                    id = scan.nextInt();

                    scan.nextLine();

                    // check if user wants to exit

                    if(id == -1)

                           break;

                    // check if id is invalid

                    else if(id < 0 || id > 9)

                    {

                           System.out.println("Invalid id. Id of the car should be between 0-9");

                    }else

                    {

                           // loop that continues till the user exits

                           do

                           {

                                 System.out.println("1. Set Make");

                                 System.out.println("2. Set Model");

                                 System.out.println("3. Set Year");

                                 System.out.println("4. Set Cost");

                                 System.out.println("5. Set Color");

                                 System.out.println("6. Display Car Information");

                                 System.out.println("7. Exit, and choose new car ID");

                                 System.out.print("Enter choice(1-7) : ");

                                 choice = scan.nextInt();

                                 scan.nextLine();

                                

                                 // perform the operation based on the choice

                                 switch(choice)

                                 {

                                 case 1:

                                        System.out.print("Enter make of the car : ");

                                        make = scan.nextLine();

                                        cars[id].setMake(make);

                                        break;

                                       

                                 case 2:

                                        System.out.print("Enter model of the car : ");

                                        model = scan.nextLine();

                                        cars[id].setModel(model);

                                        break;

                                       

                                 case 3:

                                        System.out.print("Enter year of the car : ");

                                        year = scan.nextInt();

                                        scan.nextLine();

                                        cars[id].setYear(year);

                                        break;

                                       

                                 case 4:

                                        System.out.print("Enter cost of the car : ");

                                        cost = scan.nextDouble();

                                        scan.nextLine();

                                        cars[id].setCost(cost);

                                        break;

                                       

                                 case 5:

                                        System.out.print("Enter color of the car : ");

                                        color = scan.nextLine();

                                        cars[id].setColor(color);

                                        break;

                                       

                                 case 6:

                                        System.out.println(cars[id].toString());

                                        break;

                                 case 7:

                                        break;

                                 default:

                                        System.out.println("Invalid choice");

                                       

                                 }

                           }while(choice != 7);

                          

                    }

             }

            

             scan.close();

       }

}

//end of CarDealership.java

Output:

Add a comment
Know the answer?
Add Answer to:
Use your Car class from the previous Programming Assignment. For this assignment, create a new class...
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
  • I need an OUTLINE ONLY (pseudocode/comments). DO NOT DO THE PROGRAMMING ASSIGNMENT. Part I: PA3 Outline...

    I need an OUTLINE ONLY (pseudocode/comments). DO NOT DO THE PROGRAMMING ASSIGNMENT. Part I: PA3 Outline (10 points). Create an outline in comments/psuedocode for the programming assignment below. Place your comments in the appropriate files: main.cpp, functions.h, dealer.cpp, dealer.h, dealer.cpp (as noted below). Place into a file folder named LastnamePA3, the zip the content and hand in a zip file to Canvas. Part II: PA3: Car Dealership (40 points) For Programming Assignment 3 you will be creating a program to...

  • Write a java program that creates a class Car. Class Car contains the following private member...

    Write a java program that creates a class Car. Class Car contains the following private member fields: make, model, year, and miles. The class Car has a constructor that takes as arguments the car make, model, and year. In addition, the constructor sets miles to 100 as default. Class Car has accessors and mutators to get and set the make, model, year and miles on the car. The program should complete the following tasks: Ask the user to enter make,...

  • Programming Assignment #2 (Arrays) I. The Assignment This assignment is to take your Garage class from...

    Programming Assignment #2 (Arrays) I. The Assignment This assignment is to take your Garage class from the previous assignment and modify it so that it uses an array of Car objects as the principal data structure instead of an ArrayList-of-Car. This is an example of the OOP principle of information hiding as your Car and test classes will not have to be modified at all. Unless you broke encapsulationon the previous assignment, that is   II. Specifications Specifications for all 3...

  • Java Programing Code only Ragged Array Assignment Outcome: Student will demonstrate the ability to create and...

    Java Programing Code only Ragged Array Assignment Outcome: Student will demonstrate the ability to create and use a 2D array Student will demonstrate the ability to create and use a jagged array Student will demonstrate the ability to design a menu system Student will demonstrate the ability to think Program Specifications: Write a program that does the following: Uses a menu system Creates an array with less than 25 rows and greater than 5 rows and an unknown number of...

  • C++ programming For this assignment, write a program that will act as a geometry calculator. The...

    C++ programming For this assignment, write a program that will act as a geometry calculator. The program will be menu-driven and should continue to execute as long as the user wants to continue. Basic Program Logic The program should start by displaying a menu similar to the following: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a triangle 3. Quit Enter your choice(1-3): and get the user's choice as an integer. After the choice...

  • Car Class Background: Write a class named Car that will be used to store information and...

    Car Class Background: Write a class named Car that will be used to store information and control the acceleration and brake of a car. Functionality: 1. Write a class named "Car” that has the following member variables: • year Model - an int that holds the car's year model • make-A string that holds the make of the car • speed - an int that holds the car's current speed 2. In addition the class should have the following member...

  • Lab 7 Add three instance variables to your class RightTriangle from lab 5. The first two...

    Lab 7 Add three instance variables to your class RightTriangle from lab 5. The first two are of type int and are called xLoc and yLoc and the third is of type int called ID. Also add a static variable of type double called scaleFactor. This should be initialized to a default value of 1. Update the constructor to set the three new instance variables and add appropriate get and set methods for the four new variables. All set methods...

  • this is for java programming Please use comments Use the Account class created in homework 8_2...

    this is for java programming Please use comments Use the Account class created in homework 8_2 to simulate an ATM machine. Create ten checking accounts in an array with id 0, 1, …, 9, initial balance of $100, and annualInterestRate of 0. The system prompts the user to enter an id between 0 and 9, or 999 to exit the program. If the id is invalid (not an integer between 0 and 9), ask the user to enter a valid...

  • Programming Assignment 6 Write a Java program that will implement a simple appointment book. The ...

    Programming Assignment 6 Write a Java program that will implement a simple appointment book. The program should have three classes: a Date class, an AppointmentBook class, and a Driver class. • You will use the Date class that is provided on Blackboard (provided in New Date Class example). • The AppointmentBook class should have the following: o A field for descriptions for the appointments (i.e. Doctor, Hair, etc.). This field should be an array of String objects. o A field...

  • Please i need the .H, .cpp and main.cpp files. Please use vector. its for my midterm so correct code that can run only....

    Please i need the .H, .cpp and main.cpp files. Please use vector. its for my midterm so correct code that can run only. Thank you. Pointers and Dynamic Memory – Chapter 11 1. Write code using pointers and the Accounts class from week 1 assignment as follows – create a program in which you a. Create 10 accounts using an array with id’s 0 to 9 and refer to each account using pointer notation b. Add an initial balance of...

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