Help with creating the correct functions for my ATM


#1

I know it is sloppy or most likely not done correct, i’m not very good at this but i’m still trying. I have been able to assign each numbered button to display its corresponding number but when it comes to creating the functions for the (balance, deposit, withdraw, clear, and enter) buttons I can not seem to find the right way to form it. Any help would be much appreciated. I’ve asked my professor for help on this and I haven’t gotten a response back, so you guys are my last hope of finally understanding this.

// Global variables
boolean enterClicked, balanceClicked, depositClicked, withdrawClicked, clearClicked;

String[] bLabel = {"0","1","2","3","4","5","6","7","8","9","Balance","Deposit","Withdraw","Clear","Enter"};

int[]  bX       = {145,50,145,240,50,145,240,50,145,240,365,365,365,365,125},

       bY       = {275,25,25,25,95,95,95,165,165,165,25,95,165,275,375},

       bW       = {75,75,75,75,75,75,75,75,75,75,100,100,100,100,115},

       bH       = {50,50,50,50,50,50,50,50,50,50,50,50,50,50,50};

int bID,

    prevOp;

float acc, num, total, balance, withdraw;

// setp()

void setup(){

  size(500,500);
  total = 0;
  num = bID;
  withdraw = 0;
  balance = bID + bID;
  enterClicked = false;
  balanceClicked = false;
  depositClicked = false;
  withdrawClicked = false;
  clearClicked = false;
 

}

 

// draw()

void draw(){

  for (int i=0; i<15; i++){

    fill(255);

    rect(bX[i],bY[i],bW[i],bH[i]);

    fill(0);

    textAlign(CENTER,CENTER);

    textSize(20);

    text(bLabel[i],bX[i]+bW[i]/2,bY[i]+bH[i]/2);

  }

}

 

// Functions

 

void mousePressed(){

  bID = -1;

  for(int i=0; i<15; i++){

    if((mouseX >= bX[i])&(mouseX <= bX[i]+bW[i])&

       (mouseY >= bY[i])&(mouseY <= bY[i]+bH[i]))

       bID = i;

  }

 

  if(bID != -1){

    //println(bLabel[bID]);
 
 
    switch(bID){
      case 0:
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
      case 7:
      case 8:
      case 9:
        println(bID);
        break;
      case 10: 
        balanceClicked = true;
          total = num;
          
          println("Balance",total);
          break;
          
      // Balance Button
      case 11:
         depositClicked = true;
          num = total + bID;
          
            println(num,"has been deposited");
            break;
      // Deposit Button
      case 12:
      withdrawClicked = true;
          withdraw = total - bID;
          
            println(withdraw,"has been withdrawn");
            break;
      // Withdraw Button
      case 13:
        if(!clearClicked)
          clearClicked = true;
            println("");
            break;
      // Clear Button
      case 14: // Enter Button
        
        println(num);
    }}
}

#2

Hi,

I presume that this code is the basic structure your teacher has given you and that you need to build on op of that right?

You need to break it down in small problems. The first thing you probably want to do is to display the value the user is typing so if is click on “1” and then “5” it is displaying “15”.

Do you have any instruction on where you need to display that? Should you use only the console?


#3

Its actually for a project we had to build our own code to create an ATM. I was actually able to figure out how to create multi digit numbers by multiply them by 10 then adding it with the next number pressed. So that’s all set.

Also we only need it to be displayed in the console, we don’t have to worry about creating a display.

Now I’ve been working on it and i’m having issues making it so that each function will only be performed if that function such as (balance, deposit, withdraw) is followed up with enter.


#4

You almost have everything then. What about having 3 functions performing each operations and call them at the right moment in your code?

// Global variables
boolean enterClicked, balanceClicked, depositClicked, withdrawClicked, clearClicked;

String[] bLabel = {"0","1","2","3","4","5","6","7","8","9","Balance","Deposit","Withdraw","Clear","Enter"};

