第二次博客作业

前言

第四次大作业:本次大作业有四道题,其中有两道是菜单计价程序,相较于后两次的菜单计价程序来说还是相对简单不少的

第五次大作业:本次作业只有《菜单计价程序-4》一道题,因为涉及到错误判断,比起《菜单计价程序-3》,代码量极速上升,大概是《菜单计价程序-3》的两倍,一开始没上课的时候,老师还没给我们讲关于正则表达式的用法,所以在判断菜单4新增的错误输入的时候显得及其的复杂。当学会如何使用正则表达式时候,代码量下降了许多,但是还是十分复杂,测试点也及其得多,最后我还是没有做到满分

第六次大作业:本次作业也是只有《 菜单计价程序-5》一道题,本次作业也是根据《 菜单计价程序-3》迭代而来,虽然是根据根据菜单3迭代而来,但是我的初始代码仍是在菜单4的基础上删除错误判断而来,因为在做菜单4的过程中,发现了菜单3代码的很多不足,所以初始代码选择了在菜单4的基础上删减错误判断。本次作业增加了特殊菜的概念,输入输出的内容更加丰富,但是比较第四次大作业,还是要更简单一点的

期中考试:相比我们的PTA大作业,这次期中考试倒是相当简单,考的基本是基础知识,,基本按题目写就可以全部过,但是最后一题编程题由于时间的问题,没有来得及写完。但是在考试题目里还有一些选择题,是一些java里的概念题,有好多道题目不清楚,所以在这方面我还得加强。

设计与分析

菜单计价程序-4

本体大部分内容与菜单计价程序-3相同,增加的部分用加粗文字进行了标注。

设计点菜计价程序,根据输入的信息,计算并输出总价格。

输入内容按先后顺序包括两部分:菜单、订单,最后以"end"结束。

菜单由一条或多条菜品记录组成,每条记录一行

每条菜品记录包含:菜名、基础价格 两个信息。

订单分:桌号标识、点菜记录和删除信息、代点菜信息。每一类信息都可包含一条或多条记录,每条记录一行或多行。

桌号标识独占一行,包含两个信息:桌号、时间。

桌号以下的所有记录都是本桌的记录,直至下一个桌号标识。

点菜记录包含:序号、菜名、份额、份数。份额可选项包括:1、2、3,分别代表小、中、大份。

不同份额菜价的计算方法:小份菜的价格=菜品的基础价格。中份菜的价格=菜品的基础价格1.5。小份菜的价格=菜品的基础价格2。如果计算出现小数,按四舍五入的规则进行处理。

删除记录格式:序号 delete

标识删除对应序号的那条点菜记录。

如果序号不对,输出"delete error"

代点菜信息包含:桌号 序号 菜品名称 份额 分数

代点菜是当前桌为另外一桌点菜,信息中的桌号是另一桌的桌号,带点菜的价格计算在当前这一桌。

程序最后按输入的桌号从小到大的顺序依次输出每一桌的总价(注意:由于有代点菜的功能,总价不一定等于当前桌上的菜的价格之和)。

每桌的总价等于那一桌所有菜的价格之和乘以折扣。如存在小数,按四舍五入规则计算,保留整数。

折扣的计算方法(注:以下时间段均按闭区间计算):

周一至周五营业时间与折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余时间不营业。

周末全价,营业时间:9:30-21:30

如果下单时间不在营业范围内,输出"table " + t.tableNum + " out of opening hours"

参考以下类的模板进行设计(本内容与计价程序之前相同,其他类根据需要自行定义):

菜品类:对应菜谱上一道菜的信息。

Dish {

String name;//菜品名称

int unit_price; //单价

int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份) }

菜谱类:对应菜谱,包含饭店提供的所有菜的信息。

Menu {

Dish[] dishs ;//菜品数组,保存所有菜品信息

Dish searthDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。

Dish addDish(String dishName,int unit_price)//添加一道菜品信息

}

点菜记录类:保存订单上的一道菜品记录

Record {

int orderNum;//序号

Dish d;//菜品\\

int portion;//份额(1/2/3代表小/中/大份)

int getPrice()//计价,计算本条记录的价格

}

订单类:保存用户点的所有菜的信息。

Order {

Record[] records;//保存订单上每一道的记录

int getTotalPrice()//计算订单的总价

Record addARecord(int orderNum,String dishName,int portion,int num)//添加一条菜品信息到订单中。

delARecordByOrderNum(int orderNum)//根据序号删除一条记录

findRecordByNum(int orderNum)//根据序号查找一条记录

}

本次课题比菜单计价系列-3增加的异常情况:

1、菜谱信息与订单信息混合,应忽略夹在订单信息中的菜谱信息。输出:"invalid dish"

2、桌号所带时间格式合法(格式见输入格式部分说明,其中年必须是4位数字,月、日、时、分、秒可以是1位或2位数),数据非法,比如:2023/15/16 ,输出桌号+" date error"

3、同一桌菜名、份额相同的点菜记录要合并成一条进行计算,否则可能会出现四舍五入的误差。

4、重复删除,重复的删除记录输出"deduplication :"+序号。

5、代点菜时,桌号不存在,输出"Table number :"+被点菜桌号+" does not exist";本次作业不考虑两桌记录时间不匹配的情况。

6、菜谱信息中出现重复的菜品名,以最后一条记录为准。

7、如果有重复的桌号信息,如果两条信息的时间不在同一时间段,(时段的认定:周一到周五的中午或晚上是同一时段,或者周末时间间隔1小时(不含一小时整,精确到秒)以内算统一时段),此时输出结果按不同的记录分别计价。

8、重复的桌号信息如果两条信息的时间在同一时间段,此时输出结果时合并点菜记录统一计价。前提:两个的桌号信息的时间都在有效时间段以内。计算每一桌总价要先合并符合本条件的饭桌的点菜记录,统一计价输出。

9、份额超出范围(1、2、3)输出:序号+" portion out of range "+份额,份额不能超过1位,否则为非法格式,参照第13条输出。

10、份数超出范围,每桌不超过15份,超出范围输出:序号+" num out of range "+份数。份数必须为数值,最高位不能为0,否则按非法格式参照第16条输出。

11、桌号超出范围[1,55]。输出:桌号 +" table num out of range",桌号必须为1位或多位数值,最高位不能为0,否则按非法格式参照第16条输出。

12、菜谱信息中菜价超出范围(区间(0,300)),输出:菜品名+" price out of range "+价格,菜价必须为数值,最高位不能为0,否则按非法格式参照第16条输出。

13、时间输入有效但超出范围[2022.1.1-2023.12.31],输出:"not a valid time period"

14、一条点菜记录中若格式正确,但数据出现问题,如:菜名不存在、份额超出范围、份数超出范围,按记录中从左到右的次序优先级由高到低,输出时只提示优先级最高的那个错误。

15、每桌的点菜记录的序号必须按从小到大的顺序排列(可以不连续,也可以不从1开始),未按序排列序号的输出:"record serial number sequence error"。当前记录忽略。(代点菜信息的序号除外)

16、所有记录其它非法格式输入,统一输出"wrong format"

17、如果记录以“table”开头,对应记录的格式或者数据不符合桌号的要求,那一桌下面定义的所有信息无论正确或错误均忽略,不做处理。如果记录不是以“table”开头,比如“tab le 55 2023/3/2 12/00/00”,该条记录认为是错误记录,后面所有的信息并入上一桌一起计算。

本次作业比菜单计价系列-3增加的功能:

菜单输入时增加特色菜,特色菜的输入格式:菜品名+英文空格+基础价格+"T"

例如:麻婆豆腐 9 T

菜价的计算方法:

周一至周五 7折, 周末全价。

注意:不同的四舍五入顺序可能会造成误差,请按以下步骤累计一桌菜的菜价:

计算每条记录的菜价:将每份菜的单价按份额进行四舍五入运算后,乘以份数计算多份的价格,然后乘以折扣,再进行四舍五入,得到本条记录的最终支付价格。

最后将所有记录的菜价累加得到整桌菜的价格。

代码如下:

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.temporal.ChronoField;
import java.util.Scanner;
class Table {
     private int num;
        private Order order;
        private LocalDate date;
        private LocalTime time;
        private double total;
        
        public Table(int num,String date,String time) {
            this.num = num;
            if(check(date)) {
                String[] DATE = date.split("/");
                this.date = LocalDate.of(Integer.parseInt(DATE[0]),Integer.parseInt(DATE[1]),Integer.parseInt(DATE[2]));
            }
            else
                this.date = LocalDate.of(1,1,1);
            String[] TIME = time.split("/");
            this.time = LocalTime.of(Integer.parseInt(TIME[0]),Integer.parseInt(TIME[1]),Integer.parseInt(TIME[2]));
            this.total = 0.0;
        }
        
        public LocalTime getTime() {
            return time;
        }
        public double getTotal() {
            return total;
        }
        
        public Order getOrder() {
            return order;
        }
        public LocalDate getDate() {
            return date;
        }
        
        public void setOrder(Order order) {
            this.order = order;
        }
        public int getNum() {
            return num;
        }


        public int getWeek() {
            return date.get(ChronoField.DAY_OF_WEEK);
        }
        public int OpeningHours(int week,LocalTime time,boolean T) {
            if(week <= 5) {
                if(T) {
                    return 7;
                }
                else {
                    if(time.isAfter(LocalTime.of(16,59,59))&&time.isBefore(LocalTime.of(20,30,1)))
                        return 8;
                    else if(time.isAfter(LocalTime.of(10,29,59))&&time.isBefore(LocalTime.of(14,30,1)))
                        return 6;
                    else
                        return -1;
                }
                
            }
            else {
                if(time.isAfter(LocalTime.of(9,30,0))&&time.isBefore(LocalTime.of(21,30,0)))
                    return 10;
                else
                    return -1;
            }
        }
        public int getDiscountedPrice() {
            int discountedPrice = 0;
            Record[] records = order.getRecords();
            double[] price = new double[records.length];
            for(int i = 0; i < records.length; i++) {
                price[i] = records[i].getPrice()*OpeningHours(getWeek(),getTime(),records[i].getDish().getT())*0.1;
                if(price[i] - (int)price[i] >= 0.5) 
                    price[i] = (int)price[i] + 1;
                else
                    price[i] = (int)price[i];
            }
            for(int i = 0; i < price.length; i++)
                discountedPrice += (int)price[i];
            return discountedPrice;
        }
        public void showTotal() {
            int judge = OpeningHours(getWeek(),getTime(),false);
            if(judge == -1)
                System.out.println("table"+" "+num+"out of opening hours");
            else {
                if(date.isAfter(LocalDate.of(2020,1,1))&&date.isBefore(LocalDate.of(2023,12,31)))
                    System.out.println("table"+" "+num+": "+order.GETALLPrice()+" "+getDiscountedPrice());
                else
                    System.out.println("not a valid time period");
            }
        }
        public static boolean check (String str) {
            SimpleDateFormat sd = new SimpleDateFormat("yyyy/MM/dd");
            try {
                sd.setLenient(false);
                sd.parse(str);
            }
            catch (Exception e) {
                return false;
            }
            return true;
        }

}

