How to check customers from another class (Java Object and Class)
I am just trying to find the customers who have motorsize = 2.0. I created a method to figure it out which is checkmotorsize. However, it does not work. Can anyone help me about it ? Thanks
car.java
package day27;
public class Car {
String brand;
double motorSize;
}
person.java
package day27;
import java.util.ArrayList;
public class Person {
String name;
String surname;
Car car;
public static void main(String[] args) {
Person customer1 = createCustomer("Sally", "Greg", "Mercedes", 2.0);
Person customer2 = createCustomer("Linda", "Greg", "BMW", 3.0);
Person customer3 = createCustomer("Joseph", "Greg", "Toyota", 2.0);
Person customer4 = createCustomer("Midge", "Greg", "Lexus", 3.0);
Person customer5 = createCustomer("John", "Greg", "Mercedes", 1.5);
ArrayList<Person> listOfCustomer = createListOfCustomer(customer1, customer2, customer3, customer4, customer5);
checkMotorSize(listOfCustomer);
checkSurname(listOfCustomer);
}
public static Person createCustomer(String name, String surname, String brand, double motorSize) {
Person customer = new Person();
customer.name = name;
customer.surname = surname;
customer.car.brand = brand;
customer.car.motorSize = motorSize;
return customer;
}
public static ArrayList<Person> createListOfCustomer(Person... customer) {
ArrayList<Person> list = new ArrayList<>();
for (Person person : customer) {
list.add(person);
}
return list;
}
public static void checkMotorSize(ArrayList<Person> list) {
for (Person person : list) {
if (person.car.motorSize == 2.0) {
System.out.println(person);
}
}
}
public static void checkSurname(ArrayList<Person> list) {
for (Person person : list) {
if (person.surname == "Greg") {
System.out.println(person);
}
}
}
}
I am just trying to find the customers who have motorsize = 2.0. I created a method to figure it out which is checkmotorsize. However, it does not work. Can anyone help me about it ? Thanks
I am just trying to find the customers who have motorsize = 2.0. I created a method to figure it out which is checkmotorsize. However, it does not work. Can anyone help me about it ? Thanks