0

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

Ades
  • 3
  • 1

1 Answers1

0

This is the proper and correct way of doing this. You should have a constructor for Car .

 public class Car {

    String brand;
    double motorSize;

    public Car(String brand, double motorSize){
        this.brand =brand;
        this.motorSize =motorSize;
    }
 }

And You should change the Person class too.

import java.util.ArrayList;

public class Person {

String name;
String surname;
Car car;

public static void main(String[] args) {


    Person customer1 = new Person("Sally", "Greg", new Car("Mercedes", 2.0 ) );
    Person customer2 = new Person("Linda", "Greg", new Car("BMW", 3.0));
    Person customer3 = new Person("Joseph", "Greg",new Car( "Toyota", 2.0));
    Person customer4 = new Person("Midge", "Greg", new Car("Lexus", 3.0));
    Person customer5 = new Person("John", "Greg", new Car("Mercedes", 1.5));

    ArrayList<Person> listOfCustomer = createListOfCustomer(customer1,customer2,customer3,customer4,customer5);

    checkMotorSize(listOfCustomer);

}

public Person (String name, String surname, Car car) {

    this.name = name;
    this.surname = surname;
    this.car= car;

}

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.name);
        }
    }
}

public static void checkSurname(ArrayList<Person> list) {

    for (Person person : list) {
        if (person.surname == "Greg") {
            System.out.println(person);
        }

    }
}


}
Sarangan
  • 803
  • 7
  • 20