Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中的银行删除帐户_Java_Account_Bank - Fatal编程技术网

java中的银行删除帐户

java中的银行删除帐户,java,account,bank,Java,Account,Bank,你好,我正在用Java制作一个银行应用程序 我的问题是我不知道如何删除创建的帐户 当我创建一个新帐户时,它会得到一个新号码,但如果有3个帐户,我想删除帐号2,我不知道如何删除一个帐户,当我想创建一个新帐户时,它会得到ID号2。(有数字1和3) 当我想创建另一个帐户时,我想将其ID更改为4 提前感谢。:) import java.util.Scanner; 公共类BankApp{ 公共静态void main(字符串[]args){ 扫描仪s=新的扫描仪(System.in); 银行myBank=新

你好,我正在用Java制作一个银行应用程序

我的问题是我不知道如何删除创建的帐户

当我创建一个新帐户时,它会得到一个新号码,但如果有3个帐户,我想删除帐号2,我不知道如何删除一个帐户,当我想创建一个新帐户时,它会得到ID号2。(有数字1和3)

当我想创建另一个帐户时,我想将其ID更改为4

提前感谢。:)

import java.util.Scanner;
公共类BankApp{
公共静态void main(字符串[]args){
扫描仪s=新的扫描仪(System.in);
银行myBank=新银行();
int user_choice=2;
做{
//向用户显示菜单
//询问用户的选择并验证它(确保它介于1和6之间)
System.out.println();
System.out.println(“1)开立新的银行账户”);
系统输出打印(“2)存入银行账户”);
系统输出打印(“3)提取到银行账户”;
System.out.println(“4)打印短期账户信息”);
System.out.println(“5)打印详细的账户信息,包括最近的交易”);
系统输出打印(“6)退出”);
System.out.println();
系统输出打印(“输入选项[1-6]:”;
user_choice=s.nextInt();
开关(用户选择){
案例1:System.out.println(“输入客户名称”);
字符串cn=s.next();
系统输出打印项次(“输入期初余额”);
双d=s.nextDouble();
System.out.println(“账户已创建,其编号如下:“+myBank.openNewAccount(cn,d));
打破
案例2:System.out.println(“输入帐号”);
int an=s.nextInt();
System.out.println(“输入存款金额”);
双da=s.nextDouble();
我的银行存款给(安,da);
打破
案例3:System.out.println(“输入帐号”);
int acn=s.nextInt();
System.out.println(“输入提取金额”);
双wa=s.nextDouble();
myBank.提款(华盛顿州acn);
打破
案例4:System.out.println(“输入帐号”);
int anum=s.nextInt();
myBank.printAccountInfo(anum);
打破
案例5:System.out.println(“输入帐号”);
anum=s.nextInt();
myBank.printTransactionInfo(anum);
打破
默认值:System.out.println(“无效选项,请重试”);
}
}
while(用户选择!=“6”);
}
静态类库{
私人银行账户[]账户;//此银行的所有银行账户
private int numOfAccounts;//此银行的银行帐户数
//构造函数:一个新的Bank对象最初并不™不包含任何帐户。
公共银行(){
账户=新银行账户[100];
numOfAccounts=0;
}
//使用给定的客户名称和期初余额作为参数创建新的银行帐户
//和返回此新帐户的帐户号。它还将此帐户添加到帐户列表中
//银行调用对象的名称。
public int openNewAccount(字符串customerName,双重openingBalance){
银行账户b=新银行账户(客户名称、期初余额);
账户[numOfAccounts]=b;
NUMOF++;
返回b.getAccountNum();
}
//从给定帐号的帐户中提取给定金额。如果该帐户为
//在银行不可用,它应该打印一条消息。
公共作废取款(int accountNum,双倍金额){

对于(int i=0;i)也许你应该考虑重新思考这个逻辑。 但是,解决方案可以是标记要删除的银行帐户,并添加一个字段,例如:

class BankAccount{
...
private boolean isMarkedForDeletion = false;
...
}
再加上

现在您可以:

1) 删除账户时,将该字段切换为true。

public int closeAccount(int id) {
boolean isAccountFound = false;
int i = 0;
while(i < numOfAccounts && isAccountFound == false)
{
  // if() // Add here the condition to find the account to delete.
  // { accounts[i].isMarkedForDeletion = true; } 
}
public int closeAccount(int-id){
布尔值isAccountFound=false;
int i=0;
而(i
现在,不再对该帐户执行存款或其他操作

2) 创建新帐户时,循环检查每个现有帐户,并验证帐户是否标记为删除-可能使用while循环。 如果一个帐户被发现为“markedfordeletion=true”,那么您将重新声明当前迭代的元素为一个新帐户,该帐户具有与旧帐户相同的id

public int openNewAccount(String customerName, double openingBalance) {
BankAccount b = new BankAccount(customerName, openingBalance);

boolean isAccountFound = false;
int i = 0;
while(isAccountFound == false && i < accounts.length)
{
  // if() add conditions for this account to replace the old one.
  // hint: use the isMarkedForDeletion field!
  // Finally add the new value assignment ( accounts[i] = ... )
  // return the account number. 
}
// This will happen in the case there were no matches.
accounts[numOfAccounts] = b;
numOfAccounts++;
return b.getAccountNum();
}
public int openNewAccount(字符串customerName,双重openingBalance){
银行账户b=新银行账户(客户名称、期初余额);
布尔值isAccountFound=false;
int i=0;
while(isAccountFound==false&&i
重复使用帐号原则上是错误的,但如果您坚持这样做:

您可能希望将帐户标记为活动或非活动。创建帐户时,将其标记为活动。删除帐户时,将其标记为非活动

当程序决定“新”帐户的帐号时,首先获取所有非活动帐户的有序列表并选择第一个。如果没有任何非活动帐户,则使用递增的numOfAccounts变量。

public int openNewAccount(String customerName, double openingBalance) { BankAccount b = new BankAccount(customerName, openingBalance); boolean isAccountFound = false; int i = 0; while(isAccountFound == false && i < accounts.length) { // if() add conditions for this account to replace the old one. // hint: use the isMarkedForDeletion field! // Finally add the new value assignment ( accounts[i] = ... ) // return the account number. } // This will happen in the case there were no matches. accounts[numOfAccounts] = b; numOfAccounts++; return b.getAccountNum(); }
public int openNewAccount(String customerName, double openingBalance) {
    for (int i = 0; i < 100; i++) {
        if (accounts[i] == null) { //Empty slot
            BankAccount b = new BankAccount(customerName, openingBalance);
            b.accountNum = i + 1;
            break;
        }
    }
    return b.getAccountNum();
}
public void deleteAccount(int accountNo) {
    accounts[accountNo - 1] = null;
}