Java class and methods returning wrong value

Tags: ,



I am writing code in java to simulate entering a circus at a very low level code wise. It is just a couple of classes under a main class, Customer.

My code is just the main class and its methods, then some print statements, My admitted method is returning false for all values when it should return true if they can afford the ticket. Here’s the code:

class Customerrr {
    String name;
    int age;
    float money;

    public Customerrr(String initName, int initAge) {
        name = initName;
        age = initAge;
    }

    public Customerrr(String initName, int initAge, float initMoney) {
        name = initName;
        age = initAge;
        money = initMoney;
    }

    public Customerrr() {
    }

    public double computeFee() {
        //adult fee $12.75 anyone 18+
        //3 and under no fee
        //65 or oldr %50
        //4 to 17 $8.50
        double result;

        if (age < 3) {
            result = 0.0;
        }

        else if (age < 17){
            result = 8.50;
        }
        else if (age < 64) {
            result = 12.75;
        }
        else {
            result = 12.75/2;
        }
        return result;
    }
    public boolean spend(float amount) {
        boolean checker;
        if (amount > money) {
            amount = (float) (amount - computeFee());

            checker = true;
        }
        else {
            amount = amount;
            checker = false;
        }
        return checker;
    }

    public boolean hasMoreMoneyThan(Customerrr c){
        boolean result;
        if (money > c.money) {
            result = true;
        }
        else {
            result = false;
        }
        return result;
    }
    public boolean payAdmission() {
        //System.out.println(name + " has paid $" + computeFee() + " for admission");
        boolean result;
        if (spend(money)) {
            result = true;
        }
        else {
            result = false;
        }
        return result;
    }

    public boolean admitted () {
        boolean result;
        if (payAdmission()) {
            result = true;
        }
        else {
            result = false;
        }
        return result;
    }
}



public class AddingMethods {
    public static void main(String args[]) {
        Customerrr c1, c2, c3, c4;
        c1 = new Customerrr("Bob", 17, 100);
        c2 = new Customerrr("Dottie", 3, 1000);
        c3 = new Customerrr("Jane", 24, 40);
        c4 = new Customerrr("Sam", 72, 5);

        System.out.println(" Bob has been admitted ... " + c1.admitted());
        System.out.println(" Dottie has been admitted ... " + c2.admitted());
        System.out.println(" Jane has been admitted ... " + c3.admitted());
        System.out.println(" Sam has been admitted ... " + c4.admitted());
        c1.payAdmission();
        System.out.println("Bob has been admitted ... " + c1.admitted());
        c2.payAdmission();
        System.out.println("Dottie has been admitted ... " + c2.admitted());
        c3.payAdmission();
        System.out.println("Jane has been admitted ... " + c3.admitted());
        c4.payAdmission();
        System.out.println("Sam has been admitted ... " + c4.admitted());
        System.out.println(" Bob has $" + c1.money);
        System.out.println(" Dottie has $" + c2.money);
        System.out.println(" Jane has $" + c3.money);
        System.out.println(" Sam has $" + c4.money);


    }
}

Answer

Your problem is in the spend() method. Here is your code with comments showing what is happening at every line:

public boolean spend(float amount) {
    boolean checker;
    if (amount > money) { //if the customer cannot afford the fee
        amount = (float) (amount - computeFee()); // set the local variable "amount" (which is never used after this)

        checker = true;
    }
    else { //if the customer has enough money
        amount = amount; //do absolutely nothing
        checker = false;
    }
    return checker; //returns true if the customer couldn't afford the fee, false otherwise
}

I believe you wanted something more like this:

public boolean spend(float amount) {
    if (money > amount) { //if the customer has enough money
        money -= (float) computeFee(); //subtract the fee from the instance variable "money"
        return true; //returning true because the customer does have enough money
    }

    //if the customer does not have enough money
    return false; //returns false and does not change the customer's "money" variable
}


Source: stackoverflow