第二次大作业博客分析

菜单计价程序—4

一丶错误分析:

1.这一题的测试点很多,在做的时候没有理清逻辑,导致在解决了部分测试点后,当尝试通过剩余测试点时,总会影响到之前的测试点通过。

2.对于格式处理时单一的通过·split空格,判断共有几段字符串。后改为正则表达式,但正则表达式规则写的太松。事实上正则表达式没起到什么筛选的作用。

3.特色菜的计价是单独计价,打折只看是否在周六日。这一点困惑很久。

二丶总体分析

1.题目没有刚开始做的时候有阻力。很快就100了。很高兴在这道题里掌握了正则表达式的基本规则,感受到了正则表达式的方便与强大。

2.主类过长,应当提取。可将输入的错误分析单独提取成一个类,输出类单独提取成一个类。后续的菜单计价程序-5做到了。

三丶类设计:

菜品类:

 1 class Dish {
 2     String name;//菜品名称
 3     boolean isSpecial = false;//是不是特色菜
 4     int unit_price;    //单价
 5     boolean exist = true;
 6     //int num;
 7 
 8     int getPrice(int portion) {
 9         int peic = 0;
10         if (portion == 1) {
11             peic = unit_price ;
12         } else if (portion == 2) {
13             peic = Math.round((float) (unit_price * 1.5)) ;
14         } else if (portion == 3) {
15             peic = (unit_price * 2) ;
16         }
17         return peic;//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
18     }
19 }

菜单类:

class Menu {
    Dish[] dishs = new Dish[10];//菜品数组,保存所有菜品信息
    int count = 0;
    Dish searthDish(String dishName){
        Dish temd = null;
        for(int i=count-1;i>=0;i--){
            if(dishName.equals(dishs[i].name)&&dishs[i].exist){
                temd = dishs[i];
                break;
            }
        }
        if(temd==null){
            System.out.println(dishName+" does not exist");
        }
        return temd;
    }//根据菜名在菜谱中查找菜品信息,返回Dish对象。
    Dish addDish(String dishName,int unit_price){

        Dish dh = new Dish();
        dh.name = dishName;
        dh.unit_price = unit_price;
        count++;
        if(unit_price<1||unit_price>300) {
            dh.exist = false;
            System.out.println(dishName+" price out of range "+unit_price);
        }
        return dh;
    }//添加一道菜品信息
}

订单记录类:

class Record {
    int orderNum;//序号\
    Dish d = new Dish();//菜品\
    int num = 0;
    int portion;//份额(1/2/3代表小/中/大份)\
    boolean noNeed = true;
    int exist = 1;
    float realdiscnt = 1;
    //int asum = 0;
    boolean isTaked = false;//是否是给别人点的
    int getPrice(){
        return Math.round(d.getPrice(portion)*num);
    }//计价,计算本条记录的价格\
    float Checkportion(float discnt){//数量输入检查,计算价格
         if(portion>3||portion<1) {
             System.out.println(orderNum + " portion out of range " + portion);
             exist = 0;
             noNeed = false;
         }else if(num<1||num>15) {
            System.out.println(orderNum + " num out of range" +" "+num);
            exist = 0;
            noNeed = false;
        }else{
            if(d.isSpecial)
                if(discnt==0.8F||discnt==0.6F)
                    realdiscnt = 0.7F;
            if(exist==1&&!isTaked) {
                System.out.println(orderNum + " " + d.name + " " + getPrice());
            }
        }
        return realdiscnt;
    }
    boolean Checkahead(){
        if(num<1||num>15) {
            return false;
        }else if(portion>3||portion<1){
            return false;
        }
        return true;
    }
}

订单类:

class Order {
    Record[] records = new Record[100];//保存订单上每一道的记录
    int count = 0;//订单数量
    int Ordercnt = 0;//点菜点了分数
    int getTotalPrice(){
        int sum=0;
        for(int i=0;i<count;i++){
            if(records[i].exist==0)
                continue;
            sum=sum+records[i].getPrice();
        }
        return sum;
    }//计算订单的总价
    Record addARecord(boolean istosum,int orderNum,String dishName,int portion,int num){
        boolean flag = true;
        for(int i=0;i<count;i++){//不能点份额一样的菜
            if(records[i].d.name.equals(dishName)&&records[i].portion==portion)
                flag = false;
        }
        count++;
        records[count] = new Record();
        records[count].d.name = dishName;
        records[count].orderNum = orderNum;
        records[count].portion = portion;
        records[count].num = num;
        records[count].noNeed = flag;
        return records[count];
    }//添加一条菜品信息到订单中。
    Record TakeOdfor(int mynum,Table[] table, int AnotherNum, int orderNum, String dishName, int portion, int num){
        boolean flag = false;
        for(Table t : table) {
            if(t==null)
                continue;
            if (t.tableNum != mynum&&t.tableNum==AnotherNum) {
                flag = true;
                break;
            }
        }
        if(flag == true) {
            count++;
            records[count] = new Record();
            records[count].d.name = dishName;
            records[count].orderNum = orderNum;
            records[count].portion = portion;
            records[count].num = num;
            return records[count];
        }else{
            System.out.println("Table number :" + AnotherNum + " does not exist");
            return null;
        }

    }

    void delARecordByOrderNum(int orderNum){
        if(orderNum>count||orderNum<=0){
            System.out.println("delete error;");
            return;
        }else {
            if(records[orderNum-1].exist==0) {
                System.out.println("deduplication "+orderNum);
                return;
            }
            records[orderNum-1].exist = 0;
        }
    }//根据序号删除一条记录
}

桌类:

class Table {
    int tableNum;
    boolean inputIsvalid = false;
    boolean istoSum = false;//判断是否要合并计算
    String tableDtime;
    int year,month,day,week,hh,mm,ss;
    int sum = 0;//一桌价格 ;
    int primesum = 0;
    Order odt = new Order();
    float discnt = -1;
    float rediscnt = 1;
    void Gettottalprice(){
        if(discnt>0){
            primesum = odt.getTotalPrice();
            if(primesum==0)
                sum=0;
            System.out.println("table " + tableNum + ": " +primesum+" "+sum);
        }
    }
    void AheadProcess(String tableDtime){
        this.tableDtime = tableDtime;
        processTime();
        discount();
    }


