Christopher Collar
Christopher Collar

Reputation: 25

How to call a toString from a superclass

I have to make program that asks for the users name, address, phone number, customer number, and whether or not the customer would like to be added to the mailing list (displayed as true or false) and then display the user information I've got the entire program working perfectly the only issue is that there is a blank space inbetween both my toString() methods. The assignment requires me to make the entire program in one file without a blank space here is an example output:

Example

Customer:
Name: Julia Stevens
Address: 77 Massachusetts Ave Cambridge, MA 02139
Phone·Number: 617-777-7777
Customer·Number: 928734502
Recieve·Mail?: false

Here is my code: Code

public static void main(String[] args) 
{
   String personInput;
   String customerInput;
   Person A = new Person("", "", "");
   Customer B = new Customer("", false, "", "", "");

   Scanner input = new Scanner(System.in);

    System.out.print("Enter name of customer:");
    personInput = input.nextLine();
    A.setName(personInput);
    System.out.print("Enter address of customer:");
    personInput = input.nextLine();
    A.setAddress(personInput);
    System.out.print("Enter phone number of customer:");
    personInput = input.nextLine();
    A.settNumber(personInput);
    System.out.print("Enter customer number:");
    customerInput = input.nextLine();
    B.setcNumber(customerInput);
    System.out.print("Enter yes/no -- does the customer want to receive mail?:");
    customerInput = input.nextLine();
    B.setmList(false);

    if (customerInput.equals("yes"))
        B.setmList(true);
    else if (customerInput.equals("no"))
        B.setmList(false);

    System.out.println("");

    System.out.println("Cutomer: ");
    System.out.println(A.toString());
    System.out.println(B.toString());

}

private static class Person
{
    private String name;
    private String address;
    private String tNumber;

    public Person(String name, String address, String tNumber) 
    {
        this.name = name;
        this.address = address;
        this.tNumber = tNumber;
    }

    public String getName() 
    {
        return name;
    }

    public void setName(String name) 
    {
        this.name = name;
    }

    public String getAddress() 
    {
        return address;
    }

    public void setAddress(String address) 
    {
        this.address = address;
    }

    public String gettNumber() 
    {
        return tNumber;
    }

    public void settNumber(String tNumber) 
    {
        this.tNumber = tNumber;
    }

    @Override
    public String toString() {
        return "Name: " + name + "\n"
             + "Address: " + address + "\n"
             + "Phone Number: " + tNumber + "\n";
    }
}
    private static class Customer extends Person
    {
        private String cNumber;
        private boolean mList;

        public Customer(String cNumber, boolean mList, String name, String address, String tNumber) 
        {
            super(name, address, tNumber);
            this.cNumber = cNumber;
            this.mList = mList;
        }

        public String getcNumber() 
        {
            return cNumber;
        }

        public void setcNumber(String cNumber) 
        {
            this.cNumber = cNumber;
        }

        public boolean ismList() 
        {
            return mList;
        }

        public void setmList(boolean mList) 
        {
            this.mList = mList;
        }

    @Override
    public String toString() {
        return "Customer Number: " + cNumber + "\n"
             + "Receive Mail?: " + mList;
    }    

Upvotes: 1

Views: 884

Answers (2)

strash
strash

Reputation: 1321

eeeemmm Just make it System.out.print - not System.out.println on these lines

System.out.println("Cutomer: ");
System.out.print(A.toString());
System.out.print(B.toString());}

Upvotes: 0

Diego Krupitza
Diego Krupitza

Reputation: 121

Just use the super keyword to call the parent class.

    @Override
    public String toString() {
        return super.toString() + "\n Customer Number: " + cNumber + "\n"
             + "Receive Mail?: " + mList;
    }  

You can access all properties and methods with the keyword super just need the correct access specifiers!

Upvotes: 1

Related Questions