/* Cashier class Anderson, Franceschi */ import java.awt.Color; import java.awt.
ID: 3745795 • Letter: #
Question
/* Cashier class
Anderson, Franceschi
*/
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class Cashier extends JFrame
{
private Cart cart;
private Item previousItem;
private double currentTotal;
public Cashier()
{
super("Chapter 6 Programming Activity 2");
cart = new Cart();
previousItem = null;
currentTotal = 0.0;
getContentPane().setBackground(cart.getBackground());
setSize(325, 300);
setVisible(true);
}
public void checkout(int numberOfItems)
{
/* ***** Student writes the body of this method ***** */
//
// The parameter of this method, numberOfItems,
// represents the number of items in the cart. The
// user will be prompted for this number.
//
// Using a for loop, calculate the total price
// of the groceries for the cart.
//
// The getNext method (in this Cashier class) returns the next
// item in the cart, which is an Item object (we do not
// know which item will be returned; this is randomly generated).
// getNext does not take any arguments. its API is
// Item getNext()
//
// As the last statement of the body of your for loop,
// you should call the animate method.
// The animate method takes one parameter: a double,
// which is your current subtotal.
// For example, if the name of your variable representing
// the current subtotal is total, your call to the animate
// method should be:
// animate(total);
//
// The getPrice method of the Item class
// returns the price of the Item object as a double.
// The getPrice method does not take any arguments. Its API is
// double getPrice()
//
// After you have processed all the items, display the total
// for the cart in a dialog box.
// Student code starts here:
// Student code ends here.
}
public Item getNext()
{
if (cart.getTotalNumberItems() > cart.getNumberItems())
{
// get next item
cart.setCurrentItem ((int) (Math.random() * cart.getItemSize()));
// update previousItem so that we can keep track of the current total
previousItem = cart.getItems()[cart.getCurrentItem()];
// update number of items in cart
cart.updateNumberItems();
// update currentTotal
if ((previousItem != null) && (previousItem.getPrice() >= 0))
currentTotal += previousItem.getPrice();
cart.setExactTotal(currentTotal);
return (cart.getItems())[cart.getCurrentItem()];
}
else
{
JOptionPane.showMessageDialog(null, "Error: getNext() method called when cart is empty",
"Logic error", JOptionPane.ERROR_MESSAGE);
return null;
}
}
public void animate(double subtotal)
{
cart.updateTotal(subtotal);
repaint();
try {
Thread.sleep(3000); // wait for the animation to finish
}
catch (Exception e)
{
}
}
public void paint(Graphics g)
{
super.paint(g);
cart.draw(g);
}
public static void main(String [] args)
{
Cashier app = new Cashier();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int numItems = 0;
boolean goodInput = false;
do
{
String howMany = JOptionPane.showInputDialog(null,
"Enter the number of items in the cart (1 - 10)");
if (howMany == null)
System.exit(0);
try
{
numItems = Integer.parseInt(howMany);
goodInput = true;
}
catch(NumberFormatException nfe)
{
// goodInput is still false
}
} while (!goodInput || numItems < 1 || numItems > 10);
(app.cart).updateTotalNumberItems(numItems);
app.checkout(numItems);
}
}
Explanation / Answer
Providing the Cart.java and cashier.java . Changes are made at constructor of Cart.java and checkout() method in Cashier.java file.
//////////////////////////////Cart.java ////////////////////////////////////
package test.sorter;
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Color;
import java.text.DecimalFormat;
public class Cart{
Item[] items;
int itemSize = 3;
int currentItem;
int currentNumberItems;
int totalNumberItems;
double currentTotal;
double exactTotal;
Color background = new Color( 205, 205, 205 );
public Cart( )
{
items = new Item[itemSize];
//
// Student can modify constructors' arguments below
// Check out the 3 constructors
// argument represents price
//
items[0] = new Milk( 2.00 );
items[1] = new Cereal( 3.50 );
items[2] = new OrangeJuice( 3.00 );
//
//
currentNumberItems = 0;
totalNumberItems = 0;
currentTotal = 0.0;
exactTotal = 0.0;
currentItem = -1;
}
//
//Student can modify constructors' arguments below
//Check out the 3 constructors
//argument represents price
//
public Cart(double priceMilk,double priceCereal,double priceOrange)
{
items = new Item[itemSize];
items[0] = new Milk(priceMilk);
items[1] = new Cereal(priceCereal);
items[2] = new OrangeJuice(priceOrange);
//
//
currentNumberItems = 0;
totalNumberItems = 0;
currentTotal = 0.0;
exactTotal = 0.0;
currentItem = -1;
}
public void setCurrentItem( int ci )
{
currentItem = ci;
}
public int getCurrentItem( )
{
return currentItem;
}
public Item [] getItems( )
{
return items;
}
public int getItemSize( )
{
return itemSize;
}
public void updateTotal( double newCurrentTotal )
{
currentTotal = newCurrentTotal;
}
public void updateNumberItems( )
{
currentNumberItems++;
}
public int getNumberItems( )
{
return currentNumberItems;
}
public void updateTotalNumberItems( int newTotalNumberItems )
{
totalNumberItems = newTotalNumberItems;
}
public int getTotalNumberItems( )
{
return totalNumberItems;
}
public void setExactTotal( double newExactTotal )
{
exactTotal = newExactTotal;
}
public Color getBackground( )
{
return background;
}
public void draw( Graphics g )
{
g.setColor( Color.black );
g.drawString( "EXPRESS LANE", 125, 50 );
g.setColor( Color.black );
g.fillRoundRect( 50, 200, 150, 10, 2, 2 ); // belt
g.setColor( new Color( 220, 110, 55 ) );
g.fill3DRect( 195, 200, 60, 70, true ); // bag
DecimalFormat money = new DecimalFormat( "$0.00" );
String displayItemNumber = "Item # " + currentNumberItems + " of " + totalNumberItems;
String displayStudentTotal = "Your subtotal = " + money.format( currentTotal );
String displayExactTotal = "Correct subtotal = " + money.format( exactTotal );
g.setColor( Color.blue );
g.drawString( displayItemNumber, 220, 80 );
g.drawString( displayStudentTotal, 20, 90 );
g.drawString( displayExactTotal, 20, 115 );
if ( currentItem != -1 )
items[currentItem].draw( g, 50, 200, 200, background );
}
}
//////////////////////////////////////////////////////Cashier.java ///////////////////////////////////////
package test.sorter;
/* Cashier class
Anderson, Franceschi
*/
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class Cashier extends JFrame
{
private Cart cart;
private Item previousItem;
private double currentTotal;
public Cashier()
{
super("Chapter 6 Programming Activity 2");
cart = new Cart();
previousItem = null;
currentTotal = 0.0;
getContentPane().setBackground(cart.getBackground());
setSize(325, 300);
setVisible(true);
}
public void checkout(int numberOfItems)
{
/* ***** Student writes the body of this method ***** */
//
// The parameter of this method, numberOfItems,
// represents the number of items in the cart. The
// user will be prompted for this number.
//
// Using a for loop, calculate the total price
// of the groceries for the cart.
//
// The getNext method (in this Cashier class) returns the next
// item in the cart, which is an Item object (we do not
// know which item will be returned; this is randomly generated).
// getNext does not take any arguments. its API is
// Item getNext()
//
// As the last statement of the body of your for loop,
// you should call the animate method.
// The animate method takes one parameter: a double,
// which is your current subtotal.
// For example, if the name of your variable representing
// the current subtotal is total, your call to the animate
// method should be:
// animate(total);
//
// The getPrice method of the Item class
// returns the price of the Item object as a double.
// The getPrice method does not take any arguments. Its API is
// double getPrice()
//
// After you have processed all the items, display the total
// for the cart in a dialog box.
// Student code starts here:
double totalPrice=0;//Intial total price is 0
for(int i=0;i<numberOfItems;i++)
{
//Calling the getNext() method for the next item
totalPrice=totalPrice+getNext().getPrice();
//calling the animate method
animate(totalPrice);
}
//Displaying the dialogue boxs
JOptionPane.showMessageDialog(null, "Total Price is "+totalPrice);
// Student code ends here.
}
public Item getNext()
{
if (cart.getTotalNumberItems() > cart.getNumberItems())
{
// get next item
cart.setCurrentItem ((int) (Math.random() * cart.getItemSize()));
// update previousItem so that we can keep track of the current total
previousItem = cart.getItems()[cart.getCurrentItem()];
// update number of items in cart
cart.updateNumberItems();
// update currentTotal
if ((previousItem != null) && (previousItem.getPrice() >= 0))
currentTotal += previousItem.getPrice();
cart.setExactTotal(currentTotal);
return (cart.getItems())[cart.getCurrentItem()];
}
else
{
JOptionPane.showMessageDialog(null, "Error: getNext() method called when cart is empty",
"Logic error", JOptionPane.ERROR_MESSAGE);
return null;
}
}
public void animate(double subtotal)
{
cart.updateTotal(subtotal);
repaint();
try {
Thread.sleep(3000); // wait for the animation to finish
}
catch (Exception e)
{
}
}
public void paint(Graphics g)
{
super.paint(g);
cart.draw(g);
}
public static void main(String [] args)
{
Cashier app = new Cashier();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int numItems = 0;
boolean goodInput = false;
do
{
String howMany = JOptionPane.showInputDialog(null,
"Enter the number of items in the cart (1 - 10)");
if (howMany == null)
System.exit(0);
try
{
numItems = Integer.parseInt(howMany);
goodInput = true;
}
catch(NumberFormatException nfe)
{
// goodInput is still false
}
} while (!goodInput || numItems < 1 || numItems > 10);
(app.cart).updateTotalNumberItems(numItems);
app.checkout(numItems);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.