-1

arraylist displays object address not actual object , program usesinheritance where salesEmployee is the super class and salesAgent and salesPerson are the subclasses.

import java.util.Scanner;
import java.util.ArrayList;
public class tester {
public static void main(String[]args ) {

ArrayList <salesEmployee> listemps= new ArrayList <salesEmployee>();


Scanner user_input= new Scanner(System.in);


salesPerson emp1 = new salesPerson(); 


emp1.setName("Frank Long");
emp1.setppsNumber(65783);
System.out.println("Enter total value of sales earned by Frank Long");
double valeSale;
valeSale=user_input.nextDouble();
emp1.setvalSale(valeSale);
emp1.getCommission();

listemps.add(emp1);

for ( int j=0; j<listemps.size(); j++ )
      System.out.println("element " + j + ": " + listemps.get(j) );
    }
}

This is my salesPerson class

public class salesPerson extends salesEmployee{

    public salesPerson() {




    }
    public salesPerson(String name, int ppsNumber, double valSale, double commission) {
        super(name, ppsNumber,valSale,commission);

    }

    public void getCommission() {
        commission=valSale*0.15;
    }

    public String toString2() {

        return toString1()+"value of sales"+getvalSale()+"commission:"+commission;
    }

}

I'll make it more elegant later for now I am just trying to get it to work

Updated: Based on the comments to my answer, there is a different issue at play. Here's what was added in the comments:

Enter total value of sales earned by Frank Long 22.00
Exception in thread "main" java.lang.StackOverflowError at
salesPerson.toString(salesPerson.java:21) at
salesPerson.toString(salesPerson.java:21) at
salesPerson.toString(salesPerson.java:21) at
salesPerson.toString(salesPerson.java:21) – lucylio 5 mins ago

Is what comes up – lucylio 5 mins

Aleks G
  • 56,435
  • 29
  • 168
  • 265
lucylio
  • 1
  • 3
  • 1
    *FYI:* If you want `salesPerson.toString()` to call `salesEmployee.toString()`, you need to use the `super` keyword: `super.toString()` – Andreas Jan 30 '19 at 16:38
  • 1
    *FYI:* Java naming convention is for class names to start with uppercase letter, so classes should be named `SalesEmployee` and `SalesPerson` – Andreas Jan 30 '19 at 16:39
  • I updated the question with the comments from OP to my answer. Those comments change the question somewhat. – Aleks G Jan 30 '19 at 16:48

1 Answers1

0

In order for something reasonable to be displayed, you need to implement toString method in your class. You do have toString1 and toString2, but seemingly, no toString. (You haven't posted the code for salesEmployee class - but most likely it also doesn't have toString implementation).

In absence of toString, default Object.toString is called, which displays the address of the object.

Implement toString - and you'll see your results.

UPDATE: As the error you indicated doesn't correspond to the code, I'll go on a whim and suggest that, probably your toString2 method is actually toString and your toString1 method is actually a toString defined in your parent class, i.e. salesEmployee.java. In this case, instead of calling toString() from inside your toString method, use super.toString() instead:

public class salesPerson extends salesEmployee {

   ...

    public String toString2() {
        return super.toString()+"value of sales"+getvalSale()+"commission:"+commission;
    }
}
Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • Enter total value of sales earned by Frank Long 22.00 Exception in thread "main" java.lang.StackOverflowError at salesPerson.toString(salesPerson.java:21) at salesPerson.toString(salesPerson.java:21) at salesPerson.toString(salesPerson.java:21) at salesPerson.toString(salesPerson.java:21) – lucylio Jan 30 '19 at 16:33
  • Is what comes up – lucylio Jan 30 '19 at 16:33
  • 2
    @lucylio This error message does not correspond to the code you posted. You don't have method `toString` at line 21 in `salesPerson.java` – Aleks G Jan 30 '19 at 16:34
  • 1
    Almost correct: `Object.toString` doesn't display "the address of the object", it displays the identity hash. – Joachim Sauer Jan 30 '19 at 16:43
  • @JoachimSauer You're correct. The OP used the `address` terminology, I simply went with it. – Aleks G Jan 30 '19 at 16:44