我还是Java新手。对于课堂,我要创建一个面向对象的简单ATM,提供一个菜单,可以选择四种操作:存款,取款,查看余额和退出。我有一个帐户类和一个用户类。我能够计算出所有这些的时间并运行我的ATM没有错误。请记住,这是一个简单的ATM,一个用户,没有帐号或PIN,我开始使用5g。我希望领先一步,因此对于下一个作业我需要允许用户访问最多(25)个银行帐户。银行账户的数据将存储在数组中。我们需要一个银行帐户对象,一个包含该数组的银行对象和一个用户对象。该程序将由菜单驱动。
任何帮助将不胜感激。在此先感谢:)这是我到目前为止。
帐户
import java.util.Scanner;
public class ATMacct {
double Bal = 5000.0;
Scanner scannerObject = new Scanner(System.in);
void makeDeposit(){
System.out.print( "Please, enter amount to deposit $");
double lDep;
lDep = scannerObject.nextDouble();
Bal = Bal + lDep;
System.out.println( " You have depsited $" + lDep);
System.out.println( " Your new balance is $" + Bal);
}
void makeWithdrawal(){
System.out.print( "Please, enter amount to withdraw $");
double lWDraw;
lWDraw = scannerObject.nextDouble();
if (lWDraw <= Bal){
Bal = Bal - lWDraw;
System.out.println( "You have withdrawn $" + lWDraw);
System.out.println( "Your new balance is $" + Bal);
}else{
System.out.println("Insufficient funds!");
}
}
void dispBal(){
System.out.println( "Your current balance is $" + Bal);
}
}
用户
import java.util.Scanner;
public class ATMuser
{
public static void main(String[] args)
{
ATMacct myAcct = new ATMacct();
int Choice;
do{
dispMenu();
Choice = getChoice();
proChoice(Choice, myAcct);
}
while (Choice !=0);
}
static void dispMenu()
{
System.out.println( "|==================================|");
System.out.println( "| TONY'S FIRST NATIONAL BANK |");
System.out.println( "|***********Menu Options***********|");
System.out.println( "|__________________________________|");
System.out.println( "| Press 1 To Make Withdrawal |");
System.out.println( "| Press 2 To Make Deposit |");
System.out.println( "| Press 3 To View Current Balance |");
System.out.println( "| Press 0 To Exit |");
System.out.println( "|__________________________________|");
System.out.println( "| Please Make Selection Now... |");
System.out.println( "|==================================|");
}
static int getChoice()
{
Scanner scannerObject = new Scanner(System.in);
int pChoice, Choice;
pChoice = scannerObject.nextInt();
Choice = pChoice;
return Choice;
}
static void proChoice(int Choice, ATMacct myAcct)
{
switch (Choice)
{
case 1: myAcct.makeWithdrawal();
break;
case 2: myAcct.makeDeposit();
break;
case 3: myAcct.dispBal();
break;
case 0: System.out.println( "Thank you, come again.");
break;
}
}
}
答案 0 :(得分:0)
你可以使用这样的类。
public class Account {
private Integer accountNumber;
private Double balance;
public Account(final Integer accountNumber, final Double initialBalance) {
this.accountNumber = accountNumber;
balance = initialBalance;
}
public Double deposit (double depositAmmount) {
balance += depositAmmount;
return balance;
}
public Double withdraw(double withdrawAmmount) {
balance -= withdrawAmmount;
return balance;
}
public Double getBalance() {
return balance;
}
public Integer getAccountNumber() {
return accountNumber;
}
}
PS:取自这个答案here。
答案 1 :(得分:0)
[Link][1]
https://stackoverflow.com/a/40831599/3278943
ATM program complete
Output:
Nov 27, 2016 10:45:27 PM shraam.bank.atm.MyLog logit
INFO: My first log
Nov 27, 2016 10:45:27 PM shraam.bank.atm.ATMStatus <init>
INFO: ATMStatus Initialized
Currency Avaialbe in ATM
10:100 Notes
10:2000 Notes
10:50 Notes
10:10 Notes
Enter Money > 420
Required Amount : 420
Total Available amount in ATM : 21600
Plz take your money in currency
No of 2000:0
No of 100:4
No of 50:0
No of 10:2
Take your Amount = 420
Nov 27, 2016 10:48:35 PM shraam.bank.atm.MyLog logit
INFO: My first log
Nov 27, 2016 10:48:35 PM shraam.bank.atm.CalculateMoneyAtm reRun
INFO: Would u like to credit more money ? y/n
Would u like to credit more money ? y/n
y
Currency Avaialbe in ATM
6:100 Notes
10:2000 Notes
10:50 Notes
8:10 Notes
Enter Money > 5020
Required Amount : 5020
Total Available amount in ATM : 21180
Plz take your money in currency
No of 2000:2
No of 100:6
No of 50:8
No of 10:2
Take your Amount = 5020
Nov 27, 2016 10:48:47 PM shraam.bank.atm.MyLog logit
INFO: My first log
Would u like to credit more money ? y/n
Nov 27, 2016 10:48:47 PM shraam.bank.atm.CalculateMoneyAtm reRun
INFO: Would u like to credit more money ? y/n
n