java 程式.....請幫幫忙


I. Description

An inner class is a class defined within another top-level class, inner class, or a method when the inner class is only used by the enclosing class or method. Using inner classes can increase the degree of encapsulation and provide logical grouping. Therefore, whenever a class is used served as helper class of another class, the former can be designed as an inner class enclosed by the latter. In this assignment, you can practice the inner class design and programming by implementing a part of banking system.

II. Requirements

In this assignment, a CKAccount class should be designed to represent a bank checking account. Each CKAccount should include a account number, account name, account balance and transaction records. These attributes are described as follows.

A.account number: The form of an account number is 101XXXXXXX, where

XXXXXXX is a sequence number associated with the order by which the account is created.

B.account name: Each account name represents the full name of an account owner.

C.account balance: A float type real number represents the current of an account.

D.transaction records: Each account should keep the last three transactions.

A protected inner class Transaction, enclosed by CKAccount, should be designed to represent the details of a transaction. Each Transaction should include a date and time at which the transaction is processed, transaction types (including “CREATE”, “DEPOSIT”, “WITHDRAW”), transaction amount and final balance.

Constructing a CKAccount is treated as a “CREATE” transaction. A “CREATE” transaction takes two parameters: the account name and the initial deposit. The account number is automatically generated based on rule described above. Each CKAccount provides three services: deposit, withdraw, showTransaction and showBalance. The deposit service takes a float parameter as the amount deposi


as the amount depositing to the account. The withdraw service takes a float parameter as the amount to be withdraw from the account. The showTransaction service display the last three transactions performed on the account. The information of each transaction must be showed as the following order.

2 個已更新項目:

The showBalance service shows the balance information of the account as the

following format.

A Transaction provides the service toString converting the object to a String

3 個已更新項目:


To test your CKAccount class, you need to develop a Java console application, named Banking. Assume that the Banking application can handle only one account. The Banking application first show a message “Choose Transaction Type:” to ask users type in one of the five possible transaction

4 個已更新項目:

“CREATE”, “DEPOSIT”, “WITHDRAW”, “TRANSACTION” and “BALANCE”. The first selected type must be “CREATE”; otherwise, the application will show “You need to create an account first” and wait for another type.

When the “CREATE” type is selected, the application prompts “Account name:” and


5 個已更新項目:

“Initial deposit amount:”, respectively, to get the data.

When the “DEPOSIT” or “WITHDRAW” type is chosen, the application prompts “Amount:” to get transaction amount. If the balance after performing the transaction is zero, the application will show “No enough balance, transaction aborted” and

6 個已更新項目:

the transaction is aborted.

When the “TRANSACTION” or “BALANCE” is selected, the application will show the details of the last three transactions or the account balance.

1 個解答

  • 7 年前


    2013-04-09 12:28:08 補充:


    import java.util.*;

    class CKAccount {

    String ID, name;

    float balance;

    static int x = 1;

    ArrayList<Transaction> ts = new ArrayList<Transaction>();

    protected class Transaction {

    String dt;

    final static int TYPE_CREATE = 0, TYPE_DEPOSIT = 1, TYPE_WITHDRAW = 2;

    final String[] typeString = {"CREATE", "DEPOSIT", "WITHDRAW"};

    int type;

    float amount, balance;

    Transaction(int type, float a, float b) {

    dt = Calendar.getInstance().getTime().toString();

    this.type = type;

    amount = a;

    switch (type) {

    case TYPE_CREATE:

    balance = a;


    case TYPE_DEPOSIT:

    balance = a + b;



    balance = b - a;




    public String toString() {

    return "=== Transaction ===" + "\nDate & Time: " + dt +

    "\nTYPE: " + typeString[type] + "\nAmount: " + amount +

    "\nFinal Balance: " + balance;



    CKAccount(String n, float f) {

    name = n;

    ID = "101" + String.format("%07d", x++);

    Transaction t = new Transaction(Transaction.TYPE_CREATE, f, balance);

    balance = t.balance;



    void deposit(float f) {

    Transaction t = new Transaction(Transaction.TYPE_DEPOSIT, f, balance);

    balance = t.balance;



    void withdraw(float f) {

    if (balance < f) {

    System.out.println("No enough balance, transaction aborted");



    Transaction t = new Transaction(Transaction.TYPE_WITHDRAW, f, balance);

    balance = t.balance;



    void showTransactions() {

    if (ts.size() == 0) {

    System.out.println("No transcation.");



    int cnt = 1;

    while (cnt <= 3) {

    if (ts.size() - cnt < 0) break;

    System.out.println(ts.get(ts.size() - cnt++));



    void showBalance() {

    System.out.println("Balance: $" + balance);




    • Commenter avatar登入以對解答發表意見