简体   繁体   English

银行程序无法正常工作

[英]bank program not working as expected

I know that it is not a debugger site but I just wanted to ask what I am doing wrong in this piece of code. 我知道这不是调试器站点,但我只是想问一下这段代码在做什么。

When I run the program I first add a new account, then when I deposit or withdraw it says Wrong Password. 当我运行该程序时,我先添加一个新帐户,然后在存入或提取该帐户时显示错误密码。 Here is my code 这是我的代码

Code : 代码:

import java.io.*;
import java.util.Random;
public class BankA {
public static int NewRandom(int min, int max) {
    Random rand = new Random();
    int randomNum = rand.nextInt((max - min) + 1) + min;
    return randomNum;
}
public static void main(String args[])throws IOException, InterruptedException {
    InputStreamReader ir = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(ir);
    Bank myBank = new Bank();
    int Option = 1, Account_Number, Account_Password, atempts = 0, Pass;
    String Name;
    double Balance, Money;
    System.out.println("Please wait, the system is starting...");
    while(Option !=5) {
        Thread.sleep(4000);
        System.out.println("1) Open a new bank account");
        Thread.sleep(250);
        System.out.println("2) Deposit to a bank account");
        Thread.sleep(250);
        System.out.println("3) Withdraw to bank account");
        Thread.sleep(250);
        System.out.println("4) Print the detailed account information including last transactions");
        Thread.sleep(250);
        System.out.println("5) Quit");       
        System.out.println();      
        System.out.print("                       Enter Option [1-5]: ");
        Option = Integer.parseInt(br.readLine());
        switch(Option) {
            case 1 : System.out.println("Enter a customer name :");
                     Name = br.readLine();
                     System.out.println("Enter a opening balance :");
                     Balance = Double.parseDouble(br.readLine());
                     Thread.sleep(250);
                     System.out.println("Creating your account....");
                     Thread.sleep(500);
                     System.out.println("Account Has been created\n Account number: " + myBank.AddNewAccount(Name, Balance)[0]+"\nYour password : "+ myBank.AddNewAccount(Name, Balance)[1]);
                     break;
            case 2 : System.out.println("Enter a account number :");
                     Account_Number = Integer.parseInt(br.readLine());
                     System.out.println("Enter a account password :");
                     Account_Password = Integer.parseInt(br.readLine());
                     System.out.println("Enter a deposit amount :");
                     Money = Double.parseDouble(br.readLine());
                     myBank.Deposit(Account_Number, Account_Password, Money);
                     break;
            case 3 : System.out.println("Enter a account number :");
                     Account_Number = Integer.parseInt(br.readLine());
                     System.out.println("Enter a account password :");
                     Account_Password = Integer.parseInt(br.readLine());
                     System.out.println("Enter a deposit amount :");
                     Money = Double.parseDouble(br.readLine());
                     myBank.Withdraw(Account_Number, Account_Password, Money);
                     break;
            case 4 : System.out.println("Enter a account number :");
                     Account_Number = Integer.parseInt(br.readLine());
                     System.out.println("Enter a account password :");
                     Account_Password = Integer.parseInt(br.readLine());
                     myBank.Transactions(Account_Number, Account_Password);
                     break;
            case 5 : System.out.println("Please Enter your password :");
                     Pass = Integer.parseInt(br.readLine());
                     if(Pass == myBank.Password) {
                         System.out.println("                       System shutting down.....");
                         Option = 5;
                         break;
                     } 
                     else {
                         Thread.sleep(250);
                         System.out.println("You have enter a wrong password. Please try again");
                         Option = 0;
                     }
            default: System.out.println("Invalid option. Please try again.");
        }
    }
}
static class Bank {
    private int Password=2684;
    private BankAccount[] accounts;
    private int numOfAccounts;
    public Bank() {
        accounts = new BankAccount[100];
        numOfAccounts = 0;
    }
    public int [] AddNewAccount(String Name, Double Balance) {
        BankAccount b = new BankAccount(Name, Balance);
        accounts[numOfAccounts] = b;
        numOfAccounts++;
        int Acc = b.getAccountNum()[0];
        int Pass = b.getAccountNum()[1];
        int[]details = {Acc, Pass};
        return details;
    }
    public void Withdraw(int Account_Number, int pass, double Money) {
        for (int i =0; i<numOfAccounts; i++) {     
            int a = accounts[i].getAccountNum()[0];
            int p = accounts[i].getAccountNum()[1];
            if (Account_Number == a) {
                if( pass == p) {
                    accounts[i].withdraw(Money);
                    System.out.println("                       Amount withdrawn successfully");
                    return;
                } else {
                    System.out.println("Wrong Password");
                }
            }   
        }  
        System.out.println("                       Account number not found.");
    }
    public void Deposit(int Account_Number, int pass, double Money) {  
        for (int i =0; i<numOfAccounts; i++) {     
            int a = accounts[i].getAccountNum()[0];
            int p = accounts[i].getAccountNum()[1];
            if (Account_Number == a) {
                if( pass == p) {
                    accounts[i].withdraw(Money);
                    System.out.println("                       Amount deposited successfully");       
                    return;   
                } else {
                    System.out.println("Wrong Password");
                }
            }   
        }  
        System.out.println("                       Account number not found.");
    }
    public void Transactions(int Account_Number, int pass) {
        for(int i = 0;i<numOfAccounts; i++) {
            int a = accounts[i].getAccountNum()[0];
            int p = accounts[i].getAccountNum()[1];
            if (Account_Number ==  a ) {
                if( pass == p) {
                    System.out.println(accounts[i].getAccountInfo());
                    System.out.println("                        Last transaction: " + accounts[i].getTransactionInfo(accounts[i].getNumberOfTransactions()-1));
                    return;
                } else {
                    System.out.println("Wrong Password");
                }
            }
        }
        System.out.println("Account number not found.");
    }
}
static class BankAccount{
    private int User_Password;
    private int accountNum;
    private String customerName;
    private double balance;
    private double[] transactions;
    private String[] transactionsSummary;
    private int numOfTransactions;
    private  static int noOfAccounts=0;
    public String getAccountInfo(){          
        return "                        Account number: " + accountNum + "\n                        Customer Name: " + customerName + "\n                        Balance:" + balance +"\n";
    }
    public String getTransactionInfo(int n) {
        String transaction = transactionsSummary[n];
        return transaction;
        }
    public BankAccount(String abc, double xyz){        
        customerName = abc;        
        balance = xyz;        
        noOfAccounts ++;
        User_Password = NewRandom(1000, 9999);
        accountNum = NewRandom(800000000, 999999999);       
        transactions = new double[100];                         
        transactionsSummary = new String[100];               
        transactions[0] = balance;                      
        transactionsSummary[0] = "A balance of : Rs" + Double.toString(balance) + " was deposited.";       
        numOfTransactions = 1;             
    }
    public int [] getAccountNum(){
        int account = accountNum;
        int Pass = User_Password;
        int [] details = {account, Pass};
        return details;
    }
    public int getNumberOfTransactions() {           
        return numOfTransactions;          
    }         
    public void deposit(double amount){         
        if (amount<=0) {         
            System.out.println("Amount to be deposited should be positive");        
        } else {          
            balance = balance + amount;            
            transactions[numOfTransactions] = amount;            
            transactionsSummary[numOfTransactions] = "Rs." + Double.toString(amount) + " was deposited.";            
            numOfTransactions++;           
        }         
    }
    public void withdraw(double amount) {                   
        if (amount<=0){                
            System.out.println("Amount to be withdrawn should be positive"); 
        } 
        else {  
            if (balance < amount) {  
                System.out.println("Insufficient balance");   
            } else {  
                balance = balance - amount;  
                transactions[numOfTransactions] = amount;   
                transactionsSummary[numOfTransactions] = "Rs." + Double.toString(amount) + " was withdrawn.";     
                numOfTransactions++;                       
            }   
        }
    }
}
}

