Search code examples
javaoopobjectgetset

Using booleans with set and get methods


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

Solution

  • 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