简体   繁体   中英

Showing users input History

I'm new to java. I have this codes for an ATM with deposit, withdraw, balance and history option.. and now my problem is the history option.. I really don't know how to make all users input into it.. I try making one but it only shows 1 history of the last input...

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

For history you need to keep two arrays, one for keeping last transaction type (deposit, withdrawal) that changed the balance and another for the amount of the transaction. And tha array size could be constrained by a upper limit as well (say 10). So, that means at most 10 history items you would remember. Then you need to manage the chronology (sequence) of history items within that bounded set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM