gpt4 book ai didi

java - 银行账户帐号的新类

转载 作者:太空宇宙 更新时间:2023-11-04 15:23:52 26 4
gpt4 key购买 nike

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

现在我知道下面的程序尚未完成,但我想将帐号移到类(class)之外。对于将帐号移到类(class)之外,我不知道这是否意味着我必须创建一个全新的类(class)。我知道一个程序每个java文件可以有多个类,但只有一个类可以是公共(public)的,我只是不知道我是否真的需要它。我应该将我的帐号的新类别设置为私有(private)吗?如果我这样做了,我可以把它放在最后吗?我不知道该怎么办!很抱歉,我经常问我的教授,但他总是绕着这个话题,从不回答我有关类(class)的问题!

我是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);
}



}

最佳答案

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

public class BankAccounts {  

public static class AccountNumber {
// your class declaration
}

// other methods/properties
}

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

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

关于java - 银行账户帐号的新类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20079234/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com