简体   繁体   中英

How do I add an array to my ATM program?

I am still very new to Java. For class, I was to create an object-oriented simple ATM that offers a menu with a choice of four actions: Deposit money, withdrawal money, view balance, and exit. I have an account class and a user class. I was able to figure out hour to do all of that and run my ATM with no errors. Keep in mind, this is a simple ATM, one user, no account number or PIN, and I started him off with 5g's. I want to get ahead, so for the next assignment I need to allow up to max(25) bank accounts to be accessed by the user. The data for the bank accounts will be stored in arrays. We need a bank account object, a bank object containing the array, and a user object. The program will be menu driven.

Any help would be appreciated. Thanks in advance :) here is what I have so far.

Account

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);
    }
}

User

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;
    }
  }
}

You could use a class like this.

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: Taken from this answer here .

 [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

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