银行计划未按预期运作

时间:2016-04-01 09:27:49

标签: java arrays bank

我知道它不是调试器站点,但我只是想问一下我在这段代码中做错了什么。

当我运行该程序时,我首先添加一个新帐户,然后当我存入或取出它时说错误的密码。这是我的代码

代码:

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++;                       
            }   
        }
    }
}
}

4 个答案:

答案 0 :(得分:3)

您在创建新帐户的代码中遇到问题:

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

您在该行上拨打myBank.AddNewAccount()两次,这意味着打印的帐号将用于帐号#1,而打印的密码则用于帐号#2。

更改它,以便您只创建一个帐户并打印详细信息:

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

此外,在我测试您的代码时,您似乎在withdraw()中调用了deposit()而不是Bank.deposit()

accounts[i].withdraw(Money);

答案 1 :(得分:2)

这是因为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]);

在下面对代码进行更改并保持正常工作:

  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

案例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;

答案 2 :(得分:0)

你的问题在这里:

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

为什么在上述方法中添加两次新帐户?这会创建一个具有不同密码的重复帐户。将上面的行更改为此。

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

答案 3 :(得分:0)

您创建两个对象并返回第二个obj密码。

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

在上面编码你的呼叫myBank.AddNewAccount两次,它创建了两个对象并且密码你的打印是第二个对象密码,这就是它显示错误密码的原因。 你可以使用这段代码。

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