Practice : With List indicates multiplicity
Practice goal-Use list as a simulated collection operation in a class: in this exercise, you will use list to achieve multiple relationships between the bank and the customer.
Task: for banks, you can add bank classes. the Bank Object tracks the relationship between itself and its customers. This aggregated relationship is implemented with the List of the Customer object . Also maintain an integer attribute to track how many customers the bank currently has
1. Create Bank
2. The Bank class adds two properties:Customers ( Listof Customer objects) and numberofcustomers ( Integer, Number of current Customer objects )
3. add the public constructor and initialize the customersList.
4, Add addcustomer method. The method must construct a new customer object according to the parameter (surname, first name) and then put it in the customerlist .
5. Add The Getnumofcustomers access method, which returns the numberofcustomers property value.
6, Add getcustomer method. It returns the customer associated with the given index parameter.
7, compile and run the 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
Current number of customers = 4
Importjava.util.ArrayList;Importjava.util.List; Public classbank{PrivateList<customer> customers;//Customers Collection Private intNumberofcustomers;//record number of customers PublicBank () {//constructor method, initialize customerscustomers=NewArraylist<customer>(); } Public voidAddcustomer (String f,string l) {//method: Add the Customer object to the customersCustomer p=NewCustomer (f,l); Customers.add (P); } Public intGetnumofcustomers () {//get the number of customers, that is, the length of customersnumberofcustomers=customers.size (); returnnumberofcustomers; } PublicCustomer GetCustomer (intIndex) {//value based on indexreturnCustomers.get (Index);
}
}
Public classtestbanking { Public Static voidMain (string[] args) {Bank cus=NewBank () {}; Cus.addcustomer ("Simms", "Jane"); Cus.addcustomer ("Bryant", "Owen"); Cus.addcustomer ("Soley", "Tim"); Cus.addcustomer ("Soley", "Maria."); for(intI=0;i<cus.getnumofcustomers (); i++) {System.out.println ("Customer" + (i+1) + "is" + "" +Cus.getcustomer (i)); } System.out.println ("Current number of customers =" +cus.getnumofcustomers ()); System.out.println ("The second customer is:" +cus.getcustomer (2)); }}
Operation Result:
Customer class:
Public classCustomer {//declaring private properties PrivateString FirstName; PrivateString LastName; PrivateAccount Account ; //declaring ConstructorsCustomer () {}; Customer (String f,string l) { This. firstname=F; This. lastname=l; } //declaration accessor Getter/setter Method PublicString Getfirstname () {//FirstName returnFirstName; } Public voidsetfirstname (String firstName) { This. FirstName =FirstName; } PublicString Getlastname () {//LastName returnLastName; } Public voidsetlastname (String lastName) { This. LastName =LastName; } //accessor Getter/setter method--account PublicAccount Getaccount () {returnAccount ; } Public voidSetaccount (Account acct) { This. account=Acct; } @Override PublicString toString () {returnFirstName + "," +LastName; }}
Java Collection Exercise--bank