public class Main {
    public static boolean isNumer(String STR1) {
        if (STR1 == null) {
            return false;
        }
        int SZ = STR1.length();
        for (int i = 0; i < SZ; i++) {
            if (Character.isDigit(STR1.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }
    
    public static boolean isTable(String[] table) {
        if (table.length < 5) {
            if (table[0].equals("table")) {
                if (isNumer(table[1])) {
                    if (table[3].length() == 8)
                        return true;
                    else
                        return false;
                } else
                    return false;
            } else
                return false;
        } else
            return false;
    }
    
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        Scanner input = new Scanner(System.in);
        String[] newrecord = input.nextLine().split(" ");
        Dish[] dish = new Dish[10];
        
        for (int i = 0; !newrecord[0].equals("table"); i++) {
            if (isNumer(newrecord[1])) {
                if (newrecord.length > 2) {
                    if (newrecord.length < 4)
                        dish[i] = new Dish(newrecord[0], Integer.parseInt(newrecord[1]), true);
                    else {
                        System.out.println("wrong format");
                        System.exit(0);
                    }
                } else
                    dish[i] = new Dish(newrecord[0], Integer.parseInt(newrecord[1]), false);
                if (!(dish[i].getUtilPrice() > 0 && dish[i].getUtilPrice() < 300)) {
                    System.out.println(dish[i].getName() + " price out of range " + dish[i].getUtilPrice());
                    dish[i] = new Dish();
                }

            } else {
                dish[i] = new Dish();
                System.out.println("wrong format");
            }

            newrecord = input.nextLine().split(" ");
        }
        Menu menu = new Menu(dish);
        if (isTable(newrecord)) {
            Table[] table = new Table[10];
            int TABLENUM = 0;
            for (TABLENUM = 0; newrecord[0].equals("table"); TABLENUM++) {
                table[TABLENUM] = new Table(Integer.parseInt(newrecord[1]), newrecord[2], newrecord[3]);
                System.out.println("table " + table[TABLENUM].getNum() + ": ");
                Record[] RECORD = new Record[10];
                newrecord = input.nextLine().split(" ");
                for (int j = 0; !(newrecord[1].equals("delete") || isTable(newrecord)); j++) {
                    if (newrecord[0].length() > 2) {
                        if (newrecord.length > 3)
                            RECORD[j] = new Record(1, new Dish(), 0, 0);
                        else
                            System.out.println("invalid dish");
                    } else {
                        if (menu.SEARCHDish(newrecord[1]).getName().equals("notexist")) {
                            System.out.println(newrecord[1] + " does not exist");
                            RECORD[j] = new Record();
                        } else {
                            RECORD[j] = new Record(Integer.parseInt(newrecord[0]), menu.SEARCHDish(newrecord[1]),Integer.parseInt(newrecord[2]), Integer.parseInt(newrecord[3]));
                        }
                    }
                    newrecord = input.nextLine().split(" ");
                    if (newrecord[0].equals("end"))
                        break;
                }
                Order order = new Order(RECORD, menu);
                table[TABLENUM].setOrder(order);
                order.showOrder();
                if (!newrecord[0].equals("end")) {
                    while (newrecord[1].equals("delete")) {
                        order.delARecordOrderNum(Integer.parseInt(newrecord[0]));
                        newrecord = input.nextLine().split(" ");
                        if (newrecord[0].equals("end"))
                            break;
                    }
                }
            }
            for (int i = 0; i < TABLENUM; i++) {
                table[i].showTotal();
            }
        } else
            System.out.println("wrong format");
    }
}

class Record {
    private int ORDERNum;
    private Dish NEWDISH;
    private int portion;
    private int num;
    
    public Record() {
        ORDERNum = 0;
        NEWDISH = new Dish();
        portion = 0;
        num = 0;
    }
    public Record(int orderNum,Dish d,int portion,int num) {
        this.ORDERNum = orderNum;
        this.NEWDISH = d;
        this.portion = portion;
        this.num = num;
    }
    
    public Dish getDish() {
        return NEWDISH;
    }
    public int getOrderNum(){
        return ORDERNum;
    }
    public int getNum() {
        return num;
    }
    public int getPortion() {
        return portion;
    }
    
    
    public void Priceshow() {
        if(portion < 10) {
            if(!NEWDISH.getT()&&(portion < 0||portion > 3))
                System.out.println(ORDERNum+" num out of range "+portion);
            else if(NEWDISH.getT()&&(portion < 0||portion > 3))
                System.out.println(ORDERNum+" portion out of range "+portion);
            else {
                if(num > 15)
                    System.out.println(ORDERNum+" num out of range "+num);
                else
                    System.out.println(ORDERNum+" "+NEWDISH.getName()+" "+getPrice());
            }
        }
        else
            System.out.println("wrong format");
        
    }
    
    public int getPrice() {
        if(!NEWDISH.getT()&&(portion < 0||portion > 3))
            return 0;
        else if(NEWDISH.getT()&&(portion < 0||portion > 3))
            return 0;
        else {
            if(num > 15)
                return 0;
            else
                return NEWDISH.getPrice(portion)*num;
        }
    }

}

class Menu {
    private Dish[] dishs;

    public Menu(Dish[] dishs) {
        this.dishs = new Dish[dishs.length];
        for (int i = 0; i < dishs.length; i++) {
            if (dishs[i] == null)
                this.dishs[i] = new Dish();
            else
                this.dishs[i] = dishs[i];
        }
    }

    public Dish ADDDish(String dishName, int unit_price, boolean FLAG) {
        return new Dish(dishName, unit_price, FLAG);
    }
    
    public Dish SEARCHDish(String DISHNAME) {
        int num = 0;
        Dish flag = new Dish("notexist", 0, false);
        for (int i = 0; i < dishs.length; i++) {
            if (dishs[i].getName().equals(DISHNAME)) {
                num = i;
                flag = new Dish("exist", 0, false);
            }
        }
        if (flag.getName().equals("exist"))
            return dishs[num];
        else
            return flag;
    }

    

}

class Dish {
    private String NAME;
    private int unit_price;
    private boolean FLAG;

    public Dish() {
        NAME = "";
        unit_price = 0;
        FLAG = false;
    }

    public Dish(String name, int util_price, boolean FLAG) {
        this.NAME = name;
        this.unit_price = util_price;
        this.FLAG = FLAG;
    }

    public int getUtilPrice() {
        return unit_price;
    }
    public String getName() {
        return NAME;
    }
    public boolean getT() {
        return FLAG;
    }

    public int getPrice(int portion) {
        double price = 0;
        if (portion == 1) {
            price = unit_price;
        } else if (portion == 2) {
            price = Math.round((float) (unit_price * 1.5));
        } else if (portion == 3) {
            price = (unit_price * 2);
        }
        return (int) price;
    }
}

class Order {
    private Record[] RECORDS;
    private Menu MENU;
    
    public Order(Record[] records,Menu menu) {
        this.RECORDS = new Record[records.length];
        for(int i = 0; i < records.length; i++)
            if(records[i] == null)
                this.RECORDS[i] = new Record();
            else
                this.RECORDS[i] = records[i];
        this.MENU = menu;
    }
    public Record[] getRecords() {
        return RECORDS;
    }
    
    public Record addARecord(int orderNum,String dishName,int portion,int num) {
        Dish d = MENU.SEARCHDish(dishName);
        Record error = new Record();
        if(d.getName().equals(""))
            return error;
        else
            return new Record(orderNum,d,portion,num);
    }
    
    public int GETALLPrice() {
        int TotalPrice = 0;
        for(int i = 0; i < RECORDS.length; i++) {
            TotalPrice += RECORDS[i].getPrice();
        }
        return TotalPrice;
    }
    
    public int findRecordNum(int orderNum) {
        for(int i = 0; i < RECORDS.length; i++) {
            if(RECORDS[i].getOrderNum() == orderNum) {
                return i;
            }
        }
        return -1;
    }
    
    public void delARecordOrderNum(int orderNum) {
        int flag = findRecordNum(orderNum);
        if(flag != -1) {
            if(RECORDS[flag].getNum() == 0)
                System.out.println("deduplication "+ orderNum);
            RECORDS[flag] = new Record(orderNum,new Dish(),0,0);
        }
        else
            System.out.println("delete error");
    }
    
    public void showOrder() {
        for(int i = 0; i < RECORDS.length; i++) {
            if(RECORDS[i].getNum() != 0) {
                int m = i;
                for(m = i; m > 0; m--) {
                    if(RECORDS[m-1].getOrderNum() >= RECORDS[i].getOrderNum()) {
                        System.out.println("record serial number sequence error");
                        RECORDS[i] = new Record();
                        break;
                    }
                }
                if(m == 0)
                    RECORDS[i].Priceshow();
            }
            else {
                if(RECORDS[i].getOrderNum() == 1) 
                    System.out.println("wrong format");
            }
        }
    }

}

类图如下:

 

代码分析:

这次菜单4的题目是前面菜单计价程序的迭代,增加了很多关于错误输入的判断,在程序设计过程中如果能够合理运用正则表达式的处理方法,就可以使代码变得简洁许多

 

菜单计价程序-5

本题在菜单计价程序-3的基础上增加了部分内容,增加的内容用加粗字体标识。

注意不是菜单计价程序-4,本题和菜单计价程序-4同属菜单计价程序-3的两个不同迭代分支。

设计点菜计价程序,根据输入的信息,计算并输出总价格。

输入内容按先后顺序包括两部分:菜单、订单,最后以"end"结束。

菜单由一条或多条菜品记录组成,每条记录一行 

每条菜品记录包含:菜名、基础价格  三个信息。 

订单分:桌号标识、点菜记录和删除信息、代点菜信息。每一类信息都可包含一条或多条记录,每条记录一行或多行。

桌号标识独占一行,包含两个信息:桌号、时间。

桌号以下的所有记录都是本桌的记录,直至下一个桌号标识。 

点菜记录包含:序号、菜名、份额、份数。份额可选项包括:1、2、3,分别代表小、中、大份。

不同份额菜价的计算方法:小份菜的价格=菜品的基础价格。中份菜的价格=菜品的基础价格1.5。小份菜的价格=菜品的基础价格2。如果计算出现小数,按四舍五入的规则进行处理。

删除记录格式:序号  delete

标识删除对应序号的那条点菜记录。

如果序号不对,输出"delete error"

代点菜信息包含:桌号 序号 菜品名称 口味度 份额 份数

代点菜是当前桌为另外一桌点菜,信息中的桌号是另一桌的桌号,带点菜的价格计算在当前这一桌。

程序最后按输入的先后顺序依次输出每一桌的总价(注意:由于有代点菜的功能,总价不一定等于当前桌上的菜的价格之和)。

每桌的总价等于那一桌所有菜的价格之和乘以折扣。如存在小数,按四舍五入规则计算,保留整数。

折扣的计算方法(注:以下时间段均按闭区间计算):

周一至周五营业时间与折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余时间不营业。

周末全价,营业时间:9:30-21:30

如果下单时间不在营业范围内,输出"table " + t.tableNum + " out of opening hours"

参考以下类的模板进行设计:菜品类:对应菜谱上一道菜的信息。

Dish {    

   String name;//菜品名称    

   int unit_price;    //单价    

   int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)    }

菜谱类:对应菜谱,包含饭店提供的所有菜的信息。

Menu {

   Dish[] dishs ;//菜品数组,保存所有菜品信息

   Dish searthDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。

   Dish addDish(String dishName,int unit_price)//添加一道菜品信息

}

点菜记录类:保存订单上的一道菜品记录

Record {

   int orderNum;//序号\\

   Dish d;//菜品\\

   int portion;//份额(1/2/3代表小/中/大份)\\

   int getPrice()//计价,计算本条记录的价格\\

}

 

订单类:保存用户点的所有菜的信息。

Order {

   Record[] records;//保存订单上每一道的记录

   int getTotalPrice()//计算订单的总价

   Record addARecord(int orderNum,String dishName,int portion,int num)//添加一条菜品信息到订单

   delARecordByOrderNum(int orderNum)//根据序号删除一条记录

   findRecordByNum(int orderNum)//根据序号查找一条记录

}

### 输入格式:

桌号标识格式:table + 序号 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)

