java-Small exercise Simple banking procedure

Source: Internet
Author: User

2 Practice 1 : Create a simple bank package

Practice target object-oriented encapsulation in-java language and the use of constructors.

Task

In this exercise, create a simple version of the account class. Place the source file into the banking package. In the default package for creating a single account, a test program testbanking has been written. This test program initializes the account balances and performs several simple things to handle. Finally, the test program displays the final balance of the account.

1. Create a banking package

2. Create the account class under the banking package. The class must implement the model in the UML block diagram above.

    1. Declares a private object property: Balance, which retains the current (or immediate) balance of the bank account.
    2. Declares a public constructor with a parameter (init_balance) that assigns a value to the Balance property.
    3. Declares a public method getbalance, which is used to get the current balance.
    4. Declares a public method deposit, which increases the amount to the current balance.
    5. Declares a public method withdraw subtracts the amount from the current balance.

3. Compile the Testbanking.java file.

4. Run the Testbanking class. You can see the following output results:

Creating an account with a 500.00 balance

Withdraw 150.00

Deposit 22.50

Withdraw 47.62

The account has a balance of 324.88

Practice 2

Practice goal-Use member variables of reference types: In this exercise, you will extend the bank project by adding a (Customer Class) customer class. The customer class will contain an account object.

Task

    1. Create the customer class under the banking package. The class must implement the model in the above UML diagram.

A. Declare three private object properties: FirstName, LastName, and account.

B. Declare a public constructor with two parameters representing the properties of the object (F and L)

C. Declare two public accessors to access the object properties, and the methods Getfirstname and Getlastname return the corresponding properties.

D. Declare the Setaccount method to assign a value to the Account property.

E. Declare the Getaccount method to get the account property.

    1. In the Exercise2 home directory, compile and run the Testbanking program. You should see the following output:

Creating the customer Jane Smith.

Creating her account with a 500.00 balance.

Withdraw 150.00

Deposit 22.50

Withdraw 47.62

Customer [Smith, Jane] has a balance of 324.88

Practice 3 : Modify Withdraw Method

Practice goal-use a method with a return value: In this exercise, you will modify the withdraw method to return a Boolean value to indicate whether the transaction was successful.

Task

1. Modify Account Class

    1. Modify the deposit method to return True (meaning that all deposits are successful).
    2. Modify the withdraw method to check whether the number of withdrawals is greater than the balance. If Amt is less than balance, the number of withdrawals is deducted from the balance and returns true, otherwise the balance returns false.

2. After compiling and running the testbanking program in the Exercise2 home directory, you will see the following output;

Creating the customer Jane Smith.

Creating her account with a 500.00 balance.

Withdraw 150.00:true

Deposit 22.50:true

Withdraw 47.62:true

Withdraw 400.00:false

Customer [Smith, Jane] has a balance of 324.88

Practice 4 : Array representation of multiplicity

Practice goal-Use an array in a class as a simulated collection operation: In this exercise, you will use arrays to implement multiple relationships between banks and customers.

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 an array of customer objects. Also maintain an integer attribute to track how many customers the bank currently has.

    1. Create Bank Class

    1. Add two properties to the Bank class: Customers (an array of customer objects) and Numberofcustomers (integer, which 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 1 to 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 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

Practice 4 : 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 Class

    1. Add two properties to the Bank class: Customers ( Listof Customer objects) and Numberofcustomers (integer, number of current customer objects)

    1. Add the public constructor to initialize the customersList.

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

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

First small question:

 Packagecom.banking; Public classAccount {//Properties    Private Doublebalance; //Construction MethodAccount (intbalance) {         This. balance=balance; }         PublicAccount () {Super(); }    //member Methods     Public DoubleGetBalance () {return  This. Balance; }     Public DoubleDepositDoubleamt) {          This. balance+=Amt; returnAmt; }         Public DoubleWithdraw (Doubleamt) {         This. balance-=Amt; returnAmt; }}

Test:

 Package com.banking;  Public class Test01 {    publicstaticvoid  main (string[] args) {account            ac= New Account ($);        System.out.println ("withdrawal Amount:" +ac.withdraw ());        System.out.println ("Deposit Amount:" +ac.deposit (22.5));        System.out.println ("withdrawal Amount:" +ac.withdraw (47.62));        System.out.println ("Remaining Amount:" +ac.getbalance ());}                }

Results:

Second small question:

 Packagecom.banking; Public classCustomer {PrivateString FirstName; PrivateString LastName; PrivateAccount Account ; Customer (String f,string l) { This. firstname=F;  This. lastname=l; } @Override PublicString toString () {returnFirstName + "," +LastName; }     PublicAccount Getaccount () {returnAccount ; }     Public voidSetaccount (Account acct) { This. Account =Acct; }     PublicString Getfirstname () {returnFirstName; }     PublicString Getlastname () {returnLastName; }}

Test:

 Packagecom.banking; Public classTest02 { Public Static voidMain (string[] args) {Customer cu=NewCustomer ("Jane", "Smith"); System.out.println ("Creating the Customer" + "" +cu.getfirstname () + "" +cu.getlastname ()); Cu.setaccount (NewAccount (500));//Cu.setaccount (AC);//Cu.getaccount ();SYSTEM.OUT.PRINTLN ("Withdrawal Amount:" +cu.getaccount (). Withdraw (150)); System.out.println ("Deposit Amount:" +cu.getaccount (). Deposit (22.5)); System.out.println ("Withdrawal Amount:" +cu.getaccount (). Withdraw (47.62)); System.out.println ("Remaining Amount:" +Cu.getaccount (). GetBalance ()); }}

Results:

The four little questions:

 Packagecom.banking;Importjava.util.ArrayList;Importjava.util.List; Public classBank {//Defining Properties    PrivateList<customer>customer; Private intnumberofcustomers; //Constructors     PublicBank () {Customer=NewArraylist<>(); }    //define methods, add data     Public voidAddcustomer (String f,string l) {Customer cu=NewCustomer (f,l);    Customer.add (CU); }    //Get Array number     Public intgetnumofcustomers () {returncustomer.size (); }         PublicCustomer GetCustomer (intindex) {        returnCustomer.get (index); }}

Test:

 Packagecom.banking;ImportJava.util.Iterator;Importjava.util.List; Public classTestbank { Public Static voidMain (string[] args) {Bank ba=NewBank (); Ba.addcustomer ("Simms", "Jane"); Ba.addcustomer ("Bryant", "Owen"); Ba.addcustomer ("Soley", "Tim"); Ba.addcustomer ("Soley", "Maria.");  for(intI=0;i<ba.getnumofcustomers (); i++) {System.out.println ("Customer" + (i+1) + "is" +Ba.getcustomer (i)); } System.out.println ("Current number of customers =" +ba.getnumofcustomers ()); }}

Results:

java-Small exercise Simple banking procedure

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: info-contact@alibabacloud.com 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.