Tuesday 15 June 2010

Passing array elements between different classes in Java -


I am having trouble crossing elements in an array that places the name and number in the second class where Will be calculated. This is what I have for classes Public Class Employees {Private Ent Sales; The name of the private string; Public employee (string name, int sale) {this.name = name; This.sales = sale; } Public string getName () {this.name; } Public int getSales () {Return this sale. }}

To help name and sale of that category for the array. Here's the array in its different class.

  import java.util.Scanner; Public Sector Employee {Public Fixed Entrepreneur () {Scanner input = New scanner (System.in); Int numEmp; System.out.println ("Enter how employees can be compared:"); NumEmp = input.nextInt (); Input.nextLine (); Employee [] employee = new employee [number ampe]; {System.out.print ("Enter your name:") for (Int i = 0; I & lt; numEmp; i ++); String employee name = input.nextLine (); Println (); System.out.print ("Enter your annual sales:"); Intel Employees = Input Xx (); Input.nextLine (); Println (); Employee employee = new employee (employee name, employees); Employee [i] = employee; } Employee Maxellar = employee [0]; For (Int i = 0; I & lt; employee; Length; i ++) {System.out.println ("Employee's Name:" + employee [i] .getName ()); System.out.println ("Total Sales: $" + Staff [i] .getSales ()); Println (); If (Maxcelor.Gatesless) employee [i] .getSales ()) {maxSeller = employee [i]; }} System.out.println (); System.out.println ("The top seller is:" + maxseller.getName ()); System.out.println ("Top Sales are: $" + maxSeller.getSales ()); Return maxseller.getSales (); }}   

array holds the value and calls the top seller a maximum seller and receives the name and sales amount for them. The problem I am facing now is that in a different class I have to get some values ​​stored in this array to calculate something. I want to get MaxSeller's sale and compare them to the array for sale of other vendors. I am trying to demonstrate the form that how much more sales would be required for others, they need to move ahead or beyond the top vendor. This is the problem in which I am walking in. I do not know how to pass the values ​​from this array to the next, so I can calculate. Any ideas?

Instead of creating an array inside a static method, you can call it employee then you In a stable context, the array can be reused (not recommended) or to keep different employee arrays in order to maintain the various arrays of employees, it makes more sense in OOP. A coarse sketch:

  import java.util.Scanner; Public Sector Employee {Private employee [employee]; Public Employee [] Employee () {Return employees; } Public Zero Set Apache () {Scanner input = New scanner (System.in); Int numEmp; System.out.println ("Enter how employees can be compared:"); NumEmp = input.nextInt (); Input.nextLine (); Employee = new employee [numEmp]; {System.out.print ("Enter your name:") for (Int i = 0; I & lt; numEmp; i ++); String employee name = input.nextLine (); Println (); System.out.print ("Enter your annual sales:"); Intel Employees = Input Xx (); Input.nextLine (); Println (); Employee employee = new employee (employee name, employees); Employee [i] = employee; }} Public Ent MXSellerEmpires () {employee Maxellar = employee [0]; For (Int i = 0; I & lt; employee; Length; i ++) {System.out.println ("Employee's Name:" + employee [i] .getName ()); System.out.println ("Total Sales: $" + Staff [i] .getSales ()); Println (); If (Maxcelor.Gatesless) employee [i] .getSales ()) {maxSeller = employee [i]; }} System.out.println (); System.out.println ("The top seller is:" + maxseller.getName ()); System.out.println ("Top Sales are: $" + maxSeller.getSales ()); Return maxseller.getSales (); } Public static zero main (string [] args) {EmployeeArray google = new EmployeeArray (); Google.setUpArray (); Employee Array MS = New Employee (); Ms.setUpArray (); Employee Calculator and Manipulator eGoogle = New Employee, Rare Calculator and Manipuiter (); EmployeeAreacle andManipulator EMS = new employeeArralkular and manipulator (); EGoogle.doSomethingWithArray (google.getEmployees ()); EMS.doSomethingWithArray (ms.getEmployees ()); }}    

No comments:

Post a Comment