菜品记录格式:

菜名+英文空格+基础价格

如果有多条相同的菜名的记录,菜品的基础价格以最后一条记录为准。

点菜记录格式:序号+英文空格+菜名+英文空格+份额+英文空格+份数注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。

删除记录格式:序号 +英文空格+delete

代点菜信息包含:桌号+英文空格+序号+英文空格+菜品名称+英文空格+份额+英文空格+分数

最后一条记录以“end”结束。

### 输出格式:

按输入顺序输出每一桌的订单记录处理信息,包括:

1、桌号,格式:table+英文空格+桌号+”:”

2、按顺序输出当前这一桌每条订单记录的处理信息,

每条点菜记录输出:序号+英文空格+菜名+英文空格+价格。其中的价格等于对应记录的菜品\*份数,序号是之前输入的订单记录的序号。如果订单中包含不能识别的菜名,则输出“\*\* does not exist”,\*\*是不能识别的菜名

如果删除记录的序号不存在,则输出“delete error”

最后按输入顺序一次输出每一桌所有菜品的总价(整数数值)格式:table+英文空格+桌号+“:”+英文空格+当前桌的总价

以上为菜单计价系列-3的题目要求,加粗的部分是有调整的内容。本次课题相比菜单计价系列-3新增要求如下:

1、菜单输入时增加特色菜,特色菜的输入格式:菜品名+英文空格+口味类型+英文空格+基础价格+"T"

例如:麻婆豆腐 川菜 9 T

菜价的计算方法:

周一至周五 7折, 周末全价。

特色菜的口味类型:川菜、晋菜、浙菜

川菜增加辣度值:辣度0-5级;对应辣度水平为:不辣、微辣、稍辣、辣、很辣、爆辣;

晋菜增加酸度值,酸度0-4级;对应酸度水平为:不酸、微酸、稍酸、酸、很酸;

浙菜增加甜度值,甜度0-3级;对应酸度水平为:不甜、微甜、稍甜、甜;    

例如:麻婆豆腐 川菜 9 T

输入订单记录时如果是特色菜,添加口味度(辣/酸/甜度)值,格式为:序号+英文空格+菜名+英文空格+口味度值+英文空格+份额+英文空格+份数

例如:1 麻婆豆腐 4 1 9

单条信息在处理时,如果口味度超过正常范围,输出"spicy/acidity/sweetness num out of range : "+口味度值,spicy/acidity/sweetness(辣度/酸度/甜度)根据菜品类型择一输出,例如:

acidity num out of range : 5

输出一桌的信息时,按辣、酸、甜度的顺序依次输出本桌菜各种口味的口味度水平,如果没有某个类型的菜,对应的口味(辣/酸/甜)度不输出,只输出已点的菜的口味度。口味度水平由口味度平均值确定,口味度平均值只综合对应口味菜系的菜计算,不做所有菜的平均。比如,某桌菜点了3份川菜,辣度分别是1、3、5;还有4份晋菜,酸度分别是,1、1、2、2,辣度平均值为3、酸度平均值四舍五入为2,甜度没有,不输出。

一桌信息的输出格式:table+英文空格+桌号+:+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价+英文空格+"川菜"+数量+辣度+英文空格+"晋菜"+数量+酸度+英文空格+"浙菜"+数量+甜度。

如果整桌菜没有特色菜,则只输出table的基本信息,格式如下,注意最后加一个英文空格:

table+英文空格+桌号+:+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价+英文空格

例如:table 1: 60 36 川菜 2 爆辣 浙菜 1 微甜

计算口味度时要累计本桌各类菜系所有记录的口味度总和(每条记录的口味度乘以菜的份数),再除以对应菜系菜的总份数,最后四舍五入。

注:本题要考虑代点菜的情况,当前桌点的菜要加上被其他桌代点的菜综合计算口味度平均值。

2、考虑客户订多桌菜的情况,输入时桌号时,增加用户的信息:

格式:table+英文空格+桌号+英文空格+":"+英文空格+客户姓名+英文空格+手机号+日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)

例如:table 1 : tom 13670008181 2023/5/1 21/30/00

约束条件:客户姓名不超过10个字符,手机号11位,前三位必须是180、181、189、133、135、136其中之一。

输出结果时,先按要求输出每一桌的信息,最后按字母顺序依次输出每位客户需要支付的金额。不考虑各桌时间段的问题,同一个客户的所有table金额都要累加。

输出用户支付金额格式:

用户姓名+英文空格+手机号+英文空格+支付金额

 

注意:不同的四舍五入顺序可能会造成误差,请按以下步骤累计一桌菜的菜价:

计算每条记录的菜价:将每份菜的单价按份额进行四舍五入运算后,乘以份数计算多份的价格,然后乘以折扣,再进行四舍五入,得到本条记录的最终支付价格。

将所有记录的菜价累加得到整桌菜的价格。

输入格式:

桌号标识格式:table + 序号 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)

菜品记录格式:

菜名+口味类型+英文空格+基础价格

如果有多条相同的菜名的记录,菜品的基础价格以最后一条记录为准。

点菜记录格式:序号+英文空格+菜名+英文空格+辣/酸/甜度值+英文空格+份额+英文空格+份数 注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。辣/酸/甜度取值范围见题目中说明。

删除记录格式:序号 +英文空格+delete

代点菜信息包含:桌号+英文空格+序号+英文空格+菜品名称**+英文空格+辣/酸/甜度值+**英文空格+份额+英文空格+分数

最后一条记录以“end”结束。

输出格式:

按输入顺序输出每一桌的订单记录处理信息,包括:

1、桌号,格式:table+英文空格+桌号+“:”+英文空格

2、按顺序输出当前这一桌每条订单记录的处理信息,

