⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 till.java

📁 Java经典例程 从外国一大学计算机教授出版物下载的代码 经典
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;

public class Till extends Frame implements ActionListener {

  /*  The Grocery Till program  by J M Bishop Oct 1996
   *  ------------------------  Java 1.1 T Abbott and J M Bishop Oct 1997
   *                            updated August 2000
   *
   *  Simulates the operation of a grocery till for
   *  up to 12 products, together with the costs per
   *  kilogram.
   *
   *  Illustrates Panels, different layout managers,
   *  (including grid), user event handlers and the
   *  handling of events that should occur in a
   *  specified order.
   */

    private TextField weighField, totalField;
    private Button[] itemButtons;
    private Button weighButton, printButton, closeButton;

    public Till() {
      setLayout(new BorderLayout());

      Panel p = new Panel();
        p.setLayout(new FlowLayout());
        totalField = new TextField(6);
        totalField.setEditable(false);
        p.add(new Label("COST"));
        p.add(totalField);
      add("North", p);

      p = new Panel();
        p.setLayout(new GridLayout(5, 3));
        itemButtons = new Button[items.length];
        for (int i = 0; i < items.length; i++) {
          itemButtons[i] = new Button(items[i]);
          itemButtons[i].addActionListener(this);
          p.add(itemButtons[i]);
        }
        weighButton = new Button("WEIGH");
          weighButton.addActionListener(this);
          p.add(weighButton);
        printButton = new Button("PRINT");
          printButton.addActionListener(this);
          p.add(printButton);
        closeButton = new Button("CLOSE");
          closeButton.addActionListener(this);
          p.add(closeButton);
      add("Center", p);

      p = new Panel();
        p.setLayout(new FlowLayout());
        weighField = new TextField(4);
          weighField.setEditable(false);
          weighField.addActionListener(this);
          p.add(weighField);
        p.add(new Label("MASS"));
        p.add(new Label("Type return after the amount"));
      add("South", p);

      // set up the frame
      addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          System.exit(0);
        }
      });
      setTitle("Savanna Grocers");
      setVisible(true);
      pack();
    }

    public void actionPerformed(ActionEvent e) {
      Object source = e.getSource();
      if (source == closeButton) {
        System.exit(0);
      } else
      if (source == printButton) {
        printItems();
      } else
      if (source == weighButton) {
        resetWeighField();
      } else
      if (source == weighField) {
        readWeighField();
      } else
        selectItem(e.getActionCommand());
    }

   /* Here follows the main control of the program.
    * The event and action handlers above call these
    * methods, which ensure that there is only a reaction
    * if certain conditions (e.g. other previous events)
    * have already been met.
    */

    public void resetWeighField() {
      weighField.setText("");
      weighField.setEditable(true);
    }

    public void readWeighField() {
      if (weighField.isEditable()) {
        weighField.setEditable(false);
        weighField.selectAll();
        kg = (double)Integer.valueOf(
              weighField.getText().trim()).intValue();
        weighed = true;
      }
    }

    public void printItems() {
      if (weighed && chosen) {
        total = kg*unitCosts[select];
        totalField.setText("G "+total);
        System.out.println(kg+"kg "+items[select]+" @ G"+
                unitCosts[select]+" = G"+total);
        kg = 0;
        weighed = false;
        chosen = false;
        total = 0;
        weighField.select(0,0);
      }
    }


    public void selectItem(String item) {
      select = 0;
      while (!item.equals(items[select]))
        select++;
      chosen = true;
    }

    public static void main(String[] args) {
      new Till();
    }

    private String[] items = { "Apples", "Pears", "Oranges",
                               "Potatoes", "Lemons", "Squash",
                               "Onions", "Garlic", "Avocados",
                               "", "", "" };
    private double[] unitCosts = { 6.00, 5.00, 7.00,
                                   3.00, 10.00, 4.00,
                                   4.00, 12.00, 15.00,
                                   0, 0, 0 };

    private double total;
    private double kg;
    private boolean chosen = false;
    private boolean weighed = false;
    private int select = 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -