#need help with java assignment

1 messages ยท Page 1 of 1 (latest)

broken dewBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question ๐Ÿค–. Maybe it helps! In any case, a human is on the way ๐Ÿ‘. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

uld be able to search for cars based on certain criteria (e.g. model, color, price range) and view the details of the matching cars.

Here's an example of how you can implement this project:

  1. Create a Car class with attributes like model, color, price, and onHold status. Include getters and setters for these attributes.
#
public class Car {
    private String model;
    private String color;
    private double price;
    private boolean onHold;

    public Car(String model, String color, double price) {
        this.model = model;
        this.color = color;
        this.price = price;
        this.onHold = false;
    }

    // Getters and setters
}
#
  1. Create a CarDealership class to manage the car inventory. This class will have an array to store the cars and methods to add cars, search for cars based on criteria, and view car details.
#
import java.util.ArrayList;

public class CarDealership {
    private ArrayList<Car> inventory;

    public CarDealership() {
        inventory = new ArrayList<>();
    }

    public void addCar(Car car) {
        inventory.add(car);
    }

    public ArrayList<Car> searchCarsByModel(String model) {
        ArrayList<Car> matchingCars = new ArrayList<>();

        for (Car car : inventory) {
            if (car.getModel().equalsIgnoreCase(model)) {
                matchingCars.add(car);
            }
        }

        return matchingCars;
    }

    public ArrayList<Car> searchCarsByColor(String color) {
        ArrayList<Car> matchingCars = new ArrayList<>();

        for (Car car : inventory) {
            if (car.getColor().equalsIgnoreCase(color)) {
                matchingCars.add(car);
            }
        }

        return matchingCars;
    }

   public ArrayList<Car> searchCarsByPriceRange(double minPrice, double maxPrice) {
        ArrayList<Car> matchingCars = new ArrayList<>();

        for (Car car : inventory) {
            if (car.getPrice() >= minPrice && car.getPrice() <= maxPrice) {
                matchingCars.add(car);
            }
        }

        return matchingCars;
    }

    public void viewCarDetails(ArrayList<Car> cars) {
        for (Car car : cars) {
            System.out.println("Model: " + car.getModel());
            System.out.println("Color: " + car.getColor());
            System.out.println("Price: $" + car.getPrice());
            System.out.println("On Hold: " + (car.isOnHold() ? "Yes" : "No"));
            System.out.println();
        }
    }
}
#
  1. In your main method, you can create an instance of the CarDealership class and interact with it using a Scanner to get user input.
#
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        CarDealership dealership = new CarDealership();
        Scanner scanner = new Scanner(System.in);

        // Add some sample cars to the inventory
        dealership.addCar(new Car("Toyota Camry", "Red", 25000));
        dealership.addCar(new Car("Honda Civic", "Blue", 20000));
        dealership.addCar(new Car("Ford Mustang", "Yellow", 35000));

        // Search for cars by model
        System.out.print("Enter model to search for: ");
        String model = scanner.nextLine();
        
        ArrayList<Car> matchingCars = dealership.searchCarsByModel(model);
        
       if (matchingCars.isEmpty()) {
           System.out.println("No cars found with the given model.");
       } else {
           dealership.viewCarDetails(matchingCars);
       }

       // Search for cars by color
       System.out.print("Enter color to search for: ");
       String color = scanner.nextLine();

       matchingCars = dealership.searchCarsByColor(color);

       if (matchingCars.isEmpty()) {
           System.out.println("No cars found with the given color.");
       } else {
           dealership.viewCarDetails(matchingCars);
       }

       // Search for cars by price range
       System.out.print("Enter minimum price: ");
       double minPrice = scanner.nextDouble();
       System.out.print("Enter maximum price: ");
       double maxPrice = scanner.nextDouble();

       matchingCars = dealership.searchCarsByPriceRange(minPrice, maxPrice);

       if (matchingCars.isEmpty()) {
           System.out.println("No cars found within the given price range.");
       } else {
           dealership.viewCarDetails(matchingCars);
        }
    }
}