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

📄 coffeeshop2.java

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

class CoffeeShop2 {

   /* The Coffee Shop program Version 2  by J M Bishop Oct 1996
    * ---------------------------------  Java 1.1 October 1997
    *                                    Java 1.2 May 2000
    *
    * Includes the ability to sell batches of a
    * particular kind of coffee, starting at the oldest.
    *
    * Illustrates the use of the java.util LinkedList
    * and ListIterator classes
    */

   CoffeeShop2 () throws IOException {

     LinkedList coffeeStock = new LinkedList ();

     stockUp(coffeeStock);
     inventoryReport(coffeeStock);
     makeSales(coffeeStock);
   }

   void stockUp(LinkedList coffeeStock) throws IOException {
     Stream in = new Stream("coffee.dat",Stream.READ);
     System.out.println("Stocking the shop");
     while (true) {
       try {
         Coffee2 coffee = new Coffee2();
         coffee.prepareToStock(in);
         coffeeStock.add(coffee);
         boolean morebatches = true;
         while (morebatches)
           morebatches = coffee.newBatchIn(in);
       } catch (EOFException e) {
          System.out.println("That's the shop stocked for today");
          System.out.println();
          break;
       }
     }
   }

   void inventoryReport(LinkedList coffeeStock) {
     System.out.println("Nelson's Coffee Shop");
     System.out.println("Inventory control");
     System.out.println("The coffee stock on "+
	      DateFormat.getDateInstance().format
					(new Date(DateFormat.FULL)));
     for (ListIterator list =
          coffeeStock.listIterator(); list.hasNext();) {
       Coffee2 current = (Coffee2) list.next();
       current.toString();
     }
   }

   void makeSales(LinkedList coffeeStock) throws IOException {

     Stream orderFile = new Stream ("orders.data",Stream.READ);
     System.out.println("Coffee sales");

     while (true) {
       try {
         String orderName = orderFile.readString();
         double orderAmount = orderFile.readDouble();
         System.out.println("Order for " + orderName + "  "
            + orderAmount);
         System.out.println("=====");

        //Establish that the blend exists
        Coffee2 foundBlend = null;
        Coffee2 orderBlend = new Coffee2(orderName);

        ListIterator list = coffeeStock.listIterator();
        boolean found = false;

        while (list.hasNext()) {
          foundBlend = (Coffee2) list.next();
          if (foundBlend.equals(orderBlend)) {
            found = true;
            break;
          }
        }

        if (!found)
          // None of that name
          System.out.println("Sorry, we're out of that blend.\n");
        else {
          // Start selling
          double remainder = foundBlend.sell(orderAmount);
          if (remainder > 0) {
            System.out.println("Could not match "+remainder+"kg\n");
          }
        }

       } catch (EOFException e) {
           System.out.println("\nShop closed");
           break;
       }
     }
  }

  public static void main (String [] args) throws IOException {
    new CoffeeShop2 ();
  }
}

⌨️ 快捷键说明

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