简体   繁体   中英

One of my boolean methods in Java is not returning true and i don't know how to correct that

Here is my code:

public class SmartCard {

    private String name;

    //Constructor for SmartCard class
    public SmartCard(String name) {
        this.name = name;
    }
    //the getOwner() method which returns the owner's name
    public String getOwner() {
        return name;
    }

    //setStaff() method to set staff status
    public boolean setStaff(boolean status) {
            return true;
    }

    //isStaff() method returns true if card belongs to member of staff
    public boolean isStaff() {
        boolean staff;
        if (setStaff(true)) {
            staff = true;
        } else staff = false;
        return staff;
    }
}

public class Tester {

    public static void main(String... args) {
        testPart1a();
        testPart1b();
        testPart1c();`enter code here`
    }

    public static void testPart1a() {
        System.out.println("Part 1 - Accessor methods");
        System.out.println("======");

        System.out.println("--- Part 1a ---");
        System.out.println();

        System.out.println("* Creating a new SmartCard for student Anna Undergrad...");
        SmartCard card = new SmartCard("Anna Undergrad");
        System.out.println("Owner is: " + card.getOwner());
        System.out.println();
    }


    public static void testPart1b() {
        System.out.println("--- Part 1b ---");
        System.out.println();

        SmartCard card = new SmartCard("Anna Undergrad");

        System.out.println("Is " + card.getOwner() + " staff? " + card.isStaff());
        System.out.println();
    }



    public static void testPart1c() {
        System.out.println("--- Part 1c ---");
        System.out.println();

        System.out.println("* Creating a new SmartCard for staff member Dr. Bob Lecturer...");
        SmartCard card = new SmartCard("Dr. Bob Lecturer");
        card.setStaff(true);
        System.out.println("Is " + card.getOwner() + " staff? " + card.isStaff());
        System.out.println();
    }

When I run the program, my isStaff() method is always returning true when it should return false for Anna Undergrad and true for Bob Lecturer. Maybe I did it the wrong way and should change my setStaff() method.

setters don't have to return boolean (unless you want to do something very unorthodox). Change it to:

//setStaff() method to set staff status
public void setStaff(boolean status) {
    this.status = status;
}

And change your getStaff() method like this:

public boolean isStaff() {
    return staff;
}

You will also need to define boolean staff :

private String name;
private boolean staff;

...

I think your setStuff method is wrong :

public boolean setStaff(boolean status) {
        return true;
} 

And a simple note :
you can use getter and setter pattern for SmartCard :

public class SmartCard {
    private String name ;
    private boolean stuff ;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isStuff() {
        return stuff;
    }

    public void setStuff(boolean stuff) {
        this.stuff = stuff;
    }

    @Override
    public String toString() {
        return "SmartCard{" +
                "name='" + name + '\'' +
                ", stuff=" + stuff +
                '}';
    }
}

And now test class :

class MainClass {
    public static void main(String[] args) {
        SmartCard card = new SmartCard() ;
        card.setName("Sample");
        card.setStuff(false);
        System.out.println(card);

        SmartCard card2 = new SmartCard();
        card2.setName("Other Card");
        card2.setStuff(true);
        System.out.println(card2);
    }
}

Looks like you need to declare a boolean Staff property and add it to the SmartCard constructor so you can set it and get it. Otherwise your setStaff method isn't setting anything, it's just returning true all the time as a signal that the method was run instead of getting a boolean value for Staff set in each SmartCard object.

In other words set up a Staff boolean just like name where you can set it and get it.

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