I have created a simple application that reads a series of products * quantity sold and generates a final order receipt that includes the sold item name, quantity and total for each item as well as the total for the whole receipt (including 6% sales tax). I feel that I accomplished this, except I cannot test the application as nothing is printing to the "receipt" AKA ContentPane. Below are the three classes I used.
PRODUCT class:
public class Product
{
private int coffeeQTY;
private int teaQTY;
private int bagelQTY;
private int muffinQTY;
private double tax;
private double total;
private double price;
private int productNOM;
public Product()
{
total=price=0.00;
productNOM=coffeeQTY=teaQTY=bagelQTY=muffinQTY=0;
}
//setters
public void setcoffeeQTY(int c)
{
coffeeQTY += c;
}
public void setteaQTY(int t)
{
teaQTY += t;
}
public void setbagelQTY(int b)
{
bagelQTY += b;
}
public void setmuffinQTY(int m)
{
muffinQTY += m;
}
//getters
public int getcoffeeQTY()
{
return coffeeQTY;
}
public int getteaQTY()
{
return teaQTY;
}
public int getbagelQTY()
{
return bagelQTY;
}
public int getmuffinQTY()
{
return muffinQTY;
}
public double getTAX()
{
return tax;
}
public double getTotal()
{
return total;
}
private double coffeePrice;
private double teaPrice;
private double bagelPrice;
private double muffinPrice;
public void inputOrder()
{
for(int i = 0; i <= 3; i++)
{
String order = JOptionPane.showInputDialog("Product 1: Coffee $3.65 "
+ "\nProduct 2: Tea $2.45 "
+ "\nProduct 3: Bagel $1.50 "
+ "\nProduct 4: Muffins $1.85 "
+ "\nEnter the number of the product you would like to order. Enter -1 when your order is complete: ");
productNOM = Integer.parseInt(order);
boolean done = false;
switch(productNOM)
{
case 1:
coffeePrice = 3.65;
String cQTY = JOptionPane.showInputDialog("Enter the quantity of Coffee you would like to order: ");
setcoffeeQTY(Integer.parseInt(cQTY));
break;
case 2:
teaPrice = 2.45;
String tQTY = JOptionPane.showInputDialog("Enter the quantity of Tea you would like to order: ");
setteaQTY(Integer.parseInt(tQTY));
break;
case 3:
bagelPrice = 1.50;
String bQTY = JOptionPane.showInputDialog("Enter the quantity of Bagels you would like to order: ");
setbagelQTY(Integer.parseInt(bQTY));
break;
case 4:
muffinPrice = 1.85;
String mQTY = JOptionPane.showInputDialog("Enter the quantity of the Muffins you would like to order: ");
setmuffinQTY(Integer.parseInt(mQTY));
break;
default:
done = true;
break;
}
total += coffeePrice * coffeeQTY + teaPrice * teaQTY + bagelPrice * bagelQTY + muffinPrice * muffinQTY;
if(!done)
{
tax = ((coffeePrice * coffeeQTY) * 0.06) + ((teaPrice * teaQTY) * 0.06) + ((bagelPrice * bagelQTY) * 0.06) + ((muffinPrice * muffinQTY) * 0.06);
total = (coffeePrice * coffeeQTY) + (teaPrice * teaQTY) + (bagelPrice * bagelQTY) + (muffinPrice * muffinQTY) + tax;
continue;
}
else
{
break;
}
}
}
public void draw(Graphics g)
{
g.drawString("Coffee $3.65 x" +getcoffeeQTY(), 25, 100);
g.drawString("Tes $2.45 x" +getteaQTY(), 25, 125);
g.drawString("Bagel $1.50 x" +getbagelQTY(), 25, 150);
g.drawString("Muffin $1.85 x" +getmuffinQTY(), 25, 175);
g.drawString("Tax(6%): " +getTAX(), 25, 225);
g.drawString("Total: " +getTotal(), 25, 250);
}
}
SALES class
public class Sales extends JComponent
{
private Product merch;
public Sales(Product m)
{
merch = m;
}
public void printSales(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
merch.draw(g2);
}
}
PRINTSALES class
public class PrintSales
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
Product merch = new Product();
merch.inputOrder();
JFrame frame = new JFrame();
frame.setSize(500, 750);
frame.setTitle("Coffee Shop");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.WHITE);
Sales store = new Sales(merch);
frame.add(store);
frame.setVisible(true);
}
}
I don't know if its related to the addition of the tax
field, but it looks like the printSales(...)
function is never called in, so nothing will be drawn.