银行帐户中帐号的新类

  • 本文关键字:帐号 新类 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("nnCharges applied successfully.");
}
/**
 * Prints an account information - Account number, type, balance, and current number of transactions
 */
public void print(){
    System.out.println("nnAccount Number: " + acctNum  + "nAccount Type: " + getAcctType() + 
                "nBalance: " + balance + "nCurrent Number of Transactions: " + currentNumberOfTransactions);
}

}

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

public class BankAccounts {  
    public static class AccountNumber {
        // your class declaration
    }
    // other methods/properties
}

如果任何其他类的对象需要访问您的AccountNumber实例,那么您可以将其公开。如果不是这样,那么你可以把它保密。这样,只有BankAccounts成员才能实例化AccountNumber对象。

从您的示例程序来看,您的帐号似乎非常简单(只是一个int)。对于如此简单的事情,创建一个单独的类是过分的。

相关内容

最新更新