blog2

Posted on 2023-06-29 15:43  吴阿桑  阅读(53)  评论(0编辑  收藏  举报

第四次题目集

7-1 菜单计价程序-4
分数 100
作者 蔡轲
单位 南昌航空大学
本体大部分内容与菜单计价程序-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折, 周末全价。

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

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

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

输入格式:

 


 

桌号标识格式: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+英文空格+桌号+“:”+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价

输入样例1:

 


 

在这里给出一组输入。例如:

麻婆豆腐 12

油淋生菜 9 T

table 31 2023/2/1 14/20/001 麻婆豆腐 1 162 油淋生菜 4 2

end

输出样例1:

 


 

份数超出范围+份额超出范围。例如:

table 31: 1 num out of range 162 portion out of range 4

table 31: 0 0

输入样例2:

 


 

桌号信息错误。例如:

麻婆豆腐 12

油淋生菜 9 T

table a 2023/3/15 12/00/001 麻婆豆腐 1 12 油淋生菜 2 1

end

输出样例2:

 


 

在这里给出相应的输出。例如:

wrong format

输入样例3:

 


 

错误的菜谱记录。例如:

麻婆豆腐 12.0

油淋生菜 9 T

table 55 2023/3/31 12/00/00

麻辣香锅 151 麻婆豆腐 1 12 油淋生菜 2 1

end

输出样例3:

 


 

在这里给出相应的输出。例如:

wrong format

table 55:

invalid dish

麻婆豆腐 does not exist2 油淋生菜 14

table 55: 14 10

输入样例4:

 


 

桌号格式错误,不以“table”开头。例如:

麻婆豆腐 12

油淋生菜 9 T

table 1 2023/3/15 12/00/001 麻婆豆腐 1 12 油淋生菜 2 1

tab le 2 2023/3/15 12/00/001 麻婆豆腐 1 12 油淋生菜 2 1

end

输出样例4:

 


 

在这里给出相应的输出。例如:

table 1: 1 麻婆豆腐 122 油淋生菜 14

wrong formatrecord serial number sequence errorrecord serial number sequence error

table 1: 26 17

我的源码如下:

import java.time.LocalDate;import java.time.LocalDateTime;import java.util.Scanner;import java.util.regex.Matcher;import java.util.regex.Pattern;

/*输入:name表示菜品名称;unit_price是菜品价格;

计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小//大份)*/class Main{

 

    public static void main(String[] args){

        int j=0,flag=1,i=-1,k=0,m=0,n=0,p=0,q=0,flag3=0;

        Scanner scanner=new Scanner(System.in);

        Menu menu=new Menu();

        Rt rt=new Rt();

        Panduan panduan=new Panduan();

        while(true) {

            String s = scanner.nextLine();//zuopanduan

            if (s.equals("end"))

                break;

            if (s.matches("t(.*)")) {

                if(s.matches("table(.*)")) {

                    if(s.matches("table[ ]([1-9]|[1-9][0-9]+)[ ][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})")){

                        String []te=s.split(" |\\/");

                        LocalDate localDate1=LocalDate.of(2022,1,1);

                        LocalDate localDate2=LocalDate.of(2023,12,31);

                        String riqi=te[2]+"-"+te[3]+"-"+te[4];

                        String shijian=te[5]+"/"+te[6]+"/"+te[7];

                        if(!(te[1].matches("[1-9]|[1-4][0-9]|5[0-5]"))){

                            flag=0;

                            System.out.println(te[1]+" table num out of range");

                        } else if (!(riqi.matches("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))"))||!shijian.matches("([0-9]|[0-1][0-9]|20|21|22|23)[/]([0-5][0-9])[/]([0-5][0-9])")) {

                            flag=0;

                            System.out.println(te[1]+" date error");

                        }else{

                            LocalDate localDate=LocalDate.of(Integer.parseInt(te[2]), Integer.parseInt(te[3]), Integer.parseInt(te[4]));

                            LocalDateTime localDateTime=LocalDateTime.of(Integer.parseInt(te[2]), Integer.parseInt(te[3]), Integer.parseInt(te[4]), Integer.parseInt(te[5]), Integer.parseInt(te[6]), Integer.parseInt(te[7]));

                            if(localDate.isBefore(localDate1)||localDate.isAfter(localDate2)){

                                flag=0;

                                System.out.println("not a valid time period");}

                            else if (panduan.panduan2(localDateTime)==0) {

                                flag=0;

                                System.out.println("table " + te[1] + " out of opening hours");

                            } else{

                                i++;

                                flag=1;

                                rt.tables[i] = new Table();

                                rt.tables[i].tablenum = Integer.parseInt(te[1]);

                                rt.tables[i].localDateTime=localDateTime;

                                System.out.println("table"+' '+te[1]+": ");

                            }

                        }

                    }

                    else{

                        flag=0;

                        System.out.println("wrong format");

                    }

                }

                else{

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

                //这里进行i不加对上一个桌的继承。

            } else {

                if (flag == 1) {

                    if (s.matches("([1-9]|[1-9][0-9]+)[ ]delete")) {

                        String[] dd = s.split(" ");//后续可能会有bug

                        if ((rt.tables[i].findRecordByNum(rt.tables[i], Integer.parseInt(dd[0]), rt.tables[i].i) != 1) && (rt.tables[i].findRecorddByNum(rt.tables[i], Integer.parseInt(dd[0]), rt.tables[i].j) != 1))

                            System.out.println("delete error;");

                    } else if (s.matches("([1-9]|[1-9][0-9]+)[ ]([1-9]|[1-9][0-9]+)[ ][\u4E00-\u9FA5]+[ ][0-9][ ]([1-9]|[1-9][0-9]+)")) {

                        String[] dd = s.split(" ");

                        flag3=0;

                        for (j = 0; j <= i; j++) {//判断自己给自己点菜

                            if (rt.tables[j].tablenum == Integer.parseInt(dd[0])){

                                flag3=1;

                               break;}

                        }

                        if(flag3==1){

                            if(rt.tables[j].tablenum==rt.tables[i].tablenum){

                                System.out.println("Table number " + dd[0] + " does not exist");

                            }

                            else if (menu.searthDish(dd[2]) == null)

                                System.out.println(dd[2] + " " + "does not exist");

                            else{

                                rt.tables[i].recordsd[rt.tables[i].j] = rt.tables[i].addaRecordd(menu, Integer.parseInt(dd[0]), Integer.parseInt(dd[1]), dd[2], Integer.parseInt(dd[3]), Integer.parseInt(dd[4]));

                                System.out.println(rt.tables[i].recordsd[rt.tables[i].j].orderNum + " table " + rt.tables[i].tablenum + " pay for table " + dd[0] + " " + rt.tables[i].recordsd[rt.tables[i].j].getPrice);

                                rt.tables[i].j++;}

                        } else

                            System.out.println("Table number " + dd[0] + " does not exist");

                    }//

 

                    else if (s.matches("([1-9]|[1-9][0-9]+)[ ][\u4E00-\u9FA5]+[ ][0-9][ ]([1-9]|[1-9][0-9]+)")) {

                        String[] bz = s.split(" ");

                        if (rt.tables[i].i!=0){

                            if(Integer.parseInt(bz[0])<= rt.tables[i].records[rt.tables[i].i-1].orderNum){

                                System.out.println("record serial number sequence error");

                                continue;}

                        }

                        if (menu.searthDish(bz[1])== null)

                            System.out.println(bz[1] + " " + "does not exist");

                        else if (!bz[2].matches("[1-3]") && menu.searthDish(bz[1]).flag == 1)

                            System.out.println(bz[0] + " portion out of range " + bz[2]);

                        else if (!bz[2].matches("[1-3]") && menu.searthDish(bz[1]).flag == 0) {

                            System.out.println(bz[0] + " portion out of range " + bz[2]);

                        } else if (!bz[3].matches("[1-9]|1[0-5]")) {

                            System.out.println(bz[0] + " num out of range " + bz[3]);

                        } else{

                            rt.tables[i].records[rt.tables[i].i] = rt.tables[i].addaRecord(menu, Integer.parseInt(bz[0]), bz[1], Integer.parseInt(bz[2]), Integer.parseInt(bz[3]));

                            System.out.println(bz[0] + " " + bz[1] + " " + rt.tables[i].records[rt.tables[i].i].getPrice);

                            rt.tables[i].i++;}

                    }

                    else if (s.matches("[\u4E00-\u9FA5]+[ ]([1-9]|[1-9][0-9]+)") || s.matches("[\u4E00-\u9FA5]+[ ]([1-9]|[1-9][0-9]+)[ ][T]")) {//进入菜单

                        if (i == -1) {

                            int flag1 = 0, flag2 = 1;

                            boolean cheak1 = s.matches("[\u4E00-\u9FA5]+[ ]([1-9]|[1-9][0-9]+)");

                            boolean cheak2 = s.matches("[\u4E00-\u9FA5]+[ ]([1-9]|[1-9][0-9]+)[ ][T]");

                            String[] tokens = s.split(" ");

                            if (cheak1) {

                                if (s.matches("[\u4E00-\u9FA5]+[ ]([1-9][0-9]?|[1-2][0-9]{2})"))

                                    menu.addDish(tokens[0], Integer.parseInt(tokens[1]), flag1);

                                else

                                    System.out.println(tokens[0] + " price out of range " + tokens[1]);

                            }

                            if (cheak2) {

 

                                if (s.matches("[\u4E00-\u9FA5]+[ ]([1-9][0-9]?|[1-2][0-9]{2})[ ][T]"))

                                    menu.addDish(tokens[0], Integer.parseInt(tokens[1]), flag2);

                                else

                                    System.out.println(tokens[0] + " price out of range " + tokens[1]);

                            }

                        } else

                            System.out.println("invalid dish");

                    } else {

                        System.out.println("wrong format");

                    }

                }

            }

        }

 

 

        for(j=0;j<=i;j++) {

            if(rt.tables[j].flag==1){

                for(k=j+1;k<=i;k++) {

                    if(rt.tables[k].tablenum==rt.tables[j].tablenum){

                        //rt.tables[k].flag=0;

                        if(panduan.panduan(rt.tables[k].localDateTime,rt.tables[j].localDateTime)!=0||panduan.panduan1(rt.tables[k].localDateTime,rt.tables[j].localDateTime)!=0){

                            rt.tables[k].flag=0;

                            for(m=0;m<rt.tables[j].i;m++){

                                if(rt.tables[j].records[m].flag==1){

                                    for(n=m+1;n<rt.tables[j].i;n++){

                                        if(rt.tables[j].records[n].portion==rt.tables[j].records[m].portion&&rt.tables[j].records[n].d.name==rt.tables[j].records[m].d.name){

                                            rt.tables[j].records[n].flag=0;

                                            rt.tables[j].records[m].num+=rt.tables[j].records[n].num;

                                        }

                                    }

                                }

                                for (p=0; p< rt.tables[k].i; p++) {

                                    if (rt.tables[k].records[p].portion == rt.tables[j].records[m].portion && rt.tables[k].records[p].d.name == rt.tables[j].records[m].d.name) {

                                        rt.tables[k].records[p].flag = 0;

                                        rt.tables[j].records[m].num += rt.tables[k].records[p].num;

                                    }

                                }

 

                            }

 

                        }//if

                    }

                    else{

                        for(m=0;m<rt.tables[j].i;m++){

                            if(rt.tables[j].records[m].flag==1){

                                for(n=m+1;n<rt.tables[j].i;n++){

                                    if(rt.tables[j].records[n].portion==rt.tables[j].records[m].portion&&rt.tables[j].records[n].d.name==rt.tables[j].records[m].d.name){

                                        rt.tables[j].records[n].flag=0;

                                        rt.tables[j].records[m].num+=rt.tables[j].records[n].num;

                                    }

                                }

                            }

                            for (p=0; p< rt.tables[j].j; p++) {

                                if (rt.tables[j].recordsd[p].portion == rt.tables[j].records[m].portion && rt.tables[j].records[p].d.name == rt.tables[j].records[m].d.name) {

                                    rt.tables[j].recordsd[p].flag = 0;

                                    rt.tables[j].records[m].num += rt.tables[j].recordsd[p].num;

                                }

                            }

 

                        }

                    }

                }

            }

        }

        for(j=0;j<=i;j++){

            for(k=0;k<rt.tables[j].i;k++)

                rt.tables[j].TotalPrice+=rt.tables[j].records[k].d.getPrice(rt.tables[j].records[k].portion)*rt.tables[j].records[k].num;

            for(k=0;k<rt.tables[j].j;k++)

                rt.tables[j].TotalPrice+=rt.tables[j].recordsd[k].d.getPrice(rt.tables[j].recordsd[k].portion)*rt.tables[j].recordsd[k].num;

            for(k=0;k<rt.tables[j].i;k++)

                rt.tables[j].TotalPricez+=panduan.jisuan(rt.tables[j],rt.tables[j].records[k]);

            for(k=0;k<rt.tables[j].j;k++)

                rt.tables[j].TotalPricez+=panduan.jisuan(rt.tables[j],rt.tables[j].recordsd[k]);

            if(rt.tables[j].flag==1)

            System.out.println("table"+" "+rt.tables[j].tablenum+": "+ rt.tables[j].TotalPrice+" "+rt.tables[j].TotalPricez);

        }

                       /* for(int jj=0;jj<rt.tables[j].j;jj++)

                rt.tables[j].TotalPrice+=rt.tables[j].recordsd[jj].getPrice;

                for(int ii=0;ii<rt.tables[j].i;ii++)

                    rt.tables[j].TotalPrice+=rt.tables[j].records[ii].getPrice;

                if(rt.tables[j].localDateTime.getDayOfWeek().getValue()>=1&&rt.tables[j].localDateTime.getDayOfWeek().getValue()<=5){

                    if()

                    /*if(rt.tables[j].localDateTime.getHour()*3600+rt.tables[j].localDateTime.getMinute()*60+rt.tables[j].localDateTime.getSecond()>=17*3600&&rt.tables[j].localDateTime.getHour()*3600+rt.tables[j].localDateTime.getMinute()*60+rt.tables[j].localDateTime.getSecond()<=20.5*3600){

                        rt.tables[j].TotalPricez=(int)Math.round(rt.tables[j].TotalPrice*0.8);

                        System.out.println("table "+rt.tables[j].tablenum+": "+rt.tables[j].TotalPrice+" "+rt.tables[j].TotalPricez);

                    }

                    else if (rt.tables[j].localDateTime.getHour()*3600+rt.tables[j].localDateTime.getMinute()*60+rt.tables[j].localDateTime.getSecond()>=10.5*3600&&rt.tables[j].localDateTime.getHour()*3600+rt.tables[j].localDateTime.getMinute()*60+rt.tables[j].localDateTime.getSecond()<=14.5*3600){

                        rt.tables[j].TotalPricez=(int)Math.round(rt.tables[j].TotalPrice*0.6);

                        System.out.println("table "+rt.tables[j].tablenum+": "+rt.tables[j].TotalPrice+" "+rt.tables[j].TotalPricez);

                    }

                }

                else{

                    if(rt.tables[j].localDateTime.getHour()*3600+rt.tables[j].localDateTime.getMinute()*60+rt.tables[j].localDateTime.getSecond()>=9.5*3600&&rt.tables[j].localDateTime.getHour()*3600+rt.tables[j].localDateTime.getMinute()*60+rt.tables[j].localDateTime.getSecond()<=21*3600)

                        System.out.println("table "+rt.tables[j].tablenum+": "+rt.tables[j].TotalPrice+" "+rt.tables[j].TotalPricez);

                }

           }*/

 

    }

}

class Dish {

    String name;

    int flag=0;

    int unit_price;

 

    public Dish(String name,int unit_price){

        this.name = name;

        this.unit_price = unit_price;

    }

    public Dish(){

    }

    public int getPrice(int portion){

        double unit_price1=unit_price;

        if(portion==2)

            unit_price1=1.5*unit_price;

        if(portion==3)

            unit_price1=2*unit_price;

        return (int) Math.round(unit_price1);

    }

}/*输入:dishs菜品数组,用来保存所有菜品信息;

searthDish(String dishName)根据菜名在菜谱中查找菜品信息,返回Dish对象;参数类型为String,名称为dishName;

 */class  Menu {

    public Dish[] dishs=new Dish[20];

    int f=0;

    Dish searthDish(String dishName){

        int i;

        int flag=0;

        for(i=f-1;i>=0;i--){

            flag=0;

            if(dishs[i].name.equals(dishName)){

                flag=1;break;

            }

        }

        if(!(flag != 1))

            return dishs[i];

        else

            return null;

    }

    public void addDish(String dishName,int unit_price,int flag){

        dishs[f]=new Dish(dishName,unit_price);

        dishs[f].flag=flag;

        f++;

    }

}/*输入:d Dish类型的菜品;portion份额(1/2/3代表小//大份)getPrice()函数计价,计算本条记录的价格*/class Record {

    int tableNum;

    int orderNum;//序号\

    Dish d;//菜品\

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

    int getPrice;

    int num;

    int flag=1;

}

 

class Table {

    Record[] records = new Record[20];

    Record[] recordsd = new Record[20];

    int flag=1;

    int TotalPrice;

    int TotalPricez;

    int i = 0;

    int j = 0;

    int tablenum;

    LocalDateTime localDateTime;

        /*public  void setTablenum(int tablenum){

            this.tablenum=tablenum;

        }*/

 

    public Record addaRecord(Menu menu, int orderNum, String dishName, int portion, int num) {

        Record record = new Record();

        record.orderNum = orderNum;

        record.num = num;

        record.d = menu.searthDish(dishName);

        record.portion = portion;

        if ((record.d != null))

            record.getPrice = record.d.getPrice(portion) * num;

        return record;

    }

 

    public Record addaRecordd(Menu menu, int tablenum, int orderNum, String dishName, int portion, int num) {

        Record record = new Record();

        record.tableNum = tablenum;

        record.orderNum = orderNum;

        record.num = num;

        record.d = menu.searthDish(dishName);

        record.portion = portion;

        if ((record.d != null))

            record.getPrice = record.d.getPrice(portion) * num;

        return record;

    }

 

    public int findRecordByNum(Table table, int orderNum, int i) {

        for (int k = 0; k < i; k++)

            if (!(table.records[k].orderNum != orderNum)) {

                if(table.records[k].num ==0){

                    System.out.println("deduplication "+orderNum);

                    return 1;

                }else{

                    table.records[k].num = 0; //这里做删除;

                    return 1;}

            }

        return 0;

    }//根据序号查找一条记录

 

    public int findRecorddByNum(Table table, int orderNum, int j) {

        for (int m = 0; m < j; m++)

            if(table.records[m].num ==0){

                System.out.println("deduplication "+orderNum);

                return 1;

            }else{

                table.records[m].num= 0; //这里做删除;

                return 1;}

        return 0;

    }//根据序号查找一条记录

}class Rt{

    Table tables[]=new Table[20];

}class Panduan{

    int panduan(LocalDateTime localDateTime1,LocalDateTime localDateTime2) {

        if (localDateTime1.getDayOfWeek().getValue() <= 5 && localDateTime1.getDayOfWeek().getValue() >= 1 && localDateTime1.getDayOfWeek().getValue() == localDateTime2.getDayOfWeek().getValue()) {

            if ((localDateTime1.getHour() * 3600 + localDateTime1.getMinute() * 60 + localDateTime1.getSecond() >= 17 * 3600 && localDateTime1.getHour() * 3600 + localDateTime1.getMinute() * 60 + localDateTime1.getSecond() <= 20.5 * 3600)&&(localDateTime2.getHour() * 3600 + localDateTime2.getMinute() * 60 + localDateTime2.getSecond() >= 17 * 3600 && localDateTime2.getHour() * 3600 + localDateTime2.getMinute() * 60 + localDateTime2.getSecond() <= 20.5 * 3600))

                return 1;

            if ((localDateTime1.getHour() * 3600 + localDateTime1.getMinute() * 60 + localDateTime1.getSecond() >= 10.5 * 3600 && localDateTime1.getHour() * 3600 + localDateTime1.getMinute() * 60 + localDateTime1.getSecond() <= 14.5 * 3600)&&(localDateTime2.getHour() * 3600 + localDateTime2.getMinute() * 60 + localDateTime2.getSecond() >= 10.5 * 3600 && localDateTime2.getHour() * 3600 + localDateTime2.getMinute() * 60 + localDateTime2.getSecond() <= 14.5 * 3600))

                return 1;

        }

        return 0;

    }

    int panduan1(LocalDateTime localDateTime1,LocalDateTime localDateTime2){

        if(localDateTime1.getDayOfWeek().getValue()<=7&&localDateTime1.getDayOfWeek().getValue()>=6&&localDateTime1.getDayOfWeek().getValue()==localDateTime2.getDayOfWeek().getValue()) {

            if (Math.abs(localDateTime1.getHour() * 3600 + localDateTime1.getMinute() * 60 + localDateTime1.getSecond() - localDateTime2.getHour() * 3600 + localDateTime2.getMinute() * 60 + localDateTime2.getSecond()) < 3600)

                return 1;

        }

        return 0;

    }

    int panduan2(LocalDateTime localDateTime){

        if(localDateTime.getDayOfWeek().getValue()<=5&&localDateTime.getDayOfWeek().getValue()>=1){

            if((localDateTime.getHour() * 3600 + localDateTime.getMinute() * 60 + localDateTime.getSecond() >= 17 * 3600 && localDateTime.getHour() * 3600 + localDateTime.getMinute() * 60 + localDateTime.getSecond() <= 20.5 * 3600)||(localDateTime.getHour() * 3600 + localDateTime.getMinute() * 60 + localDateTime.getSecond() >= 10.5 * 3600 && localDateTime.getHour() * 3600 + localDateTime.getMinute() * 60 + localDateTime.getSecond() <= 14.5 * 3600))

                return 1;}

        if(localDateTime.getDayOfWeek().getValue()<=7&&localDateTime.getDayOfWeek().getValue()>=6){

            if(localDateTime.getHour() * 3600 + localDateTime.getMinute() * 60 + localDateTime.getSecond() >= 9.5 * 3600 && localDateTime.getHour() * 3600 + localDateTime.getMinute() * 60 + localDateTime.getSecond() <= 21.5*3600)

                return 1;

        }

        return 0;

    }

    int jisuan(Table table,Record record){

        if(record.d.flag==0){

            if(table.localDateTime.getDayOfWeek().getValue()<=5&&table.localDateTime.getDayOfWeek().getValue()>=1){

                if(table.localDateTime.getHour() * 3600 + table.localDateTime.getMinute() * 60 + table.localDateTime.getSecond() >= 17 * 3600 && table.localDateTime.getHour() * 3600 + table.localDateTime.getMinute() * 60 + table.localDateTime.getSecond() <= 20.5 * 3600)

                    return (int)Math.round(record.d.getPrice(record.portion)*record.num*0.8);

                else if (table.localDateTime.getHour() * 3600 + table.localDateTime.getMinute() * 60 + table.localDateTime.getSecond() >= 10.5 * 3600 && table.localDateTime.getHour() * 3600 + table.localDateTime.getMinute() * 60 + table.localDateTime.getSecond() <= 14.5 * 3600){

                    return (int)Math.round(record.d.getPrice(record.portion)*record.num*0.6);

                }

            }

            else{

                return record.d.getPrice(record.portion)*record.num;

            }

        }

        else{

            if(table.localDateTime.getDayOfWeek().getValue()<=5&&table.localDateTime.getDayOfWeek().getValue()>=1)

                return (int)Math.round(record.d.getPrice(record.portion)*record.num*0.7);

            else

                return record.d.getPrice(record.portion)*record.num;

        }

        return 1;

    }

}

 

 

 

 

思路分析:

先题目很长,需要仔细阅读,挺考验人的耐心的,但是只要将题目分析提取,其实大致的思路就已经出来了。分好类就可以开始敲代码了。类图如下:

 

 

 

 

然后呢分好类之后,就要根据不同的部分进行相应的操作,输入菜谱这块的话,需要先用split方法将字符串以空格分隔,然后将分好的存进一个数组当中,接着把菜名和菜价存进已经new好的dish数组当中,这就是菜谱的存储方法,接着就是像订单啊之类的操作。其实和这个都有所相似,细节不同而已。
然后将代点菜的操作完成之后,删除的代码也补上,就到了最后的计算总价的环节了,这里要注意时间不同打的折扣也不同,所以还要进行四舍五入的操作,然后根据tables数组的顺序,依次输出每桌的折扣前的价格和折扣后的价格,就大功告成了。

 

 

 

 

 

第五次题目集

题目的话我就不写了太长了,其实和上次的差不多就多了几个特色菜而已,下面只给出我的源代码吧。

我的源码如下:

import java.time.LocalDate;import java.time.LocalDateTime;import java.util.Scanner;import java.util.regex.Matcher;import java.util.regex.Pattern;

/*输入:name表示菜品名称;unit_price是菜品价格;

计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小//大份)*/class Main{

 

    public static void main(String[] args){

        int j=0,flag=1,i=-1,k=0,m=0,n=0,p=0,q=0,g=0;

        Scanner scanner=new Scanner(System.in);

        Menu menu=new Menu();

        Rt rt=new Rt();

        Panduan panduan=new Panduan();

        Kouwd kouwd=new Kouwd();

        while(true) {

            String s = scanner.nextLine();//zuopanduan

            if (s.equals("end"))

                break;

            if(s.matches("table(.*)")) {

                if(s.matches("table[ ]([1-9]|[1-9][0-9]+)[ ][:][ ]([a-z]|[A-Z]){1,10}[ ](180|181|189|133|135|136)[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})")){

                    String []te=s.split(" |\\/");

                    LocalDateTime localDateTime=LocalDateTime.of(Integer.parseInt(te[5]), Integer.parseInt(te[6]), Integer.parseInt(te[7]), Integer.parseInt(te[8]), Integer.parseInt(te[9]), Integer.parseInt(te[10]));

                    if (panduan.panduan2(localDateTime)==0) {

                        flag=0;

                        System.out.println("table " + te[1] + " out of opening hours");

                    } else{

                        i++;

                        flag=1;

                        rt.tables[i] = new Table();

                        rt.tables[i].tablenum = Integer.parseInt(te[1]);

                        rt.tables[i].name = te[3];

                        rt.tables[i].code = te[4];

                        rt.tables[i].localDateTime=localDateTime;

                        System.out.println("table"+' '+te[1]+": ");

                    }

 

                }

                else{

                    flag=0;

                    System.out.println("wrong format");

                }

            }

            else {

                if (flag == 1) {

                    /*1删除*/     if (s.matches("([1-9]|[1-9][0-9]+)[ ]delete")) {

                        String[] dd = s.split(" ");//后续可能会有bug

                        if ((rt.tables[i].findRecordByNum(rt.tables[i], Integer.parseInt(dd[0]), rt.tables[i].i) != 1) && (rt.tables[i].findRecorddByNum(rt.tables[i], Integer.parseInt(dd[0]), rt.tables[i].j) != 1))

                            System.out.println("delete error;");

                    } /*2带点菜订单*/else if (s.matches("([1-9]|[1-9][0-9]+)[ ]([1-9]|[1-9][0-9]+)[ ][\u4E00-\u9FA5]+[ ][0-9][ ]([1-9]|[1-9][0-9]+)")) {

                        String[] dd = s.split(" ");

                        if (menu.searthDish(dd[2]) == null)

                            System.out.println(dd[2] + " " + "does not exist");

                        else{

                            rt.tables[i].recordsd[rt.tables[i].j] = rt.tables[i].addaRecordd(menu, Integer.parseInt(dd[0]), Integer.parseInt(dd[1]), dd[2],-1, Integer.parseInt(dd[3]), Integer.parseInt(dd[4]));

                            System.out.println(rt.tables[i].recordsd[rt.tables[i].j].orderNum + " table " + rt.tables[i].tablenum + " pay for table " + dd[0] + " " + rt.tables[i].recordsd[rt.tables[i].j].getPrice);

                            rt.tables[i].j++;}

                    }//

 

                    /*3订单*/ else if (s.matches("([1-9]|[1-9][0-9]+)[ ][\u4E00-\u9FA5]+[ ][0-9][ ]([1-9]|[1-9][0-9]+)")) {

                        String[] bz = s.split(" ");

                        if (menu.searthDish(bz[1])== null)

                            System.out.println(bz[1] + " " + "does not exist");

                        else{

                            rt.tables[i].records[rt.tables[i].i] = rt.tables[i].addaRecord(menu, Integer.parseInt(bz[0]), bz[1],-1, Integer.parseInt(bz[2]), Integer.parseInt(bz[3]));

                            System.out.println(bz[0] + " " + bz[1] + " " + rt.tables[i].records[rt.tables[i].i].getPrice);

                            rt.tables[i].i++;}

                    } /*4特色带点*/else if (s.matches("([1-9]|[1-9][0-9]+)[ ]([1-9]|[1-9][0-9]+)[ ][\u4E00-\u9FA5]+[ ]([0-9]|[1-9][0-9]+)[ ][0-9][ ]([1-9]|[1-9][0-9]+)")) {

                        String []td=s.split(" ");

                        if (menu.searthDish(td[2]) == null)

                            System.out.println(td[2] + " " + "does not exist");

                        else if (menu.searthDish(td[2]).flag==1) {

                            if(Integer.parseInt(td[3])>5||Integer.parseInt(td[3])<0)

                                System.out.println("spicy num out of range :"+td[3]);

                            else{

                                rt.tables[i].recordsd[rt.tables[i].j] = rt.tables[i].addaRecordd(menu, Integer.parseInt(td[0]), Integer.parseInt(td[1]), td[2],Integer.parseInt(td[3]), Integer.parseInt(td[4]), Integer.parseInt(td[5]));

                                //rt.tables[Integer.parseInt(td[0])].recordtd[rt.tables[Integer.parseInt(td[0])].k]=rt.tables[Integer.parseInt(td[0])].addaRecortd(menu, Integer.parseInt(td[0]), Integer.parseInt(td[1]), td[2],Integer.parseInt(td[3]), Integer.parseInt(td[4]), Integer.parseInt(td[5]));

                                System.out.println(rt.tables[i].recordsd[rt.tables[i].j].orderNum + " table " + rt.tables[i].tablenum + " pay for table " + td[0] + " " + rt.tables[i].recordsd[rt.tables[i].j].getPrice);

                                rt.tables[i].j++;}

                        }

                        else if(menu.searthDish(td[2]).flag==2){

                            if(Integer.parseInt(td[3])>4||Integer.parseInt(td[3])<0)

                                System.out.println("acidity num out of range :"+td[3]);

                            else{

                                rt.tables[i].recordsd[rt.tables[i].j] = rt.tables[i].addaRecordd(menu, Integer.parseInt(td[0]), Integer.parseInt(td[1]), td[2],Integer.parseInt(td[3]), Integer.parseInt(td[4]), Integer.parseInt(td[5]));

                                //rt.tables[Integer.parseInt(td[0])].recordtd[rt.tables[Integer.parseInt(td[0])].k]=rt.tables[Integer.parseInt(td[0])].addaRecortd(menu, Integer.parseInt(td[0]), Integer.parseInt(td[1]), td[2],Integer.parseInt(td[3]), Integer.parseInt(td[4]), Integer.parseInt(td[5]));

                                System.out.println(rt.tables[i].recordsd[rt.tables[i].j].orderNum + " table " + rt.tables[i].tablenum + " pay for table " + td[0] + " " + rt.tables[i].recordsd[rt.tables[i].j].getPrice);

                                rt.tables[i].j++;}

                        }

                        else if(menu.searthDish(td[2]).flag==3){

                            if(Integer.parseInt(td[3])>3||Integer.parseInt(td[3])<0)

                                System.out.println("sweetness num out of range :"+td[3]);

                            else{

                                rt.tables[i].recordsd[rt.tables[i].j] = rt.tables[i].addaRecordd(menu, Integer.parseInt(td[0]), Integer.parseInt(td[1]), td[2],Integer.parseInt(td[3]), Integer.parseInt(td[4]), Integer.parseInt(td[5]));

                                // rt.tables[Integer.parseInt(td[0])].recordtd[rt.tables[Integer.parseInt(td[0])].k]=rt.tables[Integer.parseInt(td[0])].addaRecortd(menu, Integer.parseInt(td[0]), Integer.parseInt(td[1]), td[2],Integer.parseInt(td[3]), Integer.parseInt(td[4]), Integer.parseInt(td[5]));

                                System.out.println(rt.tables[i].recordsd[rt.tables[i].j].orderNum + " table " + rt.tables[i].tablenum + " pay for table " + td[0] + " " + rt.tables[i].recordsd[rt.tables[i].j].getPrice);

                                rt.tables[i].j++;}

                        }

 

                    }/*5特色菜自己输入*/ else if (s.matches("([1-9]|[1-9][0-9]+)[ ][\u4E00-\u9FA5]+[ ]([0-9]|[1-9][0-9]+)[ ][0-9][ ]([1-9]|[1-9][0-9]+)")) {

                        String []tz=s.split(" ");

                        if (menu.searthDish(tz[1])== null)

                            System.out.println(tz[1] + " " + "does not exist");

                        else if (menu.searthDish(tz[1]).flag==1) {

                            if(Integer.parseInt(tz[2])>5||Integer.parseInt(tz[2])<0)

                                System.out.println("spicy num out of range :"+tz[2]);

                            else{

                                rt.tables[i].records[rt.tables[i].i] = rt.tables[i].addaRecord(menu, Integer.parseInt(tz[0]), tz[1],Integer.parseInt(tz[2]), Integer.parseInt(tz[3]), Integer.parseInt(tz[4]));

                                System.out.println(tz[0] + " " + tz[1] + " " + rt.tables[i].records[rt.tables[i].i].getPrice);

                                rt.tables[i].i++;}

 

                        }

                        else if(menu.searthDish(tz[1]).flag==2){

                            if(Integer.parseInt(tz[2])>4||Integer.parseInt(tz[2])<0)

                                System.out.println("acidity num out of range :"+tz[2]);

                            else{

                                rt.tables[i].records[rt.tables[i].i] = rt.tables[i].addaRecord(menu, Integer.parseInt(tz[0]), tz[1],Integer.parseInt(tz[2]), Integer.parseInt(tz[3]), Integer.parseInt(tz[4]));

                                System.out.println(tz[0] + " " + tz[1] + " " + rt.tables[i].records[rt.tables[i].i].getPrice);

                                rt.tables[i].i++;}

                        }

                        else if(menu.searthDish(tz[1]).flag==3){

                            if(Integer.parseInt(tz[2])>3||Integer.parseInt(tz[2])<0)

                                System.out.println("sweetness num out of range :"+tz[2]);

                            else{

                                rt.tables[i].records[rt.tables[i].i] = rt.tables[i].addaRecord(menu, Integer.parseInt(tz[0]), tz[1],Integer.parseInt(tz[2]), Integer.parseInt(tz[3]), Integer.parseInt(tz[4]));

                                System.out.println(tz[0] + " " + tz[1] + " " + rt.tables[i].records[rt.tables[i].i].getPrice);

                                rt.tables[i].i++;}

 

                        }}

 

                    /*6菜谱输入*/     else if (s.matches("[\u4E00-\u9FA5]+[ ]([1-9]|[1-9][0-9]+)")) {//进入菜单

                        String[] tokens = s.split(" ");

                        menu.addDish(tokens[0], Integer.parseInt(tokens[1]), 0);

                    }

                    else if (s.matches("[\u4E00-\u9FA5]+[ ](川菜|晋菜|浙菜)[ ]([1-9]|[1-9][0-9]+)[ ][T]")) {

                        String[] tokens1 = s.split(" ");

                        if(tokens1[1].equals("川菜")){

                            menu.addDish(tokens1[0], Integer.parseInt(tokens1[2]), 1);}

                        else if (tokens1[1].equals("晋菜")){

                            menu.addDish(tokens1[0], Integer.parseInt(tokens1[2]), 2);}

                        else if(tokens1[1].equals("浙菜")){

                            menu.addDish(tokens1[0], Integer.parseInt(tokens1[2]), 3);}

                    }

                    else {

                        System.out.println("wrong format");

                    }

                }

            }

        }

        for(j=0;j<=i;j++){

            for(k=0;k<rt.tables[j].j;k++){

                if(rt.tables[j].recordsd[k].d.flag!=0&&rt.tables[j].recordsd[k].num!=0){

                    for(p=0;p<=i;p++) {

                        if (rt.tables[p].tablenum == rt.tables[j].recordsd[k].tableNum) {

                            rt.tables[p].recordtd[rt.tables[p].k] = rt.tables[p].addaRecortd(menu,rt.tables[j].recordsd[k].tableNum , rt.tables[j].recordsd[k].orderNum,rt.tables[j].recordsd[k].d.name, rt.tables[j].recordsd[k].feel, rt.tables[j].recordsd[k].portion, rt.tables[j].recordsd[k].num);

                            rt.tables[p].k++;

                        }

                    }

                }

            }

        }

 

        for(j=0;j<=i;j++){

            for(k=0;k<rt.tables[j].i;k++)

                rt.tables[j].TotalPrice+=rt.tables[j].records[k].d.getPrice(rt.tables[j].records[k].portion)*rt.tables[j].records[k].num;

            for(k=0;k<rt.tables[j].j;k++)

                rt.tables[j].TotalPrice+=rt.tables[j].recordsd[k].d.getPrice(rt.tables[j].recordsd[k].portion)*rt.tables[j].recordsd[k].num;

            for(k=0;k<rt.tables[j].i;k++)

                rt.tables[j].TotalPricez+=panduan.jisuan(rt.tables[j],rt.tables[j].records[k]);

            for(k=0;k<rt.tables[j].j;k++)

                rt.tables[j].TotalPricez+=panduan.jisuan(rt.tables[j],rt.tables[j].recordsd[k]);

            if(rt.tables[j].flag==1)

                System.out.print("table"+" "+rt.tables[j].tablenum+": "+ rt.tables[j].TotalPrice+" "+rt.tables[j].TotalPricez);

            for(m=0;m<rt.tables[j].i;m++){

                if(rt.tables[j].records[m].d.flag==1&&rt.tables[j].records[m].num!=0){

                    rt.tables[j].cfeel+=rt.tables[j].records[m].feel*rt.tables[j].records[m].num;

                    rt.tables[j].cnum+=rt.tables[j].records[m].num;

                }

                if(rt.tables[j].records[m].d.flag==2&&rt.tables[j].records[m].num!=0){

                    rt.tables[j].jfeel+=rt.tables[j].records[m].feel*rt.tables[j].records[m].num;

                    rt.tables[j].jnum+=rt.tables[j].records[m].num;

                }

                if(rt.tables[j].records[m].d.flag==3&&rt.tables[j].records[m].num!=0){

                    rt.tables[j].zfeel+=rt.tables[j].records[m].feel*rt.tables[j].records[m].num;

                    rt.tables[j].znum+=rt.tables[j].records[m].num;

                }

            }

            for(n=0;n<rt.tables[j].k;n++){

                if(rt.tables[j].recordtd[n].d.flag==1){

                    rt.tables[j].cfeel+=rt.tables[j].recordtd[n].feel*rt.tables[j].recordtd[n].num;

                    rt.tables[j].cnum+=rt.tables[j].recordtd[n].num;

                }

                if(rt.tables[j].recordtd[n].d.flag==2){

                    rt.tables[j].jfeel+=rt.tables[j].recordtd[n].feel*rt.tables[j].recordtd[n].num;

                    rt.tables[j].jnum+=rt.tables[j].recordtd[n].num;

                }

                if(rt.tables[j].recordtd[n].d.flag==3){

                    rt.tables[j].zfeel+=rt.tables[j].recordtd[n].feel*rt.tables[j].recordtd[n].num;

                    rt.tables[j].znum+=rt.tables[j].recordtd[n].num;

                }

            }

            g=0;

            if(rt.tables[j].cnum>0){

                System.out.print(" 川菜 "+rt.tables[j].cnum+" "+kouwd.cpanduan(Math.round(1.0*rt.tables[j].cfeel/rt.tables[j].cnum)));

                g++;}

            if(rt.tables[j].jnum>0){

                System.out.print(" 晋菜 "+rt.tables[j].jnum+" "+kouwd.jpanduan(Math.round(1.0*rt.tables[j].jfeel/rt.tables[j].jnum)));

                g++;}

            if(rt.tables[j].znum>0){

                System.out.print(" 浙菜 "+rt.tables[j].znum+" "+kouwd.zpanduan(Math.round(1.0*rt.tables[j].zfeel/rt.tables[j].znum)));

                g++;}

            if(g==0)

                System.out.print(" ");

            System.out.println();

        }

        for(q=0;q<=i;q++){

            if(rt.tables[q].flag==1){

                for(j=q+1;j<=i;j++){

                    if(rt.tables[q].name.equals(rt.tables[j].name)){

                        rt.tables[q].TotalPricez+=rt.tables[j].TotalPricez;

                        rt.tables[j].flag=0;

                    }

                }

            }

        }

        String temp1=null;

        String temp2=null;

        int temp3=0;

        for(p=0;p<i;p++){

            for(q=0;q<i-p;q++){

                if( rt.tables[q].name.charAt(0)-'0'>rt.tables[q+1].name.charAt(0)-'0'&&rt.tables[q].flag==1&&rt.tables[q+1].flag==1){

                    temp1=rt.tables[q].name;temp2=rt.tables[q].code;temp3=rt.tables[q].TotalPricez;

                    rt.tables[q].name=rt.tables[q+1].name;rt.tables[q].code=rt.tables[q+1].code;rt.tables[q].TotalPricez=rt.tables[q+1].TotalPricez;

                    rt.tables[q+1].name=temp1;rt.tables[q+1].code=temp2;rt.tables[q+1].TotalPricez=temp3;

                }

            }

        }

        for(j=0;j<=i;j++){

            if(rt.tables[j].flag==1)

                System.out.println(rt.tables[j].name+" "+rt.tables[j].code+" "+rt.tables[j].TotalPricez);

        }

    }

}

class Dish {

    String name;

    int flag=0;

    int unit_price;

 

    public Dish(String name,int unit_price){

        this.name = name;

        this.unit_price = unit_price;

    }

    public Dish(){

    }

    public int getPrice(int portion){

        double unit_price1=unit_price;

        if(portion==2)

            unit_price1=1.5*unit_price;

        if(portion==3)

            unit_price1=2*unit_price;

        return (int) Math.round(unit_price1);

    }

}/*输入:dishs菜品数组,用来保存所有菜品信息;

searthDish(String dishName)根据菜名在菜谱中查找菜品信息,返回Dish对象;参数类型为String,名称为dishName;

 */class  Menu {

    public Dish[] dishs=new Dish[20];

    int f=0;

    Dish searthDish(String dishName){

        int i;

        int flag=0;

        for(i=f-1;i>=0;i--){

            flag=0;

            if(dishs[i].name.equals(dishName)){

                flag=1;break;

            }

        }

        if(!(flag != 1))

            return dishs[i];

        else

            return null;

    }

    public void addDish(String dishName,int unit_price,int flag){

        dishs[f]=new Dish(dishName,unit_price);

        dishs[f].flag=flag;

        f++;

    }

}/*输入:d Dish类型的菜品;portion份额(1/2/3代表小//大份)getPrice()函数计价,计算本条记录的价格*/class Record {

    int tableNum;

    int orderNum;//序号\

    Dish d;//菜品\

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

    int getPrice;

    int num;

    int flag=1;

    int feel;

}

 

class Table {

    Record[] records = new Record[20];

    Record[] recordsd = new Record[20];

    Record[] recordtd =new Record[20];

    int flag=1;

    String name;

    String code;

    int cfeel;

    int jfeel;

    int zfeel;

    int cnum=0;

    int jnum=0;

    int znum=0;

    int TotalPrice;

    int TotalPricez;

    int i = 0;

    int j = 0;

    int k = 0;

    int tablenum;

    LocalDateTime localDateTime;

        /*public  void setTablenum(int tablenum){

            this.tablenum=tablenum;

        }*/

 

    public Record addaRecord(Menu menu, int orderNum, String dishName, int feel,int portion, int num) {

        Record record = new Record();

        record.orderNum = orderNum;

        record.num = num;

        record.feel=feel;

        record.d = menu.searthDish(dishName);

        record.portion = portion;

        if ((record.d != null))

            record.getPrice = record.d.getPrice(portion) * num;

        return record;

    }

 

    public Record addaRecordd(Menu menu, int tablenum, int orderNum, String dishName,int feel, int portion, int num) {

        Record record = new Record();

        record.tableNum = tablenum;

        record.orderNum = orderNum;

        record.num = num;

        record.feel=feel;

        record.d = menu.searthDish(dishName);

        record.portion = portion;

        if ((record.d != null))

            record.getPrice = record.d.getPrice(portion) * num;

        return record;

    }

    public Record addaRecortd(Menu menu, int tablenum, int orderNum, String dishName,int feel, int portion, int num) {

        Record record = new Record();

        record.tableNum = tablenum;

        record.orderNum = orderNum;

        record.num = num;

        record.feel=feel;

        record.d = menu.searthDish(dishName);

        record.portion = portion;

        if ((record.d != null))

            record.getPrice = record.d.getPrice(portion) * num;

        return record;

    }

 

    public int findRecordByNum(Table table, int orderNum, int i) {

        for (int k = 0; k < i; k++)

            if (!(table.records[k].orderNum != orderNum)) {

                table.records[k].num = 0; //这里做删除;

                table.records[k].feel=0;

                return 1;

            }

        return 0;

    }//根据序号查找一条记录

 

    public int findRecorddByNum(Table table, int orderNum, int j) {

        for (int m = 0; m < j; m++)

            if (!(table.recordsd[m].orderNum != orderNum)) {

                table.recordsd[m].num= 0; //这里做删除;

                table.recordsd[m].feel=0;

                return 1;}

        return 0;

    }

}

 

分析:

这一题的输入方面的处理其实和菜单计价4没什么太大的差距,都是使用正则表达式来控制区分输入的是哪一种处理,但是菜单计价5多了一个新的功能,就是增加了川菜浙菜等菜系的特色,在菜单的增加方面,其实和菜单4是一样的,只不过我加了一个record的新属性,就是flag,我把普通菜的glag当作0,而特色菜分别flag为1,2,3,这样我认为是不改变源代码的最为简便的方法了。

 

 

类图如下:

 

 

可以从类图看出,此题我使用了除了Main类的七个类,分别是record类,Dish类,Panduan类,Menu类和Table类,Rt类及Kouwd类用来判断口味度,因为是对菜单3的拓展,与菜单4不同的是,在菜单5里,增加了计算不同菜系口味度的方面的要求,其他在价格的计算上与第一题无差别,仍然是不同的时间对应不同的折扣。

 

 

 

期中考试

7-1 测验1-圆类设计

这题没什么难度,很基础。

 

7-2 测验2-类结构设计

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

分析:这题原题给了类图,难度也不大,直接根据类图去设计代码即可。

 

7-3 测验3-继承与多态

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

 

分析:这题其实就只是在第一题和第二题的基础上添加一个父类,第一题第二题只要写出来了,那第三题其实也大差不差了。

 

7-4 测验4-抽象类与接口

第四题没写出来。。。。。。