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

📄 curiostore3a.java

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

class CurioStore3a {

  /* Curio Store Version 3a     by J M Bishop April 2000
   * ---------------------
   *
   * This shop has stock levels and a sell option
   * This version (for Example 5.3) checks whether a
   * sale will exceed the stock level.
   * Illustrates user-defined exceptions.
   */

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

  // Declare objects relevant to all methods
  Display display = new Display ("Polelo Curio Store");
  Curio   mugs, tshirts, carvings;
  boolean open;

  // The constructor where the initialising and main work gets done
  CurioStore3a () {

    mugs = new Curio("Traditional mugs", 6, "beaded in Ndebele style", 20);
    tshirts = new Curio("T-shirts", 30, "sizes M to XL", 50);
    carvings = new Curio("Masks", 80, "carved in wood", 8);

    // print out the initial shop details
    report ();

    // Use methods to perform a sequence of actions
    stockTheStore();
    openTheStore();

    while (open) {
       sellCurios();
       open = display.getString("The store is").equals("Open");
       available();
    }
    display.println("Store closed.\nHave a good day");
  }

   void report () {
    display.println("The Polelo Curio Store sells\n");
    // use the objects' access to toString to print their contents
    display.println(""+mugs);
    display.println(""+tshirts);
    display.println(""+carvings);
  }

  void stockTheStore () {
    display.ready("Press ready when new stock levels have been entered");
    mugs.addStock(display.getInt(mugs.name));
    tshirts.addStock(display.getInt(tshirts.name));
    carvings.addStock(display.getInt(carvings.name));
   }

   void openTheStore () {
     display.prompt("Kind of curio sold","                ");
     display.prompt("Number sold",0);
     display.prompt("The store is","Open");
     open = true;
   }

   void sellCurios () {
     Curio curio;

     display.ready("Press ready when sale completed");
     String curioName = display.getString("Kind of curio sold");
     int curiosSold = display.getInt("Number sold");

     if (curioName.equals(mugs.name)) {
        curio = mugs;
     } else
     if (curioName.equals(tshirts.name)) {
       curio = tshirts;
     } else
     if (curioName.equals(carvings.name)) {
       curio = carvings;
     } else {
       display.println(curioName + " is an invalid curio name");
       return;
     }
     display.println("\nOrder for "+curiosSold+" "+curioName);
     curio.sell(curiosSold);
     display.println("RECEIPT: for "+ curiosSold + " "
          + curioName +" at G"+ curio.price+" each = G" +
          curio.price*curiosSold);
    }

   void available () {
     display.println("Available are "+mugs.stockLevel()+" "+
       tshirts.stockLevel()+" "+carvings.stockLevel()+
       " curios respectively\n");
   }

  class Curio {

    String name;
    int price;
    String description;
    int stock;

    Curio (String n, int p, String d, int t) {
      name = n;
      price = p;
      description = d;
      display.prompt(name,t);
    }

    void addStock (int n) {
      stock += n;
    }

    void sell (int n) {
      // stock could go negative
        stock -= n;
    }

    int stockLevel () {
      return stock;
    }

    // This method is accessed by println to turn
    // a Curio object into a String.
    public String toString () {
      return name + " "+description+" for G" + price;
    }
  }
}

⌨️ 快捷键说明

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