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

📄 coffee2.java

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

class Coffee2 {

    /* The Improved Coffee class        by J M bishop Oct 1996
     * -------------------------        Java 1.1 October 1997
     *                                  substantially updated
     *                                  and Java 2 May 2000
     * Keeps an inventory of batches of coffee of a
     * particular kind or blend using
     * a Linked List from java.util.
     */

    private String name;
    private double price;   // per kg
    private double stock; // kgs
    private double reorder; // kgs;
    private LinkedList batchStock = new LinkedList ();

    public Coffee2() {
        /* empty constructor, because all values
         * are read in via prepareToStock
         */
    }

    public Coffee2(String s) {
        name = new String(s);
    }

    DateFormat DF = DateFormat.getDateInstance(DateFormat.DEFAULT,
                    Locale.UK);

    void prepareToStock(Stream in) throws IOException {
        name = in.readString();
        price = in.readDouble();
        reorder = in.readInt();
        stock = 0;
        newBatchIn(in);
    }


    boolean newBatchIn(Stream in) throws IOException {
      double nextStock = in.readDouble();
      if (nextStock == 0)
        return false;
      else {
        stock += nextStock;
        Date stockDate = new Date ();
        String dateString = in.readString();
        try {
          stockDate = DF.parse(dateString);
        } catch (ParseException e) {
          System.out.println("Error in date " + dateString);
        }
        batchStock.add(new Batch(nextStock, stockDate));
        return true;
      }
    }

    double sell(double ordered) {
        double kgInBatch;
        Batch currentBatch;

        for (ListIterator list =
                batchStock.listIterator(); list.hasNext(); ) {
            currentBatch = (Batch) list.next();
            if (ordered > currentBatch.available())
                kgInBatch = currentBatch.available();
            else
                kgInBatch = ordered;
            stock -= kgInBatch;
            boolean soldOut = currentBatch.sell(kgInBatch);
            if (soldOut) {
                System.out.println("Batch now empty");
                list.remove();
            }
            ordered -= kgInBatch;
            if (ordered <= 0)
                break;
        }
        System.out.println("Stock report:");
        display();
        return ordered;
    }

    boolean equals(Coffee2 c) {
        return name.equals(c.name);
    }

    void display () {
      System.out.println(name+" @ G"+
           Stream.format(price,1,2)+" per kg");
      for (ListIterator list =
           batchStock.listIterator(); list.hasNext(); ) {
        Batch batch = (Batch) list.next();
        batch.display();
      }
      System.out.println(Stream.format(stock,1,1)+"kg in stock. Reorder level is "+
           reorder+"kg\n");
    }

}

⌨️ 快捷键说明

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