    void processTime(){//处理时间
        String[] temp = tableDtime.split(" ");
        tableNum = Integer.parseInt(temp[1]);
        String[] temp1 = temp[2].split("/");
        String[] temp2 = temp[3].split("/");

        year = Integer.parseInt(temp1[0]);
        month = Integer.parseInt(temp1[1]);
        day = Integer.parseInt(temp1[2]);

        Calendar c = Calendar.getInstance();
        c.set(year, (month-1), day);
        week = c.get(Calendar.DAY_OF_WEEK);
        if(week==1)
            week = 7;
        else
            week--;
        hh = Integer.parseInt(temp2[0]);
        mm = Integer.parseInt(temp2[1]);
        ss = Integer.parseInt(temp2[2]);

    }
    boolean DateisValid(){
        if(year<1000)
            return false;
        if(month<1||month>12)
            return false;
        YearMonth mytime = YearMonth.of(year,month);
        if(!mytime.isValidDay(day))
            return false;
        return true;
    }
    boolean Daterande(){
        boolean flag = true;
        if(year<2022||year>2023) {
            flag = false;
        }
        if(flag==false){
            System.out.println("not a valid time period");
            inputIsvalid = false;
        }
        return flag;

    }
    void discount(){

        if(week>=1&&week<=5)
        {
            if(hh>=17&&hh<20)
                discnt=0.8F;
            else if(hh==20&&mm<30)
                discnt=0.8F;
            else if(hh==20&&mm==30&&ss==0)
                discnt=0.8F;
            else if(hh>=11&&hh<=13||hh==10&&mm>=30)
                discnt=0.6F;
            else if(hh==14&&mm<30)
                discnt=0.6F;
            else if(hh==14&&mm==30&&ss==0)
                discnt=0.6F;
        }
        else
        {
            if(hh>=10&&hh<=20)
                discnt= 1.0F;
            else if(hh==9&&mm>=30)
                discnt= 1.0F;
            else if(hh==21&&mm<30||hh==21&&mm==30&&ss==0)
                discnt= 1.0F;
        }
    }
    boolean CheckSameTime(Table table){
        if(tableNum!=table.tableNum)
            return false;
        if(week==table.week&&week>=1&&week<=5){
            if(discnt==table.discnt)
                return true;
            else
                return false;
        }else if(week==table.week&&week==6||week==7){
            int subtime = hh*3600+mm*60+ss-table.hh*3600-table.mm*60-ss;
            if(Math.abs(subtime)<3600)
                return true;
            else
                return false;
        }
        return false;
    }
}

主类:

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Menu mu = new Menu();
        Table[] tablemes = new Table[57];//桌号1-55;
        int j = 0;//菜单数
        int l = 0;//订单数
        int k = 0;//代点菜数
        int z = 1;//遍历桌
        int cntTab = 0;//桌号
        String[] temp;//字符串存储
        int tempordernum = 0;//临时存上一个有效序号
        String input;
        String regex = "[\\u4e00-\\u9fa5]{0,}[ ]\\d{0,}";//非特色菜菜谱
        String regex1 = "[\\u4e00-\\u9fa5]{0,}[ ]\\d{0,}[ ][T]";//特色菜谱
        String regex2 = "\\d[ ](delete)";//删除情况
        String regex3 = "(table)[ ]([1-4][0-9]*|[5][0-5]*)[ ]\\d{4}[/]\\d{1,}[/]\\d{1,2}[ ]([0-1]?[0-9]|2[0-3])/([0-5][0-9])/([0-5][0-9])";//添桌子的情况
        String regex4 = "([1-9]|[1-9][0-9]*)[ ][\\u4e00-\\u9fa5]{1,}[ ]\\d{1,}[ ]\\d{1,}";//点菜记录的添加;
        String regex5 = "([1-4][0-9]*|[5][0-5]*|[1-9])[ ]\\d[ ][\\u4e00-\\u9fa5]{1,}[ ][123][ ]([1][0-5]|[1-9])";//代点菜
        while (true) {
            int count = 0;//空格数量;
            input = sc.nextLine();//获取输入,每行
            if(input.equals("end"))
                break;
            if(input.equals("")) {
                System.out.println("wrong format");
                continue;
            }
            temp = input.split(" ");//分割
            /*
             * 判断输入的类型*/
            count = temp.length;
          /*  if(input.contains("/")||temp[0].equals("table")) {
                cntTab++;
                tablemes[cntTab] = new Table();
            }*/
            if(count==2){//非特色菜菜谱,或者删除菜;
                if(input.matches(regex2)){//删除的情况桌子有效才删除
                    if(tablemes[cntTab].inputIsvalid){
                        tablemes[cntTab].odt.delARecordByOrderNum(Integer.parseInt(temp[0]));//删除

                    }
                }else if(input.matches(regex)){//非特色菜谱添加
                    if (cntTab == 0) {
                        mu.dishs[j] = new Dish();
                        mu.dishs[j] = mu.addDish(temp[0], Integer.parseInt(temp[1]));
                        j++;
                        continue;
                    } else if(tablemes[cntTab].inputIsvalid){
                        System.out.println("invalid dish");
                    }

                }else {
                    System.out.println("wrong format");
                    continue;
                }
            }else if(count==3){//特色菜菜谱
                if(input.matches(regex1)) {
                    if(cntTab!=0) {
                        if(tablemes[cntTab].inputIsvalid)
                            System.out.println("invalid dish");
                        else
                            continue;
                    }

                    mu.dishs[j] = new Dish();
                    mu.dishs[j] = mu.addDish(temp[0], Integer.parseInt(temp[1]));
                    mu.dishs[j].isSpecial = true;//是特色菜
                    j++;
                    continue;
                }else
                    System.out.println("wrong format");
            }
            //桌子的添加,点菜记录的添加,
            else if(temp[0].equals("table")||input.contains("/")) {
                cntTab++;
                tablemes[cntTab] = new Table();
                if(temp[0].equals("table")){
                    if(input.matches(regex3))
                        tablemes[cntTab].AheadProcess(input);
                    tablemes[cntTab].inputIsvalid=true;
                    if (input.matches(regex3)) {
                        if(tablemes[cntTab].inputIsvalid&&!tablemes[cntTab].DateisValid()){//检查日期
                            System.out.println(tablemes[cntTab].tableNum+" date error");
                            tablemes[cntTab].inputIsvalid=false;
                            continue;
                        }
                        if(tablemes[cntTab].inputIsvalid&&!tablemes[cntTab].Daterande()){
                            tablemes[cntTab].inputIsvalid=false;
                            continue;
                        }
                        for(int i =1;i<cntTab;i++){
                            if(z!=cntTab){
                                if(tablemes[i].inputIsvalid&&tablemes[cntTab].CheckSameTime(tablemes[i])) {//同一时间段
                                    tablemes[cntTab].istoSum = true;
                                    tempordernum = 0;
                                }else{
                                    l=0;
                                    tempordernum = 0;
                                }
                            }
                        }
                        if(tablemes[cntTab].discnt>0)
                            System.out.println("table " + tablemes[cntTab].tableNum + ": ");
                        else {
                            System.out.println("table " + tablemes[cntTab].tableNum + " out of opening hours");
                            tablemes[cntTab].inputIsvalid = false;
                        }
                        continue;
                    } else {//第一个是table但不合法
                        tablemes[cntTab].inputIsvalid = false;
                        if(!temp[1].equals("")&&temp[1].charAt(0)>='1'&&temp[1].charAt(0)<='9'&&!temp[1].contains("/")){
                            if(Integer.parseInt(temp[1])<1||Integer.parseInt(temp[1])>55){
                                System.out.println(Integer.parseInt(temp[1])+" table num out of range");
                                continue;
                            }
                        }
                    }
                }
                System.out.println("wrong format");
                tablemes[cntTab].inputIsvalid = false;
                if(!temp[0].equals("table"))
                    tablemes[cntTab].istoSum = true;
                continue;

            }else if(count==4){
                if(input.matches(regex4)&&(tablemes[cntTab].inputIsvalid||tablemes[cntTab].istoSum)){//点菜记录
                    Dish tem;
                    int aheadcntTab = cntTab-1;
                    boolean tem1;
                    if(!tablemes[cntTab].istoSum) {//不需要合并
                        tablemes[cntTab].odt.records[l] = new Record();
                        tablemes[cntTab].odt.records[l] = tablemes[cntTab].odt.addARecord(tablemes[cntTab].istoSum,Integer.parseInt(temp[0]), temp[1], Integer.parseInt(temp[2]), Integer.parseInt(temp[3]));
                        if(l-1>=0){//判断记录序号是否递增
                            if(Integer.parseInt(temp[0])<=tempordernum) {
                                System.out.println("record serial number sequence error");
                                tablemes[cntTab].odt.records[l].exist=0;
                            }else
                            if(tablemes[cntTab].odt.records[l]!=null&&tablemes[cntTab].odt.records[l].noNeed)
                                tempordernum = tablemes[cntTab].odt.records[l].orderNum;
                        }else if(l==0){//特殊情况
                            if(tablemes[cntTab].odt.records[l].Checkahead())
                                tempordernum = tablemes[cntTab].odt.records[l].orderNum;
                        }
                        if((tem = mu.searthDish(temp[1]))!=null){
                            float temdiscnt;//临时储存折扣
                            tablemes[cntTab].odt.records[l].d = tem;
                            temdiscnt = tablemes[cntTab].odt.records[l].Checkportion(tablemes[cntTab].discnt);
                            if(temdiscnt!=1F)
                                tablemes[cntTab].rediscnt = temdiscnt;//最终折扣
                            if(tablemes[cntTab].odt.records[l].exist==1&&tablemes[cntTab].odt.records[l].noNeed) {
                                if(tablemes[cntTab].rediscnt!=1) {
                                    tablemes[cntTab].sum += Math.round(tablemes[cntTab].odt.records[l].getPrice() * tablemes[cntTab].rediscnt);
                                    tablemes[cntTab].rediscnt=1.0F;
                                }
                                else
                                    tablemes[cntTab].sum += Math.round(tablemes[cntTab].odt.records[l].getPrice() * tablemes[cntTab].discnt);
                            }
                        }

                        if(!tablemes[cntTab].istoSum){//如果份额名字未重复;
                            l++;
                        }
                    } else{//需要合并计算
                        tablemes[aheadcntTab].odt.records[l] = new Record();
                        tablemes[aheadcntTab].odt.records[l] = tablemes[aheadcntTab].odt.addARecord(tablemes[cntTab].istoSum,Integer.parseInt(temp[0]), temp[1], Integer.parseInt(temp[2]), Integer.parseInt(temp[3]));
                        if(l-1>=0){//判断记录序号是否递增
                            if(Integer.parseInt(temp[0])<=tempordernum) {
                                System.out.println("record serial number sequence error");
                                tablemes[aheadcntTab].odt.records[l].exist=0;
                            }else
                            if(tablemes[aheadcntTab].odt.records[l]!=null&&tablemes[aheadcntTab].odt.records[l].noNeed)
                                tempordernum = tablemes[aheadcntTab].odt.records[l].orderNum;
                        }
                        if((tem = mu.searthDish(temp[1]))!=null){
                            float temdiscnt;//临时储存折扣
                            tablemes[aheadcntTab].odt.records[l].d = tem;
                            temdiscnt = tablemes[aheadcntTab].odt.records[l].Checkportion(tablemes[aheadcntTab].discnt);//计算折扣
                            if(temdiscnt==0.7F)//如果需要调整
                                tablemes[aheadcntTab].rediscnt = temdiscnt;//最终折扣//更改折扣
                            if(tablemes[aheadcntTab].odt.records[l].exist==1) {//必须存在
                                if(tablemes[aheadcntTab].rediscnt!=1) {
                                    tablemes[aheadcntTab].sum += Math.round(tablemes[aheadcntTab].odt.records[l].getPrice() * tablemes[aheadcntTab].rediscnt);
                                    tablemes[aheadcntTab].rediscnt=1.0F;
                                }
                                else
                                    tablemes[aheadcntTab].sum += Math.round(tablemes[aheadcntTab].odt.records[l].getPrice() * tablemes[cntTab].discnt)-1;
                            }
                        }

                        l++;
                    }

                }else {
                    if(tablemes[cntTab]==null) {
                        System.out.println("wrong format");
                        continue;
                    }
                    if(tablemes[cntTab]!=null&&tablemes[cntTab].inputIsvalid)
                        System.out.println("wrong format");
                    continue;
                }
            }
            else if(count==5&&tablemes[cntTab].inputIsvalid){//代点菜
                int temSum = 0;
                if(input.matches(regex5)){
                    int AnothNum = Integer.parseInt(temp[0]);
                    tablemes[cntTab].odt.records[l] = tablemes[cntTab].odt.TakeOdfor(tablemes[cntTab].tableNum,tablemes,AnothNum,Integer.parseInt(temp[1]),temp[2],Integer.parseInt(temp[3]),Integer.parseInt(temp[4]));
                    Dish tem;
                    if((tem = mu.searthDish(temp[2]))!=null&&tablemes[cntTab].odt.records[l]!=null){
                        tablemes[cntTab].odt.records[l].isTaked = true;//给别人diade
                        float temdiscnt;//临时储存折扣
                        tablemes[cntTab].odt.records[l].d = tem;
                        temdiscnt = tablemes[cntTab].odt.records[l].Checkportion(tablemes[cntTab].discnt);
                        if(temdiscnt!=1F)
                            tablemes[cntTab].rediscnt = temdiscnt;//最终折扣
                        if(tablemes[cntTab].odt.records[l].exist==1&&tablemes[cntTab].odt.records[l].noNeed) {
                            if(tablemes[cntTab].rediscnt!=1) {
                                tablemes[cntTab].sum += Math.round((temSum=tablemes[cntTab].odt.records[l].getPrice()) * tablemes[cntTab].rediscnt);
                                tablemes[cntTab].rediscnt=1.0F;
                            }
                            else
                                tablemes[cntTab].sum +=  Math.round((temSum =tablemes[cntTab].odt.records[l].getPrice()) * tablemes[cntTab].discnt);
                        }
                        System.out.println(temp[1] + " table " + tablemes[cntTab].tableNum + " pay for table " + temp[0] + " " + temSum);
                    }
                    if(tablemes[cntTab].odt.records[l]!=null)
                        l++;
                }else
                    System.out.println("wrong format");

            }else{
                if(tablemes[cntTab].inputIsvalid)
                    System.out.println("wrong format");
            }
        }
        for(int i=1;i<=cntTab;i++){
            if(tablemes[i].inputIsvalid&&!tablemes[i].istoSum)
                tablemes[i].Gettottalprice();
        }

    }
}

