Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I access an object array located inside of another object which is then inside a superclass array?
    primarykey
    data
    text
    <p>I have an abstract class <code>Employee</code>, and based on that, I created the following subclasses:</p> <p><code>FulltimeEmployee</code><br> <code>PartTimeEmployee</code><br> <code>Salesman</code></p> <p>as well as one standalone class, <code>Orders</code>.</p> <p>I use the <code>Orders</code> class to "describe the salesman" by sending an array of orders in the Salesman constructor:</p> <pre><code>public Salesman(String firstname, String lastname,int code, String address, String city,int tk,int phone,String email,int deptcode,int card, double hours,String cat,int orderno,double salary, Orders[] order){ super( firstname, lastname, code, address, city, tk, phone, email, deptcode, card, hours, cat ); this.orderno=orderno; setBaseSalary( salary ); getSalary(orders); ///////////////// } </code></pre> <p>Later, I use that array to calculate the bonus a salesman gets, depending on the amount of sales he makes.</p> <p>In <code>main</code>, I created an array of type <code>Employee</code>:</p> <pre><code>Employee employees[] = new Employee[ 7 ]; employees[ 0 ] = salary1; employees[ 1 ] = salary2; employees[ 2 ] = salary3; employees[ 3 ] = partt1; employees[ 4 ] = partt2; employees[ 5 ] = sales1; employees[ 6 ] = sales; </code></pre> <p>where each row is a different type of employee (salary = full-time, partt = part-time, and sales = salesman).</p> <p>My problem is that I want to print the orders of each salesman using the employees array. What I've done so far is</p> <pre><code>for (int i=5;i&lt;employees.length;i++){ System.out.printf("Orders of Salesman: %S %S", employees[i].getName(),employees[i].getSurname()); System.out.printf(" Total amount(money) from orders: %,.0f ", employees[i].earnings()); int j=0; ((Salesman)employees[i]).getOrderNo(arr) ; //((Orders)employees[i]).getOrderNo(); System.out.printf("ordernumber: %d orderdate:%s description: %s order amount(money): %,.0f\n "); } </code></pre> <p>The problem comes here:</p> <p><code>System.out.printf("ordernumber: %d orderdate:%s description: %s order amount(money): %,.0f\n ");</code></p> <p>How do I access the orders array inside of the Salesman object on employees array? I have tried casting, but it won't work because Orders is not a subclass of Employee.</p> <p>I need it to print, for example,</p> <blockquote> <p>Orders of Salesman: john koun</p> <p>Total amount of orders: 13000 Orders per Salesman</p> <p>Order number: 1 order date: 10/2/2010 description: machinery sale order amount: 12000</p> <p>Order number: 2 order date: 20/2/2010 description: sales of parts order amount: 1000</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload