An existing class Customer() has been provided in Customer.java. Write a program in main() of CustomerAge.java to accept one string input and one integer input for a customer's name and age fields, respectively. Use the existing customer1 and customer2 reference variables provided in the template to set the name and age of both customer objects using the setName() and setAge() methods. Determine which customer is older using its getAge() method. Output the older customer's information using its printInfo() method, ending with a newline. Ensure your program output matches the example formatting below and works for a variety of input values.

If the input is:

Jane

25

John

29

the output is:

Customer that is older:

Name: John

Age: 29

Solution


import java.util.Scanner;

public class CustomerAge {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      
      Customer customer1 = new Customer();
      Customer customer2 = new Customer();
      
      String name1 = scnr.nextLine();
      int age1 = Integer.valueOf(scnr.nextLine());

      String name2 = scnr.nextLine();
      int age2 = Integer.valueOf(scnr.nextLine());
      
      customer1.setName(name1);
      customer2.setName(name2);
      customer1.setAge(age1);
      customer2.setAge(age2);

      System.out.println("Customer that is older:");
      
      if (customer1.getAge() > customer2.getAge()){
         System.out.println("Name: " + customer1.getName());
         System.out.println("Age: " + customer1.getAge());
      }
      else {
         System.out.println("Name: " + customer2.getName());
         System.out.println("Age: " + customer2.getAge());
      }
   }
}