菜单计价程序-5

一丶错误分析:

1.该题做的时候没什么阻力,主要的错误原因是因为这一次我改变了逻辑。之前是输入一条,判断一条,输出一条。这一次改成了集中输入输出。错误集中在输出顺序上,集中输入输出导致顺序没法精准判断。

2.个别偏刁的测试点最后才想到。

二丶总体分析:

1.这道题的主类简洁,输入输出分别提取到了两个类。再写的时候思路很清晰。是我最满意的一次题目,。

2.但没有很好的用到上课学的知识,比如新概念菜系,上课老师提出的运用继承的概念就是我没想到的。错误输出可以用try coath环绕。

三丶类设计:

1.菜单类:

class Dish {
    String name;//菜品名称
    boolean exist;//是否存在
    boolean isSpecial;//是不是特色菜
    String style = "";//菜系

    Dish(){
        isSpecial = false;
        exist = true;
    }
    int unit_price;//单价
    int getPrice(int portion) {
        int peic = 0;
        if (portion == 1) {
            peic = unit_price ;
        } else if (portion == 2) {
            peic = (int) Math.round((unit_price * 1.5));
        } else if (portion == 3) {
            peic = (unit_price * 2) ;
        }
        return peic;//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
    }
}

菜品类:

class Menu {
    Dish[] dishs = new Dish[50];//菜品数组,保存所有菜品信息
    boolean isSp = false;
    int count = 0;
    Dish searthDish(String dishName){
        Dish temd = null;
        for(int i=count-1;i>=0;i--){
            if(dishName.equals(dishs[i].name)&&dishs[i].exist){
                temd = dishs[i];
                break;
            }
        }
        /*if(temd==null){
            System.out.println(dishName+" does not exist");
        }*/
        return temd;
    }//根据菜名在菜谱中查找菜品信息,返回Dish对象。
    void addDish(String dishName,int unit_price){

        Dish dh = new Dish();
        dh.name = dishName;
        dh.unit_price = unit_price;
        dishs[count] = dh;
        count++;
    }//添加一道菜品信息
    void addDish(String dishName,String dishstyle,int unit_price){
        Dish dh = new Dish();
        dh.isSpecial = true;
        dh.name = dishName;
        dh.unit_price = unit_price;
        dh.style = dishstyle;
        dishs[count] = dh;
        count++;
    }

}

订单记录类:

class Menu {
    Dish[] dishs = new Dish[50];//菜品数组,保存所有菜品信息
    boolean isSp = false;
    int count = 0;
    Dish searthDish(String dishName){
        Dish temd = null;
        for(int i=count-1;i>=0;i--){
            if(dishName.equals(dishs[i].name)&&dishs[i].exist){
                temd = dishs[i];
                break;
            }
        }
        /*if(temd==null){
            System.out.println(dishName+" does not exist");
        }*/
        return temd;
    }//根据菜名在菜谱中查找菜品信息,返回Dish对象。
    void addDish(String dishName,int unit_price){

        Dish dh = new Dish();
        dh.name = dishName;
        dh.unit_price = unit_price;
        dishs[count] = dh;
        count++;
    }//添加一道菜品信息
    void addDish(String dishName,String dishstyle,int unit_price){
        Dish dh = new Dish();
        dh.isSpecial = true;
        dh.name = dishName;
        dh.unit_price = unit_price;
        dh.style = dishstyle;
        dishs[count] = dh;
        count++;
    }

}

订单类:

class Order {
    ArrayList<Record> records = new ArrayList<>();
    HashMap<Integer,String> ResultFor = new HashMap<>();
    HashMap<Integer,String> deletemes = new HashMap<>();
    int count = 0;//订单数量

    int getTotalPrice(){//总没算折扣
        int sum=0;
        for(Record Re:records){
            if(Re.exist==0||Re.isMyOrder==2)
                continue;
            sum=sum+ Re.getPrice();
        }
        return sum;
    }//计算订单的总价
    void addARecord(int orderNum,String dishName,int portion,int num){
        records.add(count, new Record());
        records.get(count).d.name = dishName;
        records.get(count).orderNum = orderNum;
        records.get(count).portion = portion;
        records.get(count).num = num;
        count++;
    }//添加一条菜品信息到订单中。
    void addARecord(int orderNum,String dishName,int styleNum,int portion,int num){
        records.add(count, new Record());
        records.get(count).d.name = dishName;
        records.get(count).styleNum = styleNum;
        records.get(count).orderNum = orderNum;
        records.get(count).portion = portion;
        records.get(count).num = num;
        count++;
    }
    void TakeOdFor(int AnotherNum,int orderNum,String dishName,int styleNum,int portion,int num,ArrayList<Table> ta,Menu mu,int MyTabNum){
        int flag = 0;//是否有这个号
        for(Table T:ta){
            if(T.tableNum==AnotherNum){
                flag = 1;
                T.odt.addARecord(orderNum,dishName,styleNum,portion,num);
                Dish tem = mu.searthDish(dishName);
                if(tem!=null)
                    T.odt.records.get(T.odt.count-1).d = tem;

                T.odt.records.get(T.odt.count-1).d.isSpecial = true;
                T.odt.records.get(T.odt.count-1).noNeed = false;//上一个不需要打印
                T.odt.records.get(T.odt.count-1).isMyOrder = 2;//不算钱 算口味
                ResultFor.put(records.size()+1,orderNum+" table "+MyTabNum+" pay for table "+AnotherNum+" "+T.odt.records.get(T.odt.count-1).getPrice());
            }
        }
        if(flag==0&&ResultFor.containsKey(orderNum)){
            ResultFor.put(orderNum,ResultFor.get(orderNum)+"\nwrong format");
        }
        for(Table T:ta){
            if(T.tableNum==MyTabNum){
                T.TakeorderFor.add(AnotherNum);
            }
        }
        addARecord(orderNum,dishName,styleNum,portion,num);//给自己也点
        records.get(count-1).noNeed = false;//不用打印出来
        records.get(count-1).isMyOrder = 1;//算钱不算口味
    }
    void delARecordByOrderNum(int orderNum,ArrayList<Table> Tab){
        boolean isMyOrder;
        for(int i = 0;i<count;i++){
            if(records.get(i).orderNum==orderNum&&records.get(i).exist==1) {//找到要删除的号
                if(records.get(i).isMyOrder==1){//如果是给别人点的
                    for (Table T:Tab) {//遍历找一下被点的桌
                        for(Record R:T.odt.records){//遍历该桌的号
                            if(R.orderNum==orderNum&&R.isMyOrder==2){//如果满足条件
                                //T.odt.records.remove(R);//删除
                                R.exist=0;
                                // ResultFor.remove(orderNum);
                                R.deleted = false;
                            }
                        }
                    }
                }
                records.get(i).exist = 0;
                records.get(i).deleted = false;
                return;
            }
        }
        if(!deletemes.containsKey(records.size()+1))
            deletemes.put(records.size()+1,"delete error;");
        else
            deletemes.put(records.size()+2,"delete error;");
    }//根据序号删除一条记录
}

桌类:

class Table {
    int[] st = new int[3];//辣 酸 甜
    int[] stSum = new int[3];
    ArrayList<Integer> TakeorderFor = new ArrayList<>();
    int tableNum;//桌号
    String name;//点菜人姓名
    String telephone;//电话号码
    String tableDtime;//桌子点菜日期
    int year,month,day,week,hh,mm,ss;//年月日
    int sum = 0;//单独特色菜价格,没打折的
    Order odt = new Order();//桌子的订单
    float discnt = -1;//折扣
    Table(int tableNum,String name,String telephone,String tableDtime){
        this.tableNum = tableNum;
        this.name = name;
        this.telephone = telephone;
        this.tableDtime = tableDtime;
        processTime();
        discount();
    }
    int Gettottalprice(){//计算折扣价格
        for(Record R: odt.records){
            if(R.exist==1&&(R.isMyOrder==1||R.isMyOrder==0)) {
                if (R.d.isSpecial) {
                    if (discnt == 0.8F || discnt == 0.6F) {
                        sum += Math.round(R.getPrice() * 0.7F);
                    } else {
                        sum += Math.round(R.getPrice() * 1.0F);
                    }
                } else {
                    sum += Math.round(R.getPrice() * discnt);
                }
            }
        }
        return sum;
    }
    void processTime(){//处理时间
        String[] temp = tableDtime.split(" ");

        String[] temp1 = temp[0].split("/");
        String[] temp2 = temp[1].split("/");

        year = Integer.parseInt(temp1[0]);
        month = Integer.parseInt(temp1[1]);
        day = Integer.parseInt(temp1[2]);

        Calendar c = Calendar.getInstance();
        c.set(year, (month-1), day);
        week = c.get(Calendar.DAY_OF_WEEK);
        if(week==1)
            week = 7;
        else
            week--;
        hh = Integer.parseInt(temp2[0]);
        mm = Integer.parseInt(temp2[1]);
        ss = Integer.parseInt(temp2[2]);

    }

    void discount(){

        if(week>=1&&week<=5)
        {
            if(hh>=17&&hh<20)
                discnt=0.8F;
            else if(hh==20&&mm<30)
                discnt=0.8F;
            else if(hh==20&&mm==30&&ss==0)
                discnt=0.8F;
            else if(hh>=11&&hh<=13||hh==10&&mm>=30)
                discnt=0.6F;
            else if(hh==14&&mm<30)
                discnt=0.6F;
            else if(hh==14&&mm==30&&ss==0)
                discnt=0.6F;
        }
        else
        {
            if(hh>=10&&hh<=20)
                discnt= 1.0F;
            else if(hh==9&&mm>=30)
                discnt= 1.0F;
            else if(hh==21&&mm<30||hh==21&&mm==30&&ss==0)
                discnt= 1.0F;
        }
    }
}

输入类:

class InputProcess{
    /*
        特色菜
    */
    String rg0 = "[\\u4e00-\\u9fa5]{1,}[ ][\\u4e00-\\u9fa5]{2}[ ]\\d{1,}[ ][T]";
    /*
    普通菜
     */
    String rg1 = "[\\u4e00-\\u9fa5]{1,}[ ]\\d{1,}";
    /*
    桌子点菜
     */
    String rg2 = "(table)[ ]\\d{1,}[ ](:)[ ][a-z]{1,10}[ ](136|135|133|189|181|180)\\d{8}[ ]\\d{4}[/]\\d{1,}[/]\\d{1,2}[ ]([0-1]?[0-9]|2[0-3])/([0-5][0-9])/([0-5][0-9])";
    /*
    点特色菜,口味度
     */
    String rg3 = "\\d{1,}[ ][\\u4e00-\\u9fa5]{0,}[ ]\\d{1,}[ ]\\d{1,}[ ]\\d{1,}";
    /*
    非特色菜点菜
     */
    String rg4 = "\\d{1,}[ ][\\u4e00-\\u9fa5]{0,}[ ]\\d{1,}[ ]\\d{1,}";
    /*
    删除
     */
    String rg5 = "\\d{1,}[ ](delete)";
    /*
    结束
     */
    String rg6 = "(end)";
    /*
    代点菜
     */
    String rg7 = "([1-4][0-9]*|[5][0-5]*|[1-9])[ ]\\d[ ][\\u4e00-\\u9fa5]{1,}[ ]\\d{1,}[ ]([1][0-5]|[1-9])[ ]\\d{1,}";
    int Tabcount = 0;//桌号
    int RecoCount = 0;//订单数量

    void process(String input,AheadProcess ap){
        String tp[] = input.split(" ");

        if(input.matches(rg6)){
            ap.IsEnd = false;

        }else if (input.matches(rg0)) {//特色菜:麻婆豆腐 川菜 12 T
            if(tp[1].equals("川菜")||tp[1].equals("晋菜")||tp[1].equals("浙菜"))
                ap.menu.addDish(tp[0],tp[1],Integer.parseInt(tp[2]));
            else
                System.out.println("wrong format");
        } else if (input.matches(rg1)) {//普通菜 油淋生菜 9
            ap.menu.addDish(tp[0],Integer.parseInt(tp[1]));

        } else if (input.matches(rg2)) {//添桌子 table 1 : tom 13605054400 2023/5/1 21/30/00
            ap.tables.add(Tabcount, new Table(Integer.parseInt(tp[1]), tp[3], tp[4], tp[5] + " " + tp[6]));
            ap.tname.add(tp[3]);
            RecoCount = 0;
            Tabcount++;
        } else if (input.matches(rg3)) {//点特色菜,带口味度:1 麻婆豆腐 2 1 2
            if(ap.tables.size()==0||Tabcount!=ap.tables.size())
                return;
            ap.tables.get(Tabcount-1).odt.addARecord(Integer.parseInt(tp[0]),tp[1],Integer.parseInt(tp[2]),Integer.parseInt(tp[3]),Integer.parseInt(tp[4]));
            Dish temp = ap.menu.searthDish(tp[1]);
            if(temp!=null){
                ap.tables.get(Tabcount-1).odt.records.get(RecoCount).d = temp;
                ap.tables.get(Tabcount-1).odt.records.get(RecoCount).styleNum = Integer.parseInt(tp[2]);

            }else {
                ap.tables.get(Tabcount-1).odt.records.get(RecoCount).exist = 0;
            }
            RecoCount++;

        } else if (input.matches(rg4)) {//非特色菜:2 油淋生菜 2 1
            if(ap.tables.size()==0||Tabcount!=ap.tables.size())
                return;
            ap.tables.get(Tabcount-1).odt.addARecord(Integer.parseInt(tp[0]),tp[1],Integer.parseInt(tp[2]),Integer.parseInt(tp[3]));
            Dish temp = ap.menu.searthDish(tp[1]);
            if(temp!=null){
                ap.tables.get(Tabcount-1).odt.records.get(RecoCount).d = temp;

            }else {
                ap.tables.get(Tabcount-1).odt.records.get(RecoCount).exist = 0;
            }

            RecoCount++;

        } else if (input.matches(rg5)) {//删除:1 delete
            if(ap.tables.size()==0)
                return;
            ap.tables.get(Tabcount-1).odt.delARecordByOrderNum(Integer.parseInt(tp[0]),ap.tables);

        } else if (input.matches(rg7)) {//代点菜 1 1 醋浇羊肉 0 1 2
            if(ap.tables.size()==0||Tabcount!=ap.tables.size())
                return;
            ap.tables.get(Tabcount-1).odt.TakeOdFor(Integer.parseInt(tp[0]),Integer.parseInt(tp[1]),tp[2],Integer.parseInt(tp[3]),Integer.parseInt(tp[4]),Integer.parseInt(tp[5]),ap.tables,ap.menu,ap.tables.get(Tabcount-1).tableNum);
            Dish temp = ap.menu.searthDish(tp[2]);
            if(temp!=null){
                ap.tables.get(Tabcount-1).odt.records.get(RecoCount).d = temp;
                ap.tables.get(Tabcount-1).odt.records.get(RecoCount).styleNum = Integer.parseInt(tp[3]);

            }else {
                ap.tables.get(Tabcount-1).odt.records.get(RecoCount).exist = 0;
            }
            RecoCount++;

        }else{
            if(ap.tables.size()==0)
                System.out.println("wrong format");
            else{
                if(tp[0].equals("table")){
                    System.out.println("wrong format");
                    Tabcount++;
                }else{//桌子里有错误
                    ap.WrongF.put(RecoCount+1,"wrong format");//第Recount个有错误
                }
            }

        }
    }
}

输出类:

class OutputProcess {
    HashMap<Integer, String> Chuan = new HashMap<Integer, String>();
    HashMap<Integer, String> Ji = new HashMap<Integer, String>();
    HashMap<Integer, String> Zhe = new HashMap<Integer, String>();
    HashMap<String, HashMap> dishstyles = new HashMap<String, HashMap>();

    OutputProcess() {

        Chuan.put(0, "不辣");
        Chuan.put(1, "微辣");
        Chuan.put(2, "稍辣");
        Chuan.put(3, "辣");
        Chuan.put(4, "很辣");
        Chuan.put(5, "爆辣");
        Ji.put(0, "不酸");
        Ji.put(1, "微酸");
        Ji.put(2, "稍酸");
        Ji.put(3, "酸");
        Ji.put(4, "很酸");
        Zhe.put(0, "不甜");
        Zhe.put(1, "微甜");
        Zhe.put(2, "稍甜");
        Zhe.put(3, "甜");
        dishstyles.put("川菜", Chuan);
        dishstyles.put("晋菜", Ji);
        dishstyles.put("浙菜", Zhe);
    }

    void outprocess(AheadProcess ap) {
        int Tcount = 0;
        for (Table T : ap.tables) {
            Tcount++;
            int Recount = 0;//订单数
            if (T.discnt < 0) {
                System.out.println("table " + T.tableNum + " out of opening hours");
                continue;
            }
            System.out.println("table " + T.tableNum + ": ");//桌号输出
            for (Record R : T.odt.records) {//每条记录钱;
                Recount++;
                for(int key:T.odt.deletemes.keySet()){
                    if(Recount>key||Recount+1>key){
                        System.out.println(T.odt.deletemes.get(key));
                        T.odt.deletemes.remove(key);
                        break;
                    }
                }
                for(int key:ap.WrongF.keySet()){
                    if(Recount>key||Recount+1>key){
                        System.out.println(ap.WrongF.get(key));
                        ap.WrongF.remove(key);
                        break;
                    }
                }
                for(int key:T.odt.ResultFor.keySet()){
                    if(Recount>key||Recount+1>key) {
                        System.out.println(T.odt.ResultFor.get(key));
                        T.odt.ResultFor.remove(key);
                        break;
                    }
                }
                if (R.exist == 0&&R.deleted) {
                    System.out.println(R.d.name + " does not exist");
                    continue;
                } else if((R.isMyOrder==2||R.isMyOrder==0)&&R.deleted){
                    if (R.d.style.equals("川菜")) {
                        if (R.styleNum < 0 || R.styleNum > 5) {
                            System.out.println("spicy num out of range :" + R.styleNum);
                            R.exist = 0;
                            continue;
                        }
                        T.st[0]+=R.num;
                        T.stSum[0] += R.styleNum*R.num;
                    } else if (R.d.style.equals("晋菜")) {
                        if (R.styleNum < 0 || R.styleNum > 4) {
                            System.out.println("acidity num out of range :" + R.styleNum);
                            R.exist = 0;
                            continue;
                        }
                        T.st[1]+=R.num;
                        T.stSum[1] += R.styleNum*R.num;
                    } else if (R.d.style.equals("浙菜")) {
                        if (R.styleNum < 0 || R.styleNum > 3) {
                            System.out.println("sweetness num out of range :" + R.styleNum);
                            R.exist = 0;
                            continue;
                        }
                        T.st[2]+=R.num;
                        T.stSum[2] += R.styleNum*R.num;
                    }
                }
                if(R.noNeed)
                    System.out.println(R.orderNum + " " + R.d.name + " " + R.getPrice());
            }
            for (int key : T.odt.deletemes.keySet()) {
                System.out.println(T.odt.deletemes.get(key));
            }
            for (int key : T.odt.ResultFor.keySet()) {
                System.out.println(T.odt.deletemes.get(key));
            }
        }
        for (Table T : ap.tables) {
            if (T.discnt < 0)
                continue;
            String tem0 = "";
            String tem1 = "";
            String tem2 = "";
            if (T.st[0] != 0) {
                tem0 = " 川菜 " + T.st[0] + " " + Chuan.get(Math.round((1.0F)*T.stSum[0] / T.st[0]));
            }
            if (T.st[1] != 0) {
                tem1 = " 晋菜 " + T.st[1] + " " + Ji.get(Math.round((1.0F)*T.stSum[1] / T.st[1]));
            }
            if (T.st[2] != 0) {
                tem2 = " 浙菜 " + T.st[2] + " " + Zhe.get(Math.round((1.0F)*T.stSum[2] / T.st[2]));
            }
            if((tem1+tem0+tem2).equals(""))
                System.out.println("table " + T.tableNum + ": " + T.odt.getTotalPrice() + " " + T.Gettottalprice() + " ");
            else
                System.out.println("table " + T.tableNum + ": " + T.odt.getTotalPrice() + " " + T.Gettottalprice() + tem0 + tem1 + tem2);
        }
        for (String name:ap.tname) {
            int sum = 0;
            float discount = -1;
            String tele = "";
            for(Table T: ap.tables){
                if(name.equals(T.name)&&T.discnt>0){
                    discount = T.discnt;
                    tele = T.telephone;
                    sum += T.sum;
                }
            }
            if(discount>0)
                System.out.println(name + " "+tele+" "+sum);

        }
    }
}
class MyComparator implements Comparator<String>{//重写
    @Override
    public int compare(String o1,String o2){
        return o1.compareTo(o2);
    }
}

