-2

To summarize I am making a program for a metro ticket system. and I am using set and get methods for it, when it comes to boolean values (since I need to validate that the person enters enough money for the ticket) how am i supposed to put in the main class( it is defined in brain) using the set method and an if statement.Here is a little fraction of the entire code and the rest is on github(https://github.com/alexxei4/subwayticket). The main is basically the class that will be used for interaction with the user and the brain is where alot of the actions are defined.All help is appreciated, please and thank you.

 if (Choice1a == 10){
            if(subway1.ticketcounter1(true);){
                System.out.println("PRINT SUCCESSFUL, COLLECT YOUR TICKET!");
            }
            if(subway1.ticketcounter1(false);){
                System.out.println("INSEFFICIENT FUNDS, PLEASE ADD MORE");
            }

2 Answers2

0

This is not how you evaluate boolean values, you just place the value in an if statement and it will proceed if true and refuse if false, also there is no need to duplicate the statement when you can just place an else block to handle situations that are not true:

    if(subway1.ticketcounter1) {
        System.out.println("PRINT SUCCESSFUL, COLLECT YOUR TICKET!");
    }
    else {
        System.out.println("INSEFFICIENT FUNDS, PLEASE ADD MORE");
    }

Also do not include semicolons in if statements, that's incorrect syntax. Read more about how to use use boolean values here: https://codingbat.com/doc/java-if-boolean-logic.html

EDIT:

After reading through your Github code I see that ticketcounter1 indeed is a method, but what it's doing is trying to change the value of ticketcounter1 like it's a referenced object, but boolean are primitive data types and can't be referenced, and even if they could it still wouldn't work because Java is a pass-by-value language. Read here for more information on that.

public void ticketcounter1(boolean ticketcounter1){
    if (credit1 > total1){
        ticketcounter1 = true;
    }
    else {
        ticketcounter1 = false;
    }
}
public void ticketcounter2(boolean ticketcounter2){
    if (credit2 > total2){
        ticketcounter2 = true;
    }
    else {
        ticketcounter2= false;
    }

Like the other answer said you should be returning the value as boolean instead of trying to change it:

public boolean ticketcounter1(){
    if (credit1 > total1){
        return true;
    }
    else {
        return false;
    }
}
public boolean ticketcounter2(){
    if (credit2 > total2){
       return true;
    }
    else {
        return false;
    }
}

But all in all your code demonstrated fundamental flaws in understanding how the language works, I would suggest picking up a good Java for beginners kind of book or do some introductory online tutorials. Here is a good place to start your learning journey: https://docs.oracle.com/javase/tutorial/java/index.html

Matthew
  • 1,905
  • 3
  • 19
  • 26
  • I tried doing that and it only states insufficient funds even when the credit is higher than the ticket amount or the total –  May 29 '19 at 17:13
  • Jokes aside though , your explanation actually helped,and I went away from using the boolean and used a different approach, and yeah I know my java is pretty bad right now but Im still learning thanks though –  May 29 '19 at 22:38
0

You code is like this

 public void ticketcounter1(boolean ticketcounter1){
    if (credit1 > total1){
        ticketcounter1 = true;
    }
    else {
        ticketcounter1 = false;
    }
}
public void ticketcounter2(boolean ticketcounter2) {
    if (credit2 > total2){
        ticketcounter2 = true;
    }
    else {
        ticketcounter2= false;
    }
}

It should be like this. Instead of using the variable and passing it though parameter. Use getter. Besides that your code won't run since subway1.ticketcounter1(true) is giving nothing. It is only changing variables stored in Brain.java. No information is being sent to main.

 public boolean ticketcounter1(){
    if (credit1 > total1){
        return true;
    }
    else {
        return false;
    }
}
public boolean ticketcounter2(){
    if (credit2 > total2){
       return true;
    }
    else {
        return false;
    }
}

You can create functions without parameters. I don't know what were you trying to do?

if (Choice1a == 10){
   if(subway1.ticketcounter1()){
       System.out.println("PRINT SUCCESSFUL, COLLECT YOUR TICKET!");
   }
   if(subway1.ticketcounter1()){
       System.out.println("INSEFFICIENT FUNDS, PLEASE ADD MORE");
   }
}

subway1.ticketcounter1() will give either true and false. Do not use ; in if statement condition. ; ends the statement. Check this guide to learn about use of semi-colon If you do want to use ; The code should look like this

if (Choice1a == 10){
   boolean ticketCounter1 = subway1.ticketcounter1();
   if(ticketCounter1){
      System.out.println("PRINT SUCCESSFUL, COLLECT YOUR TICKET!");
   } else {
      System.out.println("INSEFFICIENT FUNDS, PLEASE ADD MORE");
   }
 }

P.S You don't need two ifs if-else would be better in this case

if(condition) {
// Conditions is true
} else {
// Condition is false
}
Yoshikage Kira
  • 1,070
  • 1
  • 12
  • 20
  • when I try using your recommendation (the first piece of code qhwere you said how its supposed to look). I get an error and it says "Cannot return a value from a method with void result type" –  May 29 '19 at 17:11
  • Your return type is most likely void. Change it to boolean and see if code works. By the way the first piece of code isn't mine. I was just quoting you from github. – Yoshikage Kira May 31 '19 at 04:38