每条点菜记录输出:序号+英文空格+菜名+英文空格+价格。其中的价格等于对应记录的菜品\*份数,序号是之前输入的订单记录的序号。如果订单中包含不能识别的菜名,则输出“\*\* does not exist”,\*\*是不能识别的菜名

如果删除记录的序号不存在,则输出“delete error”

之后按输入顺序一次输出每一桌所有菜品的价格(整数数值),

格式:table+英文空格+桌号+“:”+英文空格+当前桌的计算折扣后总价+英文空格+辣度平均值+英文空格+酸度平均值+英文空格+甜度平均值+英文空格

最后按拼音顺序输出每位客户(不考虑客户同名或拼音相同的情况)的支付金额,格式: 用户姓名+英文空格+手机号+英文空格+支付总金额,按输入顺序排列。

代码如下

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.LocalDate;
import java.util.ArrayList;

class lastPrint{
    ArrayList<Table> tables = new ArrayList<>();
    PersonPay[] personPays;
    int p;
    
    public void payPrint(){
        for(int i=0;i<p;i++){
            for(Table table:tables){
                if(table.tableName.equals(personPays[i].PERSONname)){
                    personPays[i].payPrice+=table.tablePrice();
                }
            }
        }
        for(int i=0;i<p-1;i++){
            for(int l=i+1;l<p;l++){
                    if(personPays[i].PERSONname.toLowerCase().compareTo(personPays[l].PERSONname.toLowerCase())>0) {
                        PersonPay informmation;
                        informmation=personPays[l];
                        personPays[l]=personPays[i];
                        personPays[i]=informmation;
                    }
            }
        }
        for(int k=0;k<p;k++){
            System.out.println(personPays[k].PERSONname+" "+personPays[k].TelephoneNum+" "+personPays[k].payPrice);
        }
    }
    public void print(){
        for (Table table : tables) {
            table.order.getTotalPrice();
            table.getTaste();
            if(table.SpicyNum==0&&table.aciNum==0&&table.SWEETNum==0)
                System.out.println("table " + table.tableNum + ": " + (table.order.uaualPrice + table.order.SpecialPrice) + " " + table.tablePrice()+" ");
            if(table.SpicyNum!=0&&table.aciNum==0&&table.SWEETNum==0)
                System.out.println("table " + table.tableNum + ": " + (table.order.uaualPrice + table.order.SpecialPrice) + " " + table.tablePrice() + " 川菜 " +(int)table.SpicyNum+" "+table.spicyLevel());
            if(table.SpicyNum==0&&table.aciNum!=0&&table.SWEETNum==0)
                System.out.println("table " + table.tableNum + ": " + (table.order.uaualPrice + table.order.SpecialPrice) + " " + table.tablePrice() + " 晋菜 " +(int)table.aciNum+" "+table.acidityLevel());
            if(table.SpicyNum==0&&table.aciNum==0&&table.SWEETNum!=0)
                System.out.println("table " + table.tableNum + ": " + (table.order.uaualPrice + table.order.SpecialPrice) + " " + table.tablePrice() + " 浙菜 " +(int)table.SWEETNum+" "+table.sweetnessLevel());
            if(table.SpicyNum!=0&&table.aciNum!=0&&table.SWEETNum==0)
                System.out.println("table " + table.tableNum + ": " + (table.order.uaualPrice + table.order.SpecialPrice) + " " + table.tablePrice() + " 川菜 " +(int)table.SpicyNum+" "+table.spicyLevel()+" 晋菜 "+(int)table.aciNum+" "+table.acidityLevel());
            if(table.SpicyNum!=0&&table.aciNum==0&&table.SWEETNum!=0)
                System.out.println("table " + table.tableNum + ": " + (table.order.uaualPrice + table.order.SpecialPrice) + " " + table.tablePrice() + " 川菜 " +(int)table.SpicyNum+" "+table.spicyLevel()+" 浙菜 "+(int)table.SWEETNum+" "+table.sweetnessLevel());
            if(table.SpicyNum==0&&table.aciNum!=0&&table.SWEETNum!=0)
                System.out.println("table " + table.tableNum + ": " + (table.order.uaualPrice + table.order.SpecialPrice) + " " + table.tablePrice() + " 晋菜 " +(int)table.aciNum+" "+table.acidityLevel()+" 浙菜 "+(int)table.SWEETNum+" "+table.sweetnessLevel());
            if(table.SpicyNum!=0&&table.aciNum!=0&&table.SWEETNum!=0)
                System.out.println("table " + table.tableNum + ": " + (table.order.uaualPrice + table.order.SpecialPrice) + " " + table.tablePrice() + " 川菜 " +(int)table.SpicyNum+" "+table.spicyLevel()+" 晋菜 "+(int)table.aciNum+" "+table.acidityLevel()+" 浙菜 "+(int)table.SWEETNum+" "+table.sweetnessLevel());
        }
    }
}


