• October 3rd, 2012, 09:51 PM
IronRyan
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 quantity: 5
Enter the product price: \$0.99

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

}
• October 3rd, 2012, 11:17 PM
dx8292
if (totalPrice >= 50)
{
System.out.println("Apply 10% discount (y/n)?");
discount = (in.nextLine());
if (discount=="n")
{
System.out.println(" ");
}
}
• October 3rd, 2012, 11:24 PM
IronRyan
really thats it? ill give it a try thanks man
• October 3rd, 2012, 11:27 PM
IronRyan
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?
• October 4th, 2012, 02:29 AM
jps

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.
• October 4th, 2012, 10:09 AM
copeg
• October 4th, 2012, 10:43 AM
dx8292
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)
• October 4th, 2012, 04:12 PM
IronRyan
Quote:

Originally Posted by dx8292
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
• October 4th, 2012, 04:30 PM
jps
Quote:

Originally Posted by IronRyan
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.
• October 4th, 2012, 04:50 PM
IronRyan