int[]  bX       = {145,50,145,240,50,145,240,50,145,240,365,365,365,365,125},

       bY       = {275,25,25,25,95,95,95,165,165,165,25,95,165,275,375},

       bW       = {75,75,75,75,75,75,75,75,75,75,100,100,100,100,115},

       bH       = {50,50,50,50,50,50,50,50,50,50,50,50,50,50,50};

int bID,

    prevOp;

float acc, num, total, balance, withdraw;

// setp()

void setup(){

  size(500,500);
  total = 0;
  num = bID;
  withdraw = 0;
  balance = bID + bID;
  enterClicked = false;
  balanceClicked = false;
  depositClicked = false;
  withdrawClicked = false;
  clearClicked = false;
 

}

 

// draw()

void draw(){

  for (int i=0; i<15; i++){

    fill(255);

    rect(bX[i],bY[i],bW[i],bH[i]);

    fill(0);

    textAlign(CENTER,CENTER);

    textSize(20);

    text(bLabel[i],bX[i]+bW[i]/2,bY[i]+bH[i]/2);

  }

}

 

// Functions

 

void mousePressed(){

  bID = -1;

  for(int i=0; i<15; i++){

    if((mouseX >= bX[i])&(mouseX <= bX[i]+bW[i])&

       (mouseY >= bY[i])&(mouseY <= bY[i]+bH[i]))

       bID = i;

  }

 

  if(bID != -1){
 
 
    switch(bID){
      case 0:
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
      case 7:
      case 8:
      case 9:
        println(bID);
        break;
      case 10: 
        balanceClicked = true;
          total = num;
          balanceFunction(total);
          break;
          
      // Balance Button
      case 11:
         depositClicked = true;
          num = total + bID;
            depositFunction(num);
            break;
      // Deposit Button
      case 12:
      withdrawClicked = true;
          withdraw = total - bID;
            withdrawFunction(withdraw);
            break;
      // Withdraw Button
      case 13:
        if(!clearClicked)
          clearClicked = true;
            println("");
            break;
      // Clear Button
      case 14: // Enter Button
        
        println(num);
    }}
}

void balanceFunction(float val) {
  println("Do balance operation");
}

void depositFunction(float val) {
  println("Do deposit operation");
}

void withdrawFunction(float val) {
  println("Do withdraw operation");
}

#5

I’m kinda confused by what you mean. Sorry if i do not understand what you are trying to say.

I was doing some more work on it and figure I could create a switch in case 14: since enter will be used to perform mostly all of the functions. Does that sound like the right thing to do? But once again i’m struggling with creating the functions. Mostly having the ability for the program to remember what values you have input in after pressing enter. Also after you press enter having the display reset so when you hit a number again it does not just add another number to the previous one if that makes any sense.

  • I hit the deposit button* -> then enter in 567 -> then press enter
    I can not get it so that when I try to hit a number like 3 I end up with “3” instead of 5673.
    ^^^hopefully this helps make more sense…
case 9:
      num = num*10 + bID;
        println(num);
        break;
      case 10:

I also added this into the code you just put in the post. I made it so there can be more than 1 digit numbers.


#6

Ohhhhhh, I get it now!

Let’s break it step by step.

With the process you are explaining, you have 2 modes:

  • Deposit
  • Withdrawal

The first thing you should do is implement those modes with, for example, 2 boolean variables: isDepositMode and isWithdrawalMode.

Then when you click on the deposit button, you would switch the variable accordingly and display “Deposit mode” in the console so the user has a feedback of what is happening.

After that, you can record the numbers entry to get the value the user is trying to input.

Finally, when the user hits enter, depending on the mode, use the entered valu the way it is supposed to be used.

Now be carefull, every time your are changing mode, you need to reset that entered value so it does not interfere with the new one.

Do you see how it can be done now?


#7

I understand what you mean but Im not able to really think of how I would write that out in code.

The two boolean variable you gave do make sense I have added them to the top. Now would I use each value to enter that specific mode for that specific case or will this be apart of the switch created under the enter button case?


#8

Check out the principle of this simple sketch:

boolean mode1, mode2;

void setup() {
  mode1 = false;
  mode2 = false;
}


void draw() {
  
}


void keyPressed() {
  // If user press keypad 1
  if (keyCode == 97) {
    mode1 = true;
    mode2 = false;
  }
  
  // If user press keypad 2
  if (keyCode == 98) {
    mode1 = false;
    mode2 = true;
  }
  
  // If user press enter
  if (keyCode == 10) {
    
    if (mode1) {
      println("mode 1");
      mode1 = false;
    }
    
    if (mode2) {
      println("mode 2");
      mode2 = false;
    }
  }
  
}

Now, try to implement that idea to your code.


#9

I tried to base it off the example you provided me but I am more than positive I did something wrong. Here is what I came up with.

// Global variables
boolean enterClicked, balanceClicked, depositClicked, withdrawClicked, clearClicked, isDepositMode, isWithdrawalMode;

String[] bLabel = {"0","1","2","3","4","5","6","7","8","9","Balance","Deposit","Withdraw","Clear","Enter"};

int[]  bX       = {145,50,145,240,50,145,240,50,145,240,365,365,365,365,125},

       bY       = {275,25,25,25,95,95,95,165,165,165,25,95,165,275,375},

       bW       = {75,75,75,75,75,75,75,75,75,75,100,100,100,100,115},

       bH       = {50,50,50,50,50,50,50,50,50,50,50,50,50,50,50};

int bID,

    prevOp;

float acc, num, total, balance, withdraw;

// setp()

void setup(){

  size(500,500);
  total = 0;
  num = bID;
  withdraw = 0;
  balance = bID + bID;
  enterClicked = false;
  balanceClicked = false;
  depositClicked = false;
  withdrawClicked = false;
  clearClicked = false;
  isDepositMode = false;
  isWithdrawalMode = false;
 

}

 

// draw()

void draw(){

  for (int i=0; i<15; i++){

    fill(255);

    rect(bX[i],bY[i],bW[i],bH[i]);

    fill(0);

    textAlign(CENTER,CENTER);

    textSize(20);

    text(bLabel[i],bX[i]+bW[i]/2,bY[i]+bH[i]/2);

  }

}

 

// Functions

 

void mousePressed(){

  bID = -1;

  for(int i=0; i<15; i++){

    if((mouseX >= bX[i])&(mouseX <= bX[i]+bW[i])&

       (mouseY >= bY[i])&(mouseY <= bY[i]+bH[i]))

       bID = i;

  }

 

  if(bID != -1){
 
 
    switch(bID){
      case 0:
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
      case 7:
      case 8:
      case 9:
      num = num*10 + bID;
        println(num);
        break;
      case 10: 
        balanceClicked = true;
          total = num;
          balanceFunction(total);
          break;
          
      // Balance Button
      case 11:
         if(depositClicked){
         isDepositMode = true;
           isWithdrawalMode = false;
          num = total + bID;
            depositFunction(num);
            break;
         }
      // Deposit Button
      case 12:
      if(withdrawClicked){
        isWithdrawalMode = true;
        isDepositMode = false;
          withdraw = total - bID;
            withdrawFunction(withdraw);
            break;
      }
      // Withdraw Button
      case 13:
        if(!clearClicked)
          clearClicked = true;
            println("");
            break;
      // Clear Button
      case 14:
        if(enterClicked){
          
          if(isWithdrawalMode){
            println("isWithdrawalMode");
            isWithdrawalMode = false;
          }
           if(isDepositMode){
            println("isDepositMode");
            isDepositMode = false;
          }
        }
    }
      // Enter Button
        
        println(num);
    }}


void balanceFunction(float val) {
  println("Do balance operation");
}

void depositFunction(float val) {
  println("Deposit Mode");
}

void withdrawFunction(float val) {
  println("Withdraw Mode");
}

#10

The structure is almost good. The problems are your xxxClicked variables. You are never setting them to true so you are never activating any mode.

The way to go when you have a problem like this (you think that some parts of your code do nothing) is to print some values in he console. For example the first thing I did to debug was to write the piece of code below. The line I wanted to write never appeared so I new that enterClicked was always false.

case 14:
if(enterClicked){

  println("Entering if section"); // test line
          
  if(isWithdrawalMode){
    println("isWithdrawalMode");
    isWithdrawalMode = false;
   }

   if(isDepositMode){
    println("isDepositMode");
    isDepositMode = false;
  }
}

My guess is that you tried to copy the if (keyCode == xx) of my previous code. But you don’t need that. This line is the equivalent of your case xx: lines, it is just to know what the user pressed in order to perform the appropriate action.

Anyway, if you get rid of those lines it should be working just fine for the modes and you can go to the next steps:

  1. Don’t allow the user to enter any value if you are not in one of those mode
  2. Nicely perform all the operations

Below I’m copying your code with those changes:

  • Correction of some mistakes in setup for some initial values
  • Implementing the fix discussed above
  • Getting rid of everything useless for the moment to focus on only the next steps

Please, if you use it, try at least to apply the fix discussed above to your current version and to find the bad initial values in Setup() by yourself. The goal of the following version is just so you can have a fresh start.

// Global variables
boolean isDepositMode, isWithdrawalMode;

String[] bLabel = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "Balance", "Deposit", "Withdraw", "Clear", "Enter"};

int[]  bX       = {145, 50, 145, 240, 50, 145, 240, 50, 145, 240, 365, 365, 365, 365, 125}, 

  bY       = {275, 25, 25, 25, 95, 95, 95, 165, 165, 165, 25, 95, 165, 275, 375}, 

  bW       = {75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 100, 100, 100, 100, 115}, 

  bH       = {50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50};

int bID, prevOp;

float acc, num, total, balance, withdraw;

// setp()

void setup() {
  size(500, 500);

  total = 0;
  num = 0;
  withdraw = 0;
  balance = 0;
  isDepositMode = false;
  isWithdrawalMode = false;
}



// draw()
void draw() {
  for (int i=0; i<15; i++) {
    fill(255);
    rect(bX[i], bY[i], bW[i], bH[i]);
    fill(0);
    textAlign(CENTER, CENTER);
    textSize(20);
    text(bLabel[i], bX[i]+bW[i]/2, bY[i]+bH[i]/2);
  }
}



// Functions
void mousePressed() {
  bID = -1;

  for (int i=0; i<15; i++) {
    if ((mouseX >= bX[i])&(mouseX <= bX[i]+bW[i]) & (mouseY >= bY[i])&(mouseY <= bY[i]+bH[i])) {
      bID = i;
    }
  }

  switch(bID) {
  // Numbers buttons
  case 0:
  case 1:
  case 2:
  case 3:
  case 4:
  case 5:
  case 6:
  case 7:
  case 8:
  case 9:
    num = num*10 + bID;
    println(num);
    break;

  // Balance Button
  case 10:
    // Implement balance case
    break;

  // Deposit Button
  case 11:
      isDepositMode = true;
      isWithdrawalMode = false;
      // Implement deposit case
      break;

  // Withdraw Button
  case 12:
      isWithdrawalMode = true;
      isDepositMode = false;
      // Implement withdraw case
      break;
      
  // Clear Button
  case 13:
    // Implement clear case
    break;
    
  // Enter Button
  case 14:
      if (isWithdrawalMode) {
        isWithdrawalMode = false;
        // Implement enter action for withdraw mode
      }
      if (isDepositMode) {
        isDepositMode = false;
        // Implement enter action for deposit mode
      }
    }
}