【Java】Gourmet咖啡系统
下載鏈接
Download
類圖見文末
后續篇章
實驗一:設計模式在Gourmet咖啡系統中的應用
實驗二:異常和IO在Gourmet咖啡系統中的應用
Modeling the Gourmet Coffee System
Prerequisites, Goals, and Outcomes
Prerequisites: Before you begin this exercise, you need mastery of the following:
- UML
- Knowledge of class diagram notation
- Object-Oriented Design
- Knowledge of modeling concepts:
- Identifying classes
- Identifying relationships between classes
- Identifying class attributes
- Identifying class methods
- Knowledge of modeling concepts:
Goals: Reinforce your object-oriented design skills
Outcomes: You will master the following skills:
- Produce a UML class diagram, from a specification, that shows:
- classes
- attributes
- methods
- relationships
Background
This assignment asks you to model a coffee store application.
Description
Gourmet Coffee is a store that sells coffee from countries around the globe. It also sells coffee brewing machines(咖啡沖泡機) and other accessories for coffee consumption(咖啡消費配件). The Gourmet Coffee System maintains a product catalog, processes orders, and tracks the store’s sales.
The catalog maintains the following information about the store’s products:
-
Coffee
- Code
- Description
- Price
- Country of origin
- Type of roast
- Flavor
- Aroma
- Acidity
- Body
-
Coffee brewer
- Code
- Description
- Price
- Model of the brewer
- Type of the water supply: Pour-over or Automatic
- Capacity: number of cups
-
Coffee accessory
- Code
- Description
- Price
The following tables show some of the products sold by Gourmet Coffee.
Figure 1 Coffee
Figure 2 Coffee brewers
Figure 3 Coffee accessories
The Gourmet Coffee System processes orders. An order contains a list of products, their quantities, and the total cost. The following is an example of an order:
Figure 4 Order
In the Gourmet Coffee System, the user can:
- Display the catalog: lists the code and description of each product
- Display a product
- Display the current order: lists quantity, code, and price of each product in the current order, and the total of the order.
- Add a product to the current order—if the specified product is already part of the order, this command will modify the quantity of that product
- Remove a product from the current order
- Register the sale of the current order—this command adds the order to the store’s sales and empties the current order
- Display the sales: lists all the orders that have been sold
Run the sample executable that is provided to learn more about the Gourmet Coffee System.
Files
Following is a sample executable of the Gourmet Coffee System.
- gourmet-coffee-sample-executable.jar — Download this file now. It is a sample executable.
Tasks
These steps will guide you for completing this assignment:
- First, run the sample executable by issuing the following command at the command prompt:
C:>java -jar gourmet-coffee-sample-executable.jar - Then, follow the technique described in page 1.2.5 Modeling Classes to model the Gourmet Coffee System.
- Identify the following:
- The classes
- The association relationships (include direction, multiplicity, and association attribute)
- The specialization/generalization relationships
- The attributes of each class
- The methods of each class
- Your class diagram should include:
- The class of the gourmet coffee application
- The accessor methods
- The mutator methods if are needed
- For the collections:
- The methods to add and access elements
- The methods to remove elements if are needed
- The methods that compute other values not included in the attributes.
Use Sun’s coding conventions when naming classes, methods, and attributes.
- Identify the following:
- Use Eclipse, Violet, PowerPoint, or another tool of your choosing to draw a UML class diagram.
- Save the UML class diagram in a SVG, GIF, or JPG format in a file named uml-gou-cof.
Submission
Upon completion, submit only the SVG, GIF, or JPG file uml-gou-cof. The extension of this file will depend on the tool used to create the diagram.
說明
英文一定要看得懂啊~~只不過這是一個UML建模的實驗,但是之后會要求基于Java編程實現的,下面會實現一下。
Product類
/*** Product is created to be used as the super class.* @author BlankSpace* @version 2.0*/public class Product {private String code;private String description;private double price;public Product(String code, String description, double price) {this.code = code;this.description = description;this.price = price;}public String getCode() {return this.code;}public String getDescription() {return this.description;}public double getPrice() {return this.price;}//Identifying whether objects are equal by code.@Overridepublic boolean equals(Object product) {return (product instanceof Product) && (this.getCode().equals(((Product)product).getCode()));}@Overridepublic String toString() {return this.getCode() + "," + this.getDescription() + "," + this.getPrice();}}Coffee類
/*** Coffee is Product's subclass. It represents a type of Products which is something special.* @author BlankSpace* @version 2.0*/public class Coffee extends Product {private String countryOfOrigin;private String typeOfRoast;private String flavor;private String aroma;private String acidity;private String body;public Coffee(String code, String description, double price, String countryOfOrigin, String typeOfRoast, String flavor, String aroma, String acidity, String body) {super(code, description, price);this.countryOfOrigin = countryOfOrigin;this.typeOfRoast = typeOfRoast;this.flavor = flavor;this.aroma = aroma;this.acidity = acidity;this.body = body;}public String getCountryOfOrigin() {return this.countryOfOrigin;}public String getTypeOfRoast() {return this.typeOfRoast;}public String getFlavor() {return this.flavor;}public String getAroma() {return this.aroma;}public String getAcidity() {return this.acidity;}public String getBody() {return this.body;}}CoffeeBrewer類
/*** CoffeeBrewer is Product's subclass. It represents a type of Products which is something special.* @author BlankSpace* @version 2.0*/public class CoffeeBrewer extends Product {private String modelOfTheBrewer;private String typeOfTheWaterSupply;private int numberOfCups;public CoffeeBrewer(String code, String description, double price, String modelOfTheBrewer, String typeOfTheWaterSupply, int numberOfCups) {super(code, description, price);this.modelOfTheBrewer = modelOfTheBrewer;this.typeOfTheWaterSupply = typeOfTheWaterSupply;this.numberOfCups = numberOfCups;}public String getModelOfTheBrewer() {return this.modelOfTheBrewer;}public String getTypeOfTheWaterSupply() {return this.typeOfTheWaterSupply;}public int getNumberOfCups() {return this.numberOfCups;}}OrderItem類
/*** An orderItem contains one certain type of product and the quantity of user needs.* The class's objects will be added into an order's item list.* @author BlankSpace* @version 2.0*/public class OrderItem {private Product product;private int quantity;public OrderItem(Product product, int quantity) {this.product = product;this.quantity = quantity;}public Product getProduct() {return this.product;}public void setQuantity(int quantity) {this.quantity = quantity;}public int getQuantity() {return this.quantity;}/*** to calculate the cost of purchasing the product* @return the result*/public double getValue() {return this.getProduct().getPrice() * this.getQuantity();}@Overridepublic String toString() {return this.getQuantity() + "," + this.getProduct().getCode() + "," + this.getProduct().getPrice();}}Order類
import java.util.ArrayList;/*** the class which saves a certain order's all orderItems* @author BlankSpace* @version 2.0*/public class Order {//to save all orderItems.private ArrayList<OrderItem> orderItemList = new ArrayList<>();/*** to add the orderItem to the list.* @param orderItem*/public void addOrderItem(OrderItem orderItem) {this.orderItemList.add(orderItem);}/*** to delete the orderItem which user wants to delete.* @param orderItem*/public void removeOrderItem(OrderItem orderItem) {this.orderItemList.remove(orderItem);}/*** @return the list which saves all orderItems*/public ArrayList<OrderItem> getAllOrderItem(){return this.orderItemList;}/*** to search the orderItem which user wants to find in the list.* @param product* @return the orderItem which user wants to find*/public OrderItem getOrderItem(Product product) {for (OrderItem orderItem : orderItemList) {if (orderItem.getProduct().equals(product)) {return orderItem;}}return null;}/** * @return the number of orderItems which are saved in the list.*/ public int getNumberOfOrderItems() {return this.orderItemList.size();}/*** to sum all the orderItems' value in the list.* @return the sum of all the orderItems' value.*/public double getValue() {double value = 0.0;for (OrderItem orderItem : orderItemList) {value += orderItem.getValue();}return value;}}Catalog類
import java.util.ArrayList;/*** Catalog is created to save all the product's item.* @author BlankSpace* @version 2.0*/public class Catalog {private ArrayList<Product> products;public Catalog() {products = new ArrayList<>();}/*** to add the type of products to the list.* @param product*/public void addProduct(Product product) {this.products.add(product);}/*** to search the product which user wants to find in the list.* @param code* @return the product which user wants to find.*/public Product getProduct(String code) {for (Product product : products) {if (product.getCode().equals(code)) {return product;}}return null;}/*** @return the list which saves all kinds of products.*/public ArrayList<Product> getAllProduct() {return this.products;}/*** @return the number of product categories which are saved in the list.*/public int getNumberOfProducts() {return this.products.size();}}Sales類
import java.util.ArrayList;/*** the class which can save all orders.* @author BlankSpace* @version 2.0*/public class Sales {//to save all orders.private ArrayList<Order> orders = new ArrayList<>();/*** to add the order to the list.* @param order*/public void addOrder(Order order) {this.orders.add(order);}/*** @return the list which saves all orders*/public ArrayList<Order> getAllOrder(){return this.orders;}/*** @return the number of orders which are saved in the list.*/public int getNumberOfOrders() {return this.orders.size();}}GourmetCoffeeSystem類
import java.io.IOException; import java.text.NumberFormat; import java.util.Scanner;/*** 程序包的可運行部分* 使用了單例模式* 實際上唯一存在的實例有catalog、currentOrder、sales三個屬性* @author BlankSpace* @version 2.0 */ public class GourmetCoffee {//靜態初始化塊static {CURRENCY = NumberFormat.getCurrencyInstance();}//用于后面格式化字符串private static final NumberFormat CURRENCY;//用于表示唯一的實例(實現單例模式)private static GourmetCoffee coffeeSystem = new GourmetCoffee(load());//用于獲取整個程序體的輸入private static Scanner scanner = new Scanner(System.in);//GourmetCoffeeSystem的三個屬性:catalog、currentOrder、salesprivate Catalog catalog;private Order currentOrder;private Sales sales;//private修飾的構造器private GourmetCoffee(Catalog catalog) {this.catalog = catalog;this.currentOrder = new Order();this.sales = new Sales();}//main方法,程序的入口public static void main(String[] args) throws IOException {coffeeSystem.run();}/*** 執行初始化,把信息存入Catalog的實例中* @return 所有的產品信息*/private static Catalog load() {Catalog catalog = new Catalog();//這里利用了OOP中的多態:相當于Product product = new Coffee(......);......catalog.addProduct(new Coffee("C001", "Colombia, Whole, 1 lb", 17.99, "Colombia", "Medium", "Rich and Hearty", "Rich", "Medium", "Full"));catalog.addProduct(new Coffee("C002", "Colombia, Ground, 1 lb", 18.75, "Colombia", "Medium", "Rich and Hearty", "Rich", "Medium", "Full"));catalog.addProduct(new Coffee("C003", "Italian Roasts, Whole, 1 lb", 16.80, "Latin American Blend", "Italian Roast", "Dark and heavy", "Intense", "Low", "Medium"));catalog.addProduct(new Coffee("C004", "Italian Roasts, Ground, 1 lb", 17.55, "Latin American Blend", "Italian Roast", "Dark and heavy", "Intense", "Low", "Medium"));catalog.addProduct(new Coffee("C005", "French Roasts, Whole, 1 lb", 16.80, "Latin American Blend", "French Roast", "Bittersweet, full intense", "Intense, full", "None", "Medium"));catalog.addProduct(new Coffee("C006", "French Roasts, Ground, 1 lb", 17.55, "Latin American Blend", "French Roast", "Bittersweet, full intense", "Intense, full", "None", "Medium"));catalog.addProduct(new Coffee("C007", "Guatemala, Ground, 1 lb", 17.99, "Guatemala", "Medium", "Rich and complex", "Spicy", "Medium to high", "Medium to full"));catalog.addProduct(new Coffee("C008", "Guatemala, Ground, 1 lb", 18.75, "Guatemala", "Medium", "Rich and complex", "Spicy", "Medium to high", "Medium to full"));catalog.addProduct(new Coffee("C009", "Guatemala, Whole, 1 lb", 19.99, "Sumatra", "Medium", "Vibrant and powdery", "Like dark chocolate", "Gentle", "Rich and full"));catalog.addProduct(new Coffee("C010", "Guatemala, Ground, 1 lb", 20.50, "Sumatra", "Medium", "Vibrant and powdery", "Like dark chocolate", "Gentle", "Rich and full"));catalog.addProduct(new Coffee("C011", "Special Blend, Whole, 1 lb", 16.80, "Latin American Blend", "Dark roast", "Full, roasted flavor", "Hearty", "Bold and rich", "Full"));catalog.addProduct(new Coffee("C012", "Special Blend, Ground, 1 lb", 17.55, "Latin American Blend", "Dark roast", "Full, roasted flavor", "Hearty", "Bold and rich", "Full"));catalog.addProduct(new CoffeeBrewer("B001", "Home Coffee Brewer", 150.0,"Brewer 100", "Pourover", 6));catalog.addProduct(new CoffeeBrewer("B002", "Coffee Brewer, 2 Warmers", 200.0, "Brewer 200", "Pourover", 12));catalog.addProduct(new CoffeeBrewer("B003", "Coffee Brewer, 3 Warmers", 280.0, "Brewer 210", "Pourover", 12));catalog.addProduct(new CoffeeBrewer("B004", "Commercial Brewer, 20 cups", 380.0, "Quick Coffee 100", "Automatic", 20));catalog.addProduct(new CoffeeBrewer("B005", "Commercial Brewer, 40 cups", 480.0, "Quick Coffee 200", "Automatic", 40));catalog.addProduct(new Product("A001", "Almond Flavored Syrup", 9.0));catalog.addProduct(new Product("A002", "Irish Creme Flavored Syrup", 9.0));catalog.addProduct(new Product("A003", "Mint Flavored syrup", 9.0));catalog.addProduct(new Product("A004", "Caramel Flavored Syrup", 9.0));catalog.addProduct(new Product("A005", "Gourmet Coffee Cookies", 12.0));catalog.addProduct(new Product("A006", "Gourmet Coffee Travel Thermo", 18.0));catalog.addProduct(new Product("A007", "Gourmet Coffee Ceramic Mug", 8.0));catalog.addProduct(new Product("A008", "Gourmet Coffee 12 Cup Filters", 15.0));catalog.addProduct(new Product("A009", "Gourmet Coffee 36 Cup Filters", 45.0));return catalog;}private void run() throws IOException {//對傳統印象中的for循環語句加以改造,使之更靈活for(int choice = this.getChoice(); choice != 0; choice = this.getChoice()) {//不需要default語句,因為在獲取輸入的時候就穩妥的處理了數據switch (choice) {case 1:this.displayCatalog();break;case 2:this.displayProductInformation();break;case 3:this.displayOrder();break;case 4:this.addOrModifyProduct();break;case 5:this.removeProduct();break;case 6:this.saleOrder();break;case 7:this.displayOrdersSold();break;}}}/*** 打印主菜單的方法*/private static void printMainMenu() {System.out.println(//退出系統"[0] Quit\n"//顯示目錄:列出每個產品的代碼和描述+ "[1] Display catalog\n"//顯示產品+ "[2] Display product\n"//顯示當前訂單:列出當前訂單中每個產品的數量、代碼和價格,以及訂單總價格。+ "[3] Display current order\n"//將產品添加到當前訂單,如果指定的產品已經是訂單的一部分,此命令將修改該產品的數量+ "[4] Add|modify product to|in current order\n"//從當前訂單中刪除產品+ "[5] Remove product from current order\n"//注冊當前訂單的銷售此命令將訂單添加到商店的銷售中并清空當前訂單+ "[6] Register sale of current order\n"//顯示銷售:列出所有已售出的訂單+ "[7] Display sales");}/*** 讀取、處理選擇值的方法* 與用戶交互,讀取選擇的數據加以處理* @return 選擇* @throws IOException*/private int getChoice() throws IOException {//不滿足條件,循環會一直持續下去while(true) {try {System.out.println();printMainMenu();int choice = Integer.parseInt(scanner.next());System.out.println();//提前處理數據,只有輸入0到7的整數才是合法的if (0 <= choice && choice <= 7) {return choice;}//提示用戶輸入錯誤System.out.println("Invalid choice: " + choice);} catch (NumberFormatException numberFormatException) {//打印異常System.out.println(numberFormatException);}}}/*** 打印所有產品信息的方法*/private void displayCatalog() {int numberOfProducts = this.catalog.getNumberOfProducts();if (numberOfProducts == 0) {System.out.println("The catalog is empty");} else {for (Product product : this.catalog.getAllProduct()) {System.out.println(product.getCode() + " " + product.getDescription());}}}/*** 打印所選產品信息的方法* 利用instance of來分析判斷前面的對象是否是后面的類或其子類、實現類的實例,使代碼更加健壯* @throws IOException*/private void displayProductInformation() throws IOException {Product product = this.readProduct();if (product != null) {System.out.println(" Description: " + product.getDescription());System.out.println(" Price: " + product.getPrice());if (product instanceof Coffee) {//進行強制類型轉換Coffee coffee = (Coffee)product;//輸出咖啡產品信息System.out.println(" Origin: " + coffee.getCountryOfOrigin());System.out.println(" Roast: " + coffee.getTypeOfRoast());System.out.println(" Flavor: " + coffee.getFlavor());System.out.println(" Aroma: " + coffee.getAroma());System.out.println(" Acidity: " + coffee.getAcidity());System.out.println(" Body: " + coffee.getBody());} else if (product instanceof CoffeeBrewer) {CoffeeBrewer coffeeBrewer = (CoffeeBrewer)product;System.out.println(" Model: " + coffeeBrewer.getModelOfTheBrewer());System.out.println(" Water Supply: " + coffeeBrewer.getTypeOfTheWaterSupply());System.out.println(" Number of Cups: " + coffeeBrewer.getNumberOfCups());}} else {System.out.println("There are no products with that code");}}/*** 打印訂單信息的方法*/private void displayOrder() {int numberOfOrderItems = this.currentOrder.getNumberOfOrderItems();if (numberOfOrderItems == 0) {System.out.println("The current order is empty");} else {for (OrderItem orderItem : this.currentOrder.getAllOrderItem()) {System.out.println(orderItem.toString());}System.out.println("Total: " + CURRENCY.format(this.currentOrder.getValue()));}}/*** 添加或修改訂單中產品信息的方法* @throws IOException*/private void addOrModifyProduct() throws IOException {Product product = this.readProduct();if (product != null) {int quantity = this.readQuantity();OrderItem orderItem = this.currentOrder.getOrderItem(product);if (orderItem == null) {this.currentOrder.addOrderItem(new OrderItem(product, quantity));System.out.println("The product " + product.getCode() + " has been added");} else {orderItem.setQuantity(quantity);System.out.println("The quantity of the product " + product.getCode() + " has been modified");}} else {System.out.println("There are no products with that code");}}/*** 清除訂單信息中某一產品信息的方法* @throws IOException*/private void removeProduct() throws IOException {Product product = this.readProduct();if (product != null) {OrderItem orderItem = this.currentOrder.getOrderItem(product);if (orderItem != null) {this.currentOrder.removeOrderItem(orderItem);System.out.println("The product " + product.getCode() + " has been removed from the current order");} else {System.out.println("There are no products in the current order with that code");}} else {System.out.println("There are no products with that code");}}/*** 交易當前訂單的方法*/private void saleOrder() {if (this.currentOrder.getNumberOfOrderItems() > 0) {this.sales.addOrder(this.currentOrder);this.currentOrder = new Order();System.out.println("The sale of the order has been registered");} else {System.out.println("The current order is empty");}}/*** 展示交易過的訂單的方法*/private void displayOrdersSold() {//獲取交易過的訂單數int numberOfOrders = this.sales.getNumberOfOrders();if (numberOfOrders != 0) {int count = 1;for (Order order : this.sales.getAllOrder()) {System.out.println("Order " + count++);for (OrderItem orderItem : this.currentOrder.getAllOrderItem()) {System.out.println(" " + orderItem.toString());}System.out.println(" Total: " + CURRENCY.format(order.getValue()));}} else {System.out.println("There are no sales");}}/*** 讀取要檢索的產品的方法* @return 要檢索的產品* @throws IOException*/private Product readProduct() throws IOException {System.out.print("Product code> ");return this.catalog.getProduct(scanner.next());}/*** 讀取購買數的方法* @return 購買數* @throws IOException*/private int readQuantity() throws IOException {while(true) {try {System.out.print("Quantity> ");int quantity = Integer.parseInt(scanner.next());if (quantity > 0) {return quantity;}System.out.println("Invalid input. Please enter a positive integer");} catch (NumberFormatException e) {System.out.println(e);}}}}類圖
總結
以上是生活随笔為你收集整理的【Java】Gourmet咖啡系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Python】Matplotlib利用
- 下一篇: 求子集元素之和(洛谷P2415题题解,J