我有一个问题,我认为我知道答案,但我不太确定。
所以我制作了这个程序,以下是帐户类和使用它的银行程序
帐户类
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)调用特定用户,可能必须使用.contains(),我认为包含应链接到特定且唯一的帐户ID。
2)一旦到达该用户,他们就能够使用当前的切换菜单。
这只是一个个人项目,有助于更好地理解Java的工作原理,而不是家庭作业。
答案 0 :(得分:0)
听起来你想保持简单。我们可以使用数组列表或列表...或映射。由于帐户顺序无关紧要,我建议使用地图。但是因为看起来你是java语言的新手,所以最好在大多数时候使用Lists,所以我将向你展示如何使用列表。
代码还有其他问题......比如一个帐户只运行一次。有很多可以扩展的。如果你真的很感兴趣,我可以通过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;
}
}