菜单4、5以及期中考试总结-BLOG-PTA-4、5
22201612-刘健涛
目录
(1)前言
(2)设计与分析
(3)踩坑心得
(4)改进建议
(5)总结
正文
(1)前言
| 题目 |
知识点 |
题量 | 难度 |
| 期中考试 |
|
2分 | 3分 |
| 点菜四、点菜五 |
|
4分 | 4分 |
(2)设计与分析
sourceMonitor
| 期中1 | ![]() |
| 期中2 | ![]() |
| 期中3 | ![]() |
| 期中4 | ![]() |
| 点菜4 | ![]() |
| 点菜5 | ![]() |
类图
| 期中1 | ![]() |
| 期中2 | |
| 期中3 | |
| 期中4 | |
| 点菜4 | ![]() |
| 点菜5 | ![]() |
package 期中考试第一题;
import java.util.Scanner;
import 期中考试4.Circle;
public class Main {
public static void main(String[] a)
{
Scanner in=new Scanner(System.in);
double radius=in.nextDouble();
if(radius>0.0){
Circle c=new Circle(radius);
String line=String.format("%f",getArea(c));
System.out.format(line);
}else{
System.out.println("Wrong Format");
}
}
private static String getArea(Circle c) {
String area=String.valueOf(c.radiums*c.radiums*Math.PI);
return area;
}
}
该程序通过用户输入的半径值计算圆的面积,并进行相应的输出。
如果半径值合法(大于0.0),则创建一个Circle对象c。这个Circle对象需要一个半径值作为参数来进行初始化。通过调用getArea(c)方法来计算圆的面积。该方法接受一个Circle对象作为参数,并返回计算得到的面积值。使用System.out.format()来打印输出面积值。
如果用户输入的半径值不合法(小于等于0.0),则输出"Wrong Format"。
package 期中考试第二题;
import java.util.Scanner;
import 期中考试4.Point;
import 期中考试4.Rectangle;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double x1 = in.nextDouble(), y1 = in.nextDouble(), x2 = in.nextDouble(), y2 = in.nextDouble();
Point p1=new Point(x1,y1);
Point p2=new Point(x2,y2);
Rectangle rec=new Rectangle(p1, p2);
System.out.format("%.2f", (float)rec.getArea());
}
}
这段程序的功能是获取用户输入的两个点坐标(x1、y1和x2、y2),然后创建一个矩形对象并计算其面积,并输出保留两位小数的结果。
package 期中考试第三题;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
switch (choice) {
case 1:// Circle
double radiums = input.nextDouble();
if (radiums <= 0) {
System.out.println("Wrong Format");
} else {
Shape circle = new Circle(radiums);
printArea(circle);
}
break;
case 2:// Rectangle
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();
Point leftTopPoint = new Point(x1, y1);
Point lowerRightPoint = new Point(x2, y2);
Rectangle rectangle = new Rectangle(leftTopPoint, lowerRightPoint);
printArea(rectangle);
break;
}
}
private static void printArea(Shape s) {
System.out.format("%.2f\n", (float) s.getArea());
}
}
class Circle extends Shape {
public double getradiums() {
return radiums;
}
public void setradiums(double radiums) {
this.radiums = radiums;
}
public Circle(double radiums) {
super();
this.radiums = radiums;
}
public double radiums;
public double getArea() {
return 3.14159 * radiums * radiums;
}
}
class Point {
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public Point() {
super();
}
public Point(double x, double y) {
super();
this.x = x;
this.y = y;
}
public double x;
public double y;
}
class Rectangle extends Shape {
public Point getTopLeftPoint() {
return topLeftPoint;
}
public void setTopLeftPoint(Point topLeftPoint) {
this.topLeftPoint = topLeftPoint;
}
public Point getLowerRightPoint() {
return lowerRightPoint;
}
public void setLowerRightPoint(Point lowerRightPoint) {
this.lowerRightPoint = lowerRightPoint;
}
public Rectangle() {
super();
}
public Rectangle(Point topLeftPoint, Point lowerRightPoint) {
super();
this.topLeftPoint = topLeftPoint;
this.lowerRightPoint = lowerRightPoint;
}
public Point topLeftPoint;
public Point lowerRightPoint;
public double getLength() {
Point L = this.getTopLeftPoint();
Point R = this.getLowerRightPoint();
double length = L.getX() - R.getX();
return Math.abs(length);
}
public double getHeight() {
Point L = this.getTopLeftPoint();
Point R = this.getLowerRightPoint();
double height = L.getY() - R.getY();
return Math.abs(height);
}
public double getArea() {
double area = getHeight() * getLength();
return area;
}
}
abstract class Shape {
public Shape() {
}
public abstract double getArea();
}
该代码实现了简单的图形计算功能,包括计算圆形和矩形的面积。
- 在Main类中,使用Scanner类获取用户输入的选择(choice),然后通过switch语句进入不同的分支。
- 如果用户选择1,表示计算圆形的面积。代码会继续读取用户输入的半径值(radiums),然后创建一个Circle对象,并调用printArea()方法计算和打印出圆形的面积。
- 如果用户选择2,表示计算矩形的面积。代码会读取用户输入的坐标值(x1、y1、x2、y2),分别表示矩形两个顶点的坐标。然后创建一个Rectangle对象,并调用printArea()方法计算和打印出矩形的面积。
package 点菜4;
import java.text.ParseException;
import java.time.DateTimeException;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static boolean tableValid;
public static int lastOrderNum = 0;
public static void main(String[] args) throws ParseException {
int lastState = -1;
int state = -1;
Order theOrder = null;
Scanner in = new Scanner(System.in);
ArrayList<Order> orders = new ArrayList<>();
while (true) {
String str = in.nextLine();
if (str.equals("end")) {
break;
}
String[] pieces = str.split(" ");
int ln = pieces.length;
String combine = "";
for (String ck : pieces) {
combine += ck;
}
/* Prepare menu */
if (ln == 2) {
try {
if (!pieces[1].equals("delete")) {
String dishName = pieces[0];
int unitPrice = Integer.parseInt(pieces[1]);// key
lastState = state;
state = Status.MENU_PREPARE;
boolean forward = true;
if (unitPrice < 0 || unitPrice >= 300) {
System.out.println(dishName + " price out of range " + unitPrice);
forward = false;
}
if (pieces[1].charAt(0) == '0') {
throw new NumberFormatException();
}
if (lastState > Status.MENU_PREPARE
&& tableValid == true)/* menu procedure and order procedure are mixed */
{
System.out.println("invalid dish");
forward = false;
}
if (forward) {
Menu.remove(Menu.searthDish(
dishName));/* the successive 2 statements for the already existed dish */
Menu.add(dishName, unitPrice);
}
}
} catch (NumberFormatException e) {
System.out.println("wrong format");
}
}
if (ln == 3)/* new! special dish */
{
try {
int unitPrice = Integer.parseInt(pieces[1]);
String dishName = pieces[0];
String specialOrNot = pieces[2];
lastState = state;
state = Status.MENU_PREPARE;
boolean forward = true;
if (lastState > Status.MENU_PREPARE)/* menu procedure and order procedure are mixed */
{
forward = false;
System.out.println("invalid dish");
}
if (unitPrice < 0 || unitPrice >= 300) {
System.out.println(dishName + " price out of range " + unitPrice);
forward = false;
}
if (pieces[1].charAt(0) == '0') {
throw new NumberFormatException();
}
if (forward) {
boolean isSpecial = false;
if (specialOrNot.equals("T")) {
isSpecial = true;
}
Menu.remove(Menu.searthDish(dishName));
Menu.add(dishName, unitPrice, isSpecial);
}
} catch (NumberFormatException e) {
System.out.println("wrong format");
}
}
/* prepare table */
if (ln == 4 && combine.contains("table")) {
try {
if (pieces[0].equals("table")) {
tableValid = false;
int tableNum = Integer.parseInt(pieces[1]);// key
lastState = state;
state = Status.TABLE_PREPARE;
orders.add(theOrder);// 这句的顺序一定要在这里
String[] p = pieces[2].split("/");
boolean forward = true;
if (tableNum > 55) {
System.out.println("tableNum table num out of range");
forward = false;
}
if (pieces[1].length() > 2 || pieces[1].charAt(0) == '0') {
System.out.println("wrong format");
forward = false;
}
if (p[0].length() != 4) {
throw new DateTimeException(null);
}
if (forward) {
int p1 = Integer.parseInt(p[0]);
int p2 = Integer.parseInt(p[1]);
int p3 = Integer.parseInt(p[2]);
LocalDate date = LocalDate.of(p1, p2, p3);
if (p[0].length() != 4) {
throw new NumberFormatException();
}
p = pieces[3].split("/");
p1 = Integer.parseInt(p[0]);
p2 = Integer.parseInt(p[1]);
p3 = Integer.parseInt(p[2]);
if (p[0].length() > 2 || p[1].length() > 2 || p[2].length() > 2) {
throw new NumberFormatException();
}
LocalTime time = LocalTime.of(p1, p2, p3);
theOrder = new Order(tableNum, date, time);
System.out.println("table " + tableNum + ": ");
tableValid = true;
lastOrderNum = 0;
}
} else {
System.out.println("wrong format");
}
} catch (NumberFormatException e) {
System.out.println("wrong format");
} catch (DateTimeException e) {
/* this exception contains the fault happening in "parseInt" */
int tableNum = Integer.parseInt(pieces[1]);// key
System.out.println(tableNum + " date error");
}
}
/* order dish */
if (ln == 4) {
boolean isExist = false;
try {
if (tableValid) {
int orderNum = Integer.parseInt(pieces[0]);
if (orderNum > lastOrderNum) {
boolean forward = true;
lastState = state;
state = Status.ORDER_DISH;
String dishName = pieces[1];
int portion = Integer.parseInt(pieces[2]);
int num = Integer.parseInt(pieces[3]);
if (Menu.searthDish(dishName) == null && forward) {
System.out.println(dishName + " does not exist");
forward = false;
} else {
isExist = true;
}
if ((portion < 1 || portion > 3) && forward)/* error for portion */
{
System.out.println(orderNum + " portion out of range " + portion);
forward = false;
}
if (pieces[2].length() > 1 && forward) {
System.out.println("not a valid portion");
forward = false;
}
if (num + theOrder.getTotalNum() > 15 && forward)/* error for num */
{
System.out.println(orderNum + " num out of range " + num);
forward = false;
}
if (pieces[3].charAt(0) == '0' && forward) {
System.out.println("wrong format");
forward = false;
}
if (forward) {
if (isExist) {
for (Record ck : theOrder.getRecords()) {
if (dishName.equals(ck.getD()
.getName()))/*
* same dishName Record should be ADDED UP TO the former one
*/
{
num += ck.getNum();
theOrder.deleteOrderByNumber(orderNum);
/* 计算的时候应该不会跟orderNum有关,毕竟计算时候不输出,这里就不写了 */
}
}
Record record = theOrder.addARecord(orderNum, dishName, portion, num);
int price = record.getPrice();
System.out.println(orderNum + " " + dishName + " " + price);
lastOrderNum = orderNum;
}
}
} else {
System.out.println("record serial number sequence error");
}
}
} catch (Exception e) {
}
}
/* delete dish */
if (ln == 2) {
try {
if (pieces[1].equals("delete")) {
int deleteOrderNum = Integer.parseInt(pieces[0]);
boolean isExist = false;
if (theOrder.deleteNums.contains(deleteOrderNum))/* Delete Duplication display */
{
System.out.println("deduplication " + deleteOrderNum);
} else if (theOrder.findRecordByNum(deleteOrderNum) == null) {
System.out.println("delete error;");
} else {
isExist = true;
}
if (isExist) {
theOrder.deleteOrderByNumber(deleteOrderNum);
theOrder.deleteNums.add(deleteOrderNum);
}
}
} catch (Exception e) {
}
}
/* make a treat */
if (ln == 5) {
if (tableValid && !combine.contains("table")) {
int tableNum = Integer.parseInt(pieces[0]);
int orderNum = Integer.parseInt(pieces[1]);
String dishName = pieces[2];
int portion = Integer.parseInt(pieces[3]);
int num = Integer.parseInt(pieces[4]);
Order order2 = Order.findOrderByNum(orders, tableNum);
boolean forward = true;
if (order2 == null)/* table is missing if I wanna make a treat */
{
System.out.println("Table number : " + tableNum + " does not exist");
forward = false;
}
if (forward) {
Record theRecord = order2.addARecord(orderNum, dishName, portion, num);
int pay = theRecord.getPrice();
theOrder.setExtra(1 * pay);
order2.setExtra(-1 * pay);
System.out.println(orderNum + " table " + theOrder.getTableNum() + " pay for table " + tableNum
+ " " + pay);
}
}
}
} // 必要的
orders.add(theOrder);
/* discount dish */
LocalTime night1 = LocalTime.of(16, 59, 59);
LocalTime night2 = LocalTime.of(20, 31);
LocalTime noon1 = LocalTime.of(10, 29, 59);
LocalTime noon2 = LocalTime.of(14, 31);
LocalTime weekend1 = LocalTime.of(9, 29, 59);
LocalTime weekend2 = LocalTime.of(21, 31, 00);
/* discount */
for (Order ck : orders) {
if (ck == null) {
continue;
}
int day = ck.getDate().getDayOfWeek().getValue();
LocalTime time = ck.getTime();
if (day >= 1 && day <= 5) {
if (time.isBefore(night2) && time.isAfter(night1)) {
ck.discount = 0.8;
} else if (time.isBefore(noon2) && time.isAfter(noon1)) {
ck.discount = 0.6;
} else {
ck.setValid(false);
}
} else {
if (time.isAfter(weekend1) && time.isBefore(weekend2)) {
ck.discount = 1.0;
} else {
ck.setValid(false);
}
}
}
/* table total price display */
for (Order ck : orders) {
if (ck == null) {
continue;
}
int tableNum1 = ck.getTableNum();
boolean hasSame = false;
for (Order od : orders) {
if ((od == null) || ck.equals(od)) {
continue;
}
if (tableNum1 == od.getTableNum()) {
int day1 = ck.getDate().getDayOfWeek().getValue();
int day2 = od.getDate().getDayOfWeek().getValue();
LocalTime time1 = ck.getTime();
LocalTime time2 = od.getTime();
if (day1 == day2) {
if (day1 >= 1 && day1 <= 5) {
if ((time1.isBefore(noon2) && time1.isAfter(noon1))
&& (time2.isBefore(night2) && time2.isAfter(night1))) {
}
}
} else {
long time_diff = Duration.between(time1, time2).getSeconds();
if (time_diff < 60 * 60) {
} else {
}
}
}
}
int price = ck.getTotalPrice();
int tableNum = ck.getTableNum();
int after_price = ck.getAfterPrice();
if (ck.isValid()) {
System.out.println("table " + tableNum + ": " + price + " " + after_price);
} else {
System.out.println("table " + tableNum + " out of opening hours");
}
}
}
}
package 点菜4;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
public class Dish {
private String name;
private int unit_price;
public boolean isSpecial;
public Dish(String name, int unit_price, boolean isSpecial) {
super();
this.name = name;
this.unit_price = unit_price;
this.isSpecial = isSpecial;
}
public Dish(String name, int unit_price) {
super();
this.name = name;
this.unit_price = unit_price;
}
public String getName() {
return name;
}
public int getPrice(int portion) {
int price = 0;
if (portion == 1) {
price = Math.round(1 * unit_price);
}
if (portion == 2) {
price = (int) Math.round(1.5 * unit_price);
}
if (portion == 3) {
price = Math.round(2 * unit_price);
}
return price;
}
public int getUnit_price() {
return unit_price;
}
public void setName(String name) {
this.name = name;
}
public void setUnit_price(int unit_price) {
this.unit_price = unit_price;
}
}
class Menu {
// Dish[] dishs ;
public static ArrayList<Dish> dishs = new ArrayList<>();
public static Dish searthDish(String dishName) {
for (Dish ck : dishs) {
if (ck.getName().equals(dishName)) {
return ck;
}
}
return null;
}
public Menu() {
super();
dishs.add(new Dish("麻婆豆腐", 12));
dishs.add(new Dish("清炒土豆丝", 12));
dishs.add(new Dish("西红柿炒蛋", 15));
dishs.add(new Dish("油淋生菜", 9));
}
public static void remove(Dish searthDish) {
dishs.remove(searthDish);
}
public static void add(String dishName, int unitPrice) {
dishs.add(new Dish(dishName, unitPrice));
}
public static void add(String dishName, int unitPrice, boolean isSpecial) {
dishs.add(new Dish(dishName, unitPrice, isSpecial));
}
}
class Order {
public static Order findOrderByNum(ArrayList<Order> orders, int tableNum) {
for (Order ck : orders) {
if (ck == null) {
continue;
}
if (ck.getTableNum() == tableNum) {
return ck;
}
}
return null;
}
public int getTotalNum() {
int num = 0;
for (Record ck : records) {
num += ck.getNum();
}
return num;
}
// Record[] records;//保存订单上每一道的记录
public ArrayList<Integer> deleteNums = new ArrayList<>();
private ArrayList<Record> records = new ArrayList<>();
private int tableNum;
private LocalDate date;
private LocalTime time;
private int extra;
public double discount;
private boolean valid = true;
public Order(int tableNum, LocalDate date, LocalTime time) {
super();
this.tableNum = tableNum;
this.date = date;
this.time = time;
}
public Record addARecord(int orderNum, String dishName, int portion, int num) {
Record record = new Record(orderNum, Menu.searthDish(dishName), portion, num);
records.add(record);
return record;
}
public void deleteOrderByNumber(int deleteOrderNum) {
records.remove(findRecordByNum(deleteOrderNum));
}
public Record findRecordByNum(int orderNum) {
for (Record ck : getRecords()) {
if (ck.getOrderNum() == orderNum) {
return ck;
}
}
return null;
}
public LocalDate getDate() {
return date;
}
public int getExtra() {
return extra;
}
public int getTableNum() {
return tableNum;
}
public LocalTime getTime() {
return time;
}
public int getTotalPrice() {
int sum = 0;
for (Record ck : getRecords()) {
sum += ck.getPrice();
}
return (int) (Math.round((sum + extra)));
}
public int getAfterPrice() {
int sum = 0;
int sum2 = 0;
for (Record ck : getRecords()) {
sum += ck.getPrice();
}
for (Record ck : records) {
if (ck.getD().isSpecial == true) {
int specialPrice = ck.getPrice();
sum -= specialPrice;
sum2 += specialPrice;
}
}
double specialDiscount = 0.0;
int today = this.date.getDayOfWeek().getValue();
if (today >= 1 && today <= 5) {
specialDiscount = 0.7;
} else {
specialDiscount = 1.0;
}
int price = (int) (Math.round((sum + extra) * discount)) + (int) (Math.round(sum2 * specialDiscount));
return price;
}
public boolean isValid() {
return valid;
}
public void setDate(LocalDate date) {
this.date = date;
}
public void setExtra(int extra) {
this.extra = extra;
}
public void setTime(LocalTime time) {
this.time = time;
}
public void setValid(boolean valid) {
this.valid = valid;
}
public ArrayList<Record> getRecords() {
return records;
}
}
class Record {
private int orderNum;
private Dish d;
private int portion;
private int num;
public Record(int orderNum, Dish d, int portion, int num) {
super();
this.orderNum = orderNum;
this.d = d;
this.portion = portion;
this.num = num;
}
public int getOrderNum() {
return orderNum;
}
public int getPrice() {
return getD().getPrice(portion) * getNum();
}
public void setOrderNum(int orderNum) {
this.orderNum = orderNum;
}
public Dish getD() {
return d;
}
public int getNum() {
return num;
}
}
class Status {
public static final int MENU_PREPARE = 1;
public static final int TABLE_PREPARE = 2;
public static final int ORDER_DISH = 3;
}
我主要写了以下功能:
准备桌子和订单。
- 首先,代码会检查输入的行是否包含4个单词且包含"table"。然后,它会尝试解析输入的内容以获取桌号、日期和时间信息。
- 如果解析成功,代码会创建一个新的订单对象并将其添加到订单列表中。然后,它会打印出成功准备桌子的提示信息,并更新状态。
点菜。
- 首先,代码会检查输入的行是否包含4个单词。
- 然后,它会尝试解析输入的内容,包括订单号、菜品名称、份量和数量。
- 如果解析成功,并且之前通过准备桌子功能验证过该桌号的有效性,代码会继续执行。
- 接下来,代码会检查菜品是否存在。如果菜品不存在,则打印出相应的错误信息。否则,将isExist设置为true。
- 然后,代码会检查份量和数量是否符合要求。如果不符合要求,则打印出相应的错误信息,并将forward设置为false。
- 最后,如果forward为true,即输入的内容没有错误,代码会继续执行。它会遍历订单中的记录,如果存在相同的菜品名称,将数量累加到之前的记录上,并删除原有的记录。
- 然后,代码会添加一个新的记录,并计算该记录的价格。最后,打印出订单号、菜品名称和价格。
删除菜品
- 首先,代码会检查输入的行是否包含2个单词。
- 然后,它会尝试解析输入的内容,包括订单号和"delete"关键字。
- 如果解析成功,代码会继续执行。
- 接下来,代码会检查待删除的订单是否存在。如果订单号已经存在于deleteNums集合中,说明该订单已经被删除过一次,将打印相应的信息。
- 如果订单号不存在于订单记录中,说明删除错误,将打印相应的错误信息。
- 如果订单存在并且未被删除过,代码将继续执行,并将isExist设置为true。
- 然后,代码会调用deleteOrderByNumber方法从订单中删除指定订单号的记录,并将订单号加入deleteNums集合中,以便标记为已删除。
制作赠菜
- 首先,代码会检查输入的行是否包含5个单词。
- 然后,它会判断之前通过准备桌子功能验证过该桌号的有效性,并且当前还没有进行过赠菜操作(combine集合中不包含"table"关键字)。
- 接下来,代码会尝试解析输入的内容,包括桌号、订单号、菜品名称、份量和数量。
- 如果解析成功,并且通过findOrderByNum方法找到了与桌号对应的订单记录order2,代码会继续执行。
- 然后,代码会将forward设置为true,表示可以继续执行。
- 接下来,代码会调用addARecord方法将一条新的记录添加到订单order2中,并返回该记录theRecord。
- 然后,代码会计算并更新订单和账单的金额。具体做法是为主订单(theOrder)增加赠菜的金额(pay),并为赠菜的订单(order2)减少相同金额。
- 最后,代码会打印出相关信息,包括订单号、当前桌号、要支付的桌号以及支付金额。
- 代码中使用了setExtra方法来增加或减少金额。
当输入了end后:
- 在将订单theOrder添加到订单列表orders之后,对订单进行折扣处理的逻辑。
- 首先,代码定义了一些时间范围,包括夜晚、中午和周末的起止时间。这些时间范围将用于确定订单是否适用折扣。
- 然后,代码使用一个循环遍历订单列表中的每个订单ck。
- 在循环中,代码会检查当前订单ck是否为空,如果为空,则跳过该订单继续执行下一个订单。
- 接下来,代码获取订单日期的星期几,并将结果赋值给变量day。
- 然后,代码获取订单的时间,并将结果赋值给变量time。
- 接下来,代码通过一系列条件判断来确定订单的折扣率。如果订单日期为星期一至星期五(day>=1且day<=5),则根据时间范围设定折扣率:在夜晚1范围内(night1 < time < night2)折扣率为0.8,在中午范围内(noon1 < time < noon2)折扣率为0.6,其他情况下将订单标记为无效(ck.setValid(false))。
- 如果订单日期为星期六或星期日(day>5),则根据时间范围设定折扣率:在周末范围内(weekend1 < time < weekend2)折扣率为1.0,其他情况下将订单标记为无效。
- 最后,通过循环处理所有订单的方式,对每个订单进行了折扣处理。
查看代码
package 点菜5;
public class Dish {
private String name;
private String dishType;
private int unit_price;
private boolean isSpecial;
private int degree=-1;
public Dish(String name, int unit_price) {
super();
this.name = name;
this.unit_price = unit_price;
}
public Dish(String dishName, String dishType, int unitPrice) {
this.name=dishName;
this.dishType=dishType;
this.unit_price=unitPrice;
this.isSpecial=true;
}
public String getName() {
return name;
}
public int getPrice(int portion) {
int price = 0;
if (portion == 1) {
price = Math.round(1 * unit_price);
}
if (portion == 2) {
price = (int) Math.round(1.5 * unit_price);
}
if (portion == 3) {
price = Math.round(2 * unit_price);
}
return price;
}
public int getUnit_price() {
return unit_price;
}
public void setName(String name) {
this.name = name;
}
public void setUnit_price(int unit_price) {
this.unit_price = unit_price;
}
public String getDishType() {
return dishType;
}
public void setDegree(int degree) {
this.degree = degree;
}
public int getDegree() {
return degree;
}
public boolean isSpecial() {
return isSpecial;
}
}
package 点菜5;
import java.util.ArrayList;
public class Menu {
// Dish[] dishs ;
public static ArrayList<Dish> dishs = new ArrayList<>();
public static Dish searthDish(String dishName) {
for (Dish ck : dishs) {
if (ck.getName().equals(dishName)) {
Dish dish=null;//一定不能直接找到ck后就返回ck,因为会造成一连串的错误
//要返回就应该克隆后(new 后)返回
if(ck.isSpecial())
{
dish=new Dish(ck.getName(),ck.getDishType(),ck.getUnit_price());
}else
{
dish=new Dish(ck.getName(),ck.getUnit_price());
}
return dish;
}
}
return null;
}
public Menu() {
super();
dishs.add(new Dish("麻婆豆腐", 12));
dishs.add(new Dish("清炒土豆丝", 12));
dishs.add(new Dish("西红柿炒蛋", 15));
dishs.add(new Dish("油淋生菜", 9));
}
public static void remove(Dish searthDish) {
dishs.remove(searthDish);
}
public static void add(String dishName, int unitPrice) {
dishs.add(new Dish(dishName,unitPrice));
}
public static void add(String dishName, String dishType, int unitPrice) {
dishs.add(new Dish(dishName,dishType,unitPrice));
}
}
package 点菜5;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
public class Order {
public static Order findOrderByNum(ArrayList<Order> orders, int tableNum) {
for (Order ck : orders) {
if (ck == null) {
continue;
}
if (ck.getTableNum() == tableNum) {
return ck;
}
}
return null;
}
// Record[] records;//保存订单上每一道的记录
private ArrayList<Record> records = new ArrayList<>();
private int tableNum;
public String guestName;
public String telephone;
private LocalDate date;
private LocalTime time;
private int extra;
public boolean hasSpecialDish;
public double discount;
public double spe_discount;
public int[] dishTypedata = new int[7];
private boolean valid = true;
public int total;
public Order(int tableNum, LocalDate date, LocalTime time) {
super();
this.tableNum = tableNum;
this.date = date;
this.time = time;
}
public Order(int tableNum, String guestName, String telephone, LocalDate date, LocalTime time) {
super();
this.tableNum = tableNum;
this.guestName = guestName;
this.telephone = telephone;
this.date = date;
this.time = time;
}
public Record addARecord(int orderNum, String dishName, int portion, int num) {
Record record = new Record(orderNum, Menu.searthDish(dishName), portion, num);
getRecords().add(record);
return record;
}
public Record addARecord(int orderNum, String dishName, int degree, int portion, int num) {
Record record = new Record(orderNum, Menu.searthDish(dishName), degree, portion, num);
getRecords().add(record);
return record;
}
public void deleteOrderByNumber(int deleteOrderNum) {
getRecords().remove(findRecordByNum(deleteOrderNum));
}
public Record findRecordByNum(int orderNum) {
for (Record ck : getRecords()) {
if (ck.getOrderNum() == orderNum) {
return ck;
}
}
return null;
}
public LocalDate getDate() {
return date;
}
public int getExtra() {
return extra;
}
public int getTableNum() {
return tableNum;
}
public LocalTime getTime() {
return time;
}
public int getTotalPrice() {
int price = 0;
int no_special_sum = 0;
int special_sum = 0;
for (Record ck : getRecords()) {
if (ck.getD().isSpecial() == false) {
no_special_sum += ck.getPrice();
} else {
special_sum += ck.getPrice();
}
}
// price = (int) (Math.ceil(((no_special_sum + extra) * discount)+(special_sum*spe_discount)));
price = (int) (Math.round((no_special_sum * discount)+((special_sum+extra)*spe_discount)));
return price;
}
public int getBeforePrice() {
int sum = 0;
for (Record ck : getRecords()) {
sum += ck.getPrice();
}
return (int) (Math.round((sum + extra) * 1));
}
public boolean isValid() {
return valid;
}
public void setDate(LocalDate date) {
this.date = date;
}
public void setExtra(int extra) {
this.extra += extra;
}
public void setTime(LocalTime time) {
this.time = time;
}
public void setValid(boolean valid) {
this.valid = valid;
}
public ArrayList<Record> getRecords() {
return records;
}
}
package 点菜5;
public class Record {
private int orderNum;
private Dish d;
private int portion;
private int num;
public Record(int orderNum, Dish d, int portion, int num) {
super();
this.orderNum = orderNum;
this.d = d;
this.portion = portion;
this.num = num;
}
public Record(int orderNum, Dish d, int degree,int portion, int num) {
super();
d.setDegree(degree);
this.orderNum = orderNum;
this.d = d;
this.portion = portion;
this.num = num;
}
public int getOrderNum() {
return orderNum;
}
public int getPrice() {
return getD().getPrice(portion)*getNum();
}
public void setOrderNum(int orderNum) {
this.orderNum = orderNum;
}
public Dish getD() {
return d;
}
public int getNum() {
return num;
}
}
package 点菜5;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
public class Main implements Comparator<Order> {
public static void main(String[] args) {
Order theOrder = null;
Scanner in = new Scanner(System.in);
ArrayList<Order> orders = new ArrayList<>();
while (true) {
String str = in.nextLine();
if (str.equals("end")) {
break;
}
String[] pieces = str.split(" ");
int ln = pieces.length;
/* start to serve for guests */
/* prepare menu */
if (ln == 2) {
try {
int unitPrice = Integer.parseInt(pieces[1]);
String dishName = pieces[0];
Menu.remove(Menu.searthDish(dishName));
Menu.add(dishName, unitPrice);
} catch (Exception e) {
}
}
if (ln == 4) {
if (pieces[3].equals("T")) {
String dishName = pieces[0];
String dishType = pieces[1];
int unitPrice = Integer.parseInt(pieces[2]);
Menu.remove(Menu.searthDish(dishName));
Menu.add(dishName, dishType, unitPrice);
}
}
if(str.matches(".*T"))
{
if(ln==2)
{
}else if(ln==4) {
}else {
System.out.println("wrong format");
}
}
/* prepare table */
// if (ln == 4 && pieces[0].equals("table")) {
// try {
// int tableNum = Integer.parseInt(pieces[1]);
// orders.add(theOrder);// 这句的顺序一定要在这里
// String[] p = pieces[2].split("/");
// int p1 = Integer.parseInt(p[0]);
// int p2 = Integer.parseInt(p[1]);
// int p3 = Integer.parseInt(p[2]);
// LocalDate date = LocalDate.of(p1, p2, p3);
// p = pieces[3].split("/");
// p1 = Integer.parseInt(p[0]);
// p2 = Integer.parseInt(p[1]);
// p3 = Integer.parseInt(p[2]);
// LocalTime time = LocalTime.of(p1, p2, p3);
// theOrder = new Order(tableNum, date, time);
// System.out.println("table " + tableNum + ": ");
// } catch (Exception e) {
// }
// }
if (ln == 7 && pieces[0].equals("table") && pieces[2].equals(":")) {
try {
int tableNum = Integer.parseInt(pieces[1]);
String guestName = pieces[3];
String telephone = pieces[4];
String threeNumbers = "" + telephone.charAt(0) + telephone.charAt(1) + telephone.charAt(2);
boolean forward = true;
if (guestName.length() > 10) {
forward = false;
}
if (telephone.length() != 11) {
forward = false;
}
if (!(threeNumbers.equals("180") || threeNumbers.equals("181") || threeNumbers.equals("189")
|| threeNumbers.equals("133") || threeNumbers.equals("135")
|| threeNumbers.equals("136"))) {
forward = false;
}
if(forward==false)
{
System.out.println("wrong format");
}
if (forward) {
if (theOrder != null) {
orders.add(theOrder);// 这句的顺序一定要在这里
}
String[] p = pieces[5].split("/");
int p1 = Integer.parseInt(p[0]);
int p2 = Integer.parseInt(p[1]);
int p3 = Integer.parseInt(p[2]);
LocalDate date = LocalDate.of(p1, p2, p3);
p = pieces[6].split("/");
p1 = Integer.parseInt(p[0]);
p2 = Integer.parseInt(p[1]);
p3 = Integer.parseInt(p[2]);
LocalTime time = LocalTime.of(p1, p2, p3);
LocalTime night1 = LocalTime.of(16, 59, 59);
LocalTime night2 = LocalTime.of(20, 31);
LocalTime noon1 = LocalTime.of(10, 29, 59);
LocalTime noon2 = LocalTime.of(14, 31);
LocalTime weekend1 = LocalTime.of(9, 29, 59);
LocalTime weekend2 = LocalTime.of(21, 31, 00);
boolean valid = true;
int day = date.getDayOfWeek().getValue();
if (day >= 1 && day <= 5) {
if (time.isBefore(night2) && time.isAfter(night1)) {
} else if (time.isBefore(noon2) && time.isAfter(noon1)) {
} else {
valid = false;
}
} else {
if (time.isAfter(weekend1) && time.isBefore(weekend2)) {
} else {
valid = false;
}
}
if (valid) {
theOrder = new Order(tableNum, guestName, telephone, date, time);
System.out.println("table " + tableNum + ": ");
} else {
System.out.println("table " + tableNum + " out of opening hours");
}
}
} catch (Exception e) {
}
}
/* order dish */
if (ln == 4) {
boolean isExist = false;
try {
int orderNum = Integer.parseInt(pieces[0]);
String dishName = pieces[1];
int portion = Integer.parseInt(pieces[2]);
int num = Integer.parseInt(pieces[3]);
if (Menu.searthDish(dishName) == null) {
System.out.println(dishName + " does not exist");
} else {
isExist = true;
}
if (isExist) {
Record record = theOrder.addARecord(orderNum, dishName, portion, num);
int price = record.getPrice();
System.out.println(orderNum + " " + dishName + " " + price);
}
} catch (Exception e) {
}
}
if (ln == 5) {
boolean isExist = false;
try {
int orderNum = Integer.parseInt(pieces[0]);
String dishName = pieces[1];
int degree = Integer.parseInt(pieces[2]);
int portion = Integer.parseInt(pieces[3]);
int num = Integer.parseInt(pieces[4]);
Dish dish = Menu.searthDish(dishName);
String dishType = null;
if(theOrder!=null)
{
if (dish == null) {
System.out.println(dishName + " does not exist");
} else {
isExist = true;
dishType = dish.getDishType();
}
boolean forward = false;
if (isExist) {
String degreeType = null;
if (dishType.equals("川菜")) {
degreeType = "spicy";
if (degree <= 5 && degree >= 0) {
forward = true;
}
} else if (dishType.equals("晋菜")) {
degreeType = "acidity";
if (degree <= 4 && degree >= 0) {
forward = true;
}
} else if (dishType.equals("浙菜")) {
degreeType = "sweetness";
if (degree <= 3 && degree >= 0) {
forward = true;
}
}
if (!forward) {
System.out.println(degreeType + " num out of range : " + degree);
}
}
if (forward) {
Record record = theOrder.addARecord(orderNum, dishName, degree, portion, num);
int price = record.getPrice();
System.out.println(orderNum + " " + dishName + " " + price);
theOrder.hasSpecialDish = true;
}
}
} catch (Exception e) {
}
}
/* delete record */
if (ln == 2) {
try {
int deleteOrderNum = Integer.parseInt(pieces[0]);
boolean isExist = false;
if (theOrder.findRecordByNum(deleteOrderNum) == null) {
System.out.println("delete error;");
} else {
isExist = true;
}
if (isExist) {
theOrder.deleteOrderByNumber(deleteOrderNum);
}
} catch (Exception e) {
}
}
/* make a treat */
if (ln == 5) {
try {
int tableNum = Integer.parseInt(pieces[0]);
int orderNum = Integer.parseInt(pieces[1]);
String dishName = pieces[2];
int portion = Integer.parseInt(pieces[3]);
int num = Integer.parseInt(pieces[4]);
Order order2 = Order.findOrderByNum(orders, tableNum);
Record theRecord = order2.addARecord(orderNum, dishName, portion, num);
int pay = theRecord.getPrice();
theOrder.setExtra(1 * pay);
order2.setExtra(-1 * pay);
System.out.println(
orderNum + " table " + theOrder.getTableNum() + " pay for table " + tableNum + " " + pay);
} catch (NumberFormatException e) {
}
}
if (ln == 6) {
try {
int tableNum = Integer.parseInt(pieces[0]);
int orderNum = Integer.parseInt(pieces[1]);
String dishName = pieces[2];
int degree = Integer.parseInt(pieces[3]);
int portion = Integer.parseInt(pieces[4]);
int num = Integer.parseInt(pieces[5]);
Order order2 = Order.findOrderByNum(orders, tableNum);
Record theRecord = order2.addARecord(orderNum, dishName, degree, portion, num);
// Record theRecord = new Record(orderNum, Menu.searthDish(dishName), degree, portion, num);
int pay = theRecord.getPrice();
theOrder.setExtra(1 * pay);
order2.setExtra(-1 * pay);
// order2.setExtra(0);
System.out.println(
orderNum + " table " + theOrder.getTableNum() + " pay for table " + tableNum + " " + pay);
} catch (NumberFormatException e) {
}
}
}
/* necessary */
/* make discount */
orders.add(theOrder);
LocalTime night1 = LocalTime.of(16, 59, 59);
LocalTime night2 = LocalTime.of(20, 31);
LocalTime noon1 = LocalTime.of(10, 29, 59);
LocalTime noon2 = LocalTime.of(14, 31);
LocalTime weekend1 = LocalTime.of(9, 29, 59);
LocalTime weekend2 = LocalTime.of(21, 31, 00);
for (Order ck : orders) {
if (ck == null) {
continue;
}
int day = ck.getDate().getDayOfWeek().getValue();
LocalTime time = ck.getTime();
if (day >= 1 && day <= 5) {
if (time.isBefore(night2) && time.isAfter(night1)) {
ck.discount = 0.8;
ck.spe_discount=0.7;
} else if (time.isBefore(noon2) && time.isAfter(noon1)) {
ck.discount = 0.6;
ck.spe_discount=0.7;
} else {
ck.setValid(false);
}
} else {
if (time.isAfter(weekend1) && time.isBefore(weekend2)) {
ck.discount = 1.0;
ck.spe_discount=1.0;
} else {
ck.setValid(false);
}
}
}
/* pay for bills */
for (int i = 0; i < orders.size(); i++) {
Order ck = orders.get(i);
if (ck == null) {
orders.remove(ck);
continue;
}
if (!ck.hasSpecialDish) {
int beforePrice = ck.getBeforePrice();
int price = ck.total = ck.getTotalPrice();
int tableNum = ck.getTableNum();
if (ck.isValid()) {
System.out.println("table " + tableNum + ": " + beforePrice + " " + price);
} else {
System.out.println("table " + tableNum + " out of opening hours");
}
} else {
int beforePrice = ck.getBeforePrice();
int price = ck.total = ck.getTotalPrice();
int tableNum = ck.getTableNum();
if (ck.isValid()) {
System.out.print("table " + tableNum + ": " + beforePrice + " " + price);
} else {
System.out.println("table " + tableNum + " out of opening hours");
}
for (Record record : ck.getRecords()) {
Dish dish = record.getD();
String dishType = dish.getDishType();
if (dishType != null) {
if (dishType.equals("川菜")) {
ck.dishTypedata[1] += record.getNum();
ck.dishTypedata[2] += dish.getDegree()*record.getNum();
} else if (dishType.equals("晋菜")) {
ck.dishTypedata[3] += record.getNum();
ck.dishTypedata[4] += dish.getDegree()*record.getNum();
} else if (dishType.equals("浙菜")) {
ck.dishTypedata[5] += record.getNum();
ck.dishTypedata[6] += dish.getDegree()*record.getNum();
}
}
}
ck.dishTypedata[2] = (int) (Math.round(ck.dishTypedata[2] / 1.0 / ck.dishTypedata[1]));
ck.dishTypedata[4] = (int) (Math.round(ck.dishTypedata[4] / 1.0 / ck.dishTypedata[3]));
ck.dishTypedata[6] = (int) (Math.round(ck.dishTypedata[6] / 1.0 / ck.dishTypedata[5]));
for (int idx = 1; idx <= 6; idx += 2) {
String dishSmell = null;
String dishType = null;
String typeDegree = null;
int dishNum = ck.dishTypedata[idx];
if (idx % 2 == 1 && ck.dishTypedata[idx] != 0) {
switch (ck.dishTypedata[idx + 1]) {
case 0:
typeDegree = "不";
break;
case 1:
typeDegree = "微";
break;
case 2:
typeDegree = "稍";
break;
case 3:
typeDegree = "";
break;
case 4:
typeDegree = "很";
break;
case 5:
typeDegree = "爆";
break;
}
switch (idx) {
case 1:
dishSmell = "辣";
dishType = "川菜";
break;
case 3:
dishSmell = "酸";
dishType = "晋菜";
break;
case 5:
dishSmell = "甜";
dishType = "浙菜";
break;
}
System.out.print(" " + dishType + " " + dishNum + " " + typeDegree + dishSmell);
}
}
System.out.print("\n");
continue;
}
}
/* guest rank */
orders.sort((o1, o2) -> o1.guestName.compareToIgnoreCase(o2.guestName));
for (Order ck : orders) {
String name = ck.guestName;
String phone = ck.telephone;
int pay = ck.total;
System.out.println(name + " " + phone + " " + pay);
}
}
@Override
public int compare(Order o1, Order o2) {
String name1 = o1.guestName;
String name2 = o2.guestName;
if (name1.compareToIgnoreCase(name2) > 0)
return 1;
else if (name1.compareToIgnoreCase(name2) == 0)
return 0;
else
return -1;
}
}
基本相同和点菜4,解读比较新颖的部分:
客人进行排名和显示客人信息的部分。
- 首先,它使用sort方法对订单列表(orders)进行排序。排序的规则是通过比较订单中客人姓名(guestName)的字母顺序(不区分大小写)来进行排序。
- 然后,通过遍历排序后的订单列表(orders),对每个订单进行处理:
- 获取客人姓名(name)、电话号码(phone)和支付金额(pay)。
- 打印客人姓名、电话号码和支付金额。
(3)踩坑心得
期中考试:
- 计算面积或创建对象时,没有进行参数有效性检查。这可能导致传入无效的参数值,例如负数或空值,从而导致错误的结果或异常。
- 计算面积或获取坐标时,缺乏错误处理机制。当出现异常情况时,没有提供合适的错误提示或返回特定的错误码,导致难以识别或解决问题。
- 在不同的类中可能存在一些重复的代码段,例如计算距离或判断点是否在图形内部的逻辑。这增加了代码的维护成本,并可能导致逻辑错误的可能性。
- 部分类的职责可能不够明确,负责了多个功能。这违反了单一职责原则,使得类的设计不够模块化、可扩展和可维护。
点菜4、5:
- 时间范围的设定可能存在问题,如边界值是否包含、是否考虑了分钟和秒等。在设定时间范围时,要确保覆盖所有需要的时间段,并充分考虑时间精度。(后面就修改了时间)
(4)改进建议
期中考试:
- 在计算面积的方法中,可以添加判断输入参数是否合法,如长度和宽度是否大于0。
- 异常处理,为方法添加适当的异常处理。
- 识别重复的代码段,并将其抽象成独立的方法或公共函数,以便在不同类中复用。
- 在关键的方法和类定义上添加适当的注释。
- 明确类的功能,确保每个类的功能定义清晰明确,符合单一职责原则。
点菜4、5:
- 可以考虑将折扣逻辑和订单列表操作分离为独立的方法或类,以提高代码的可拓展性和重用性。
- 添加更详细的注释来解释代码的功能,提高代码的可读性和可维护性。
- 可以添加处理日期或时间格式错误的异常。
(5)总结
我运用了日期、时间和输入输出处理的相关类和包。通过定义合适的变量和集合,我能够有效地管理订单和菜单信息。主循环的设计让程序能够持续监听用户输入,根据命令执行相应的功能。其中,准备菜单操作、预定餐桌和点菜操作等功能,虽然是困难的吧,但是就是和实际相关,要多用心用脑子思考,怎么设计方法能达到效果。










浙公网安备 33010602011771号