0% found this document useful (0 votes)
78 views

Assignment 2 Example

The document contains the code for a Java program that calculates the total cost of coffee bags ordered based on volume discounts. It takes the number of bags ordered as input, calculates the discount based on thresholds, applies the discount to the bag price, and outputs the total cost. The code was refactored multiple times to improve readability and separate concerns.

Uploaded by

Maurice Walsh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Assignment 2 Example

The document contains the code for a Java program that calculates the total cost of coffee bags ordered based on volume discounts. It takes the number of bags ordered as input, calculates the discount based on thresholds, applies the discount to the bag price, and outputs the total cost. The code was refactored multiple times to improve readability and separate concerns.

Uploaded by

Maurice Walsh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

import java.util.Scanner; public class CoffeeCosts { public static void main(String [] args){ double coffeeBagPrice = 5.

50; double coffeeBagsOrdered; double discount = 0; double totalCost; Scanner input = new Scanner(System.in); System.out.println ("This program computes the amount of dicount of IT125 Coffee Company."); System.out.println ("Discounts are listed below: "); System.out.println("\n\t\t Order volume \t Discount " ); System.out.println("\t\t ------------ \t ---------------- " ); System.out.println(" System.out.println(" System.out.println(" System.out.println(" System.out.println(" System.out.println(" >= >= >= >= >= >= 25 Bags 50 Bags 100 Bags 150 Bags 200 Bags 300 Bags 5%"); 10%"); 15%"); 20%"); 25%"); 30%");

System.out.println("Enter number of coffee bags purchased: "); coffeeBagsOrdered = input.nextDouble(); System.out.println("Number of Bags Ordered:" + coffeeBagsOrdered + " - " + coffeeBagsOrdered * coffeeBagPrice); totalCost= coffeeBagsOrdered * coffeeBagPrice;

if(coffeeBagsOrdered >= 300) discount = 0.3; else if(coffeeBagsOrdered discount = 0.25; else if(coffeeBagsOrdered discount = 0.2; else if(coffeeBagsOrdered discount = 0.15; else if(coffeeBagsOrdered discount = 0.1; else if(coffeeBagsOrdered discount = 0.05;

>= 200) >= 150) >= 100) >= 50) >= 25)

System.out.println ("Discount: "+ + coffeeBagsOrdered * coffeeBagPrice * discount);

discount * 100 + "%" + " - "

totalCost= (coffeeBagsOrdered * coffeeBagPrice * (1 - discount)); System.out.println("Your Total Cost: " + " " + } } totalCost);

import java.util.Scanner; public class CoffeeCosts { public static void main(String [] args){ double coffeeBagPrice = 5.50; double coffeeBagsOrdered; double bulkDiscount = 0; double totalCost; Scanner input = new Scanner(System.in); System.out.println("Number of Bags Ordered: "); coffeeBagsOrdered = input.nextDouble(); System.out.println("Number of Bags Ordered:" + coffeeBagsOrdered + " " + coffeeBagsOrdered * coffeeBagPrice); totalCost= coffeeBagsOrdered * coffeeBagPrice; if(coffeeBagsOrdered >= 300) System.out.println("BulkDiscount * 30%" + " - " + coffeeBagsOrdered * coffeeBagPrice * 0.3); else if(coffeeBagsOrdered >= 200) System.out.println ("BulkDiscount * 25%" + " - " + coffeeBagsOrdered * coffeeBagPrice * 0.25); else if(coffeeBagsOrdered >= 150) System.out.println ("BulkDiscount = 20%" + " - " + coffeeBagsOrdered * coffeeBagPrice * 0.2); else if(coffeeBagsOrdered >= 100) System.out.println ("BulkDiscount = 15%" + " - " + coffeeBagsOrdered * coffeeBagPrice * 0.15); else if(coffeeBagsOrdered >= 50) System.out.println ("BulkDiscount = 10%" + " - " + coffeeBagsOrdered * coffeeBagPrice * 0.10); else if(coffeeBagsOrdered >= 25) System.out.println ("BulkDiscount = .05%" +" - " + coffeeBagsOrdered * coffeeBagPrice * 0.05); totalCost= (coffeeBagsOrdered * coffeeBagPrice - bulkDiscount); System.out.println("Your Total Cost:" + " " + totalCost); } }

