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 11 of 11

Thread: Please help, simple IF/ELSE decison question.

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    My Mood
    Depressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Please help, simple IF/ELSE decison question.

    this is my assignment

    Requirements
    The software you developed in the previous exercise must be extended to allow for the application of a 10% discount on purchases over $50. When a transaction’s sub-total before tax is equal to or greater than $50, the cashier should be given the opportunity to apply the discount to the purchase. GST is then to be applied to the adjusted sub-total. A sample transaction is shown below:
    Program Output [Sample applied discount]

    **********************************
    ****** Maria’s Bakeshop ******
    **********************************

    Begin Transaction

    Enter the product name: Sweet Cakes
    Enter the quantity: 30
    Enter the product price: $2.75

    Apply 10% discount (y/n)? Y

    PRODUCT: Sweet Cakes
    SUB-TOTAL: $82.5
    DISCOUNT: -$8.25
    GST: $3.71
    TOTAL: $77.96

    Transaction End
    Program Output [Sample denied discount]

    **********************************
    ****** Maria’s Bakeshop ******
    **********************************

    Begin Transaction

    Enter the product name: Sweet Cakes
    Enter the quantity: 30
    Enter the product price: $2.75

    Apply 10% discount (y/n)? n

    PRODUCT: Sweet Cakes
    SUB-TOTAL: $82.5
    GST: $4.12
    TOTAL: $86.62

    Transaction End
    Program Output [Sample without discount]

    **********************************
    ****** Maria’s Bakeshop ******
    **********************************

    Begin Transaction

    Enter the product name: Bread
    Enter the quantity: 5
    Enter the product price: $0.99

    PRODUCT: Bread
    SUB-TOTAL: $4.95
    GST: $0.24
    TOTAL: $5.19

    Transaction End
    Technical Specifications
    The technical specifications of the extensions to the program are as follows:
    The program will utilize constants where appropriate
    The program will prompt the user for the following:
    To apply discount if sub-total is $50 or more
    Display a message if the discount is not applied for sub-totals of $50 or more
    The program will display the following
    Discount amount (calculated value) if a discount was applied
    When prompting for whether or not to apply a discount, ensure that both uppercase and lowercase responses are handled properly
    All program code must conform to best practices and coding standards
    Optional Requirement










    this is the code i have, all i need to know is how i can make the user imput matter, everythign works but when i hit "n" on user imput it still shows my discount how do i make it so when i hit "n" it doesnt do the calculation or show it. ive tired everything and its not working please help



    /*
    Author:Ryan
    Date:09/18/2012
    Purpose: A program to allow the bakery to process a transaction as well as a discounted price,
    per amount allocated for specified minimum amount has been reached (50$)
    */

    import java.util.Scanner;

    public class MariaBakery {

    public static void main(String args[]) {
    // local variables

    String productName = "";
    double price = -1.0;
    double gstAmount = -1.0;
    double totalPrice = -1.0;
    int quantity = 1;
    double subTotal = -1.0;
    String discount = "0";
    double discountAmount = -1.0;

    // DISCOUNT rate
    final double DISCOUNT = .10;

    // tax rate
    final double GST_RATE = 0.05;

    // Define a scanner for keyboard input
    Scanner in = new Scanner(System.in);

    // Display the title
    System.out.println("****************************** ****");
    System.out.println("****** Maria's Bakeshop ******");
    System.out.println("****************************** ****");

    System.out.println("\nBegin transaction\n");

    System.out.print("Enter the product name : ");
    productName = in.nextLine();

    System.out.print("Enter the quantity : ");
    quantity = Integer.parseInt(in.nextLine());

    System.out.print("Enter the product price : $ ");
    price = Double.parseDouble(in.nextLine());

    // perform calculations

    gstAmount = (price * quantity * GST_RATE);

    subTotal = (price * quantity);

    discountAmount = (subTotal * DISCOUNT);

    totalPrice = (quantity * price) + gstAmount - discountAmount;

    // Make Decison

    if (totalPrice >= 50) {
    System.out.println("Apply 10% discount (y/n)?");
    discount = (in.nextLine());
    } else {
    System.out.println(" ");
    }

    // display output
    System.out.println("\nProduct: " + productName);
    System.out.println("Sub total: $ " + subTotal);

    if (totalPrice >= 50) {
    System.out.println("Discount: $-" + discountAmount);
    }

    System.out.println("Gst Amount: $ " + gstAmount);
    System.out.println("Total Price: $ " + totalPrice);

    System.out.println("\nTransaction End");
    }

    }


  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    21
    My Mood
    Confused
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Please help, simple IF/ELSE decison question.

    if (totalPrice >= 50)
    {
    System.out.println("Apply 10% discount (y/n)?");
    discount = (in.nextLine());
    if (discount=="n")
    {
    System.out.println(" ");
    }
    }

  3. The Following User Says Thank You to dx8292 For This Useful Post:

    IronRyan (October 3rd, 2012)

  4. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    My Mood
    Depressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Please help, simple IF/ELSE decison question.

    really thats it? ill give it a try thanks man

  5. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    My Mood
    Depressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Please help, simple IF/ELSE decison question.

    nah it didnt work, all i want to do is use the users input after i ask apply discount and if they say yes display a certain thing and if they say no display a certain thing. ive tried like everything, is it somthing to do with my variables?

  6. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Please help, simple IF/ELSE decison question.

    @dx8292 please see this link.

    @IronRyan please see the same link (above) also.
    Also please use code tags when posting code. If you do not know how then the announcements page is a must read.

    * The program will utilize constants where appropriate
    -this requirement has not been met, you have magical numbers in the logic of the code.

    * To apply discount if sub-total is $50 or more
    -this requirement has almost been met, but the discount seems to always be applied

    * Display a message if the discount is not applied for sub-totals of $50 or more
    -this requirement does not appear to have been included (yet?)

    * When prompting for whether or not to apply a discount, ensure that both uppercase and lowercase responses are handled properly
    -

    * All program code must conform to best practices and coding standards
    -this requirement seems flexible enough to host the opinion of the instructor, so go the extra mile on every detail... Some processing could be done in methods outside main. You did not use code tags so you will have to be the judge on the formatting. There are no javadoc comments. The comments that are there could be improved upon. A good comment will make the reader fully understand what the following code is going to do before they see the code. If I walked up to you on a street and asked you to "do calculations" or "make decision" you probably would not have the correct answer. Try to make the comments make a bit more sense in a stand alone context, like "calculate sub- and totals", or "check discount eligibility" ...not that your comments are bad, they just could be improved. What happens if the user input is no good? For example someone types "joe" for the quantity.

    I am only guessing your class has covered the things I have mentioned...


    The discount logic: Try to get an idea of what should happen written down before you start at it again.
    You get the user's input and apply the discount before deciding if the discount should be applied.
    After: System.out.println("Apply 10% discount (y/n)?"); the user is prompted for input, which is assigned to discount, and never used, which means anything can be typed there without changing the way the program executes.

  7. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Please help, simple IF/ELSE decison question.

    @dx8292, please read the forum rules and the following: http://www.javaprogrammingforums.com...n-feeding.html

  8. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    21
    My Mood
    Confused
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Please help, simple IF/ELSE decison question.

    jps i don't know if he has gotten as far as .equals and toUpperCase methods yet, just cause it looks like a basic if/else chapter in some book

    and IronRyan I forgot something in discount = (in.nextLine());, so that might be causing a problem.....(Think...what type of variable is it supposed to be)

  9. #8
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    My Mood
    Depressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Please help, simple IF/ELSE decison question.

    Quote Originally Posted by dx8292 View Post
    jps i don't know if he has gotten as far as .equals and toUpperCase methods yet, just cause it looks like a basic if/else chapter in some book

    and IronRyan I forgot something in discount = (in.nextLine());, so that might be causing a problem.....(Think...what type of variable is it supposed to be)
    char but i dont know how i could use that, and no i havent gotten to .equals or toUppercase yet

  10. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Please help, simple IF/ELSE decison question.

    Quote Originally Posted by IronRyan View Post
    char but i dont know how i could use that, and no i havent gotten to .equals or toUppercase yet
    As a requirement of your project:
    When prompting for whether or not to apply a discount, ensure that both uppercase and lowercase responses are handled properly
    Congratulations, you have 'gotten to .equals and toUppercase' as of right now.

  11. #10
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    My Mood
    Depressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Please help, simple IF/ELSE decison question.

    he told us in class thats for bonus marks, im not worried about that right now

  12. #11
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Please help, simple IF/ELSE decison question.

    See post #5.
    Attempt to correct as many of the problems as you can.
    When you go as far as you can go, post the new version of the code and your question.

Similar Threads

  1. A very simple question...
    By jhy2a in forum Java Theory & Questions
    Replies: 2
    Last Post: April 6th, 2012, 10:18 PM
  2. Simple Question (I Think...?)
    By srwoltering in forum Object Oriented Programming
    Replies: 3
    Last Post: March 22nd, 2012, 02:56 PM
  3. Simple I/O Question...well could be simple for you?
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 30th, 2011, 06:44 AM
  4. simple question...
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 2nd, 2010, 07:29 AM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM

Tags for this Thread