Using arrays to represent multiplicity
Practice goal-use an array as an analog collection operation in a class: in this exercise, you will use arrays to achieve multiple relationships between banks and customers.
Task
for banks, you can add Bank class. the Bank Object tracks the relationship between itself and its customers. This aggregated relationship is implemented with an array of Customer objects. Also maintain an integer attribute to track how many customers the bank currently has.
- Create Bank class
- to be The Bank class adds two properties:Customers ( An array of Customer objects ) and numberofcustomers ( integer that tracks the next Customers array index )
- Add the public constructor to initialize the customers array with the appropriate maximum size (at least greater than 5) .
- Add the Addcustomer method. The method must construct a new customer object according to the parameter (surname, first name) and then put it in the customer array. You must also add 1to the value of the numberofcustomers property.
- adds The Getnumofcustomers access method, which returns the numberofcustomers property value.
- Add the GetCustomer method. It returns the customer associated with the given index parameter.
- Compile and run Testbanking Program. You can see the following output results:
Customer [1] is Simms,jane
Customer [2] is Bryant,owen
Customer [3] is Soley,tim
Customer [4] is Soley,maria
Package Banking;import java.lang.reflect.array;import java.util.iterator;import java.util.List; Public classbank{//member Properties PrivateString customers[]; Private intNumberofcustomers =1 ; //Construction Method PublicBank () {} PublicBank (String customers[]) { This. Customers =Newstring[6] ; This. Customers =customers; } Publicstring[] Addcustomer (String firstName, String lastName) {System. out. println ("customers["+numberofcustomers+"]"+" is"+firstname+" , "+lastName); Numberofcustomers++ ; returncustomers; } Public intgetnumberofcustomers () {returnnumberofcustomers; } }
Bank BK=NewBank (); Bk.addcustomer ("Simms","Jane" ); Bk.addcustomer ("Bryant","Owen" ); Bk.addcustomer ("soley","Tim" ); Bk.addcustomer ("soley","Maria");
Using arrays to represent multiplicity