import java.util.*; public class CoffeeCosts { public static void main(String [] args){ double coffeeBagPrice = 5.50; double coffeeBagsOrdered; double coffeeCost; double bulkDiscount = 0; double totalCost; Scanner input = new Scanner(System.in); System.out.println("Number of Bags Ordered: "); while((coffeeBagsOrdered = input.nextDouble())%1 != 0) { System.out.println("Please enter a whole number."); } if(coffeeBagsOrdered >= 25) { bulkDiscount = .05; } if(coffeeBagsOrdered >= 50) { bulkDiscount = .10; } if(coffeeBagsOrdered >= 100) { bulkDiscount = .15; } if(coffeeBagsOrdered >= 150) { bulkDiscount = .20; } if(coffeeBagsOrdered >= 200) { bulkDiscount = .25; } if(coffeeBagsOrdered >= 250) { bulkDiscount = .30; } System.out.println("Number of Bags Ordered:" + coffeeBagsOrdered + " - " + coffeeBagsOrdered * coffeeBagPrice); System.out.println("Discount:" + bulkDiscount * 100 + "% - " + coffeeBagsOrdered * coffeeBagPrice * bulkDiscount); System.out.println("Your Total Cost:" + " - " + coffeeBagsOrdered * coffeeBagPrice * bulkDiscount);

} }

import java.util.*; class MyJavaProcessor { public static void main(String [] args)

{ final final final final final double double double double double double double double double double double double double COFFEE_BAG_PRICE = 5.50; LARGE_BOX_PRICE = 1.80; MEDIUM_BOX_PRICE = 1.00; SMALL_BOX_PRICE = 0.60; TN_TAX = 0.0925;

bulkDiscount = 0; coffeeBagsOrdered; coffeeCost; subTotal; discountAmount; boxCost; taxAmount; totalCost;

int largeBox; int mediumBox; int smallBox; Scanner input = new Scanner(System.in); System.out.println("Number of Bags Ordered: "); while((coffeeBagsOrdered = input.nextDouble())%1 != 0) { System.out.println("Please enter a whole number."); } System.out.println(coffeeBagsOrdered + " - $" + coffeeBagsOrdered*COFFEE_BAG_PRICE); if(coffeeBagsOrdered >= 25) { bulkDiscount = .05; } if(coffeeBagsOrdered >= 50) { bulkDiscount = .10; } if(coffeeBagsOrdered >= 100) { bulkDiscount = .15; } if(coffeeBagsOrdered >= 150) { bulkDiscount = .20; } if(coffeeBagsOrdered >= 200) { bulkDiscount = .25; } if(coffeeBagsOrdered >= 250) { bulkDiscount = .30; } System.out.println("Discount: " + bulkDiscount*100 + "% $" + coffeeBagsOrdered*bulkDiscount); largeBox = (int) coffeeBagsOrdered/20; mediumBox = (int) (coffeeBagsOrdered%20)/10; if(coffeeBagsOrdered%10 <= 5) { smallBox = 1; } if(coffeeBagsOrdered%10 > 5 ) { smallBox = 2; } else { smallBox = 0; } boxCost = largeBox*LARGE_BOX_PRICE + mediumBox*MEDIUM_BOX_PRICE + smallBox*SMALL_BOX_PRICE; coffeeCost = coffeeBagsOrdered*COFFEE_BAG_PRICE; discountAmount = coffeeBagsOrdered*bulkDiscount; subTotal = coffeeCost + boxCost; taxAmount = subTotal*TN_TAX;

totalCost = subTotal + taxAmount; System.out.println("Boxes Used:"); if(largeBox > 0) { System.out.println("\t" + largeBox + " Large - $" + (largeBox*LARGE_BOX_PRICE)); } if(mediumBox > 0) { System.out.println("\t" + mediumBox + " Medium - $" + (mediumBox*MEDIUM_BOX_PRICE)); } if(smallBox > 0) { System.out.println("\t" + smallBox +" Small - $" + (smallBox*SMALL_BOX_PRICE)); } System.out.println("Your total charge before taxes is: $" + subTotal); System.out.println("Your tax is: $" + taxAmount); System.out.println("Total Charge: $" + totalCost);

} }

You might also like