我是java的新手。我有这个代码的ATM存款,取款,余额和历史选项..现在我的问题是历史选项..我真的不知道如何让所有用户输入它.. 我尝试制作一个,但它只显示最后一个输入的1个历史...
package atm;
import java.util.ArrayList;
import java.util.Scanner;
public class Atm {
static Scanner input = new Scanner(System.in);
static int choose,back;
static double withdraw,balance,deposit,depos,post;
static String us,username="admin",ps,password="admin";
static int []array;
public static void main(String[] args) {
System.out.println("\t Welcome Visitors");
start();
}//end of main
public static void hello(){
System.out.println("\t\tHello: "+username);
options();
}
public static void start(){
try{
System.out.println("\t Username: ");
us=input.nextLine();
if(us.equalsIgnoreCase(username)){
password();
}
else{
System.err.println("Incorrect Username");
start();
}
}//end of try
catch(Exception a){
System.err.print("\t Invalid Input");
start();
}//end of catch(Exception a)
}//end of public static void start()
public static void password(){
System.out.println("\t Password: ");
ps=input.nextLine();
if(ps.equalsIgnoreCase(password)){
hello();
}
else{
System.err.println("Incorrect Password");
password();
}
}
public static void depot(){
array=new int[1];
System.out.println();
System.out.println("\t========================");
System.out.println("\t| Input Amount to Deposit: ");
deposit=input.nextDouble();
System.out.println();
System.out.println("\t========================");
System.out.println("\t| Successfully Deposited|");
System.out.println("\t| $"+deposit+" in your account |");
System.out.println("\t========================");
System.out.println();
calculateDepot(deposit);
options();
}//end of static void deposit();
public static void calculateDepot(double depot){
balance=balance+depot;
calculateHist(deposit);
options();
}//end of public static void calculateDepot(double depot)
public static void withd(){
System.out.println("\t========================");
System.out.println("\t| Input Amount to Withdraw ");
withdraw=input.nextDouble();
if(withdraw>balance){
System.out.println();
System.err.println("\t=============================");
System.err.println("\t| Sorry You Dont Have |");
System.err.println("\t| Enough Balance to Withdraw |");
System.err.println("\t=============================");
System.out.println();
withd();
}
else{
System.out.println();
System.out.println("\t========================");
System.out.println("\t| Successfully Withdraw|");
System.out.println("\t| $"+withdraw+" in your account");
System.out.println("\t========================");
System.out.println();
calculateWithd(withdraw);
options();
}
}//end of static void withdraw
public static void calculateWithd(double withd){
balance=balance-withd;
options();
}//end of public static void calculateWithd(double withd)
public static void bal(){
System.out.println();
System.out.println("\t========================");
System.out.println("\t| Your Total Balance is");
System.out.println("\t| $"+balance);
System.out.println("\t========================");
System.out.println();
options();
}//end of static void balance
public static void calculateHist(double depot){
depos=depot;
options();
}
public static void hist(){
System.out.print("\t|"+depos);
options();
}//end of static void history
public static void options(){
System.out.println("\t========================");
System.out.println("\t| Choose Your Option |");
System.out.println("\t========================");
try{
System.out.println("\t========================");
System.out.println("\t| [1] Deposit |");
System.out.println("\t| [2] Withdraw |");
System.out.println("\t| [3] Balance |");
System.out.println("\t| [4] History |");
System.out.println("\t| [5] Exit |");
System.out.println("\t========================");
choose=input.nextInt();
if(choose==1){
depot();
}//end of if(choose==1)
else if(choose==2){
if(balance==0){
System.err.println();
System.err.println("\t=============================");
System.err.println("\t| Sorry You Dont Have |");
System.err.println("\t| Enough Balance to Withdraw |");
System.err.println("\t=============================");
System.err.println();
options();
}
else{
withd();}
}//end of else if(choose==2)
else if(choose==3){
bal();
}//end of else if(choose==3)
else if(choose==4){
hist();
}//end of else if(choose==4)
else if(choose==5){
System.out.println();
System.out.println("\t| GoodBye! Thank You |");
System.out.println();
}//end of else if(choose==5)
else if(choose<=6){
System.err.print("\t| Invalid Input |");
options();
}//end of else if(choose<5)
}//end of try
catch(Exception b){
System.err.print("\t| Invalid Input |");
input.nextLine();
options();
}//end of catch
}//end of static void option
}//MAIN END
答案 0 :(得分:-1)
对于历史记录,您需要保留两个数组,一个用于保留更改余额的最后一个交易类型(存款,取款),另一个用于交易金额。并且阵列大小也可以受到上限的约束(比如10)。所以,这意味着你会记住最多10个历史项目。然后,您需要管理该有界集中历史项的年表(序列)。