1. 前言:
-
类定义:代码中定义了多个类。Dish类菜品信息,
Menu
类用于管理菜单项,包括添加菜品、删除菜品等功能;Record类 记录,Order
类用于管理订单记录,包括添加订单、删除订单、查找订单等功能。 -
对象创建:在
main
方法中,首先创建对象。然后使用Scanner
类从标准输入读取用户输入的命令。 -
命令解析:通过循环读取用户输入的命令,并根据命令的不同执行相应的操作。这里使用了字符串数组
input
来存储命令的各个部分,例如add
命令的第一个元素是"add",第二个元素是菜品名称,第三个元素是菜品价格。 -
条件判断:根据命令的第一个元素进行条件判断,执行相应的操作。例如,如果命令是"add",则调用
menu.addDish()
方法将菜品添加到菜单中;如果命令是"order",则调用order.addARecord()
方法将订单记录添加到订单中;以此类推。 -
输出结果:根据命令的不同,执行相应的操作后,可能会输出一些结果。例如,如果命令是"find",则调用
order.findRecordByNum()
方法查找订单记录,并输出相关信息;如果命令是"delete",则调用order.delARecordByOrderNum()
方法删除订单记录。 -
计算总价:在所有命令执行完毕后,调用
order.getTotalPrice()
方法计算订单的总价,并输出结果。
2. 设计与分析:重点对题目的提交源码进行分析,可参考SourceMonitor的生成报表内容以及PowerDesigner的相应类图,要有相应的解释和心得(做到有图有真相),本次Blog主要分析PTA中的菜单计价系列的题目以及期中考试的三道题目
菜单计价程序4:
有Dish、Menu、Record 、Order四个类,实现菜品记录(输入)、菜单记录(输入)、点菜记录(输入)、订单记录(价格输出、删除其中某条记录)
思路分析:
首先需要创建一个菜单类,包含菜品信息和查找菜品的方法。然后创建一个点菜记录类,包含序号、菜品、份额和份数,以及计算价格的方法。接着创建一个订单类,包含点菜记录数组,计算总价的方法,添加点菜记录的方法,删除点菜记录的方法和查找点菜记录的方法。最后根据输入的格式进行相应的处理,输出结果。
- Dish(菜品):表示一道菜品,包含菜品名称和单价。
- Menu(菜单):表示餐厅的菜单,包含一个Dish数组,用于存储菜品信息。提供了添加菜品、查找菜品的方法。
- Record(记录):表示一次订单记录,包含订单编号、菜品、份数和数量。提供了计算价格的方法。
- Order(订单):表示一次完整的订单,包含一个Record数组,用于存储订单记录。提供了添加记录、删除记录、查找记录和计算总价的方法。
- Main(主类):程序的入口,负责接收用户输入的命令并调用相应的方法执行操作。
在主函数中,首先创建了一个Menu对象和一个Order对象,然后使用Scanner对象接收用户输入的命令。根据命令的不同,执行不同的操作,如添加菜品、下单、删除订单或查询订单等。最后输出订单的总价。
/*import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.math.BigDecimal; import java.time.LocalDate; import java.time.LocalTime; import java.util.Arrays; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String message = null; boolean isOrder = false; Menu menu = new Menu(); Order[] orders = new Order[1]; String users = ""; int i = -1; while (true) { message = scanner.nextLine(); if ("end".equals(message)) { break; } if (message.contains("delete")) { String[] strings = message.split(" "); orders[i].delARecordByOrderNum(Integer.parseInt(strings[0])); continue; } if (message.contains("table")) { i++; isOrder = true; if (i > 0) { Order[] newOrders = Arrays.copyOf(orders, orders.length + 1); orders = newOrders; } String[] strings = message.split(" "); if (strings[3].length()>10) { continue; } if (strings[4].length() != 11 || !(strings[4].substring(0,3).equals("180") || strings[4].substring(0,3).equals("181")||strings[4].substring(0,3).equals("189")||strings[4].substring(0,3).equals("133")||strings[4].substring(0,3).equals("135")||strings[4].substring(0,3).equals("136"))){ continue; } orders[i] = new Order(); // orders[i].table = Integer.parseInt(strings[1]); orders[i].name = strings[3]; if (i == 0) { users += strings[3]; } else if (!users.contains(strings[3])) { users = users + "," + strings[3]; } orders[i].phone = strings[4]; orders[i].date = strings[5]; orders[i].time = strings[6]; continue; } if (isOrder) { orders[i] = addRecord(message,menu,orders,i); } else { String[] strings = message.split(" "); if (message.contains("T")) { if (strings.length != 4) { System.out.println("wrong format"); continue; } menu.addDish(strings[0], strings[1], Integer.parseInt(strings[2]), strings[3]); } else { if (strings.length!=3) { //System.out.println("wrong format"); continue; } menu.addDish(strings[0], Integer.parseInt(strings[1])); } } } String chuan; String jin; String zhe; int chuanNum; int jinNum; int zheNum; User use ; for (String user : users.split(",")) { use = new User(); for (int j = 0; j < orders.length; j++) { chuan = ""; jin = ""; zhe = ""; chuanNum = 0; jinNum = 0; zheNum = 0; if (user == null || user.length() == 0) { return; } if (!user.equals(orders[j].name)) { continue; } if (orders == null) { return; } if (orders[j].records == null) { continue; } int totalPrice = orders[j].getTotalPrice(); String price = getPrice(use,orders[j].time, orders[j].date, totalPrice); if (price.equals(" out of opening hours")) { System.out.print("table " + orders[j].table +":" +price); return; } System.out.println("table " + orders[j].table + ":"); for (Record record : orders[j].records) { String name = record.d.name; if (record.d.taste != null) { if (record.d.taste.equals("川菜")) { chuan += "," + record.degree; chuanNum += record.num; } else if (record.d.taste.equals("晋菜")) { jin += "," +record.degree; jinNum += record.num; } else if (record.d.taste.equals("浙菜")) { zhe += "," + record.degree; zheNum += record.num;; } } for (Dish dish : menu.dishs) { if (dish.name.equals(name)) { record.d.unit_price = dish.unit_price; } } System.out.println(record.orderNum + " " + record.d.name + " " + record.getPrice()); } System.out.print("table " + orders[j].table +":" + totalPrice + price); taste(chuan,zhe,jin,chuanNum,zheNum,jinNum); System.out.println(); use.name = orders[i].name; use.phone = orders[i].phone; ; } if (use.name == null || use.name.length() == 0) { continue; } System.out.println(use.name + " " + use.phone + " " + use.total ); } } static void taste(String chuan, String zhe, String jin, int chuanNum, int zheNum, int jinNum) { int sum = 0; int j = 0; if (chuan != null || chuan.length() != 0) { j = 0; String[] split = chuan.substring(1).split(","); for (String s : split) { j++; sum += Integer.parseInt(s); } System.out.print(" 川菜 " + chuanNum+ " "); int degree = sum / j; if (degree == 0) { System.out.print("不辣"); }else if (degree ==1) { System.out.print("微辣"); } else if (degree ==2) { System.out.print("稍辣"); } else if (degree ==3) { System.out.print("辣"); } else if (degree == 4) { System.out.print("很辣"); } else if (degree == 5) { System.out.print("爆辣"); } } if (jin != null && jin.length() != 0) { j = 0; sum = 0; String[] split = jin.substring(1).split(","); for (String s : split) { j++; sum += Integer.parseInt(s); } System.out.print(" 晋菜 " + jinNum+ " "); int degree = sum / j; if (degree == 0) { System.out.print("不酸"); }else if (degree ==1) { System.out.print("微酸"); } else if (degree ==2) { System.out.print("稍酸"); } else if (degree ==3) { System.out.print("酸"); } else if (degree == 4) { System.out.print("很酸"); } } if (zhe != null && zhe.length() != 0) { j = 0; sum = 0; String[] split = zhe.substring(1).split(","); for (String s : split) { j++; sum += Integer.parseInt(s); } System.out.print(" 浙菜 " + zheNum+ " "); int degree = sum / j; if (degree == 0) { System.out.print("不甜"); }else if (degree ==1) { System.out.print("微甜"); } else if (degree ==2) { System.out.print("稍甜"); } else if (degree ==3) { System.out.print("甜"); } } } public static Order addRecord(String message, Menu menu, Order[] orders, int i) { String[] strings = message.split(" "); if (isNumeric(strings[1])) { Dish dish = menu.searchDish(strings[2]); if (dish == null) { System.out.println(strings[2] + "does not exist"); return orders[i]; } int index = Integer.parseInt(strings[0]); if (strings.length == 5) { orders[index].addARecord(Integer.parseInt(strings[1]), dish, Integer.parseInt(strings[3]), Integer.parseInt(strings[4])); System.out.println(strings[1] + " table " + (i + 1) + " pay for table " + index + " " + dish.getPrice(Integer.parseInt(strings[4]))); return orders[i]; } else { if ("川菜".equals(dish.taste) && Integer.parseInt(strings[3]) > 5) { System.out.println("spicy num out of range:" + strings[3]); return orders[i]; } else if("晋菜".equals(dish.taste) && Integer.parseInt(strings[3]) > 4) { System.out.println("acidity num out of range:" + strings[3]); return orders[i]; }else if("浙菜".equals(dish.taste) && Integer.parseInt(strings[3]) > 3) { System.out.println("sweetness num out of range:" + strings[3]); return orders[i]; } orders[Integer.parseInt(strings[0])-1].addARecord(Integer.parseInt(strings[1]), dish, Integer.parseInt(strings[3]), Integer.parseInt(strings[4]), Integer.parseInt(strings[5])); System.out.println(strings[1] + " table " + (i + 1) + " pay for table " + index + " " + Integer.parseInt(strings[5])*dish.getPrice(Integer.parseInt(strings[4]))); } } Dish dish = menu.searchDish(strings[1]); if (dish == null) { // System.out.println(strings[1] + "does not exist"); return orders[i]; } if ("T".equals(dish.characteristic)) { if ("川菜".equals(dish.taste) && Integer.parseInt(strings[2]) > 5) { System.out.println("spicy num out of range:" + strings[2]); return orders[i]; } else if("晋菜".equals(dish.taste) && Integer.parseInt(strings[2]) > 4) { System.out.println("acidity num out of range:" + strings[2]); return orders[i]; }else if("浙菜".equals(dish.taste) && Integer.parseInt(strings[2]) > 3) { System.out.println("sweetness num out of range:" + strings[2]); return orders[i]; } orders[i].addARecord(Integer.parseInt(strings[0]), dish, Integer.parseInt(strings[2]), Integer.parseInt(strings[3]), Integer.parseInt(strings[4])); } else { orders[i].addARecord(Integer.parseInt(strings[0]), dish, Integer.parseInt(strings[2]), Integer.parseInt(strings[3])); } return orders[i]; } static boolean isNumeric(String str) { Pattern pattern = Pattern.compile("[0-9]*"); Matcher isNum = pattern.matcher(str); if (!isNum.matches()) { return false; } return true; } public static String getPrice(User use, String times, String dates, int price) { String[] date = dates.split("/"); String[] time = times.split("/"); LocalDate nowDate = LocalDate.of(Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2])); LocalTime nowTime = LocalTime.of(Integer.parseInt(time[0]), Integer.parseInt(time[1]), Integer.parseInt(time[2])); if (nowDate.getDayOfWeek().getValue() == 7 || nowDate.getDayOfWeek().getValue() == 6) { LocalTime start = LocalTime.of(9, 30, 00); LocalTime end = LocalTime.of(21, 30, 00); if (nowTime.isAfter(start) && nowTime.isBefore(end)) { use.total +=price; return " " + price; } else { return " out of opening hours"; } } LocalTime nigthStart = LocalTime.of(16, 59, 59); LocalTime nigthEnd = LocalTime.of(20, 30, 1); if (nowTime.isAfter(nigthStart) && nowTime.isBefore(nigthEnd)) { BigDecimal bigDecimal = new BigDecimal(price * 0.7).setScale(0, BigDecimal.ROUND_HALF_UP); use.total += bigDecimal.intValue(); return " " + bigDecimal.intValue(); } LocalTime start = LocalTime.of(10, 29, 59); LocalTime end = LocalTime.of(14, 30, 1); if (nowTime.isAfter(start) && nowTime.isBefore(end)) { BigDecimal bigDecimal = new BigDecimal(price * 0.7).setScale(0, BigDecimal.ROUND_HALF_UP); price = bigDecimal.intValue(); use.total += price; return " " + price; } return " out of opening hours"; } } class User{ String name; String phone; int total; } class Dish { String name;//菜品名称 String taste;// 特色类型 String characteristic; // 是否特色 int unit_price; //单价 int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份) { if (portion == 1) { return unit_price; } else if (portion == 2) { BigDecimal bigDecimal = new BigDecimal(unit_price * 1.5).setScale(0, BigDecimal.ROUND_HALF_UP); int price = bigDecimal.intValue(); return price; } else if (portion == 3) { return unit_price * 2; } return -1; } public Dish(String name, int unit_price) { this.name = name; this.unit_price = unit_price; } public Dish(String dishName) { name = dishName; } public Dish(String name, String taste, String characteristic, int unit_price) { this.name = name; this.taste = taste; this.characteristic = characteristic; this.unit_price = unit_price; } } class Menu { Dish[] dishs ;//菜品数组,保存所有菜品信息 Dish searchDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。 { if (dishs == null) { return null; } for (Dish dish : dishs) { if (dishName.equals(dish.name)) { return dish; } } return null; } void addDish(String dishName,int unit_price)//添加一道菜品信息 { Dish newDish = new Dish(dishName, unit_price); int length = 0; if (dishs != null) { length = dishs.length; } else { length = 0; } Dish[] newDishs = new Dish[length + 1]; for (int i = 0; i < length; i++) { newDishs[i] = dishs[i]; } newDishs[length] = newDish; dishs = newDishs; } void addDish(String dishName,String taste, int unit_price,String characteristic)//添加一道菜品信息 { Dish newDish = new Dish(dishName,taste,characteristic,unit_price); int length = 0; if (dishs != null) { length = dishs.length; } else { length = 0; } Dish[] newDishs = new Dish[length + 1]; for (int i = 0; i < length; i++) { newDishs[i] = dishs[i]; } newDishs[length] = newDish; dishs = newDishs; } } class Order { int table; String time; String date; String name; // 客户 String phone; // 电话 Record[] records;//保存订单上每一道的记录 int getTotalPrice()//计算订单的总价 { int sum = 0; for (Record record : records) { sum += record.getPrice(); } return sum; } void addARecord(int orderNum, Dish dish, int degree,int portion, int num)//添加一条菜品信息到订单中。 { Record record = new Record(orderNum, dish ,degree, portion, num); Record[] newRecords = null; if (this.records == null) { newRecords = new Record[1]; newRecords[0] = record; } else { newRecords = Arrays.copyOf(this.records, records.length + 1); newRecords[records.length] = record; } records = newRecords; } void addARecord(int orderNum, Dish dish, int portion, int num)//添加一条菜品信息到订单中。 { Record record = new Record(orderNum, dish, portion, num); Record[] newRecords = null; if (this.records == null) { newRecords = new Record[1]; newRecords[0] = record; } else { newRecords = Arrays.copyOf(this.records, records.length + 1); newRecords[records.length] = record; } records = newRecords; } void delARecordByOrderNum(int orderNum)//根据序号删除一条记录 { Record[] newRecord = new Record[records.length - 1]; int j = 0; for (int i = 0; i < records.length; i++) { if (records[i].orderNum == orderNum) { continue; } else { if (j == records.length - 1) { System.out.println("delete error"); return; } newRecord[j++] = records[i]; } } records = newRecord; } Record findRecordByNum(int orderNum)//根据序号查找一条记录 { for (Record record : records) { if (record.orderNum == orderNum) { return record; } } return null; } } class Record { int orderNum;//序号\\ Dish d;//菜品\\ int portion;//份额(1/2/3代表小/中/大份)\\ int num; int degree;//度 int getPrice()//计价,计算本条记录的价格\\ { return d.getPrice(portion) * num; } public Record(int orderNum, Dish dish, int portion, int num) { this.orderNum = orderNum; this.d = dish; this.portion = portion; this.num = num; } public Record(int orderNum, Dish dish, int degree, int portion, int num) { this.orderNum = orderNum; this.d = dish; this.portion = portion; this.num = num; this.degree = degree; } } class Table { private int tableNum; private String name; private String phoneNumber; private String date; private String time; private List orders; public Table(int tableNum, String name, String phoneNumber, String date, String time) { this.tableNum = tableNum; this.name = name; this.phoneNumber = phoneNumber; this.date = date; this.time = time; this.orders = new ArrayList<>(); } public void addOrder(Order order) { orders.add(order); } public int getTableNum() { return tableNum; } public String getName() { return name; } public String getPhoneNumber() { return phoneNumber; } public String getDate() { return date; } public String getTime() { return time; } public List<Order> getOrders() { return orders; } }*/ import java.util.Scanner; class Dish { String name; int unit_price; public Dish(String name, int unit_price) { this.name = name; this.unit_price = unit_price; } public int getPrice(int portion) { return unit_price * portion; } } class Menu { Dish[] dishs; public Menu() { dishs = new Dish[100]; } public void addDish(String name, int unit_price) { for (int i = 0; i < dishs.length; i++) { if (dishs[i] == null) { dishs[i] = new Dish(name, unit_price); break; } } } public Dish searthDish(String dishName) { for (Dish dish : dishs) { if (dish != null && dish.name.equals(dishName)) { return dish; } } return null; } } class Record { int orderNum; Dish d; int portion; int num; public Record(int orderNum, Dish d, int portion, int num) { this.orderNum = orderNum; this.d = d; this.portion = portion; this.num = num; } public int getPrice() { return d.getPrice(portion) * num; } } class Order { Record[] records; int index; Menu menu; public Order() { Menu menu=new Menu(); records = new Record[100]; index = 0; } public void addARecord(int orderNum, String dishName, int portion, int num) { Dish dish = Menu.searthDish(dishName); if (dish != null) { records[index++] = new Record(orderNum, dish, portion, num); } else { System.out.println("** does not exist"); } } public void delARecordByOrderNum(int orderNum) { for (int i = 0; i < index; i++) { if (records[i].orderNum == orderNum) { for (int j = i; j < index - 1; j++) { records[j] = records[j + 1]; } index--; System.out.println("delete error"); return; } } } public Record findRecordByNum(int orderNum) { for (Record record : records) { if (record != null && record.orderNum == orderNum) { return record; } } return null; } public int getTotalPrice() { int totalPrice = 0; for (int i = 0; i < index; i++) { totalPrice += records[i].getPrice(); } return totalPrice; } } public class Main { public static void main(String[] args) { Menu menu = new Menu(); Order order = new Order(); Scanner scanner = new Scanner(System.in); String[] input = scanner.nextLine().split(" "); while (!input[0].equals("end")) { if (input[0].equals("add")) { menu.addDish(input[1], Integer.parseInt(input[2])); } else if (input[0].equals("order")) { int orderNum = Integer.parseInt(input[1]); order.addARecord(orderNum, input[2], Integer.parseInt(input[3]), Integer.parseInt(input[4])); } else if (input[0].equals("delete")) { order.delARecordByOrderNum(Integer.parseInt(input[1])); } else { Record record = order.findRecordByNum(Integer.parseInt(input[0])); if (record != null) { System.out.println(record.orderNum + " " + record.d.name + " " + record.getPrice()); } else { System.out.println("** does not exist"); } } input = scanner.nextLine().split(" "); } System.out.println(order.getTotalPrice()); } } 菜单计价程序5:
import java.util.*;
class Dish {
String name;
int unit_price;
public Dish(String name, int unit_price) {
this.name = name;
this.unit_price = unit_price;
}
public int getPrice(int portion) {
return (int) Math.round(unit_price * portion);
}
}
class Menu {
Map<String, Dish> dishs = new HashMap<>();
public void addDish(String name, int unit_price) {
dishs.put(name, new Dish(name, unit_price));
}
public Dish searthDish(String name) {
return dishs.get(name);
}
}
class Record {
int orderNum;
Dish d;
int portion;
int num;
public Record(int orderNum, Dish d, int portion, int num) {
this.orderNum = orderNum;
this.d = d;
this.portion = portion;
this.num = num;
}
public int getPrice() {
return d.getPrice(portion) * num;
}
}
class Order {
List<Record> records = new ArrayList<>();
public void addARecord(int orderNum, String dishName, int portion, int num) {
Dish d = menu.searthDish(dishName);
if (d == null) {
System.out.println("** does not exist");
return;
}
records.add(new Record(orderNum, d, portion, num));
}
public int getTotalPrice() {
int totalPrice = 0;
for (Record r : records) {
totalPrice += r.getPrice();
}
return totalPrice;
}
}
public class Main {
static Menu menu = new Menu();
static Map<Integer, Order> orders = new TreeMap<>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String line;
while (!(line = sc.nextLine()).equals("end")) {
String[] parts = line.split(" ");
if (parts[0].equals("table")) {
int tableNum = Integer.parseInt(parts[1]);
orders.put(tableNum, new Order());
} else if (parts[0].equals("delete")) {
int orderNum = Integer.parseInt(parts[1]);
Order o = orders.get(orderNum);
if (o != null) {
o.records.clear();
} else {
System.out.println("delete error");
}
} else if (parts.length >= 4 && parts[3].equals("T")) {
String dishName = parts[0];
int basePrice = Integer.parseInt(parts[2]);
menu.addDish
菜单计价程序6:
解析:
- 首先需要创建一个菜品类,包含菜品名、口味类型、基础价格和辣度等级等属性。
- 然后创建一个菜单类,包含一个菜品列表,用于存储所有的菜品信息。
- 在菜单类中,需要实现添加菜品、删除菜品、查找菜品等功能。
- 创建一个点菜记录类,包含序号、菜品名、份数、辣度等级等信息。
- 在点菜记录类中,需要实现计算总价的功能。
- 创建一个订单类,包含一个点菜记录列表,用于存储当前桌的所有点菜记录。
- 在订单类中,需要实现获取总价的功能。
- 创建一个用户类,包含用户名、手机号和订单列表等属性。
- 在用户类中,需要实现添加订单、删除订单、查找订单等功能。
- 最后,需要在主函数中读取输入数据,创建相应的对象,并调用相应的方法进行处理。
import java.util.*;
class Dish {
String name;
String type;
int basePrice;
int spiciness;
public Dish(String name, String type, int basePrice) {
this.name = name;
this.type = type;
this.basePrice = basePrice;
}
public int getPrice(int portion) {
return basePrice * portion;
}
}
class Menu {
List<Dish> dishes = new ArrayList<>();
public void addDish(Dish dish) {
dishes.add(dish);
}
public void deleteDish(String name) {
dishes.removeIf(dish -> dish.name.equals(name));
}
public Dish findDish(String name) {
for (Dish dish : dishes) {
if (dish.name.equals(name)) {
return dish;
}
}
return null;
}
}
class Record {
int orderNum;
Dish d;
int portion;
int spiciness;
public Record(int orderNum, Dish d, int portion, int spiciness) {
this.orderNum = orderNum;
this.d = d;
this.portion = portion;
this.spiciness = spiciness;
}
public int getPrice() {
return d.getPrice(portion) * spiciness;
}
}
class Order {
List<Record> records = new ArrayList<>();
public void addARecord(int orderNum, Dish d, int portion, int spiciness) {
records.add(new Record(orderNum, d, portion, spiciness));
}
public void delARecordByOrderNum(int orderNum) {
records.removeIf(record -> record.orderNum == orderNum);
}
public Record findRecordByOrderNum(int orderNum) {
for (Record record : records) {
if (record.orderNum == orderNum) {
return record;
}
}
return null;
}
public int getTotalPrice() {
int totalPrice = 0;
for (Record record : records) {
totalPrice += record.getPrice();
}
return totalPrice;
}
}
class User {
String name;
String phone;
List<Order> orders = new ArrayList<>();
public User(String name, String phone) {
this.name = name;
this.phone = phone;
}
public void addOrder(Order order) {
orders.add(order);
}
public void delOrder(int orderNum) {
orders.removeIf(order -> order.orderNum == orderNum);
}
public Order findOrder(int orderNum) {
for (Order order : orders) {
if (order.orderNum == orderNum) {
return order;
}
}
return null;
}
}
public class Main { public static void main(String[] args) {
Menu menu = new Menu();
Order order = new Order();
Scanner scanner = new Scanner(System.in);
String[] input = scanner.nextLine().split(" ");
while (!input[0].equals("end")) {
if (input[0].equals("add")) {
menu.addDish(input[1], Integer.parseInt(input[2]));
} else if (input[0].equals("order")) {
int orderNum = Integer.parseInt(input[1]);
order.addARecord(orderNum, input[2], Integer.parseInt(input[3]), Integer.parseInt(input[4]));
}
else if (input[0].equals("delete")) {
order.delARecordByOrderNum(Integer.parseInt(input[1]));
} else {
Record record = order.findRecordByNum(Integer.parseInt(input[0]));
if (record != null) {
System.out.println(record.orderNum + " " + record.d.name + " " + record.getPrice());
} else {
System.out.println("** does not exist");
}
} input = scanner.nextLine().split(" ");
}
System.out.println(order.getTotalPrice());
}
}
3. 采坑心得:
-
缺少异常处理:在处理用户输入时,可能会出现各种异常情况,如输入格式错误、数据类型转换错误等。建议使用try-catch语句进行异常处理,并在catch语句中输出相应的错误信息。
-
代码结构不清晰:在
Order
类的addARecord
方法中,直接调用了menu.searthDish(dishName)
方法,而没有先判断菜单中是否存在该菜品。这可能导致在菜单中不存在该菜品时,仍然尝试创建记录并添加到订单中。建议在创建记录之前,先检查菜单中是否存在该菜品。 -
代码冗余:在
Order
类的getTotalPrice
方法中,使用了for循环遍历所有记录并累加价格。实际上,可以直接使用Java 8的Stream API对记录列表进行求和操作,以提高代码的简洁性和可读性。
4. 改进建议:对相应题目的编码改进给出自己的见解,做到可持续改进
-
在
Dish
类中,可以添加一个构造函数,用于设置菜品的辣度。 -
在
Record
类中,可以添加一个构造函数,用于设置订单号、菜品、份数和辣度。 -
在
Order
类中,可以添加一个构造函数,用于设置订单号。 -
在
User
类中,可以添加一个构造函数,用于设置用户姓名和电话号码。 -
在
Main
类中,可以添加一个方法,用于读取输入数据并创建相应的对象。 -
在
Main
类中,可以添加一个方法,用于处理用户的订单。 -
在
Main
类中,可以添加一个方法,用于显示用户的订单信息。 -
在
Main
类中,可以添加一个方法,用于计算用户的总消费。 -
在
Main
类中,可以添加一个方法,用于删除用户的订单。 -
在
Main
类中,可以添加一个方法,用于修改用户的订单。 -
在
Main
类中,可以添加一个方法,用于查找用户的订单。 -
在
Main
类中,可以添加一个方法,用于显示所有用户的订单信息。
5. 总结:对本阶段(6-11周)综合性总结:
这段时间通过课程学习了专业领域的基础知识和理论,对相关概念和方法有了更深入的理解。其次,通过作业和实验,我学会了如何应用所学知识解决实际问题,并提高了自己的实践能力。此外,课上及课下组织方式也让我更好地与教师和其他同学进行交流和合作,提高了学习效果。
然而,我也意识到自己在某些方面还需要进一步学习和研究。首先,我需要加强对一些重要概念和理论的理解和掌握,以便能够更深入地应用到实际问题中。其次,我需要提高自己的问题解决能力和创新思维,培养独立思考和解决问题的能力。此外,我还需要加强对相关工具和技术的学习和掌握,以提高自己的实践能力。
对于教师、课程、作业、实验、课上及课下组织方式等方面的改进建议和意见:
-
课程内容可以更加贴近实际应用场景,增加实践性的案例和项目,让学生能够更好地将所学知识应用到实际问题中。
-
作业和实验可以更加注重培养学生的问题解决能力和创新思维。可以设置一些开放性的问题和项目,让学生能够自主思考和探索解决方案。
-
课上及课下组织方式可以更加灵活多样,鼓励学生之间的合作和交流。可以设置小组项目、讨论会等形式,让学生能够相互学习和借鉴。
本阶段的学习让我受益匪浅,但也存在一些需要改进的地方。通过不断学习和努力,我相信我能够进一步提高自己的能力和水平。