public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        Table table = new Table();
        Record[] records = new Record[20];
        Dish[] dishes = new Dish[20];
        SpecialDish_taste[] specialDishes = new SpecialDish_taste[20];
        Order order = new Order();
        Menu menu = new Menu();
        ArrayList<Table> tables = new ArrayList<>();

        PersonPay[] personPays = new PersonPay[10];

        lastPrint PrintMESS = new lastPrint();
        int X = 0 , z1 = 0 , per = 0 , Y = 0 , z = 0 , tableNum , portion , num , unit_price ,dishNum, delNum , tNum;
        String meName , DTime , HTime , name , tabName , telNum , TASTE;
        boolean havingTable = false , Othertable = false;
        for(;;){
            String input = in.readLine();
            if(input.equals("end")){
                if(X!=0){
                    table.order = order;
                    tables.add(X-1 , table);
                }
                
                PrintMESS.personPays = personPays;
                PrintMESS.tables = tables;
                PrintMESS.print();
                PrintMESS.payPrint();
                break;
            }
            String[] getInput = input.split(" ");
            if(input.matches("^(table)( )([1-9][0-9]*)( )(:)( )(\\S+)( )((136|133|135|180|181|189)[0-9]{8})( )([0-9]{4})(/)([0-9]|[0-9]{2})(/)([0-9]|[0-9]{2})( )([0-9]|[0-9]{2})(/)([0-9]|[0-9]{2})(/)([0-9]|[0-9]{2})$")) {
                if(X != 0) {
                    table.order = order;
                    tables.add(X-1 , table);
                    table = new Table();
                    Y = 0;
                }
                havingTable = true;
                tableNum = Integer.parseInt(getInput[1]);
                DTime = getInput[5];
                HTime = getInput[6];
                tabName = getInput[3];
                telNum = getInput[4];

                table.tableNum = tableNum;
                table.NORMALTime = DTime;
                table.tableName = tabName;
                table.telephoneNum = telNum;
                table.remainTime = HTime;

                if(!table.timeJudgement1()){
                    havingTable = false;
                    System.out.println("table " +table.tableNum + " out of opening hours");
                    continue;
                }
                order = new Order();
                records = new Record[10];
                if(X==0){
                    personPays[per] = new PersonPay();
                    personPays[per].TelephoneNum = telNum;
                    personPays[per].PERSONname = tabName;

                    per++;
                    PrintMESS.p = per;
                }
                else{
                    for(int i=0;i<per;i++){
                        if(personPays[i].PERSONname.equals(tabName)&&personPays[i].TelephoneNum.equals(telNum))
                            break;
                        if(i==per-1){
                            personPays[per] = new PersonPay();
                            personPays[per].TelephoneNum = telNum;
                            personPays[per].PERSONname = tabName;

                            per++;
                            PrintMESS.p = per;
                        }
                    }
                }
                System.out.println("table "+tableNum+": ");
                X++;
            }

            else if(input.matches("^(\\S+)( )(\\S+)( )([1-9][0-9]*)( )(T)$")){
                int i = 0;
                meName = getInput[0];
                TASTE = getInput[1];
                unit_price = Integer.parseInt(getInput[2]);
                if(z1 == 0) {
                    specialDishes[0] = menu.addSpecialDish(meName , unit_price , TASTE);
                    z1++;
                }
                else{
                    for(;i < z1 ; i++) {
                        if(meName.equalsIgnoreCase(specialDishes[i].name)) {
                            specialDishes[i].unit_price = unit_price;
                            break;
                        }
                    }
                }
                if(i == z1){
                    specialDishes[z1] = menu.addSpecialDish(meName , unit_price , TASTE);
                    z1++;
                }
                menu.specialDishes = specialDishes;
                menu.dishNum1 = z1;
            }
            else if(input.matches("^([1-9][0-9]*)( )(\\S+)( )([1-9][0-9]*)( )([1-9][0-9]*)$")){
                if(!havingTable)
                    continue;
                dishNum = Integer.parseInt(getInput[0]);
                name = getInput[1];
                portion = Integer.parseInt(getInput[2]);
                num = Integer.parseInt(getInput[3]);
                if(menu.searthDish(name) != null) {
                    records[Y] = new Record();
                    records[Y] = order.addARecord(dishNum , name , portion , num , menu);
                    records[Y].ISSPECIALDISH = false;
                    System.out.println(records[Y].hasorder+" "+records[Y].d.name+" "+records[Y].getPrice());
                    Y++;
                    order.records = records;
                    order.dishNum = Y;
                }
                else{
                    System.out.println(name+" does not exist");
                }
            }
            
            else if(input.matches("([1-9][0-9]*)( )(delete)")) {
                if(!havingTable)
                    continue;
                delNum = Integer.parseInt(getInput[0]);
                if(order.FindRcord(delNum) == 1){
                    order.delARecordByOrderNum(delNum);
                }
                else
                    System.out.println("delete error;");
            }
            
            else if(input.matches("^([1-9][0-9]*)( )(\\S+)( )([0-9]+)( )([1-9][0-9]*)( )([1-9][0-9]*)$")){
                if(!havingTable)
                    continue;
                dishNum = Integer.parseInt(getInput[0]);
                name = getInput[1];
                tNum = Integer.parseInt(getInput[2]);
                num = Integer.parseInt(getInput[4]);
                portion = Integer.parseInt(getInput[3]);

                if(menu.searthSpecialDish(name) != null) {
                    if(((menu.searthSpecialDish(name).taste.equals("川菜") && tNum >= 0 && tNum <= 5) || (menu.searthSpecialDish(name).taste.equals("晋菜") && tNum>=0 && tNum <= 4) || (menu.searthSpecialDish(name).taste.equals("浙菜") && tNum>=0 && tNum <= 3))){
                        records[Y] = new Record();
                        records[Y] = order.addASpecialRecord(dishNum , name , portion , num , menu , tNum , Othertable);
                        records[Y].ISSPECIALDISH = true;
                        System.out.println(records[Y].hasorder+" "+records[Y].d.name+" "+records[Y].getPrice());
                        Y++;
                        order.records = records;
                        order.dishNum = Y;
                    }
                }
                else{
                    System.out.println(name+" does not exist");
                    continue;
                }
                if(menu.searthSpecialDish(name).taste.equals("浙菜")){
                    if(tNum<0||tNum>3)
                        System.out.println("sweetness num out of range :"+tNum);
                }
                if(menu.searthSpecialDish(name).taste.equals("川菜")){
                    if(tNum<0||tNum>5)
                        System.out.println("spicy num out of range :"+tNum);
                }
                if(menu.searthSpecialDish(name).taste.equals("晋菜")){
                    if(tNum<0||tNum>4)
                        System.out.println("acidity num out of range :"+tNum);
                }


            }

            
            else if(input.matches("^([1-9][0-9]*)( )([1-9][0-9]*)( )(\\S+)( )([0-9]*)( )([1-9][0-9]*)( )([1-9][0-9]*)$")) {
                if(!havingTable)
                    continue;
                int t = Integer.parseInt(getInput[0]);
                dishNum = Integer.parseInt(getInput[1]);
                name = getInput[2];
                tNum = Integer.parseInt(getInput[3]);
                portion = Integer.parseInt(getInput[4]);
                num = Integer.parseInt(getInput[5]);
                for(int i = 0;i<X-1;i++){
                    if(tables.get(i).tableNum == t){
                        if(menu.searthSpecialDish(name) != null) {
                            if(((menu.searthSpecialDish(name).taste.equals("川菜") && tNum >= 1 && tNum <= 5) || (menu.searthSpecialDish(name).taste.equals("晋菜") && tNum <= 4) || (menu.searthSpecialDish(name).taste.equals("浙菜") && tNum <= 3))){
                                records[Y] = new Record();
                                records[Y] = order.addASpecialRecord(dishNum , name , portion , num , menu , tNum , !Othertable);
                                records[Y].ISSPECIALDISH = true;
                                System.out.println(dishNum+" table "+table.tableNum+" pay for table "+t+" "+records[Y].getPrice());
                                tables.get(i).giveTaste(name,num,tNum,menu);
                                Y++;
                                order.records = records;
                                order.dishNum = Y;
                            }
                        }

                        break;
                    }
                }
            }
            else if(input.matches("^([1-9][0-9]*)( )([1-9][0-9]*)( )(\\S+)( )([1-9][0-9]*)( )([1-9][0-9]*)$")) {
                if(!havingTable)
                    continue;
                int t = Integer.parseInt(getInput[0]);
                dishNum = Integer.parseInt(getInput[1]);
                name = getInput[2];
                portion = Integer.parseInt(getInput[3]);
                num = Integer.parseInt(getInput[4]);
                for(int i = 0;i<X-1;i++){
                    if(tables.get(i).tableNum == t){
                        if(menu.searthDish(name) != null) {
                            records[Y] = new Record();
                            records[Y] = order.addARecord(dishNum , name , portion , num , menu);
                            records[Y].ISSPECIALDISH = false;
                            System.out.println(dishNum+" table "+table.tableNum+" pay for table "+t+" "+records[Y].getPrice());
                            Y++;
                            order.records = records;
                            order.dishNum = Y;
                        }
                        break;
                    }
                }
            }
            else if(input.matches("^(\\S+)( )([1-9][0-9]*)$")){
                int i = 0;
                meName = getInput[0];
                unit_price = Integer.parseInt(getInput[1]);
                if(z == 0) {
                    dishes[0] = menu.addDish(meName , unit_price);
                    z++;
                }
                else{
                    for(;i < z ; i++) {
                        if(meName.equalsIgnoreCase(dishes[i].name)) {
                            dishes[i].unit_price = unit_price;
                            break;
                        }
                    }
                }
                if(i == z){
                    dishes[z] = menu.addDish(meName , unit_price);
                    z++;
                }
                menu.dishes = dishes;
                menu.dishNum2 = z;
            }

            else {
                System.out.println("wrong format");
            }
        }
    }
}