You have a problem in your code where you create the new account: 您在创建新帐户的代码中遇到问题:

System.out.println("Account Has been created\n Account number: " + myBank.AddNewAccount(Name, Balance)[0]+"\nYour password : "+ myBank.AddNewAccount(Name, Balance)[1]);

You're calling myBank.AddNewAccount() twice on that line, which means that the account number printed will be for account #1, while the password printed will be for account #2. 您在该行上两次调用myBank.AddNewAccount() ,这意味着打印的帐号将用于帐户#1,而打印的密码将用于帐户#2。

Change it so that you're only creating one account and printing the details for it: 进行更改,以便仅创建一个帐户并打印其详细信息:

int[] newAccount = myBank.AddNewAccount(Name, Balance);
System.out.println("Account Has been created\n Account number: " + newAccount[0]+"\nYour password : "+ newAccount[1]);

Also, while I tested your code, it seems you're calling withdraw() instead of deposit() in Bank.deposit() : 另外,当我测试您的代码时,似乎您是在Bank.deposit()调用withdraw()而不是deposit() Bank.deposit()

accounts[i].withdraw(Money);

It's because myBank.AddNewAccount(Name, Balance) is getting called twice in below statement, actually it should be called only once per account. 这是因为myBank.AddNewAccount(Name, Balance)在下面的语句中被调用了两次,实际上每个帐户只能调用一次。

