Using arrays to represent multiplicity

Source: Internet
Author: User

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.


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.

    1. Create Bank class

    1. to be The Bank class adds two properties:Customers ( An array of Customer objects ) and numberofcustomers ( integer that tracks the next Customers array index )

    1. Add the public constructor to initialize the customers array with the appropriate maximum size (at least greater than 5) .

    1. 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.

    1. adds The Getnumofcustomers access method, which returns the numberofcustomers property value.

    1. Add the GetCustomer method. It returns the customer associated with the given index parameter.

    1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.