class SpecialDish_taste extends Dish{
    String taste;
}

class Dish{
    
    int unit_price;
    String name;

    public int GETPRICE(int portion){
        double get_Price=0;
        if(portion==1)
            get_Price=unit_price;
        else if(portion==2)
            get_Price=unit_price*1.5;
        else if(portion==3)
            get_Price=unit_price*2;
        return (int) Math.round(get_Price);
    }
}


class Record{
    int aci;
    int SWEET;
    boolean ISSPECIALDISH;
    int hasorder;
    Dish d;
    int portion;
    int Spicy;
    int num;


    public int getPrice() {
        return d.GETPRICE(portion)*num;
    }
}

class PersonPay{
    String PERSONname;
    String TelephoneNum;
    int payPrice;
}


class Table{
    double SpicyNum;
    double aciNum;
    double SWEETNum;
    int tableNum;
    String tableName;
    String telephoneNum;
    String NORMALTime;
    String remainTime;
    Order order;
    double cDiscount;
    double sDiscount;
    int aveSpicy;
    int aveAcidity;
    int aveSweetness;

    public boolean timeJudgement1(){
        String[] x = remainTime.split("/");
        String[] y = NORMALTime.split("/");
        double hour = Double.parseDouble(x[0]);
        double minute = Double.parseDouble(x[1]);
        double second = Double.parseDouble(x[2]);
        int year = Integer.parseInt(y[0]);
        int month = Integer.parseInt(y[1]);
        int day = Integer.parseInt(y[2]);
        LocalDate date = LocalDate.of(year,month,day);
        int weekOfDay = date.getDayOfWeek().getValue();

        if (weekOfDay >= 1 && weekOfDay <= 5) {
            if (hour > 17 && hour < 20 || hour == 17 && minute >= 0 || hour == 20 && minute < 30 || hour == 20 && minute == 30 && second == 0) {
                cDiscount = 0.8;
                sDiscount = 0.7;
                return true;
            }
            else if (hour > 10 && hour < 14 || hour == 10 && minute >= 30 || hour == 14 && minute < 30 || hour == 14 && minute == 30 && second == 0) {
                cDiscount = 0.6;
                sDiscount = 0.7;
                return true;
            }
            else {
                return false;
            }
        }
        
        if (weekOfDay == 6 || weekOfDay == 7) {
            if (hour >= 10 && hour < 21 || hour == 9 && minute >= 30 || hour == 21 && minute < 30 ||hour == 21 && minute == 30 && second == 0) {
                sDiscount = cDiscount = 1;
                return true;
            }
            else {
                return false;
            }
        }
        return false;
    }
    public int tablePrice(){
        return this.order.getTotalPrice(cDiscount,sDiscount);
    }
    
    public void getTaste(){
        for(int i = 0;i<this.order.dishNum;i++){
            if(order.records[i].ISSPECIALDISH){
                if(order.records[i].SWEET!=-1){
                    aveSweetness+=order.records[i].SWEET*order.records[i].num;
                    SWEETNum=order.records[i].num+SWEETNum;
                }
                if(order.records[i].Spicy!=-1){
                    aveSpicy+=order.records[i].Spicy*order.records[i].num;
                    SpicyNum=order.records[i].num+SpicyNum;
                }

                if(order.records[i].aci!=-1){
                    aveAcidity+=order.records[i].aci*order.records[i].num;
                    aciNum=order.records[i].num+aciNum;
                }
            }
        }
        if(SpicyNum!=0){
            aveSpicy=(int)Math.round(aveSpicy/SpicyNum);
        }
        
        if(SWEETNum!=0){
            aveSweetness=(int)Math.round(aveSweetness/SWEETNum);
        }
        
        if(aciNum!=0){
            aveAcidity=(int)Math.round(aveAcidity/aciNum);
        }

    }

    public String acidityLevel(){
        if(aveAcidity==0)
            return "不酸";
        if(aveAcidity==1)
            return "微酸";
        if(aveAcidity==2)
            return "稍酸";
        if(aveAcidity==3)
            return "酸";
        if(aveAcidity==4)
            return "很酸";
        return null;
    }
    
    public String spicyLevel(){
        if(aveSpicy==0)
            return "不辣";
        if(aveSpicy==1)
            return "微辣";
        if(aveSpicy==2)
            return "稍辣";
        if(aveSpicy==3)
            return "辣";
        if(aveSpicy==4)
            return "很辣";
        if(aveSpicy==5)
            return "爆辣";
        return null;
    }
    
    public String sweetnessLevel(){
        if(aveSweetness==0)
            return "不甜";
        if(aveSweetness==1)
            return "微甜";
        if(aveSweetness==2)
            return "稍甜";
        if(aveSweetness==3)
            return "甜";
        return null;
    }
    public void giveTaste(String name , int num , int tLevel , Menu menu){
        if(menu.searthSpecialDish(name).taste.equals("浙菜")){
            aveSweetness+=tLevel*num;
            SWEETNum+=num;
        }
        if(menu.searthSpecialDish(name).taste.equals("川菜")){
            aveSpicy+=tLevel*num;
            SpicyNum+=num;
        }
        if(menu.searthSpecialDish(name).taste.equals("晋菜")){
            aveAcidity+=tLevel*num;
            aciNum+=num;
        }

    }
}
class Order {
    int dishNum;
    int uaualPrice;
    int SpecialPrice;
    Record[] records;

   
    
    public void addPortion() {
        for(int i = 0;i < dishNum-1;i++){
            for(int l = i+1;l<dishNum;l++){
                if(records[i].d.name.equals(records[l].d.name)&&records[i].portion == records[l].portion){
                    records[i].num+=records[l].num;
                    records[l].num = 0;
                }
            }
        }
    }
    
    
    
    public int getTotalPrice(double commonDiscount,double specialDiscount){
        int allmoney = 0;
        for(int i=0;i<dishNum;i++) {
            if(!records[i].ISSPECIALDISH)
                allmoney+=Math.round(records[i].getPrice()*commonDiscount);
            else
                allmoney+=Math.round(records[i].getPrice()*specialDiscount);
        }
        return allmoney;
    }
    
    public void getTotalPrice(){
        for(int k=0;k<dishNum;k++) {
            if(!records[k].ISSPECIALDISH)
                uaualPrice+=records[k].getPrice();
            else
                SpecialPrice+=records[k].getPrice();
        }
    }
    
    public Record addARecord(int orderNum,String dishName,int portion,int num , Menu menu){
        Record newrecord=new Record();
        newrecord.portion=portion;
        newrecord.num=num;
        newrecord.d=menu.searthDish(dishName);
        newrecord.hasorder=orderNum;
        return newrecord;
    }
    public Record addASpecialRecord(int orderNum,String dishName,int portion,int num ,Menu menu,int tasteNum , boolean forOther){
        Record newrecord=new Record();
        newrecord.d=menu.searthSpecialDish(dishName);
        newrecord.hasorder=orderNum;
        newrecord.portion=portion;
        newrecord.num=num;
        if(!forOther){
            if(menu.searthSpecialDish(dishName).taste.equals("浙菜")){
                newrecord.SWEET = tasteNum;
                newrecord.Spicy = -1;
                newrecord.aci = -1;
                
            }
            if(menu.searthSpecialDish(dishName).taste.equals("晋菜")){
                newrecord.aci = tasteNum;
                newrecord.Spicy = -1;
                newrecord.SWEET = -1;
            }
            if(menu.searthSpecialDish(dishName).taste.equals("川菜")){
                newrecord.Spicy = tasteNum;
                newrecord.aci = -1;
                newrecord.SWEET = -1;
            }
        }
        else{
            newrecord.aci = -1;
            newrecord.Spicy = -1;
            newrecord.SWEET = -1;
        }
        return newrecord;
    }
    
