2016-04-01 12 views
0

Ich weiß, dass es keine Debugger-Website ist, aber ich wollte nur fragen, was ich in diesem Stück Code falsch mache.Bank Programm funktioniert nicht wie erwartet

Wenn ich das Programm starte, füge ich zuerst ein neues Konto hinzu, und wenn ich es einlege oder zurückziehe, steht "Falsches Passwort". Hier ist mein 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++;      
      } 
     } 
    } 
} 
} 
+0

Es sieht aus wie das Passwort ist eine zufällige Ganzzahl, sind Sie sicher, dass Sie es richtig eingeben? – epoch

+0

Sie können es versuchen. –

+0

Ich bin 100% sicher –

Antwort

2

Es ist, weil myBank.AddNewAccount(Name, Balance) zweimal in unter Anweisung aufgerufen wird immer, eigentlich sollte es nur einmal pro Konto aufgerufen werden.

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

machen unter Änderungen am Code und sitzen sollte funktionieren:

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 

Schluss Code für Fall 1 sein sollte:

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; 
3

Sie haben ein Problem in Ihrem Code, wo Sie Erstellen Sie das neue Konto:

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

Sie rufen myBank.AddNewAccount() zweimal in dieser Zeile an, was bedeutet, dass die gedruckte Kontonummer für Konto Nr. 1 und das gedruckte Passwort für Konto Nr. 2 gilt.

ändern es so, dass Sie nur ein Konto erstellen und drucken Sie die Details für sie:

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

Auch während ich Ihren Code getestet, es scheint, dass Sie withdraw() statt deposit() in Bank.deposit() sind Aufruf:

accounts[i].withdraw(Money); 
0

Ihr Problem ist hier:

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

Warum fügen Sie in der obigen Methode zweimal ein neues Konto hinzu? Dies erstellt einen doppelten Account mit einem anderen Passwort. Ändern Sie die obige Zeile in diese.

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

Sie erstellen zwei Objekte und geben das zweite Obj-Passwort zurück.

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

oben Code Ihren Aufruf myBank.AddNewAccount zwei mal so, es ist die Schaffung von zwei Objekten und das Passwort Ihres Druck zweites Objekt Passwort ist, das ist, warum es falsch Passwort angezeigt wird. können Sie diesen Code verwenden.

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