Java 我如何调用和存储此信息
我有一个问题,我想我知道答案,但我不太确定 所以我做了这个程序,下面是account类和使用它的银行程序 帐户类别Java 我如何调用和存储此信息,java,arraylist,binary-search-tree,Java,Arraylist,Binary Search Tree,我有一个问题,我想我知道答案,但我不太确定 所以我做了这个程序,下面是account类和使用它的银行程序 帐户类别 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();
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);
}
}
所以“创建帐户”只适用于一个人,如果我再次创建它,那么它只会覆盖名称和余额
所以,如果我想基本上存储所有这些信息,我是否只需要使用一个数组?但是,我如何才能给每个用户打电话,并从这些特定帐户中提取/存入资金呢
我认为二叉搜索树可能有效,但同时我不确定一个简单的数组列表是否有效
为了澄清,我希望我能够
1调用特定用户时,可能必须使用.contains,我认为contains应该链接到特定且唯一的帐户ID
2一旦到达该用户,他们就可以按当前状态使用开关菜单
这只是一个帮助您更好地理解Java工作原理的个人项目,而不是家庭作业。听起来您希望它保持简单。我们可以使用数组列表或列表。。。或者地图。既然账户的顺序无关紧要,我建议你用一张地图。但是,由于您似乎是java语言的新手,因此最好在大部分时间使用列表,因此我将向您展示如何使用列表 代码还有其他问题。。。就像它将只运行一次与一个帐户。有很多东西需要扩展。如果你真的感兴趣,我可以在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;
}
}
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;
}
}