System.out.println("Account Has been created\n Account number: " + myBank.AddNewAccount(Name, Balance)[0]+"\nYour password : "+ myBank.AddNewAccount(Name, Balance)[1]);

make below changes to your code and sit should work fine: 对您的代码进行以下更改,并可以正常使用:

  int[] arrDetails= myBank.AddNewAccount(Name, Balance);//added new line of code                     
  System.out.println("Account Has been created\n Account number: " + arrDetails[0]+"\nYour password : "+ arrDetails[1]);// modified existing code

Final code for Case 1 should be: 情况1的最终代码应为:

case 1 : System.out.println("Enter a customer name :");
                     Name = br.readLine();
                     System.out.println("Enter a opening balance :");
                     Balance = Double.parseDouble(br.readLine());
                     Thread.sleep(250);
                     System.out.println("Creating your account....");
                     Thread.sleep(500);

                     int[] arrDetails= myBank.AddNewAccount(Name, Balance);                     
                     System.out.println("Account Has been created\n Account number: " + arrDetails[0]+"\nYour password : "+ arrDetails[1]);
                     break;

Your problem is here: 您的问题在这里:

System.out.println("Account Has been created\n Account number: " + myBank.AddNewAccount(Name, Balance)[0]+"\nYour password : "+ myBank.AddNewAccount(Name, Balance)[1]);

Why do you add a new account twice in the above method? 为什么用上述方法两次添加一个新帐户? This creates a duplicate account with a different password. 这将创建一个使用不同密码的重复帐户。 Change the above line to this. 将上面的行更改为此。

myBank.AddNewAccount(Name, Balance);
System.out.println("Account Has been created\n Account number: " + myBank.getAccountNum()[0].+"\nYour password : "+ myBank.getAccountNum()[1]);

Your creating two objects and returning second obj password. 您创建两个对象并返回第二个obj密码。

System.out.println("Account Has been created\n Account number: " + myBank.AddNewAccount(Name, Balance)[0]+"\nYour password : "+ myBank.AddNewAccount(Name, Balance)[1]);

In Above code your calling myBank.AddNewAccount two times so, it is creating two objects and THE PASSWORD YOUR PRINTING IS SECOND OBJECT PASSWORD, that's why it is showing wrong password. 在上面的代码中,您两次调用myBank.AddNewAccount ,它正在创建两个对象,并且您的打印密码是第二个目标密码,这就是为什么显示错误密码的原因。 you can use this code. 您可以使用此代码。

int[] details = myBank.AddNewAccount(Name, Balance);
System.out.println("Account Has been created\n Account number: " + details [0]+"\nYour password : "+ details[1]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM