Answer to Question #263526 in Java | JSP | JSF for Laiba Shahid

Question #263526


You are required to design a Car class which has following attributes

        String brand;

        Double price;

        Double quantity;

        String Model;

        Boolean available;

1.     Provide default & Parameterized constructors.

2.     Provide getters & setters for data members.

3.     Provide a toString() method to print values.

 

 Now create an ArrayList of Cars in a class ShowRoom having name and address as attributes as well, Provide appropriate methods, store values in array List (take values form user).

Provide a buy method which will ask user to buy a Car by providing its name, check that it is present or not then ask for quantity and create bill for user.

After that you have to print the following

1.     Add Car

2.     Delete Car

3.     Buy a Car.

4.     Display All cars also display

·        Car of brand “Audi”;

·        Car with name starting with ‘L’.

·        Car name end with ‘n’.

·        Display only last three and first three letters of Showroom name.

 


1
Expert's answer
2021-11-10T08:27:51-0500
public class Car {
    private String brand;
    private double price;
    private double quantity;
    private String model;
    private boolean available;

    public Car() {
        this("Unknown", -1, -1, "Unknown", false);
    }

    public Car(String brand, double price, double quantity, String model, boolean available) {
        this.brand = brand;
        this.price = price;
        this.quantity = quantity;
        this.model = model;
        this.available = available;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getQuantity() {
        return quantity;
    }

    public void setQuantity(double quantity) {
        this.quantity = quantity;
    }

    public String getModel() {
        return model;
    }

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

    public boolean isAvailable() {
        return available;
    }

    public void setAvailable(boolean available) {
        this.available = available;
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", price=" + price +
                ", quantity=" + quantity +
                ", model='" + model + '\'' +
                ", available=" + available +
                '}';
    }
}


import java.util.ArrayList;
import java.util.Scanner;

public class ShowRoom {
    private String name;
    private String address;
    private ArrayList<Car> cars;

    public ShowRoom(String name, String address) {
        this.name = name;
        this.address = address;
        cars = new ArrayList<>();
    }

    public void addCar() {
        Scanner in = new Scanner(System.in);
        cars.add(new Car());
        System.out.println("Brand:");
        cars.get(cars.size() - 1).setBrand(in.nextLine());
        System.out.println("Price:");
        cars.get(cars.size() - 1).setPrice(Double.parseDouble(in.nextLine()));
        System.out.println("Quantity:");
        cars.get(cars.size() - 1).setQuantity(Double.parseDouble(in.nextLine()));
        System.out.println("Model:");
        cars.get(cars.size() - 1).setModel(in.nextLine());
        System.out.println("Available:");
        cars.get(cars.size() - 1).setAvailable(Boolean.parseBoolean(in.nextLine()));
    }

    public void buyCar() {
        Scanner in = new Scanner(System.in);
        System.out.println("Model:");
        String model = in.nextLine();
        System.out.println("Quantity:");
        int quantity = Integer.parseInt(in.nextLine());
        for (Car car : cars) {
            if (car.getModel().equals(model)) {
                System.out.println("Bill:");
                System.out.println("Price: " + car.getPrice());
                System.out.println("Quantity: " + quantity);
                System.out.println("Total: " + car.getPrice() * quantity);
                break;
            }
        }
    }

    public void deleteCar() {
        Scanner in = new Scanner(System.in);
        System.out.println("Model:");
        String model = in.nextLine();
        int index = -1;
        for (int i = 0; i < cars.size(); i++) {
            if (cars.get(i).getModel().equals(model)) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            cars.remove(index);
        }
    }

    public void display() {
        for (Car car : cars) {
            System.out.println(car);
        }
        System.out.println();
        for (Car car : cars) {
            if (car.getBrand().equals("Audi")) {
                System.out.println(car);
            }
        }
        System.out.println();
        for (Car car : cars) {
            if (car.getModel().charAt(0) == 'L') {
                System.out.println(car);
            }
        }
        System.out.println();
        for (Car car : cars) {
            if (car.getModel().charAt(car.getModel().length() - 1) == 'n') {
                System.out.println(car);
            }
        }
        System.out.println();
        System.out.println(name.substring(0, 3));
        System.out.println(name.substring(name.length() - 3));
    }

    public void menu() {
        Scanner in = new Scanner(System.in);
        while (true) {
            System.out.println("\n1. Add Car" +
                    "\n2. Delete Car" +
                    "\n3. Buy Car" +
                    "\n.4 Display All cars");
            switch (in.nextLine()) {
                case "1":
                    addCar();
                    break;
                case "2":
                    deleteCar();
                    break;
                case "3":
                    buyCar();
                    break;
                case "4":
                    display();
                    break;
            }
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS