Question

I need this in java please Create an automobile class that will be used by a...

I need this in java please Create an automobile class that will be used by a dealership as a vehicle inventory program. The following attributes should be present in your automobile class: private string make private string model private string color private int year private int mileage. Your program should have appropriate methods such as: default constructor parameterized constructor add a new vehicle method list vehicle information (return string array) remove a vehicle method update vehicle attributes method. All methods should include try..catch constructs. Except as noted all methods should return a success or failure message (failure message defined in “catch”). Create an additional class to call your automobile class (e.g., Main or AutomobileInventory). Include a try..catch construct and print it to the console any errors. Call automobile class with parameterized constructor (e.g., "make, model, color, year, mileage"). Then call the method to list the values. Loop through the array and print to the screen. Call the remove vehicle method to clear the variables. Print the return value. Add a new vehicle. Print the return value. Call the list method and print the new vehicle information to the screen. Update the vehicle. Print the return value. Call the listing method and print the information to the screen. Display a message asking if the user wants to print the information to a file (Y or N). Use a scanner to capture the response. If “Y”, print the file to a predefined location (e.g., C:\Temp\Autos.txt). Note: you may want to create a method to print the information in the main class. If “N”, indicate that a file will not be printed.

Any additional information and comments explaining more in depth would be appreciated. Thank you

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

import java.io.PrintWriter;
import java.util.Scanner; // Required for user input
import java.util.*;

public class Main { // Note that my automobile class is in another file 'Automobile.java'

    public static void main(String[] args) {

        Automobile car = new Automobile();
            car.setMake("");
            String make = car.getMake();

            car.setModel("");
            String model = car.getModel();

            car.setColor("");
            String color = car.getColor();

            car.setYear(0);
            Integer year = car.getYear();

            car.setMileage(0);
            Integer mileage = car.getMileage();

        int removeCar;
        int updateCar;

        Scanner input = new Scanner(System.in); // This allows the users to enter data
        ArrayList<String> carMake = new ArrayList<>();
        ArrayList<String> carModel = new ArrayList<>();
        ArrayList<String> carColor = new ArrayList<>();
        ArrayList<Integer> carYear = new ArrayList<>();
        ArrayList<Integer> carMileage = new ArrayList<>();

        do { // this is here so my menu loops after the users performs an action
            try {
                // User Menu
                System.out.println(""); // Just want some spacing for aesthetics
                System.out.println("-----------------------------------");
                System.out.println("What do you want to do? (Type 1-5)");
                System.out.println("-----------------------------------");
                System.out.println("1. Add new vehicle");
                System.out.println("2. Remove a vehicle");
                System.out.println("3. Update existing vehicle");
                System.out.println("4. View all vehicles");
                System.out.println("5. Print all data to text file");
                System.out.println("-----------------------------------");

                int menu = input.nextInt(); // Only allow numbers for simplicity

                if (menu == 1) {
                    System.out.println("Type the Make of the car you wish to enter:");
                    make = input.next();
                    carMake.add(make);

                    System.out.println("Type the Model of the car you wish to enter:");
                    model = input.next();
                    carModel.add(model);

                    System.out.println("Type the Color of the car you wish to enter:");
                    color = input.next();
                    carColor.add(color);

                    System.out.println("Type the Year of the car you wish to enter:");
                    year = input.nextInt();
                    carYear.add(year);

                    System.out.println("Type the Mileage of the car you wish to enter:");
                    mileage = input.nextInt();
                    carMileage.add(mileage);

                } else if (menu == 2) {
                    System.out.println("What car do you want to remove? (Type the number... IE: 0)");

                    // Give the user options
                    for (int i = 0; i < carMake.size() && i < carModel.size() && i < carColor.size() && i < carYear.size() && i < carMileage.size(); i++)
                        System.out.println(carMake.indexOf(carMake.get(i)) + ": " +carMake.get(i) + ", " + carModel.get(i) + ", " + carColor.get(i) + ", " + carYear.get(i) + ", " + carMileage.get(i) + ".");

                    // Remove the indexes for the car selected... It's important to remove them all because if you don't the remaining cars will be out of sync
                    removeCar = input.nextInt();
                    carMake.remove(removeCar);
                    carModel.remove(removeCar);
                    carColor.remove(removeCar);
                    carYear.remove(removeCar);
                    carMileage.remove(removeCar);

                } else if (menu == 3) {
                    System.out.println("What car do you want to update? (Type the number... IE: 0)");

                    for (int i = 0; i < carMake.size() && i < carModel.size() && i < carColor.size() && i < carYear.size() && i < carMileage.size(); i++)
                        System.out.println(carMake.indexOf(carMake.get(i)) + ": " +carMake.get(i) + ", " + carModel.get(i) + ", " + carColor.get(i) + ", " + carYear.get(i) + ", " + carMileage.get(i) + ".");

                    updateCar = input.nextInt();
                    System.out.println("Set the new Car Make:");
                    carMake.set(updateCar, input.next());
                    System.out.println("Set the new Car Model:");
                    carModel.set(updateCar, input.next());
                    System.out.println("Set the new Car Color:");
                    carColor.set(updateCar, input.next());
                    System.out.println("Set the new Car Year:");
                    carYear.set(updateCar, input.nextInt());
                    System.out.println("Set the new Car Mileage:");
                    carMileage.set(updateCar, input.nextInt());

                } else if (menu == 4) {
                    System.out.println("Here are all of the cars currently in the inventory:");
                    for (int i = 0; i < carMake.size() && i < carModel.size() && i < carColor.size() && i < carYear.size() && i < carMileage.size(); i++)
                        System.out.println(carMake.get(i) + ", " + carModel.get(i) + ", " + carColor.get(i) + ", " + carYear.get(i) + ", " + carMileage.get(i) + ".");

                } else if (menu == 5) {
                    System.out.println("Printing all data...");

                    PrintWriter writer = new PrintWriter("TravisWoodward_AutomobileProgram.txt", "UTF-8");
                    for (int i = 0; i < carMake.size() && i < carModel.size() && i < carColor.size() && i < carYear.size() && i < carMileage.size(); i++)
                        writer.println(carMake.get(i) + ", " + carModel.get(i) + ", " + carColor.get(i) + ", " + carYear.get(i) + ", " + carMileage.get(i) + ".");
                    writer.close();

                } else {
                    System.out.println("You must select a number between 1-5 (IE: 2).");
                }
            }
            catch (Exception e) {
                input.next(); // dont let users type strings where ints go!
                System.out.println("You may not put letters in that field. Please try again.\n");
            }
        }
        while (true);
    }
}

Automobile.java

public class Automobile {

    private String make;
    private String model;
    private String color;
    private int year;
    private int mileage;

    public void setMake(String make) {
        this.make = make;
    }

    public String getMake() {
        return make;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getModel() {
        return this.model;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getColor() {
        return this.color;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getYear() {
        return this.year;
    }

    public void setMileage(int mileage) {
        this.mileage = mileage;
    }

    public int getMileage() {
        return this.mileage;
    }

    public static void main(String[] args){

        System.out.println("Please run the Main.java file...");

    }
}

Add a comment
Answer #2

Here's a possible implementation of the Automobile class in Java:

import java.io.FileWriter;

import java.io.IOException;


public class Automobile {

    private String make;

    private String model;

    private String color;

    private int year;

    private int mileage;


    public Automobile() {

        // default constructor

    }


    public Automobile(String make, String model, String color, int year, int mileage) {

        this.make = make;

        this.model = model;

        this.color = color;

        this.year = year;

        this.mileage = mileage;

    }


    public String[] getVehicleInformation() {

        String[] info = new String[5];

        info[0] = "Make: " + make;

        info[1] = "Model: " + model;

        info[2] = "Color: " + color;

        info[3] = "Year: " + year;

        info[4] = "Mileage: " + mileage;

        return info;

    }


    public boolean addNewVehicle(String make, String model, String color, int year, int mileage) {

        try {

            this.make = make;

            this.model = model;

            this.color = color;

            this.year = year;

            this.mileage = mileage;

            return true;

        } catch (Exception e) {

            System.out.println("Error adding new vehicle: " + e.getMessage());

            return false;

        }

    }


    public boolean removeVehicle() {

        try {

            make = null;

            model = null;

            color = null;

            year = 0;

            mileage = 0;

            return true;

        } catch (Exception e) {

            System.out.println("Error removing vehicle: " + e.getMessage());

            return false;

        }

    }


    public boolean updateVehicleAttributes(String make, String model, String color, int year, int mileage) {

        try {

            if (make != null) {

                this.make = make;

            }

            if (model != null) {

                this.model = model;

            }

            if (color != null) {

                this.color = color;

            }

            if (year > 0) {

                this.year = year;

            }

            if (mileage > 0) {

                this.mileage = mileage;

            }

            return true;

        } catch (Exception e) {

            System.out.println("Error updating vehicle attributes: " + e.getMessage());

            return false;

        }

    }


    public boolean printToFile(String filePath) {

        try {

            FileWriter writer = new FileWriter(filePath);

            String[] info = getVehicleInformation();

            for (String s : info) {

                writer.write(s + "\n");

            }

            writer.close();

            return true;

        } catch (IOException e) {

            System.out.println("Error printing vehicle information to file: " + e.getMessage());

            return false;

        }

    }

}

And here's an example usage of the class in a main method:

import java.util.Scanner;


public class AutomobileInventory {

    public static void main(String[] args) {

        Automobile vehicle = new Automobile("Honda", "Civic", "Blue", 2015, 50000);

        String[] info = vehicle.getVehicleInformation();

        for (String s : info) {

            System.out.println(s);

        }

        vehicle.removeVehicle();

        System.out.println(vehicle.removeVehicle() ? "Vehicle removed." : "Error removing vehicle.");

        System.out.println(vehicle.addNewVehicle("Toyota", "Corolla", "Red", 2020, 10000) ? "Vehicle added." : "Error adding new vehicle.");

        info = vehicle.getVehicleInformation();


answered by: Hydra Master
Add a comment
Know the answer?
Add Answer to:
I need this in java please Create an automobile class that will be used by a...
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
  • Final PYTHON program: Create a home inventory class that will be used by a National Builder...

    Final PYTHON program: Create a home inventory class that will be used by a National Builder to maintain inventory of available houses in the country. The following attributes should be present in your home class: -private int squarefeet -private string address -private string city -private string state -private int zipcode -private string Modelname -private string salestatus (sold, available, under contract) Your program should have appropriate methods such as: -constructor -add a new home -remove a home -update home attributes At...

  • Please help with a source code for C++ and also need screenshot of output: Step 1:...

    Please help with a source code for C++ and also need screenshot of output: Step 1: Create a Glasses class using a separate header file and implementation file. Add the following attributes. Color (string data type) Prescription (float data type) Create a default constructor that sets default attributes. Color should be set to unknown because it is not given. Prescription should be set to 0.0 because it is not given. Create a parameterized constructor that sets the attributes to the...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • Using your Dog class from earlier this week, complete the following: Create a new class called...

    Using your Dog class from earlier this week, complete the following: Create a new class called DogKennel with the following: Instance field(s) - array of Dogs + any others you may want Contructor - default (no parameters) - will make a DogKennel with no dogs in it Methods: public void addDog(Dog d) - which adds a dog to the array public int currentNumDogs() - returns number of dogs currently in kennel public double averageAge() - which returns the average age...

  • In java netbean8.1 or 8.2 please. The class name is Stock. It has 5 private attributes...

    In java netbean8.1 or 8.2 please. The class name is Stock. It has 5 private attributes (name, symbol, numberOfShares, currentPrice and boughtPrice)and one static private attribute (numberOfStocks). All attributes must be initialized (name and symbol to “No name/ symbol yet” and numberOfShares, currentPrice and boughtPrice to 0. Create two constructors, one with no arguments and the other with all the attributes. (However, remember to increase the numberOfStocks in both constructors. Write the necessary mutators and accessors (getters and setters) for...

  • using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship...

    using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship (all attributes private) String attribute for the name of ship float attribute for the maximum speed the of ship int attribute for the year the ship was built Add the following behaviors constructor(default and parameterized) , finalizer, and appropriate accessor and mutator methods Override the toString() method to output in the following format if the attributes were name="Sailboat Sally", speed=35.0f and year built of...

  • In Java: Requirements: 1. Create a class called Bike. 2. Create these private attributes: int currentSpeed...

    In Java: Requirements: 1. Create a class called Bike. 2. Create these private attributes: int currentSpeed int currentGear String paintColor 3. Create a constructor that takes 2 arguments: speed, gear, color. 4. Create another constructor that takes no arguments. 5. Create a method called goFaster(int amount) which increases the speed by the given amount. 6. Create a method called brake() which sets the speed to 0. 7. Create a method called print() which describes the bike and the speed it...

  • Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters...

    Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters long encryptedPassword : String key int Must be between 1 and 10(inclusive) accountId - A unique integer that identifies each account nextIDNum – a static int that starts at 1000 and is used to generate the accountID no other instance variables needed. Default constructor – set all instance variables to a default value. Parameterized constructor Takes in clearPassword, key. Calls encrypt method to create...

  • CIS247C Week 3 Project Overview The objective of this week is to enhance last week's Vehicle cla...

    CIS247C Week 3 Project Overview The objective of this week is to enhance last week's Vehicle class by making the following changes: • Create a static variable called numVehicles that holds an int and initialize it to zero. This will allow us to count all the Vehicle objects created in the main class. • Add the copy constructor • Increment numVehicles in all of the constructors • Decrement numVehicle in destructor • Add overloaded versions of setYear and setMpg that...

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