I have a class called Customer with customerName, customerEmail and customerAddress as variables set in an object called a. How do I print the variables customerName, customerEmail and customerAddress in another class called BookingConfirmation? I tried:
System.out.println("Thanks for your booking " + a.getCustomerName());
Here is the code for Customer class:
public class Customer {
String customerName;
String customerEmail;
String customerAddress;
public static void customerDetails() throws IOException {
Customer a = new Customer();
Scanner seekName = new Scanner(System.in); // Create a Scanner object
System.out.print("Your name: ");
a.customerName = seekName.nextLine(); // Read user input
Scanner seekEmail = new Scanner(System.in); // Create a Scanner object
System.out.print("Your email: ");
a.customerEmail = seekEmail.nextLine(); // Read user input
Scanner seekAddress = new Scanner(System.in); // Create a Scanner object
System.out.print("Your residential address: ");
a.customerAddress = seekAddress.nextLine(); // Read user input
System.out.println("Thanks for your booking " + a.getCustomerName());
System.out.println("Eemail: " + a.getCustomerEmail());
System.out.println("Address: " + a.getCustomerAddress());
System.out.println();
}
public String getCustomerName() {
return customerName;
}
public String getCustomerEmail() {
return customerEmail;
}
public String getCustomerAddress() {
return customerAddress;
}
}
a) from outside its scope. In your example the objectahas method scope and won't be accessible from outside customerDetails() function.Customerobject you made in your static method, and then you have a valid factory pattern. Also, encapsulate your fields (make themprivate, look up getters and setters)customerDetails()toCustomerinstead ofvoid, and return the objecta. And try accessing it from within yourBookingConfirmationclass.