/* Cart * Anderson, Franceschi */ import javax.swing.JFrame; import java.awt.Gra
ID: 3827678 • Letter: #
Question
/* Cart
* Anderson, Franceschi
*/
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Color;
import java.text.DecimalFormat;
public class Cart
{
Item[] items;
int itemSize = 4;
int currentItem;
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 );
//
//
items[3] = new Divider( -0.99 );
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 double getTotal( )
{
return currentTotal;
}
public void setExactTotal( double newExactTotal )
{
exactTotal = newExactTotal;
}
public Color getBackground( )
{
return background;
}
public void draw( Graphics g )
{
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 displayStudentTotal = "Your subtotal = " + money.format( currentTotal );
String displayExactTotal = "Correct subtotal = " + money.format( exactTotal );
g.setColor( Color.BLUE );
g.drawString( displayStudentTotal, 20, 75 );
g.drawString( displayExactTotal, 20, 100 );
if ( currentItem != -1 )
items[currentItem].draw( g, 50, 200, 200, background );
}
}
/* Cashier class
Anderson, Franceschi
*/
import java.awt.Graphics;
import java.awt.Color;
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 1");
cart = new Cart();
previousItem = null;
currentTotal = 0.0;
getContentPane().setBackground(cart.getBackground());
setSize(300, 300);
setVisible(true);
}
public void checkout()
{
/* ***** Student writes the body of this method ***** */
//
// Using a while loop, calculate the total price
// of the groceries
//
// The getNext method (in this Cashier class) returns the
// next item on the conveyor belt, which is an Item object
// (we do not know which item and we do not know how many items
// are in the cart - this is randomly generated).
// getNext does not take any arguments. Its API is:
// Item getNext()
//
// Right after you update the current subtotal,
// 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 instance method getPrice of the Item class
// returns the price of the Item object.
// The method getPrice does not take any arguments.
// Its API is:
// double getPrice()
//
// The cart is empty when the getNext method returns
// the divider Item.
// You detect the divider item because its price
// is -0.99. So an Item with a price of -0.99
// is the sentinel value for the loop.
//
// After you scan the divider, display the total
// for the cart in a dialog box.
// Student code starts here:
// Student code ends here.
}
public Item getNext()
{
// get next item
// if the first item is the divider, that is ok - the cart is empty
int number = ((int) (Math.random() * cart.getItemSize()));
cart.setCurrentItem(number);
// update previousItem so that we can keep track of the current total
previousItem = cart.getItems()[cart.getCurrentItem()];
// update currentTotal
if ((previousItem != null) && (previousItem.getPrice() >= 0))
currentTotal += previousItem.getPrice();
cart.setExactTotal(currentTotal);
// animate divider if necessary
if (number == 3)
animate(cart.getTotal());
return (cart.getItems())[cart.getCurrentItem()];
}
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);
app.checkout();
}
}
/* Cereal
* Anderson, Franceschi
*/
import java.awt.Graphics;
import java.awt.Color;
import java.text.DecimalFormat;
public class Cereal extends Item
{
public Cereal( double p )
{
super( p );
}
public void draw( Graphics g, int startX, int endX, int y, Color eraseColor )
{
DecimalFormat money = new DecimalFormat( "$0.00" );
String display1 = "Cereal: Unit price = " + money.format( price );
g.setColor( Color.BLUE );
g.drawString( display1, 20, 50 );
for ( int x = startX; x < endX; x += 5 )
{
g.setColor( new Color( 255, 255, 51 ) );
g.fillRect( x, y - 68, 48, 65 );
g.setColor( Color.BLACK );
g.drawString("Cereal", x + 5, y - 45 );
g.setColor( Color.RED );
g.drawOval( x + 18, y - 35, 8, 8 );
g.drawOval( x + 13, y - 30, 8, 8 );
g.drawOval( x + 23, y - 30, 8, 8 );
g.fillArc( x + 10, y - 35, 24, 25, 0, -180 );
try {
Thread.sleep( ( int )( 100 ) );
}
catch ( InterruptedException e )
{
e.printStackTrace( );
}
// erase
g.setColor( eraseColor ); // background
g.fillRect( x, y - 70, 50, 70 );
}
}
}
/** Divider
* Anderson, Franceschi
*/
import java.awt.Graphics;
import java.awt.Color;
import java.text.DecimalFormat;
public class Divider extends Item
{
public Divider( double p )
{
super( p );
}
public void draw( Graphics g, int startX, int endX, int y, Color eraseColor )
{
DecimalFormat money = new DecimalFormat( "$0.00");
for ( int x = startX; x < endX; x += 5 )
{
g.setColor( Color.YELLOW );
g.fillRoundRect( x, y - 20, 63, 18, 5, 5 );
g.setColor( Color.BLACK );
g.drawRoundRect( x, y - 20, 63, 18, 5, 5 );
g.drawString("Thank you!", x + 2, y - 5 );
try
{
Thread.sleep( ( int )( 100 ) );
}
catch ( InterruptedException e )
{
e.printStackTrace( );
}
// erase
g.setColor( eraseColor );
g.fillRect( x, y - 70, 65, 70 );
}
}
}
/* Item
* Anderson, Franceschi
*/
import java.awt.Graphics;
import java.awt.Color;
public abstract class Item
{
protected double price;
public Item( double p )
{
price = p;
}
public void setPrice( double newPrice )
{
price = ( newPrice > 0 ? newPrice : 0 );
}
public double getPrice( )
{
return price;
}
public abstract void draw( Graphics g, int startX, int endX, int y, Color eraseColor );
}
/* Milk
* Anderson, Franceschi
*/
import java.awt.Graphics;
import java.awt.Color;
import java.text.DecimalFormat;
public class Milk extends Item
{
public Milk( double p )
{
super( p );
}
public void draw( Graphics g, int startX, int endX, int y, Color eraseColor )
{
DecimalFormat money = new DecimalFormat( "$0.00" );
String display1 = "Milk: Unit price = " + money.format( price );
g.setColor( Color.BLUE );
g.drawString( display1, 20, 50 );
for ( int x = startX; x < endX; x += 5 )
{
g.setColor( Color.WHITE );
g.fillRect( x +1, y - 67, 30, 65 );
g.setColor( Color.LIGHT_GRAY );
g.drawLine( x, y - 55, x + 30, y - 55 );
g.drawRect( x, y - 68, 31, 66 );
g.setColor( Color.BLACK );
g.drawString( "Milk", x + 3, y - 44 );
try {
Thread.sleep( ( int )( 100 ) );
}
catch ( InterruptedException e )
{
e.printStackTrace( );
}
// erase
g.setColor( eraseColor );
g.fillRect( x, y - 70, 50, 70 );
}
}
}
/* OrangeJuice
* Anderson, Franceschi
*/
import java.awt.Graphics;
import java.awt.Color;
import java.text.DecimalFormat;
public class OrangeJuice extends Item
{
public OrangeJuice( double p )
{
super( p );
}
public void draw( Graphics g, int startX, int endX, int y, Color eraseColor )
{
DecimalFormat money = new DecimalFormat( "$0.00" );
String display1 = "Orange Juice: Unit price = " + money.format( price );
g.setColor( Color.BLUE );
g.drawString( display1, 20, 50 );
for ( int x = startX; x < endX; x += 5 )
{
g.setColor( new Color( 244, 244, 102 ) );
g.fillRect( x, y - 68, 30, 65 );
g.setColor( Color.LIGHT_GRAY );
g.drawLine( x, y - 52, x + 30, y - 52 );
g.setColor( Color.BLACK );
g.drawString("OJ", x + 7, y - 38 );
g.setColor( new Color( 255, 132, 41 ) );
g.fillOval( x + 8, y - 33, 15, 17 );
try
{
Thread.sleep( ( int )( 100 ) );
}
catch ( InterruptedException e )
{
e.printStackTrace( );
}
// erase
g.setColor( eraseColor );
g.fillRect( x, y - 70, 50, 70 );
}
}
}
Explanation / Answer
Ans.
Cashier Class
package javaapplication6;
import java.awt.Color;
import java.awt.Graphics;
import java.text.DecimalFormat;
import java.util.Scanner;
import javax.swing.JFrame;
/*
Author Rashmi Tiwari
*/
public class Cashier extends JFrame
{
private Cart cart;
private Item previousItem;
private double currentTotal;
public Cashier(double priceCereal,double priceMilk,double priceOrangeJuice,double priceDivider)
{
super("Chapter 6 Programming Activity 1");
cart = new Cart(priceCereal,priceMilk,priceOrangeJuice,priceDivider);
previousItem = null;
currentTotal = 0.0;
getContentPane().setBackground(cart.getBackground());
setSize(300, 300);
setVisible(true);
}
public void checkout(Cashier c)
{
double check=1;
while(check>0){
Item i= c.getNext();
currentTotal+=i.getPrice();
animate(currentTotal);
check=i.getPrice();
}
}
public Item getNext()
{
// get next item
// if the first item is the divider, that is ok - the cart is empty
int number = ((int) (Math.random() * cart.getItemSize()));
cart.setCurrentItem(number);
// update previousItem so that we can keep track of the current total
previousItem = cart.getItems()[cart.getCurrentItem()];
// update currentTotal
if ((previousItem != null) && (previousItem.getPrice() >= 0))
currentTotal += previousItem.getPrice();
cart.setExactTotal(currentTotal);
// animate divider if necessary
if (number == 3)
animate(cart.getTotal());
return (cart.getItems())[cart.getCurrentItem()];
}
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);
DecimalFormat money = new DecimalFormat( "$0.00" );
String displayStudentTotal = "Your subtotal = " + money.format( currentTotal );
g.setColor( Color.BLUE );
g.drawString( displayStudentTotal, 20, 150 );
}
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter price for cereal");
double priceCereal=sc.nextDouble();
System.out.println("Enter price for Milk");
double priceMilk=sc.nextDouble();
System.out.println("Enter price for orange juice");
double priceOrangeJuice=sc.nextDouble();
System.out.println("Enter price for Divider");
double priceDivider=sc.nextDouble();
Cashier app = new Cashier(priceCereal,priceMilk,priceOrangeJuice,priceDivider);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.checkout(app);
}
}
Cereal Class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication6;
import java.awt.Color;
import java.awt.Graphics;
import java.text.DecimalFormat;
/**
*
* @author Rashmi Tiwari
*/
public class Cereal extends Item
{
public Cereal( double p )
{
super( p );
}
public void draw( Graphics g, int startX, int endX, int y, Color eraseColor )
{
DecimalFormat money = new DecimalFormat( "$0.00" );
String display1 = "Cereal: Unit price = " + money.format( price );
g.setColor( Color.BLUE );
g.drawString( display1, 20, 50 );
for ( int x = startX; x < endX; x += 5 )
{
g.setColor( new Color( 255, 255, 51 ) );
g.fillRect( x, y - 68, 48, 65 );
g.setColor( Color.BLACK );
g.drawString("Cereal", x + 5, y - 45 );
g.setColor( Color.RED );
g.drawOval( x + 18, y - 35, 8, 8 );
g.drawOval( x + 13, y - 30, 8, 8 );
g.drawOval( x + 23, y - 30, 8, 8 );
g.fillArc( x + 10, y - 35, 24, 25, 0, -180 );
try {
Thread.sleep( ( int )( 100 ) );
}
catch ( InterruptedException e )
{
e.printStackTrace( );
}
// erase
g.setColor( eraseColor ); // background
g.fillRect( x, y - 70, 50, 70 );
}
}
}
//Divider Class
package javaapplication6;
import java.awt.Color;
import java.awt.Graphics;
import java.text.DecimalFormat;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Rashmi Tiwari
*/
public class Divider extends Item
{
public Divider( double p )
{
super( p );
}
public void draw( Graphics g, int startX, int endX, int y, Color eraseColor )
{
DecimalFormat money = new DecimalFormat( "$0.00");
for ( int x = startX; x < endX; x += 5 )
{
g.setColor( Color.YELLOW );
g.fillRoundRect( x, y - 20, 63, 18, 5, 5 );
g.setColor( Color.BLACK );
g.drawRoundRect( x, y - 20, 63, 18, 5, 5 );
g.drawString("Thank you!", x + 2, y - 5 );
try
{
Thread.sleep( ( int )( 100 ) );
}
catch ( InterruptedException e )
{
e.printStackTrace( );
}
// erase
g.setColor( eraseColor );
g.fillRect( x, y - 70, 65, 70 );
}
}
}
//Item Class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication6;
import java.awt.Color;
import java.awt.Graphics;
/**
*
* @author Rashmi Tiwari
*/
public abstract class Item
{
protected double price;
public Item( double p )
{
price = p;
}
public void setPrice( double newPrice )
{
price = ( newPrice > 0 ? newPrice : 0 );
}
public double getPrice( )
{
return price;
}
public abstract void draw( Graphics g, int startX, int endX, int y, Color eraseColor );
}
//Milk Class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication6;
import java.awt.Color;
import java.awt.Graphics;
import java.text.DecimalFormat;
/**
*
* @author Rashmi Tiwari
*/
public class Milk extends Item
{
public Milk( double p )
{
super( p );
}
public void draw( Graphics g, int startX, int endX, int y, Color eraseColor )
{
DecimalFormat money = new DecimalFormat( "$0.00" );
String display1 = "Milk: Unit price = " + money.format( price );
g.setColor( Color.BLUE );
g.drawString( display1, 20, 50 );
for ( int x = startX; x < endX; x += 5 )
{
g.setColor( Color.WHITE );
g.fillRect( x +1, y - 67, 30, 65 );
g.setColor( Color.LIGHT_GRAY );
g.drawLine( x, y - 55, x + 30, y - 55 );
g.drawRect( x, y - 68, 31, 66 );
g.setColor( Color.BLACK );
g.drawString( "Milk", x + 3, y - 44 );
try {
Thread.sleep( ( int )( 100 ) );
}
catch ( InterruptedException e )
{
e.printStackTrace( );
}
// erase
g.setColor( eraseColor );
g.fillRect( x, y - 70, 50, 70 );
}
}
}
//Orange Juice Class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication6;
import java.awt.Color;
import java.awt.Graphics;
import java.text.DecimalFormat;
/**
*
* @author Rashmi Tiwari
*/
public class OrangeJuice extends Item
{
public OrangeJuice( double p )
{
super( p );
}
public void draw( Graphics g, int startX, int endX, int y, Color eraseColor )
{
DecimalFormat money = new DecimalFormat( "$0.00" );
String display1 = "Orange Juice: Unit price = " + money.format( price );
g.setColor( Color.BLUE );
g.drawString( display1, 20, 50 );
for ( int x = startX; x < endX; x += 5 )
{
g.setColor( new Color( 244, 244, 102 ) );
g.fillRect( x, y - 68, 30, 65 );
g.setColor( Color.LIGHT_GRAY );
g.drawLine( x, y - 52, x + 30, y - 52 );
g.setColor( Color.BLACK );
g.drawString("OJ", x + 7, y - 38 );
g.setColor( new Color( 255, 132, 41 ) );
g.fillOval( x + 8, y - 33, 15, 17 );
try
{
Thread.sleep( ( int )( 100 ) );
}
catch ( InterruptedException e )
{
e.printStackTrace( );
}
// erase
g.setColor( eraseColor );
g.fillRect( x, y - 70, 50, 70 );
}
}
}
//Cart Class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication6;
/**
*
* @author Rashmi Tiwari
*/
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Color;
import java.text.DecimalFormat;
public class Cart
{
Item[] items;
int itemSize = 4;
int currentItem;
double currentTotal;
double exactTotal;
Color background = new Color( 205, 205, 205 );
public Cart(double priceCereal,double priceMilk,double priceOrangeJuice,double priceDivider )
{
items = new Item[ itemSize ];
items[0] = new Milk(priceMilk );
items[1] = new Cereal( priceCereal);
items[2] = new OrangeJuice( priceOrangeJuice );
//
//
items[3] = new Divider( priceDivider );
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 double getTotal( )
{
return currentTotal;
}
public void setExactTotal( double newExactTotal )
{
exactTotal = newExactTotal;
}
public Color getBackground( )
{
return background;
}
public void draw( Graphics g )
{
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 displayStudentTotal = "Your subtotal = " + money.format( currentTotal );
String displayExactTotal = "Correct subtotal = " + money.format( exactTotal );
g.setColor( Color.BLUE );
g.drawString( displayStudentTotal, 20, 75 );
g.drawString( displayExactTotal, 20, 100 );
if ( currentItem != -1 )
items[currentItem].draw( g, 50, 200, 200, background );
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.