Home > OS >  nothing i do is fixing this error:"Incompatible types. Found: 'Car', required: '
nothing i do is fixing this error:"Incompatible types. Found: 'Car', required: '

Time:12-08

im trying to create a car rental system but it keeps giving me this error "Incompatible types. Found: 'Car', required: 'java.lang.Object'" can anyone help? im very new to programming and idk how to fix it and i dont understand what im supposed to do.

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

 class Car {

     private String make;
    private String model;
    private String regNo;
    private int deposit;
    private int rate;

    public Car(String newMake, String newModel, String newRegNo,
               int newDeposit, int newRate) {
        make = newMake;
        model = newModel;
        regNo = newRegNo;
        deposit = newDeposit;
        rate = newRate;
    }

    public String getMake() {
        return make;
    }

    public String getModel() {
        return model;
    }

    public String getRegNo() {
        return regNo;
    }

    public int getDeposit() {
        return deposit;
    }

    public int getRate() {
        return rate;
    }
}

 class TestProject {
    public static void main(String args[]) {
        List carlist = new ArrayList();
        carlist.add(new Car("Toyota", "Altis", "SJC2456X", 100, 60));
        carlist.add(new Car("Toyota", "Vios", "SJG9523B", 100, 50));
        carlist.add(new Car("Nissan", "Latio", "SJB7412B", 100, 50));
        carlist.add(new Car("Murano", "SJC8761M", "Nissan", 300, 150));
        carlist.add(new Car("Honda", "Jazz", "SJB4875N", 100, 60));
        carlist.add(new Car("Honda", "Civic", "SJD73269C", 120, 70));
        carlist.add(new Car("Honda", "Stream", "SJL5169J", 120, 70));
        carlist.add(new Car("Honda", "Odyssey", "SJB3468E", 200, 150));
        carlist.add(new Car("Subaru", "WRX", "SJB8234L", 300, 200));
        carlist.add(new Car("Subaru", "Imprezza", "SJE8234K", 150, 80));
        Scanner input = new Scanner(System.in);
        System.out.print("Enter model to rent: ");
        String model = input.nextLine();
        for (Car s : carlist) {
            if (model.equals(s.getModel())) {
                System.out.println("Model "   model   " is available");
                System.out.print("Enter number of days: ");
                int days = input.nextInt();
                System.out.println("***************Details*****************");
                int cost = (days * s.getRate())   s.getDeposit();
                System.out.println("Deposit  DailyRate  Duration  TotalCost");
                System.out.println(s.getDeposit()   "       "   s.getRate()   "            "   days   "        "   cost);
                System.out.print("Proceed to rent?( y/n ): ");
                String dec = input.next();
                if (dec.equals("y")) {
                    System.out.println("Enter Customer Name: ");
                    String name = input.next();
                    System.out.println("Enter IC Number: ");
                    int num = input.nextInt();
                    System.out.println("************Receipt*************");
                    System.out.println("Name   ICNo   Car  RegNo Duration   TCost");
                    System.out.println(name   "   "   num   "   "   model
                              "   "   s.getRegNo()   "   "   days   "   "   cost);
                    System.out.println("Serving Next Customer ");
                } else if (dec.equals("n")) {
                    System.out.println("Serving Next Customer: ");
                }
            }
        }
    }
}

thats the full code

i already tried changing it to "object" but it just gives me more errors so please help

CodePudding user response:

An easy fix for your problem is to create the list like this:

List<Car> carlist = new ArrayList<>();

If you can't do that, then you could use

for (Car s : (List<Car>)carlist) {
  • Related