stockdemo.java

来自「现在在国外大学里最流行的java学习软件,同时还有大量的example,在名为p」· Java 代码 · 共 93 行

JAVA
93
字号
/** * Demonstrate the StockManager and Product classes. * The demonstration becomes properly functional as * the StockManager class is completed. *  * @author David J. Barnes and Michael Kolling * @version 2006.03.30 */public class StockDemo{    // The stock manager.    private StockManager manager;    /**     * Create a StockManager and populate it with a few     * sample products.     */    public StockDemo()    {        manager = new StockManager();        manager.addProduct(new Product(132, "Clock Radio"));        manager.addProduct(new Product(37,  "Mobile Phone"));        manager.addProduct(new Product(23,  "Microwave Oven"));    }        /**     * Provide a very simple demonstration of how a StockManager     * might be used. Details of one product are shown, the     * product is restocked, and then the details are shown again.     */    public void demo()    {        // Show details of all of the products.        manager.printProductDetails();        // Take delivery of 5 items of one of the products.        manager.delivery(132, 5);        manager.printProductDetails();    }        /**     * Show details of the given product. If found,     * its name and stock quantity will be shown.     * @param id The ID of the product to look for.     */    public void showDetails(int id)    {        Product product = getProduct(id);        if(product != null) {            System.out.println(product.toString());        }    }        /**     * Sell one of the given item.     * Show the before and after status of the product.     * @param id The ID of the product being sold.     */    public void sellProduct(int id)    {        Product product = getProduct(id);                if(product != null) {            showDetails(id);            product.sellOne();            showDetails(id);        }    }        /**     * Get the product with the given id from the manager.     * An error message is printed if there is no match.     * @param id The ID of the product.     * @return The Product, or null if no matching one is found.     */    public Product getProduct(int id)    {        Product product = manager.findProduct(id);        if(product == null) {            System.out.println("Product with ID: " + id +                               " is not recognised.");        }        return product;    }    /**     * @return The stock manager.     */    public StockManager getManager()    {        return manager;    }}

⌨️ 快捷键说明

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