2014-12-13 17 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; 
} 
}