数据类:

class AheadProcess{
    Menu menu;
    ArrayList<Table> tables;
    Set<String> tname;
    HashMap<Integer,String> WrongF;
    boolean IsEnd = true;//主体结束标志
    AheadProcess() {//前提
        this.menu = new Menu();
        this.tables = new ArrayList<Table>();
        tname = new TreeSet<String>(new MyComparator());
        WrongF = new HashMap<>();
    }
}

期中考试分析:

一丶代码:

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        double input = sc.nextDouble();
        Circle circle = new Circle();
        circle.SetR(input);
    }
}
class Circle{
    private double r;
    public void SetR(double r){
        if(r<=0){
            System.out.print("Wrong Format");
            return;
        }
        this.r = r;
        System.out.printf("%.2f\n", Math.PI*r*r);
    }
}

第二题:

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        Rectangle rt = new Rectangle();
        float x1,y1,x2,y2;
        x1 = sc.nextFloat();
        y1 = sc.nextFloat();
        x2 = sc.nextFloat();
        y2 = sc.nextFloat();
        rt.SetPoint(x1,y1,x2,y2);
        rt.inputS();
    }
}
class Rectangle{
    private Point LeftPoint;
    private Point RightPoint;
    Rectangle(){
        LeftPoint = new Point();
        RightPoint = new Point();
    }
    private float S;
    public void SetPoint(float x1,float y1,float x2,float y2){
        LeftPoint.SetXY(x1,y1);
        RightPoint.SetXY(x2,y2);
    }
    public void inputS(){
        S = Math.abs(LeftPoint.x-RightPoint.x) * Math.abs(LeftPoint.y- RightPoint.y);
        System.out.printf("%.2f",S);
    }
}
class Point{
    float x,y;
    public void SetXY(float x,float y){
        this.x = x;
        this.y = y;
    }
}

第三题:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int choice = input.nextInt();

        switch (choice) {
            case 1://Circle
                double radiums = input.nextDouble();
                Shape circle = new Circle(radiums);
                printArea(circle);
                break;
            case 2://Rectangle
                double x1 = input.nextDouble();
                double y1 = input.nextDouble();
                double x2 = input.nextDouble();
                double y2 = input.nextDouble();

                Point leftTopPoint = new Point(x1, y1);
                Point lowerRightPoint = new Point(x2, y2);

                Rectangle rectangle = new Rectangle(leftTopPoint, lowerRightPoint);

                printArea(rectangle);
                break;
        }

    }

    private static void printArea(Shape shape) {
        System.out.printf("%.2f",shape.printArea());
    }
}
class Shape{
    double Area;
    double printArea(){
        return Area;
    }
}
class Rectangle extends Shape{
    private Point LeftPoint;
    private Point RightPoint;


    public Rectangle(Point leftTopPoint, Point lowerRightPoint) {
        LeftPoint = leftTopPoint;
        RightPoint = lowerRightPoint;
    }

    public double printArea(){
        Area = Math.abs(LeftPoint.x-RightPoint.x) * Math.abs(LeftPoint.y- RightPoint.y);
        return Area;
    }
}
class Point{
    double x,y;

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

}
class Circle extends Shape{
    private double r;
    Circle(double r){
        if(r<=0){
            System.out.print("Wrong Format");
            System.exit(0);
        }
        this.r = r;
    }
    public double printArea(){
        Area =Math.PI*r*r;
        return Area;
    }
}

第四题:

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        ArrayList<Shape> list = new ArrayList<>();

        int choice = input.nextInt();

        while(choice != 0) {
            switch(choice) {
                case 1://Circle
                    double radiums = input.nextDouble();
                    Shape circle = new Circle(radiums);
                    list.add(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);
                    list.add(rectangle);
                    break;
            }
            choice = input.nextInt();
        }

        list.sort(Comparator.naturalOrder());//正向排序

        for(int i = 0; i < list.size(); i++) {
            System.out.print(String.format("%.2f", list.get(i).printArea()) + " ");
        }
    }
}
abstract class Shape implements Comparable<Shape>{
    @Override
    public int compareTo(Shape shape) {
        return Double.compare(printArea(), shape.printArea());
    }
    abstract double printArea();
}
class Rectangle extends Shape{
    private Point LeftPoint;
    private Point RightPoint;


    public Rectangle(Point leftTopPoint, Point lowerRightPoint) {
        LeftPoint = leftTopPoint;
        RightPoint = lowerRightPoint;
    }

    public double printArea(){
        return Math.abs(LeftPoint.x-RightPoint.x) * Math.abs(LeftPoint.y- RightPoint.y);

    }
}
class Point{
    double x,y;

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

}
class Circle extends Shape{
    private double r;
    Circle(double r){
        if(r<=0){
            System.out.print("Wrong Format");
            System.exit(0);
        }
        this.r = r;
    }
    public double printArea(){
        return Math.PI*r*r;
    }
}

考试总分析:

没啥难的,没什么阻力,一口气写过来的。几处浪费时间是因为看错题目,和理解错题目意思。

最终分析:

在大作业中,我尝试并成功运用了很多新的数据容器,如集合,映射,动态数组等,在第五次作业中,我预先设计好了类之间的关系,写代码的思路越来越清晰。到100分的时间越来越短。

不足的是:每一次大作业几乎都是一次推倒重写。老师的初衷是迭代,在每一次的基础上添加修改。并且没有很好的运用到上课学的新知识。一味的去凑简单的100分,没有去认真体会java这门语言的优势。

 

posted @ 2023-05-16 22:19  Pattrick-Star  阅读(54)  评论(0)    收藏  举报