



|
|
P Dunn's Super CS1 Site
|
Programming Challenge Archive
General Problems Solving
Coffee Beans
|
|
Java Application --
CoffeeBeans
// General Information
// ---------------------------------------------------
// File: CoffeeBeans.java
// Type: java application file
// Date: Sun Sep 10, 2000
// Name: Patrick Dunn
// Line: Determines the total cost of coffee beans with
// shipping & handling
// Application Description
// ---------------------------------------------------
/*
Determines the total cost of three different types
of coffee beans plus tax and shipping and handling
which allows the customer to figure out what they
need to make the check out for.
*/
// Required Packages
// ---------------------------------------------------
import blue.io.*;
import blue.math.*;
import java.lang.*;
import java.text.*;
// Application Class
// ---------------------------------------------------
class CoffeeBeans
{
static public void main (String args[])
{
// set up variables used in the program
double numPoundsKonaBeans; // number of pounds of Kon+
a beans purchased
double numPoundsColumbianBeans; // number of pounds o+
f Columbian beans purchased
double numPoundsEspressoBeans; // number of pounds of+
Espresso beans purchased
double totalCostKonaBeans; // total cost of Kona Bean+
s
double totalCostEspressoBeans; // total cost of+
Espresso Beans
double totalCostColumbianBeans; // total cost of Colom+
bian Beans
double totalNumPounds = 0.0; // holds the total number+
of pounds of beans
double totalCost = 0.0; // total cost of what's purch+
ased
double taxCost; // tax cost storage
double shippingCost; // shipping cost storage
// set up variable to handle number format, in t+
his case, currency
NumberFormat form = NumberFormat.getCurrencyInstance()+
;
// constants
double salesTaxPercentage = .07; // sales tax percenta+
ge
double shippingHandlingPrice = .50; // s & h price
double konaBeanPrice = 16.95; // price of kona beans
double espressoBeanPrice = 12.25; // price of espresso+
beans
double columbianBeanPrice = 8.95; // price of columbia+
n beans
// intro to program
IO.println();
IO.println("Welcome to the DunnBay Coffee Company");
IO.println("-------------------------------------");
IO.println("We currently have the following beans in s+
tock");
IO.println("Kona Beans at $" + konaBeanPrice + " per p+
ound");
IO.println("Espresso Beans at $" + espressoBeanPrice ++
" per pound");
IO.println("Columbian Beans at $" + columbianBeanPrice+
+ " per pound");
IO.println("-------------------------------------");
// get input for Kona Beans
IO.print("Enter how many pounds of Kona Beans you want+
purchase ->");
numPoundsKonaBeans = IO.read_double();
IO.println();
// add to total pounds the number of pounds of Kona be+
ans purchased
totalNumPounds = totalNumPounds + numPoundsKonaBeans;
// figure price and store based on the amount of pound+
s purchased
totalCostKonaBeans = numPoundsKonaBeans * konaBeanPric+
e;
// get input for Espresso Beans
IO.print("Enter how many pounds of Espresso Beans you +
want purchase ->");
numPoundsEspressoBeans = IO.read_double();
IO.println();
// add to total pounds the number of pounds of Espress+
o beans purchased
totalNumPounds = totalNumPounds + numPoundsEspressoBea+
ns;
// figure price and store based on the amount of pound+
s purchased
totalCostEspressoBeans = numPoundsEspressoBeans * espr+
essoBeanPrice;
// get input for Kona Beans
IO.print("Enter how many pounds of Columbian Beans you+
want purchase ->");
numPoundsColumbianBeans = IO.read_double();
IO.println();
// add to total pounds the number of pounds of Colombi+
an beans purchased
totalNumPounds = totalNumPounds + numPoundsColumbianBe+
ans;
// figure price and store based on the amount of pound+
s purchased
totalCostColumbianBeans = numPoundsColumbianBeans * co+
lumbianBeanPrice;
// figure out the total cost
totalCost = totalCostKonaBeans + totalCostEspressoBean+
s + totalCostColumbianBeans;
// figure out the total tax
taxCost = totalCost * salesTaxPercentage;
// figure out shipping based on total pounds
shippingCost = totalNumPounds * shippingHandlingPrice;+
// add it all up
totalCost = totalCost + taxCost + shippingCost;
// Display to user their costs
IO.println();
IO.println("DunnBay Coffee Company Invoice");
IO.println("------------------------------");
IO.println("Lbs. of Kona Beans Purchased: " + numPound+
sKonaBeans + " @ $" +
konaBeanPrice + " for a total of: " + form.format(to+
talCostKonaBeans));
IO.println("Lbs. of Espresso Beans Purchased: " + numP+
oundsEspressoBeans + " @ $" +
espressoBeanPrice + " for a total of: " +
form.format(totalCostEspressoBeans));
IO.println("Lbs. of Columbian Beans Purchased: " + num+
PoundsColumbianBeans + " @ $" +
columbianBeanPrice + " for a total of: " +
form.format(totalCostColumbianBeans));
IO.println("------------------------------------------+
------");
// Display total due
IO.println("The total amount due to DunnBay Coffee Com+
pany is " +
form.format(totalCost));
IO.println();
}
}
// Demo
// ---------------------------------------------------
/*
$ javac CoffeeBeans.java
$ java CoffeeBeans
Welcome to the DunnBay Coffee Company
-------------------------------------
We currently have the following beans in stock
Kona Beans at $16.95 per pound
Espresso Beans at $12.25 per pound
Columbian Beans at $8.95 per pound
-------------------------------------
Enter how many pounds of Kona Beans you want purchase ->4.+
25
Enter how many pounds of Espresso Beans you want purchase +
->5.5
Enter how many pounds of Columbian Beans you want purchase+
->9
DunnBay Coffee Company Invoice
------------------------------
Lbs. of Kona Beans Purchased: 4.25 @ $16.95 for a total of+
: $72.04
Lbs. of Espresso Beans Purchased: 5.5 @ $12.25 for a total+
of: $67.38
Lbs. of Columbian Beans Purchased: 9.0 @ $8.95 for a total+
of: $80.55
------------------------------------------------
The total amount due to DunnBay Coffee Company is $244.73
$
*/
|
|
|