银行账户中新账号

时间:2013-11-19 18:28:39

标签: java

我正在做一个增强的BankAccount类,以了解抽象和封装如何实现对软件的进化改变。

现在我知道我下面的程序还不完整,但我想把帐号移到课外。 如果这意味着我必须创建一个全新的类,那么为了在课程之外移动帐号我就不会得到。我知道一个程序每个java文件可以有多个类,但只有一个可以公开,我只是不知道我是否真的需要它。我应该将我的帐号的新课程设为私人吗?如果我这样做,我可以把它扔到最后?我不知道怎么回事!对不起,我经常问我的教授,但他围绕这个话题反弹,从不回答我关于课程的问题!

我是java的新手,所以不要给我整个代码更正!我想学习

以下是老师为我提供的课程(不完整):

import java.util.Random;

/**
 * Bank Account Class
 * @author Morgan
 * @version 1.1
 *
 */
public class BankAccounts {

//data members
private double balance;    // account balance
private int acctNum;    // account number
private byte acctType;    // types of account: 1 for Checking; 2 for savings
private int currentNumberOfTransactions;    // current number of transactions per month
private double perTransactionFlatFee;    // charge per transaction - depends on type of account

private static Random generator = new Random(System.currentTimeMillis());    //create random number generate object

private final static int CHECKING_MAX_NUMBER_OF_TRANSACTIONS_PER_MONTH = 5;
private final static int SAVINGS_MAX_NUMBER_OF_TRANSACTIONS_PER_MONTH = 2;

/**
 * default constructor - initializes account balance and account number
 */
private BankAccounts(){
    balance = 0.0;        // initialize account balance

    // generate random number accordingly and assign to account number
    acctNum = generator.nextInt(1000) + 9999;    
    currentNumberOfTransactions = 0;    // initialize 
    perTransactionFlatFee = 0.0;        // initialize    
}

/**
 * constructor with current balance; initialize account number
 * @param balance    initial account balance
 * @param type        type of account (1 for Checking; 2 for Savings)
 */
    public BankAccounts(double balance, byte type, int acctNum?){
        this();    // use default constructor

        this.balance = balance;    // set initial balance
        acctType = type;        // set account type

        switch (type){
        case 1:    perTransactionFlatFee = 0.10; break;
        case 2: perTransactionFlatFee = 0.20; break;
        }
    }

/**
 * constructor with account number; initialize balance
 * @param type    type of account (1 for Checking; 2 for Savings)
 */
    public BankAccounts(byte type){
        this();    // use default constructor

        acctType = type;        // set account type

        switch (type){
        case 1:    perTransactionFlatFee = 0.10; break;
        case 2: perTransactionFlatFee = 0.20; break;
        }

    }


/**
 * getter to return current balance        
 * @return    current account balance
 */
public double getBalance() {
    return balance;
}

/**
 * deposit into account
 * @param amount    amount to deposit; not more than 10,000
 * @return            true or false
 */
public void deposit(double amount){
    // deposit amount - deduct per transaction fee based on type of account

}

/**
 * withdraw from account
 * @param amount    amount to withdraw; not more than 10,000
 * @return            true or false
 */
public void withdraw(double amount) {
    // withdraw amount - deduct per transaction fee based on type of account

}


private String getAcctType() {
    String ret = "";

    switch(acctType){
    case 1: ret = "Checking"; break;
    case 2: ret =  "Savings"; break;
    }

    return ret;
}

/**
 * Resets the current number of transactions to 0
 * @param bankAccount    Account to reset current number of transactions
 */

public static void reSetAccount(BankAccounts bankAccount){
    bankAccount.currentNumberOfTransactions = 0;
}

/**
 * Deducts the applicable monthly charges from the account balance.
 *             May lead to negative balance.
 * @param bankAccount    Account to apply the monthly charge
 * @param chargeAmt        Amount to charge if maximum allowable transactions have been exceeded
 */

public static void deductMonthlyCharges(BankAccounts bankAccount, double chargeAmt){
   // deduct from balance the chargeAmt if applicable maximum allowable transactions have been exceeded
        balance = balance - chargeAmt


    // reset current number of transactions
        //add my code here


    System.out.println("\n\nCharges applied successfully.");
}

/**
 * Prints an account information - Account number, type, balance, and current number of transactions
 */
public void print(){
    System.out.println("\n\nAccount Number: " + acctNum  + "\nAccount Type: " + getAcctType() + 
                "\nBalance: " + balance + "\nCurrent Number of Transactions: " + currentNumberOfTransactions);
}



}

1 个答案:

答案 0 :(得分:0)

对于您的情况,我建议将AccountNumber设为静态嵌套类BankAccount -

public class BankAccounts {  

    public static class AccountNumber {
        // your class declaration
    }

    // other methods/properties
}

如果任何其他类的对象需要访问您的AccountNumber实例,那么您可以将其公开。如果不是这种情况,那么您可以将其设为私有。这样,只有BankAccounts个成员才能实例化AccountNumber个对象。

在您的示例程序中,您的帐号似乎非常简单(仅int)。对于这么简单的事情,创建一个单独的类是过度的。