0

So I'm working on this program for a school project and I have to create an ATM. For some reason, entering W in the program below doesn't do anything but R works as intended. I've tried having an Else If and putting the "R" code above the "W" code but nothing seems to work. Sorry for the long piece of code, thanks.

import java.util.ArrayList;

public class ConsutingProblem {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int totalCash;
        String choice;
        String rrr = "R";
        String www = "W";
        String iii = "I";
        String qqq = "Q";
        boolean stopAtm = false;
        int hundred = 10;
        int fifty = 10;
        int twenty = 10;
        int ten = 10;
        int five = 10;
        int one = 10;
        totalCash = (hundred*100) + (fifty*50) + (twenty*20) + (ten*10) + (five*5) + one;
        do {
            System.out.println("Please enter the letter corresponding to your chosen action.");
            System.out.println("R for a restock.");
            System.out.println("W for withdrawl followed by amount. Example: W $145");
            System.out.println("I for number of bills present with that denomination. Example: I $20");
            System.out.println("Q for quitting this aplication.");
            choice = input.nextLine();
            if ((choice.substring(0, 0)).equals(www)) {
                int withdrawlAmount = Integer.parseInt(choice.substring(3));
                totalCash -= withdrawlAmount;
                int displayWithdrawl = withdrawlAmount;
                int hundredBack = (int)(displayWithdrawl/100);
                displayWithdrawl -= (hundredBack*100);
                int fiftyBack = (int)(displayWithdrawl/50);
                displayWithdrawl -= (fiftyBack*50);
                int twentyBack = (int)(displayWithdrawl/20);
                displayWithdrawl -= (twentyBack*20);             
                int tenBack = (int)(displayWithdrawl/10);
                displayWithdrawl -= (tenBack*10);
                int fiveBack = (int)(displayWithdrawl/5);
                displayWithdrawl -= (fiveBack*5);
                int oneBack = (int)(displayWithdrawl/1);                   
                displayWithdrawl -= (oneBack*1);
                if (withdrawlAmount <= totalCash && hundredBack <= hundred && fiftyBack <= fifty && twentyBack <= twenty && tenBack <= ten && fiveBack <= five && oneBack <= one) {
                    System.out.println("Success: Dispensed $" + withdrawlAmount);
                    hundred -= hundredBack;
                    fifty -= fiftyBack;
                    twenty -= twentyBack;
                    ten -= tenBack;
                    five -= fiveBack;
                    one -= oneBack;
                    System.out.println("Machine Balance:");
                    System.out.println("$100 - " + hundred);
                    System.out.println("$50 - " + fifty);
                    System.out.println("$20 - " + twenty);
                    System.out.println("$10 - " + ten);
                    System.out.println("$5 - " + five);
                    System.out.println("$1 - " + one);
                } else {
                    System.out.println("Faliure: insufficient funds");
                }
            }
            if (choice.equals(rrr)) {
                hundred = 10;
                fifty = 10;
                twenty = 10;
                ten = 10;
                five = 10;
                one = 10;
                totalCash = (hundred*100) + (fifty*50) + (twenty*20) + (ten*10) + (five*5) + one;
                System.out.println("Machine Balance:");
                System.out.println("$100 - " + hundred);
                System.out.println("$50 - " + fifty);
                System.out.println("$20 - " + twenty);
                System.out.println("$10 - " + ten);
                System.out.println("$5 - " + five);
                System.out.println("$1 - " + one);
            }
        } while (stopAtm == false);


    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

3 Answers3

2

substring(0, 0) does not return a string, as the second index of substring is exclusive. it should be substring(0, 1), to get the first char of a string

botg
  • 31
  • 5
0

this here looks strange:

choice.substring(0, 0)).equals(www)

from the doc: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int,%20int)

"hamburger".substring(4, 8) returns "urge"

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

The substring in your first if statement is the problem. Maybe try to do the statement similar to the later if statement (rrr).

Btw you could also check for one specific character via String.charAt method: Is it possible to get only the first character of a String?

doct0re
  • 373
  • 4
  • 10
  • Can't do same as rrr, since `W` is followed by a number, `R` is a no-arg command. Could use `choice.startsWith("W ")` though. – Andreas Jun 10 '21 at 22:48