1

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


    }
}

2
  • Did you try to debug it step by step? Commented Jul 6, 2020 at 4:10
  • Inside spend method you have an useless assignment: amount = amount Commented Jul 6, 2020 at 4:15

3 Answers 3

1

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for the help. I understand where I went wrong now
1

If you see the spend method , amount and money are always equal , because you are passing money from payAdmission method as amount in the spend method. It will always result in returning false. Hence isAdmitted is always false, you need to change the logic here in spend method.

 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 payAdmission() {
        //System.out.println(name + " has paid $" + computeFee() + " for admission");
        boolean result;
        if (spend(money)) {
            result = true;
        }
        else {
            result = false;
        }
        return result;
    }

1 Comment

Thank you, I completely forgot about that
1

amount and money variables are same, i have updated code; or you can either declare different variables. It depends upon your logic

    public boolean spend(float amount) {
        boolean checker;
        if (amount >= money) {
            amount = (float) (amount - computeFee());

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.