    public int FindRcord(int orderNum){
        for(int i = 0; i<dishNum;i++) {
            if(records[i].hasorder==orderNum) {
                return 1;
            }
        }
        return 0;
    }
    
    public void delARecordByOrderNum(int orderNum){
        for(int i=0;i<dishNum;i++) {
            if(orderNum==records[i].hasorder) {
                records[i].num=0;
            }
        }
    }

}

class Menu{
    
    int dishNum2;
    int dishNum1;
    Dish[] dishes;
    SpecialDish_taste[] specialDishes;


    public SpecialDish_taste searthSpecialDish(String dishName){
        for(int k=0;k<dishNum1;k++) {
            if(dishName.equals(specialDishes[k].name)){
                return specialDishes[k];
            }
        }
        return null;
    }
    
    public Dish searthDish(String dishName){
        for(int k=0;k<dishNum2;k++) {
            if(dishName.equals(dishes[k].name)){
                return dishes[k];
            }
        }
        return null;
    }
    
    public SpecialDish_taste addSpecialDish(String dishName,int unit_price,String taste){
        SpecialDish_taste addDish=new SpecialDish_taste();
        addDish.name=dishName;
        addDish.unit_price=unit_price;
        addDish.taste=taste;
        return addDish;
    }

    public Dish addDish(String dishName,int unit_price){
        Dish addDish=new Dish();
        addDish.name=dishName;
        addDish.unit_price=unit_price;
        return addDish;
    }
}

类图如下:

 

期中考试

7-1 圆类设计

创建一个圆形类(Circle),私有属性为圆的半径,从控制台输入圆的半径,输出圆的面积

输入格式:

输入圆的半径,取值范围为(0,+∞),输入数据非法,则程序输出Wrong Format,注意:只考虑从控制台输入数值的情况

输出格式:

输出圆的面积(保留两位小数,可以使用String.format(“%.2f”,输出数值)控制精度)

 

代码如下:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        double r;
        Scanner input = new Scanner(System.in);
        r = input.nextDouble();
        if(r>0) {
            Circle circle = new Circle(r);
            double Area;
            Area = circle.getArea();
            System.out.println(String.format("%.2f", Area));
        }
        else
            System.out.println("Wrong Format");
        
    }
}
class Circle {
    double r;
    
    public Circle (double r) {
        this.r = r;
    }
    
    public double getradius() {
        return r;
    }
    
    public void setradius() {
        this.r = r;
    }
    public double getArea() {
        return r*r*Math.PI;
    }
    
}

此题较为简单,根据题目的要求就能完成

7-2 类结构设计

设计一个矩形类,其属性由矩形左上角坐标点(x1,y1)及右下角坐标点(x2,y2)组成,其中,坐标点属性包括该坐标点的X轴及Y轴的坐标值(实型数),求得该矩形的面积。类设计如下图:


image.png

输入格式:

分别输入两个坐标点的坐标值x1,y1,x2,y2。

输出格式:

输出该矩形的面积值(保留两位小数)。

代码如下:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double x1;
        double x2;
        double y1;
        double y2;
        double Area;
        x1 = input.nextDouble();
        y1 = input.nextDouble();
        x2 = input.nextDouble();
        y2 = input.nextDouble();
        Area = Math.abs((x1-x2)) * Math.abs((y1-y2));
        System.out.println(String.format("%.2f", Area));
    }

}

这题根据题目里面提供的类图,设计出相对应的类,就能完成题目的要求

7-3 继承与多态

将测验1与测验2的类设计进行合并设计,抽象出Shape父类(抽象类),Circle及Rectangle作为子类,类图如下所示:


image.png

试编程完成如上类图设计,主方法源码如下(可直接拷贝使用)

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        
        int choice = input.nextInt();
        
        switch(choice) {
        case 1://Circle
            double radiums = input.nextDouble();
            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;
        }
        
    }

 

其中,printArea(Shape shape)方法为定义在Main类中的静态方法,体现程序设计的多态性。

输入格式:

输入类型选择(1或2,不考虑无效输入)
对应图形的参数(圆或矩形)

输出格式:

图形的面积(保留两位小数)

代码如下:

import java.util.Scanner;
public class Main {

    public static void printArea(Shape shape) {
        System.out.printf("%.2f", shape.getArea());
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);

        int choice = input.nextInt();

        switch (choice) {
        case 1:// Circle
            double radiums = input.nextDouble();
            if (radiums > 0) {
                Shape circle = new Circle(radiums);
                printArea(circle);
            } else
                System.out.println("Wrong Format");
            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;
        }
    }
}

abstract class Shape {
    public abstract double getArea();
}

class Point {
    double x;
    double y;
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }
    public void setX() {
        this.x = x;
    }
    
    public double getY() {
        return y;
    }
    public void setY() {
        this.y = y;
    }
}

class Circle extends Shape{
    double r;
    
    public Circle (double r) {
        this.r = r;
    }
    
    public double getradius() {
        return r;
    }
    
    public void setradius() {
        this.r = r;
    }
    public double getArea() {
        return r*r*Math.PI;
    }
}

class Rectangle extends Shape{
    Point topleftpoint;
    Point lowerrightpoint;
    
    public Rectangle(Point topleftpoint,Point lowerrightpoint) {
        this.topleftpoint = topleftpoint;
        this.lowerrightpoint = lowerrightpoint;
    }
    
    public double getArea() {
        double hight = Math.abs(topleftpoint.getY()-lowerrightpoint.getY());
        double width = Math.abs(topleftpoint.getX()-lowerrightpoint.getX());
        double area = hight * width;
        return area;
    }

}

踩坑心得

在pta作业中,几次菜单计价程序的迭代题目比较复杂,尤其是菜单四里面的检测错误输入尤为繁琐,但是在使用正则表达式之后就简便了许多

期中考试的题目相比较大作业来说是较为简单的。在做圆类设计的那道题的时候,在计算圆的面积的时候,我用了3.14导致测试点过不去,后来才知道圆周率在Java里面有表达的公式为Math.PI

总结

 大作业的难度逐渐上升,需要熟练的掌握Java中类的使用及Object和String类中一些方法的使用。其中,最主要的是对类的使用,如类的继承、封装和多态性等。更加深入地理解面向对象的编程思想,熟悉Java编程语言的类和对象的使用方法。重点是对Javabean的基本使用,类的广泛应用。通过这些练习,我们可以更深入地掌握Java编程语言的常用技术,如数据结构、输入输出、异常处理等。在今后的学习中,我要更加注重编程思想的学习,这样才能更快更好的设计出代码。

posted @ 2023-11-19 19:33  Nammm  阅读(17)  评论(0编辑  收藏  举报