The assignment has been submitted, and the deadline has passed, so no one gets any benefit from seeing my code....except for me. As noted in an earlier post, after an interminable period of massive confusion, the concept of objected oriented programming finally crystallized into coherent thoughts (at least momentarily). I'm proud of this project....not only is this the first project completed without major input from my professor, it is the first project I have completed ahead of schedule.
The gist of the problem: A customer orders a specified number of bags of coffee. Take that order, develop a price (with applicable discounts), pack the bags into 3 kinds of boxes (based on number of bags) and report the cost of the boxes and the net cost of the order.
We were encouraged to develop an interface class, with a second class handling the pricing and boxing. I opted to use the interface class, but used seperate classes for the pricing and boxing operations. It was a lot of fun, finally!
Order
import java.util.Scanner;
import java.text.DecimalFormat;
/**
* Prompts for and accepts the number of bags ordered.
* Prints the details and total cost of the order.
*
* @author (Ad Ingle)
* @version (1.0)
*
* I certify that I have no assistance on this project
* beyond conversations with Dr. McCauley
*/
public class Order
{
//set price of coffee/bag as a constant
public final static double COFFEE_PRICE = 5.50;
// set discount amounts as constants
public final static double DISCOUNT_300 = .30;
public final static double DISCOUNT_250 = .25;
public final static double DISCOUNT_200 = .20;
public final static double DISCOUNT_100 = .15;
public final static double DISCOUNT_50 = .10;
public final static double DISCOUNT_25 = .05;
//set box sizes as constants
public final static int BOX_LG = 20;
public final static int BOX_MD = 10;
public final static int BOX_SM = 5;
//set box cost as constants
public final static double LGBOX_COST = 2.00;
public final static double MDBOX_COST = 1.00;
public final static double SMBOX_COST = 0.50;
public static void main(String[] args)
{
//create scanner object for input from keyboard
Scanner in = new Scanner(System.in);
System.out.println("Please enter the number of bags desired:");
int numBags = 0;
numBags = in.nextInt(); //reads an integer into variable "numBags"
//test input for a valid number (must be greater than 0)
if(numBags >0)
{
//create Pricing object to perform price calculations
Pricing pricing = new Pricing(numBags);
//local variables for method calls to Pricing class (pricing object)
double price = pricing.getPrice();
double discount = pricing.getDiscountAmt();
double netCost = pricing.getNetCoffeeCost();
//create Shipping object to perform box calculations
Shipping shipping = new Shipping(numBags);
//call boxAlgorithm to initiate calculations (void - so no return)
shipping.boxAlgorithm(numBags); //do I need this parameter?
//local variables for method calls to Shipping class (boxing object)
int largeBox = shipping.getLargeBox();
int medBox = shipping.getMedBox();
int smallBox = shipping.getSmBox();
double largeBoxCost = shipping.getLargeBoxCost();
double medBoxCost = shipping.getMedBoxCost();
double smallBoxCost = shipping.getSmBoxCost();
double grandTotal = netCost + largeBoxCost + medBoxCost + smallBoxCost;
//create DecimalFormat object to output double amounts as money
DecimalFormat money = new DecimalFormat("$0.00");
System.out.println(); //spacing
System.out.println("Your order for " + numBags + " bags of coffee at $5.50 per bag has been placed.");
System.out.println("The price for the order is " + money.format(netCost) + ".");
System.out.println("This includes a volume discount of " + money.format(discount) + ".");
System.out.println("The order is packed in " + largeBox + " large box(es), " + medBox + " medium box(es) and " + smallBox + " small box");
System.out.println("The charge for large box(es) is " + money.format(largeBoxCost) + ".");
System.out.println("The charge for medium box(es) is " + money.format(medBoxCost) + ".");
System.out.println("The charge for small box(es) is " + money.format(smallBoxCost) + ".");
System.out.println("The total order cost is: " + money.format(grandTotal) + ".");
}
else
{
//warning message for false result in if statement
System.out.println("You must enter a quantity greater than 0.");
}
}
}
Pricing
/**
* Calculate order total, including quantity discount.
*
* @author (Ad Ingle)
* @version (1.0)
*/
public class Pricing
{
// instance variables
private int numBags;
private double price;
private double discountAmt;
private double netCoffeeCost;
//note: all constants in Order class
/**
* Constructor for objects of class Pricing
*
* @ param qty input number of bags ordered
*/
public Pricing(int qty)
{
numBags = qty;
}
//methods
/**
* calculate price for coffee
*/
public double getPrice()
{
price = numBags * Order.COFFEE_PRICE;
return price;
}
/**
* calculate discount based on volume (numBags)
*/
public double getDiscountAmt()
{
double discount = 0;
if (numBags >= 300)
{
discount = Order.DISCOUNT_300;
}
else if (numBags >= 250)
{
discount = Order.DISCOUNT_250;
}
else if (numBags >= 200)
{
discount = Order.DISCOUNT_200;
}
else if (numBags >= 100)
{
discount = Order.DISCOUNT_100;
}
else if (numBags >= 50)
{
discount = Order.DISCOUNT_50;
}
else if (numBags >= 25)
{
discount = Order.DISCOUNT_25;
}
discountAmt = price * discount;
return discountAmt;
}
/**
* net coffee price (price less discount amount)
*
*/
public double getNetCoffeeCost()
{
netCoffeeCost = price - discountAmt;
return netCoffeeCost;
}
}
Shipping
/**
* Calculate box packing and box cost.
*
* @author (Ad Ingle)
* @version (Jan 30, 2009)
*/
public class Shipping
{
//instance variables
private int numBags;
private int numBoxLg;
private int numBoxMd;
private int numBoxSm;
//note: all constants in Order class
/**
* Constructor for objects of class Shipping
*
* @ param qty input number of bags ordered
*/
public Shipping(int qty)
{
numBags = qty;
}
//methods
/**
* calculate the number of boxes needed
*/
public void boxAlgorithm(int qty)
{
numBags = qty;
//divide number of bags by large box qty
//and subtract the boxed bags from the total
//remaining
numBoxLg = numBags / Order.BOX_LG;
numBags = numBags - (numBoxLg * Order.BOX_LG);
//divide number of bags by med box qty
//subtract boxed bags from total
numBoxMd = numBags / Order.BOX_MD;
numBags = numBags - (numBoxMd * Order.BOX_MD);
//if balance remaining is larger than
//small box, put into med box
if(numBags > Order.BOX_SM)
{
numBoxMd++;
}
//balance into 1 small box
else if(numBags > 0 && numBags <= Order.BOX_SM)
{
numBoxSm = 1;
}
}
public int getLargeBox()
{
return numBoxLg;
}
public int getMedBox()
{
return numBoxMd;
}
public int getSmBox()
{
return numBoxSm;
}
public double getLargeBoxCost()
{
double boxCostLg = numBoxLg * Order.LGBOX_COST;
return boxCostLg;
}
public double getMedBoxCost()
{
double boxCostMd = numBoxMd * Order.MDBOX_COST;
return boxCostMd;
}
public double getSmBoxCost()
{
double boxCostSm = numBoxSm * Order.SMBOX_COST;
return boxCostSm;
}
}
Recent Comments