Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Boolean method help!

  1. #1
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Boolean method help!

    Hello all!

    So, I'm creating a program that deals with a discount loyalty system. When the customer buys $100.00 worth of items, they get a $10.00 discount. This discount needs to be a boolean method (discountReached()). Thus far, I have:

    class Customer {
     
    //Declare variables
    double totalCost;
    double discount = 10;
     
     
    public Customer() {
    totalCost = 0;
    }
     
    public void makePurchase(double amount) {
    totalCost = totalCost + amount;
    }
     
    public double getTotal() {
    return totalCost;
    }
     
     
     
    }

    So, I have the total amount all worked out. Now I don't know how to go about having the boolean method ONLY run once when the total reaches over $100.00.

    Any help is appreciated!

    Thanks,
    Scorks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Boolean method help!

    having the boolean method ONLY run once when the total reaches over $100.00.
    If you want to call the method the first time the total reaches that amount, use a boolean variable to remember that the method has been called. Initialize it to false.
    Test the variable before calling the method and set it immediately after calling the method. Next time testing the variable will tell you that the method has been called
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Boolean method help!

    Okay, but I'm not sure how to make the boolean method work, either. I guess I would start like:
    public boolean discountReached() {
    totalCost - 10;
    return totalCost;
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Boolean method help!

    how to make the boolean method work
    By boolean method I assume you mean a method that returns a boolean value.
    When should the method return true?
    When should the method return false?

    When you answer those questions, you should be able to write an if statement that implements the logic need to return the desired value.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. check for boolean method.
    By daitobu in forum Object Oriented Programming
    Replies: 1
    Last Post: September 1st, 2012, 08:54 AM
  2. I need a little help with a boolean method.
    By bankston13 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 30th, 2012, 08:02 PM
  3. Boolean method issue
    By mysticCHEEZE in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 23rd, 2011, 11:26 PM
  4. Need help with boolean method! =(
    By leao in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 14th, 2010, 10:18 AM
  5. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 10:56 AM

Tags for this Thread