简体   繁体   中英

How can I access an object array located inside of another object which is then inside a superclass array?

I have an abstract class Employee , and based on that, I created the following subclasses:

FulltimeEmployee
PartTimeEmployee
Salesman

as well as one standalone class, Orders .

I use the Orders class to "describe the salesman" by sending an array of orders in the Salesman constructor:

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);
 /////////////////

 }

Later, I use that array to calculate the bonus a salesman gets, depending on the amount of sales he makes.

In main , I created an array of type Employee :

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;

where each row is a different type of employee (salary = full-time, partt = part-time, and sales = salesman).

My problem is that I want to print the orders of each salesman using the employees array. What I've done so far is

for (int i=5;i<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 ");

           }

The problem comes here:

System.out.printf("ordernumber: %d orderdate:%s description: %s order amount(money): %,.0f\\n ");

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.

I need it to print, for example,

Orders of Salesman: john koun

Total amount of orders: 13000 Orders per Salesman

Order number: 1 order date: 10/2/2010 description: machinery sale order amount: 12000

Order number: 2 order date: 20/2/2010 description: sales of parts order amount: 1000

your Salesman constructor accepts an Order[] orders but it doesn't look like you keep a reference to it anywhere (unless you do that in getSalary or in the commented out portion).

You will need to add something like

this.orders = orders;

in your Salesman constructor so you can refer to the array as a field/property of the Salesman object.

Typically you should use the Bean pattern whereby each field has a getter/setter method:

public Order[] getOrders(){
    return orders;
}

public void setOrders(Order[] orders){
    this.orders=orders;
}

then in your constructor add setOrders(orders);

and then in your debug/output code add:

Salesman salesman = (Salesman) employees[i];
for (Order order : salesman.getOrders())
    System.out.println(order); // i don't know what fields order has!

If what you're saying is that you have an array of Employee , and each Salesman (only) has a member that provides the orders himself has sold, then you have to work with an array of array.

At first view, your Employee array is more likely a vector, that is, a one dimensional array. Plus, your Salesman class has an Order array which also seems to be a vector. If this is what you actually got, you only have to iterate through your Salesman.Orders member to get the sales your Salesman did.

Since it is tagged as [homework] , I refuse to write the code and give you it all cooked for you.

Besides, here are some hints:

  1. You already iterate through an array of Employee , so you know how to iterate through one;

  2. You say that a Salesman derives from the Employee class, hence making Employee perhaps type-castable to Salesman , and then you shall get your instance of the Salesman class, it this actual Employee IS a Salesman , otherwise it should return null;

  3. Now, you access your fields through property methods (getters and setters methods);

So, how could you make Orders accessible from the outside of your class, and expose it as an array?

If you've been able to expose it as an array, how do you iterate through an array to get each of the orders amount so that you may calculate how much this instance of Salesman has sold?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM