J
java10
//i will be grateful if someone can rewrite this code for me, what i
trying to do i the comment below, it is abiut a supermarket checkout
system ,customers shop,pay,receive change
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.applet.Applet;
//<APPLET code = shopping21.class width=400 height=250> </APPLET>
public class shopping21 extends Applet implements ActionListener {
private Choice food = new Choice();
private Choice number = new Choice();
private Button AddItem =new Button("Add");
private Button Show = new Button ("show cost");
private double [] price ={0.35, 1.00, 0.50, 1.15, 2.50, 0.50,
1.30, 0.75, 0.65, 1.00};
private static DecimalFormat twoDP = new
DecimalFormat("0.00");
private double bill=0;
private List list;
private String[] s;
public void init () {
add(food);
food.addItem("Select Item");
food.addItem("Beans");
food.addItem("cornflakes");
food.addItem("sugar");
food.addItem("Tea bag");
food.addItem("coffee");
food.addItem("Bread");
food.addItem("Sausage");
food.addItem("Eggs");
food.addItem("Milk");
food.addItem("Potatoes");
add(number);
number.addItem("----");
number.addItem("1");
number.addItem("2");
number.addItem("3");
number.addItem("4");
number.addItem("5");
number.addItem("6");
number.addItem("7");
number.addItem("8");
number.addItem("9");
number.addItem("10");
number.addItem("11");
number.addItem("12");
number.addItem("13");
number.addItem("14");
number.addItem("15");
number.addItem("16");
number.addItem("17");
number.addItem("18");
number.addItem("19");
number.addItem("20");
add(Show);
Show.addActionListener(this);
add(AddItem);
AddItem.addActionListener(this);
list= new List (10,true); //i am trying to list the product
selected by customer, but didn't work
list.add("Beans");
list.add("cornflakes");
list.add("sugar");
list.add("Tea bag");
list.add("coffee");
list.add("Bread");
list.add("Sausage");
list.add("Eggs");
list.add("Milk");
list.add("Potatoes");
add(list);
list.addActionListener(this);
s = list.getSelectedItems();
}
/**
* calculates the total of the sale from the lineItem subtotals
* @return total of the sale
*/
public double calcPayments() {
paymentsTotal = 0.0;
Iterator i = payments.iterator();
while (i.hasNext()) paymentsTotal += ((Payment)i.next()).getAmount();
return total;
}
/**
* calculates the total of the sale from the lineItem subtotals
* @return total of the sale
*/
public double calcTotal() {
total = 0.0;
Iterator i = lineItems.iterator();
while (i.hasNext()) total += ((LineItem)i.next()).calcTotal();
return total;
}
/**
* retrieves the cached total of the sale
* @return total of sale, 0.0 if calcTotal() has not yet been called
*/
public double getTotal() {
return total;
}
/**
* sets the status of the sale to compltete if payments equal price
* @exception Exce[tion thrown if payments do not equal price
*/
public void complete() throws Exception {
if (calcTotal() != calcPayments()) {
throw new Exception("Payments do not equal total price");
}
status = "Complete";
}
/** status of sale */
private String status = "Incomplete";
/**
* adds a line item for a quantity of a type of item
* @param product the type of item being sold
* @param qty the quantity of the item being sold
*/
public void addLineItem(Product product, int qty) throws Exception {
if (status.equals("Incomplete")) {
lineItems.add(new LineItem(product, qty));
}
else {
throw new Exception("Cannot add items to a completed sale");
}
}
//CashPayment Class
/**
* a subclass that extends the Payment class to represent cash payments
* @stereotype moment-interval
*/
public class CashPayment extends Payment {
/**
* mandatory value constructor requires values for all attributes needed
to
* put the new object into a valid state
*
* @param amount the amount tendered for this payment
*/
public CashPayment(double amount) {
super(amount);
}
/**
* amount tendered is what was passed into the constructor
* @return amount tendered
*/
public double getAmountTendered() {
return super.getAmount();
}
/**
* calculate the change to be given for a required amount
* @return value of change
*/
public double calcChange( double requiredAmount ) {
change = super.getAmount() - requiredAmount;
return change;
}
/**
* override superclass to calc actual amount based on change given
* @return amountTendered - change given
*/
public double getAmount() {
return super.getAmount() - change;
}
/** amopunt of change given */
private double change = 0.0;
//the line below may not necessary be in code
/** public void actionPerformed (ActionEvent e) {
s = list.getSelectedItems();
repaint();
if (e.getSource()== AddItem)
{
int index = food.getSelectedIndex();
int num = number.getSelectedIndex()+1;
bill +=price[index]*num;
}
else
{
JOptionPane.showMessageDialog(this, "please pay this amount £" +
twoDP.format(bill));
JOptionPane.showMessageDialog(this, "thank you for shopping and visit us
again");
JOptionPane.showMessageDialog(this, "I am ");
}
public void paint (Graphics g) {
for ((int i=0; i< s.lenght;i++){
g.drawSting(s(i),50, 150, 100+20*i);"
}*/
}
}
}
trying to do i the comment below, it is abiut a supermarket checkout
system ,customers shop,pay,receive change
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.applet.Applet;
//<APPLET code = shopping21.class width=400 height=250> </APPLET>
public class shopping21 extends Applet implements ActionListener {
private Choice food = new Choice();
private Choice number = new Choice();
private Button AddItem =new Button("Add");
private Button Show = new Button ("show cost");
private double [] price ={0.35, 1.00, 0.50, 1.15, 2.50, 0.50,
1.30, 0.75, 0.65, 1.00};
private static DecimalFormat twoDP = new
DecimalFormat("0.00");
private double bill=0;
private List list;
private String[] s;
public void init () {
add(food);
food.addItem("Select Item");
food.addItem("Beans");
food.addItem("cornflakes");
food.addItem("sugar");
food.addItem("Tea bag");
food.addItem("coffee");
food.addItem("Bread");
food.addItem("Sausage");
food.addItem("Eggs");
food.addItem("Milk");
food.addItem("Potatoes");
add(number);
number.addItem("----");
number.addItem("1");
number.addItem("2");
number.addItem("3");
number.addItem("4");
number.addItem("5");
number.addItem("6");
number.addItem("7");
number.addItem("8");
number.addItem("9");
number.addItem("10");
number.addItem("11");
number.addItem("12");
number.addItem("13");
number.addItem("14");
number.addItem("15");
number.addItem("16");
number.addItem("17");
number.addItem("18");
number.addItem("19");
number.addItem("20");
add(Show);
Show.addActionListener(this);
add(AddItem);
AddItem.addActionListener(this);
list= new List (10,true); //i am trying to list the product
selected by customer, but didn't work
list.add("Beans");
list.add("cornflakes");
list.add("sugar");
list.add("Tea bag");
list.add("coffee");
list.add("Bread");
list.add("Sausage");
list.add("Eggs");
list.add("Milk");
list.add("Potatoes");
add(list);
list.addActionListener(this);
s = list.getSelectedItems();
}
/**
* calculates the total of the sale from the lineItem subtotals
* @return total of the sale
*/
public double calcPayments() {
paymentsTotal = 0.0;
Iterator i = payments.iterator();
while (i.hasNext()) paymentsTotal += ((Payment)i.next()).getAmount();
return total;
}
/**
* calculates the total of the sale from the lineItem subtotals
* @return total of the sale
*/
public double calcTotal() {
total = 0.0;
Iterator i = lineItems.iterator();
while (i.hasNext()) total += ((LineItem)i.next()).calcTotal();
return total;
}
/**
* retrieves the cached total of the sale
* @return total of sale, 0.0 if calcTotal() has not yet been called
*/
public double getTotal() {
return total;
}
/**
* sets the status of the sale to compltete if payments equal price
* @exception Exce[tion thrown if payments do not equal price
*/
public void complete() throws Exception {
if (calcTotal() != calcPayments()) {
throw new Exception("Payments do not equal total price");
}
status = "Complete";
}
/** status of sale */
private String status = "Incomplete";
/**
* adds a line item for a quantity of a type of item
* @param product the type of item being sold
* @param qty the quantity of the item being sold
*/
public void addLineItem(Product product, int qty) throws Exception {
if (status.equals("Incomplete")) {
lineItems.add(new LineItem(product, qty));
}
else {
throw new Exception("Cannot add items to a completed sale");
}
}
//CashPayment Class
/**
* a subclass that extends the Payment class to represent cash payments
* @stereotype moment-interval
*/
public class CashPayment extends Payment {
/**
* mandatory value constructor requires values for all attributes needed
to
* put the new object into a valid state
*
* @param amount the amount tendered for this payment
*/
public CashPayment(double amount) {
super(amount);
}
/**
* amount tendered is what was passed into the constructor
* @return amount tendered
*/
public double getAmountTendered() {
return super.getAmount();
}
/**
* calculate the change to be given for a required amount
* @return value of change
*/
public double calcChange( double requiredAmount ) {
change = super.getAmount() - requiredAmount;
return change;
}
/**
* override superclass to calc actual amount based on change given
* @return amountTendered - change given
*/
public double getAmount() {
return super.getAmount() - change;
}
/** amopunt of change given */
private double change = 0.0;
//the line below may not necessary be in code
/** public void actionPerformed (ActionEvent e) {
s = list.getSelectedItems();
repaint();
if (e.getSource()== AddItem)
{
int index = food.getSelectedIndex();
int num = number.getSelectedIndex()+1;
bill +=price[index]*num;
}
else
{
JOptionPane.showMessageDialog(this, "please pay this amount £" +
twoDP.format(bill));
JOptionPane.showMessageDialog(this, "thank you for shopping and visit us
again");
JOptionPane.showMessageDialog(this, "I am ");
}
public void paint (Graphics g) {
for ((int i=0; i< s.lenght;i++){
g.drawSting(s(i),50, 150, 100+20*i);"
}*/
}
}
}