1.用户故事;作为每天都在上课的大学生,中午排队买饭就是个 巨大的折磨,还要防止别人插队。这时候他们就需要一个点餐软件。所以我们为我们的咸肉饭店做了一个咸肉饭点餐软件。它主要能在线上提前点好饭然后在下课后根据单号取饭。我们的优势就是其他家没有点餐软件。
2.项目;
我们先定义了几种可能购买的食物,并且创建了一个空的订单order。将可购买的食物列表展示给用户,用户输入食物编号来添加到订单中。当用户选择结束选购后,展示购物车中的食物,计算出应付金额。用户选择支付方式后,调用相应的
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
List<Food> foodList = new ArrayList<>();
foodList.add(new Food("101", "素鸡蛋肠", 12));
foodList.add(new Food("102", "小酥肉", 18));
foodList.add(new Food("103", "红烧肉", 22));
foodList.add(new Food("104", "双鸡腿肉", 28));
System.out.println("请选择您要购买的商品编号(输入end结束选购):");
Scanner scanner = new Scanner(System.in);
String input = "";
while (!"end".equalsIgnoreCase(input)) {
order.showFoodList();
input = scanner.next();
if ("end".equalsIgnoreCase(input)) {
break;
}
if (!order.addFoodToCart(input)) {
System.out.println("该商品不存在,请重新选择!");
}
}
order.showCart();
System.out.println("您需要支付的金额为:" + order.getTotalPrice() + "元");
System.out.println("请选择支付方式(输入1支付宝,输入2微信):");
input = scanner.next();
if ("1".equals(input)) {
Alipay alipay = new Alipay();
alipay.pay(order.getTotalPrice());
} else if ("2".equals(input)) {
WechatPay wechatPay = new WechatPay();
wechatPay.pay(order.getTotalPrice());
} else {
System.out.println("请输入正确的支付方式编号!");
}
}
}
class Order {
private List<Food> foodList;
private List<Food> cart = new ArrayList<>();
public Order(List<Food> foodList) {
this.foodList = foodList;
}
public boolean addFoodToCart(String id) {
for (Food food : foodList) {
if (food.getId().equals(id)) {
cart.add(food);
return true;
}
}
return false;
}
public void showFoodList() {
System.out.println("---商品列表---");
for (Food food : foodList) {
System.out.println(food.getId() + " " + food.getName() + " " + food.getPrice() + "元");
}
System.out.println("--------------");
}
public void showCart() {
System.out.println("---购物车---");
for (Food food : cart) {
System.out.println(food.getName() + " " + food.getPrice() + "元");
}
System.out.println("--------------");
}
public double getTotalPrice() {
double totalPrice = 0;
for (Food food : cart) {
totalPrice += food.getPrice();
}
return totalPrice;
}
}
class Food {
private String id;
private String name;
private double price;
public Food(String id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
interface PayAPI {
void pay(double amount);
}
class Alipay implements PayAPI {
@Override
public void pay(double amount) {
System.out.println("使用支付宝支付成功,支付金额为:" + amount + "元");
}
}
class WechatPay implements PayAPI {
@Override
public void pay(double amount) {
System.out.println("使用微信支付支付成功,支付金额为:" + amount + "元");
}
}
类图:其中,Main类为程序入口,Order类表示订单信息,Food类表示商品信息,PayAPI接口表示支付API,Alipay和WechatPay实现了PayAPI接口,表示支付宝支付和微信支付。
+---------------------+
| Main |
+---------------------+
| - main() |
+---------------------+
+---------------------+
| Order |
+---------------------+
| - foodList : List |
| - cart : List |
+---------------------+
| + addFoodToCart() |
| + showFoodList() |
| + showCart() |
| + getTotalPrice() |
+---------------------+
+---------------------+
| Food |
+---------------------+
| - id : String |
| - name : String |
| - price : double |
+---------------------+
| + getters and setters|
+---------------------+
+---------------------+
| PayAPI |
+---------------------+
| + pay() |
+---------------------+
+---------------------+
| Alipay |
+---------------------+
| + pay() |
+---------------------+
+---------------------+
| WechatPay |
+---------------------+
| + pay() |
+---------------------+
在该系统中,用户可以浏览菜单,将商品添加到购物车,生成订单,进行支付等基本操作。同时,系统管理员可以添加、编辑、删除菜单项,管理订单等操作。该用户故事用例图展示了各个用户角色的功能和操作,并指明了他们之间的关系和交互。下一步,我们可以基于这个用户故事用例图来进行类和接口设计。
标签:String,+---------------------+,System,实践,课程,println,设计,public,out From: https://www.cnblogs.com/z235419/p/17456308.html