Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- public class retail {
- public static void main (String[] args){
- //compute the discount on retail sales depending on the day
- //declares all variables
- double discount = 0;
- double discount_amount = 0;
- double retail_price = 0;
- double total_retail_price =0;
- double retail_price_after_discount = 0;
- double total_retail_price_after_discount = 0;
- double total_discount = 0;
- int total_items = -1;
- int day = 0;
- boolean tryAgain = true;
- //try catch loop to check for the input for the day is between 1 and 7 and is a number
- do {
- try {
- day = Integer.parseInt(JOptionPane.showInputDialog("Please enter the number according to the day of purchase: \n 1: Sunday\n 2: Monday\n 3: Tuesday\n 4: Wednesday\n 5: Thursday\n 6: Friday\n 7: Saturday"));
- if (day >= 1 && day <= 7){
- JOptionPane.showMessageDialog(null, "Day chosen.");
- tryAgain = false;
- }
- else if (day < 1 || day > 7){
- throw new NumberFormatException("Integer is out of range.");
- }
- }
- catch (NumberFormatException e1) { // Range check
- JOptionPane.showMessageDialog(null, "The number you entered is not between 1 and 6. Try again.");
- }
- }
- while(tryAgain);
- //prompt for day to control selection
- // day = Integer.parseInt(JOptionPane.showInputDialog
- //("Please enter the number according to the day of purchase: \n 1: Sunday\n 2: Monday\n 3: Tuesday\n 4: Wednesday\n 5: Thursday\n 6: Friday\n 7: Saturday"));
- do {
- //try and catch to ensure the user input is only a numeric and is only 1-6
- try
- {
- retail_price = Double.parseDouble(JOptionPane.showInputDialog("Please enter the retail price of the item or a -1 if you have no more items"));
- }
- catch (NumberFormatException e)
- {
- JOptionPane.showMessageDialog(null, "ERROR! The number for the day must be numeric");
- }
- //if statements depending on the day input to run in the loop to adjust the prices
- if (day == 1 || day == 2){
- discount = 0;
- discount_amount = retail_price * discount;
- retail_price_after_discount = retail_price - discount_amount;
- total_retail_price = total_retail_price + retail_price;
- total_retail_price_after_discount = total_retail_price_after_discount + retail_price_after_discount;
- total_discount = total_discount + discount_amount;
- total_items++;
- }
- else if(day == 3){
- discount = .08;
- discount_amount = retail_price * discount;
- retail_price_after_discount = retail_price - discount_amount;
- total_retail_price = total_retail_price + retail_price;
- total_retail_price_after_discount = total_retail_price_after_discount + retail_price_after_discount;
- total_discount = total_discount + discount_amount;
- total_items++;
- }
- else if(day == 4){
- discount = .16;
- discount_amount = retail_price * discount;
- retail_price_after_discount = retail_price - discount_amount;
- total_retail_price = total_retail_price + retail_price;
- total_retail_price_after_discount = total_retail_price_after_discount + retail_price_after_discount;
- total_discount = total_discount + discount_amount;
- total_items++;
- }
- else if(day == 5){
- discount = .24;
- discount_amount = retail_price * discount;
- retail_price_after_discount = retail_price - discount_amount;
- total_retail_price = total_retail_price + retail_price;
- total_retail_price_after_discount = total_retail_price_after_discount + retail_price_after_discount;
- total_discount = total_discount + discount_amount;
- total_items++;
- }
- else if(day == 6){
- discount = .32;
- discount_amount = retail_price * discount;
- retail_price_after_discount = retail_price - discount_amount;
- total_retail_price = total_retail_price + retail_price;
- total_retail_price_after_discount = total_retail_price_after_discount + retail_price_after_discount;
- total_discount = total_discount + discount_amount;
- total_items++;
- }
- else if(day == 7){
- discount = .40;
- discount_amount = retail_price * discount;
- retail_price_after_discount = retail_price - discount_amount;
- total_retail_price = total_retail_price + retail_price;
- total_retail_price_after_discount = total_retail_price_after_discount + retail_price_after_discount;
- total_discount = total_discount + discount_amount;
- total_items++;
- }
- else {
- JOptionPane.showMessageDialog(null, "No items were entered");
- }
- //loop until the user input is -1
- } while(retail_price != -1);
- //output receipt
- JOptionPane.showMessageDialog
- (null, "Total number of items is: " + total_items + "\n" + "Total price without discount is: " + total_retail_price + "\n" + "Total price with the discount is: " + total_retail_price_after_discount + "\n" + "Total discount saved: " + total_discount);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement