please I need help on my java Applet

From: java10 (dragonmen_at_fastmail.co.uk)
Date: 12/22/04


Date: Wed, 22 Dec 2004 07:08:52 -0500


//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);"
                        }*/
                        
                }
                }

}



Relevant Pages

  • Re: sum sub levels.
    ... Column B has amount, ... You'll notice that I have to add (PAYMENTS, TRANSFERS AND SUBSIDIES, ... I should do the same for (TAX RECEIPTS, SALES GOODS & SERV NON CAP ASS, ...
    (microsoft.public.excel.misc)
  • Re: What would you do with the money ?
    ... was one of those that pay installments for several years. ... half of the value of the winnings in one lump sum. ... that means giving up a substantial amount of the prize. ... die before you receive many of those payments. ...
    (misc.transport.trucking)
  • Re: Reading google groups, 101. [was: Unangband questions]
    ... always accompanies any kind of online thing that requires payments, ... walking out with merchandise, I refuse to participate. ... and the sheer amount of spam I get trying to ...
    (rec.games.roguelike.angband)
  • Re: Endowment compensation - complex example
    ... month to month from the loan amount and the payments made. ... process must be used to calculate the monthly payment. ...
    (uk.finance)
  • Forms Automatic Entry
    ... form On Exit of the last Field the User inputs data ... specify the Amount Due associated with the Current ... >Payments) with every entry. ...
    (microsoft.public.access.formscoding)