2014-12-13 16 views
0

我有一個問題,我想我知道答案但我不太確定。我將如何撥打和存儲此信息

所以我做了這個節目,以下是賬戶類,並使用它

賬戶類

import java.util.Scanner; 
import java.util.UUID; 


public class Account { 

double balance; 

String name; 

UUID AcctN; 

int pin; 


public void Account() 
{ 
    UUID idOne = UUID.randomUUID(); 
    Scanner sc = new Scanner (System.in); 
    Scanner sa = new Scanner (System.in); 

    System.out.println ("Your starting balance is 0.0"); 
    double initBal = 0; 
    balance = initBal; 

    System.out.println ("Enter your full name"); 
    String owner = sa.nextLine(); 
    name = owner; 

    UUID number = idOne; 
    AcctN = number; 

    System.out.println ("Enter your pin number containing ONLY 4 numbers"); 
    int pins = sc.nextInt(); 
    pin = pins; 



} 

public void withdraw (double amount) 
{ 
    if (balance <= 0) 
    { 
     System.out.println ("Insufficent funds"); 
    } 
    else 
    { 
     balance -= amount; 
    } 


} 



public void deposit (double amount) 
{ 
    if (amount <= 0) 
    { 
     System.out.println ("You cannot deposit negative or 0 funds"); 
    } 
    else 
    { 
     balance += amount; 
    } 


} 

public void withdrawfee (double amount) 
{ 

    if (balance <= 0) 
    { 
     System.out.println ("Insufficent funds"); 
    } 
    else 
    { 
     double fee = 10; 
     balance -= amount + fee; 
    } 

} 


public double getBalance() 
{ 
    return balance; 
} 
public String toString() 
{ 
    return String.format ("User info: %s\n Balance: $%s\n Account Number: %s\n Pin Number: %s",  name, balance, AcctN, pin); 
} 
} 

主要類

import java.util.Scanner; 

public class Tester { 

public static void main (String [] args) 
{ 
    Scanner sc = new Scanner (System.in); 
    boolean quit = false; 
    Account ACT = new Account(); 



    do 
    { 
     System.out.println ("Welcome to the Bank\n\n***********\n 1: Create account\n 2: Check accounts\n 3: Withdraw funds\n 4: Withdraw with fee\n 5: Deposit funds\n 6: Quit"); 
     int input = sc.nextInt(); 
     switch (input) 
     { 
     case 1: 
      System.out.println ("Make an account"); 
      ACT.Account(); 
      System.out.println ("Current information: "); 
      System.out.println (ACT.toString()); 
      break; 
     case 2: 
      System.out.println (ACT.toString()); 
      break; 
     case 3: 
      System.out.println ("How much do you wish to withdraw?"); 
      ACT.withdraw(sc.nextDouble()); 
      System.out.println ("Thank you for your service"); 
      break; 
     case 4: 
      System.out.println ("How much do you wish to withdraw?"); 
      ACT.withdrawfee(sc.nextDouble()); 
      System.out.println ("An additional 10 dollars has been deducted from your account"); 
      System.out.println ("Thank you for your service"); 
      break; 
     case 5: 
      System.out.println ("How much do you wish to deposit"); 
      ACT.deposit(sc.nextDouble()); 
      System.out.println ("Thank you for your service"); 
      break; 
     case 6: 
      quit = true; 
      break; 
     } 

    }while(!quit); 
    System.out.println ("Thank you for using the Bank.."); 

} 

} 

所以「創建賬戶開戶銀行計劃'適用於一個人,如果我再次創建它,那麼它只會覆蓋名稱和平衡。

所以,如果我想本質上存儲所有這些信息,我只需要使用一個數組?但那麼我將如何能夠呼叫每個用戶並從這些特定賬戶中提取/存入資金。

我想二叉搜索樹可能會工作,但同時我不確定一個簡單的數組列表是否會有效?

爲了澄清我希望我能夠

1)撥打一個特定的用戶,可能會不得不使用。載有()和我想的是,含有應該鏈接到一個特定的唯一帳戶ID 。

2)一旦到達該用戶,他們就能夠像當前一樣使用開關菜單。

這只是一個個人項目,有助於更好地理解Java如何工作,而不是作業。

回答

0

聽起來像你想保持簡單。我們可以使用數組列表或列表...或地圖。由於帳戶的順序無關緊要,我會建議一張地圖。但是因爲你看起來對Java語言來說是新手,所以最好在大部分時間使用List,這樣我會告訴你如何使用列表。

還有其他的代碼問題......就像它只會運行一次一個帳戶。有很多可以擴展。如果你真的感興趣,我可以在skype @ yawang2247上完成這個簡單的項目,幫助你。

否則,以下內容將解決您當前的問題。 這應該是你修改後的主類:

import java.util.Scanner; 

public class Tester { 

ArrayList<Account> myList; 

public static void main (String [] args) 
{ 
myList = new ArrayList<ACT>(); 
Scanner sc = new Scanner (System.in); 
boolean quit = false; 
Account ACT = new Account(); 



do 
{ 
    System.out.println ("Welcome to the Bank\n\n***********\n 1: Create account\n 2: Check accounts\n 3: Withdraw funds\n 4: Withdraw with fee\n 5: Deposit funds\n 6: Quit"); 
    int input = sc.nextInt(); 
    switch (input) 
    { 
    case 1: 
    if(listContains(ACT.getAcctN())){ 
    // you need to make the above getAcctN() method in you Account.java 
     System.out.println ("You already have an account!"); 
     break; 
    } 
     System.out.println ("Make an account"); 
     ACT.Account(); 
     System.out.println ("Current information: "); 
     System.out.println (ACT.toString()); 
     myList.add(ACT); 
     break; 
    case 2: 
     System.out.println (ACT.toString()); 
     break; 
    case 3: 
     System.out.println ("How much do you wish to withdraw?"); 
     ACT.withdraw(sc.nextDouble()); 
     System.out.println ("Thank you for your service"); 
     break; 
    case 4: 
     System.out.println ("How much do you wish to withdraw?"); 
     ACT.withdrawfee(sc.nextDouble()); 
     System.out.println ("An additional 10 dollars has been deducted from your account"); 
     System.out.println ("Thank you for your service"); 
     break; 
    case 5: 
     System.out.println ("How much do you wish to deposit"); 
     ACT.deposit(sc.nextDouble()); 
     System.out.println ("Thank you for your service"); 
     break; 
    case 6: 
     quit = true; 
     break; 
    } 

}while(!quit); 
System.out.println ("Thank you for using the Bank.."); 

} 
// below searches array for a particular account 
public static boolean listContains(UUID accountNum) { 
    for(int i = 0; i < myList.size(); i++) { 
     if(myList.get(i).getAcctN() == accountNum) { 
      return true; 
     } 
    } 
    return false; 
} 
}