J
J
Joukai2021-10-22 19:31:08
Java
Joukai, 2021-10-22 19:31:08

if does not work with classes, how to solve?

package sample;

import java.util.*;

public class Main {

    public static void main(String args[]) {
        Random r = new Random();
        ArrayList<Car> a = new ArrayList<Car>();
        ArrayList<Integer> p = new ArrayList<Integer>(Arrays.asList(1000, 100, 3456, 123, 678));
        ArrayList<String> c = new ArrayList<String>(Arrays.asList("white", "black", "purple", "pink", "green"));
        for (int i = 0; i < 5; i++) {
            a.add(new Car(c.get(r.nextInt(5)), p.get(r.nextInt(5))));
        }
        Car car = new Car("", 0);
        for (int i = 0; i < a.size(); i++) {
            if (a.get(i).getPrice() > car.getPrice())
               car.getPrice() = a.get(i).getPrice();
        }
    }

}

package sample;

public class Car {
    private int price;
    private String color;
    public Car(String color, int price) {
        this.price = price;
        this.color = color;
    }

    public int getPrice() {
        return price;
    }

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
    
    public String toString() {
        return "Color " + getColor() + " Price " + getPrice();
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BorLaze, 2021-10-22
@Joukai

Firstly, you can’t write like that: car.getPrice() = a.get(i).getPrice();
Secondly, how will you see that it works? There is no check after actions in main, no output of the result ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question