I asked a question earlier on this same project but I’m still having issues that I can’t get through.
The project has a Person class, Validator class, Customer class, and Employee class. The Person class stores data about the person (name, email) the Customer class extends the person class and adds a customer number to the toString method. The Employee class also extends the Person class and extends a social security number to the toString method by overriding it.
At the bottom of the page I am trying to print the toString methods from my customer class OR my employee class. I want to make sure I am printing the right class based on what the user selected (if they are entering customer info or employee info)
The assignment specifically says “To print the data for an object to the console, this application should use a static method named print that accepts a Person object.”
I think I have it started but I’m getting all kinds of red lines under what I have coded. Starting around the
public void toString()
line down towards the bottom.
I’m starting to think I am getting deeper into trouble by looking it up online so if someone can help me through it I would be greatful. My book doesn’t show much on how to do this and all of the examples it shows seem to create the input and then print it but I’m trying to get the input from a user so I’m getting confused.
import java.util.Scanner;
public class PersonApp
{
public static void main(String[] args)
{
//welcome user to person tester
System.out.println("Welcome to the Person Tester Application");
System.out.println();
Scanner in = new Scanner(System.in);
//set choice to y
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
//prompt user to enter customer or employee
System.out.println("Create customer or employee (c/e): ");
String input = in.nextLine();
if (input.equalsIgnoreCase("c"))
{
String firstName = Validator.getString(in, "Enter first name: ");
String lastName = Validator.getString(in, "Enter last name: ");
String email = Validator.getEmail(in, "Enter email address: ");
String custNumber = Validator.getString(in, "Customer number: ");
Customer customer = new Customer(firstName, lastName, email, custNumber);
}
else if(input.equalsIgnoreCase("e"))
{
String firstName = Validator.getString(in, "Enter first name: ");
String lastName = Validator.getString(in, "Enter last name: ");
String email = Validator.getEmail(in, "Enter email address: ");
int empSoc = Validator.getInt(in, "Social security number: ");
Employee employee = new Employee(firstName, lastName, email, empSoc);
}
public void toString()
{
Person p;
p = c;
System.out.println(c.toString());
p = e;
System.out.println(e.toString());
}
System.out.println("Continue? y/n: ");
choice = in.nextLine();
System.out.println();
}
}
}
You can’t define methods inside another method. The way you have the brackets,
toStringis defined insidemain, which is illegal.Also, you can’t have
toStringreturnvoid, since it overrides thetoStringmethod fromObject. Rename your method or have it return aString.