BLOG-1

(1)

前言:菜单这一系列的作业,对于我来说是有一定的难度的,一开始还可以比较轻易地拿到高分甚至是满分,但随着题目难度的加大,题目开始让我陷入一个比较折磨的处境,本这既然写了这么久,但又拿不到满分的就感觉很不自在的心理,在那改来改去却任然没有什么起色,所以我感觉这一系列题的一个主要的槽点就是————样例给的不够多,也不够详细,有的时候想改却无能为力,徒然耗费时间罢了。整体上来说,题目虽然是越来越困难,但是题量也随之减少,对于我们负担不是很大,而且题目也没有没有硬性要求要我们拿到多少多少分才能过,还是比较宽松的,总的来说只要肯花一定的时间去写,拿到一半分还是不难的。

(2)

设计与分析:

7-1:作为第一次的菜单设计题还是比较简单的,下面是我写的源码,经供参考;

import java.util.Scanner;

 

public class Main{

    public static void main(String[] args){

        Scanner input=new Scanner(System.in);

        String []menu =new String[]{"西红柿炒蛋","清炒土豆丝","麻婆豆腐","油淋生菜"};

        String dish;

        int cnt=0,size1;

        while(true){

            int a=0;

            dish=input.next();

            if(dish.equals("end"))break;

            size1=input.nextInt();

            //input.nextLine();

            if(dish.equals(menu[0])){

                a=1;

                if(size1==1)cnt+=15;

                if(size1==2)cnt+=23;

                if(size1==3)cnt+=30;

            }

            if(dish.equals(menu[1])){

                a=1;

                if(size1==1)cnt+=12;

                if(size1==2)cnt+=18;

                if(size1==3)cnt+=24;

            }

            if(dish.equals(menu[2])){

                a=1;

                if(size1==1)cnt+=12;

                if(size1==2)cnt+=18;

                if(size1==3)cnt+=24;

            }

            if(dish.equals(menu[3])){

                a=1;

                if(size1==1)cnt+=9;

                if(size1==2)cnt+=14;

                if(size1==3)cnt+=18;

            }

            if(a==0)System.out.println(dish+" does not exist");

            //System.out.println(cnt);

        }

        System.out.println(cnt);

    }

}

 

本题的要求还是比较少的,只要稍微注意一下题目的要求,逻辑清晰一点,写出来还是不难的,而且本题因为没有太多的要求,可以直接把菜单的相关信息放在一个数组里面,便于快速完成本题。

7-2

本题对于7-1,增加了对于所点菜删除的功能,增加的需求不是很多。下面给出我的相关源码;

import java.util.Scanner;

 

public class Main{

    public static void main(String[] args){

        Scanner input=new Scanner(System.in);

        String []name=new String[500];          //菜单储存

        int []price=new int[500];           //价格储存

        String ds;          //读入菜名

        int pr,i=0;             //读入价格

        String t;             //点菜的序号,菜品的种类(i);

        while(true){

            int rt=1;               //判断是否菜品重复输入

            ds=input.next();

            if(ds.equals("1")){t="1";break;}        //开始点菜

            if(ds.equals("end")){t="0";break;}      //只有菜单没有订单

            pr=input.nextInt();

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

                if(ds.equals(name[k])){price[k]=pr;rt=0;break;}         //检查菜品是否重复出现

 

            if(rt==1){                    //菜品无重复

                name[i]=ds;

                price[i]=pr;

                i++;

            }

        }

        int cnt=0,sum=0,max=0;      //cnt,每个菜品点单的价格,sum,点菜价格总和,max订单数量

        int []recording=new int[100];       //记录每次点菜的价格

        int re=0,flag3=1,flag2=0;       //flag2,flag3判断订单时连续删除还是删除后继续点单,re点菜数

        String h="";

        if(t.equals("1"))       //开始点菜

            while(!t.equals("end")){

                cnt=0;

                int flag=0;     //判断输入订单是否存在

                String dish=input.next();

               

                if(dish.equals("delete")){

                    if(flag2==1&&flag3==0)

                        t=h;

                    int p=Integer.parseInt(t);      //字符转数字

                   

                    if(p<1||p>max||recording[p-1]==0)System.out.println("delete error;");   //删除错误

                    if(p>=1&&p<=max&&recording[p-1]!=0){

                        sum-=recording[p-1];

                        recording[p-1]=0;       //删除后清除订单记录的价格

                    }

                    h=input.next();flag3=0;

                    if(h.equals("end"))break;

                    if(!h.equals("end")) {flag2=1;t=h;continue;}

                }

                else flag3=1;       //判断是否连续删除

               

                int size1=input.nextInt();      //点菜的份额

                int b=input.nextInt();      //点菜的份数

               

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

                    if(dish.equals(name[j])){       //是否订单菜存在

                        flag=1;

                        if(size1==1)cnt+=price[j];

                        if(size1==2){

                            if(price[j]%2==1)

                                cnt+=price[j]*1.5+1;

                            else cnt+=price[j]*1.5;

                        }

                        if(size1==3)cnt+=2*price[j];

                    }

 

                }

               

                if(flag==0) {

                    recording[re++]=0;

                    System.out.println(dish+" does not exist");

                    max++;

                }

 

                if(flag==1)

                {

                    cnt*=b;sum+=cnt;

                    recording[re++]=cnt;

                    System.out.println(t+" "+dish+" "+cnt);

                    max++;

                }

                t=input.next();

            }

       

        if(!t.equals("0"))

            System.out.println(sum);

        else System.out.println("0");

    }

}

 

对于这次题目只需注意,点菜的格式与删除菜品格式的不同,在上一次的代码添加相应的功能即可,并且我在代码旁给出了相关的解释。

7-3

这次题目难度相比于上次是有了质的飞跃,添加了很多需求,逻辑也变得复杂了许多。

  1. 这次题目涉及了日期的相关知识,对于点菜的价格也有了许多要求,多了一个随时间不同折扣也不同的规则,同时也对与输入格式有了一定的要求,对于不合法的输入将不会继续输出下去,而是会输出相应报错提醒。
  2. 对于菜单的要求也变得苛刻了,不在是有现成的菜单,而是需要用户手动输入,输入完可以保存下来,这样就不能简单的把菜单存入一些数组,而是需要建立相关的对象,把相关的信息存入其中。
  3. 对于点菜也不再局限与一张桌子点菜,而是添加了可以有多张桌子一起点菜的功能,同时也添加了一张桌子为其他桌子点菜的代点菜功能。

由于这次题目我并没有满分,所以下面的源码参考一下即可;

import java.util.HashMap;

import java.util.Scanner;

 

public class Main {

    public static int week(int y, int m, int d) {

        int num = 0;

        if (m == 1 || m == 2) {

            m += 12;

            y--;

        }

        int a = y / 100;

        int b = y % 100;

        num = (a / 4) - 2 * a + b + (b / 4) + (13 * (m + 1) / 5) + d - 1;

        return num % 7;

    }

 

    public static void main(String[] args) {

        int year, month, day, hh, mm;

        double ts = 0;

        Scanner input = new Scanner(System.in);

        String ds = "";

        int pr, i = 0, wek = 0;

        int cn = 1;

        String[] put = new String[100];

        String t = "";

        int num = 1;

        String time;

        String h = "";

        HashMap<String, Integer> menu = new HashMap<String, Integer>();

        while (true) {

            int rt = 1;

            if (!t.equals("0"))

                ds = input.next();

            if (ds.equals("table") || ds.equals("1")) {

                t = "1";

                break;

            }

            if (ds.equals("end")) {

                t = "0";

                break;

            }

            pr = input.nextInt();

            if (menu.containsKey(ds)) {

                menu.put(ds, pr);

                rt = 0;

            } else {

                menu.put(ds, pr);

                i++;

            }

        }

        while (true) {

            int flag4 = 1;

            if (t.equals("table")) {

                cn++;

                flag4 = 0;

                num = input.nextInt();

                time = input.nextLine().replaceAll("[/]", " ");

                String[] Tim = time.split(" ");

                year = Integer.parseInt(Tim[1]);

                month = Integer.parseInt(Tim[2]);

                day = Integer.parseInt(Tim[3]);

                hh = Integer.parseInt(Tim[4]);

                mm = Integer.parseInt(Tim[5]);

                wek = week(year, month, day);

                ts = hh * 1.0 + mm / 60.0;

                if (wek == 0)

                    wek = 7;

                //System.out.println(t + " " + num + ": ");

                t = "1";

            }

            if (t.equals("end") || h.equals("end"))

                break;

            if (t.equals("0")) {

                System.out.println("");

                return;

            }

            int cnt = 0, sum = 0, max = 0;

            int[] recording = new int[100];

            int re = 0, flag3 = 1, flag2 = 0;

            if (t.equals("1")) {

                if (ds.equals("table")) {

                    if (flag4 == 1) {

                        num = input.nextInt();

                        time = input.nextLine().replaceAll("[/]", " ");

                        String[] Tim = time.split(" ");

                        year = Integer.parseInt(Tim[1]);

                        month = Integer.parseInt(Tim[2]);

                        day = Integer.parseInt(Tim[3]);

                        hh = Integer.parseInt(Tim[4]);

                        mm = Integer.parseInt(Tim[5]);

                        wek = week(year, month, day);

                        ts = hh * 1.0 + mm / 60.0;

                    }

                    if (wek == 0)

                        wek = 7;

                    System.out.println(ds + " " + num + ": ");

                }

                while (!t.equals("end")) {

                    if (flag3 == 1 )

                        t = input.next();

                    if (t.equals("end") || t.equals("table"))

                        break;

                    cnt = 0;

                    int flag = 0;

                    String dish = input.next();

                    if (dish.equals("delete")) {

                        if (flag2 == 1 && flag3 == 0)

                            t = h;

                        int p = Integer.parseInt(t);

                        if (p < 1 || p > max || recording[p - 1] == 0)

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

                        if (p >= 1 && p <= max && recording[p - 1] != 0) {

                            sum -= recording[p - 1];

                            recording[p - 1] = 0;

                        }

                        h = input.next();

                        flag3 = 0;

                        if (h.equals("end"))

                            break;

                        if (!h.equals("end")) {

                            flag2 = 1;

                            t = h;

                            continue;

                        }

                    } else

                        flag3 = 1;

                    String size1 = input.next();

                    if (size1.length()!=1) {

                        String a;

                        int b;

                        a = input.next();

                        b = input.nextInt();

                        if (menu.containsKey(size1)) {

                            int price = menu.get(size1);

                            if (a.equals("1"))

                                cnt += price;

                            else if (a.equals("2")) {

                                if (price % 2 == 1)

                                    cnt += price / 2 + price + 1;

                                else

                                    cnt += price / 2 + price;

                            }

                            else if (a.equals("3"))

                                cnt += 2 * price;

                           

                            cnt*=b;

                            sum += cnt;

                            System.out.println(dish + " table" + " " + num + " pay for " + "table" + " " + t + " " + cnt);

                        } else

                            {System.out.println(dish + " does not exist");cnt=0;}

                        continue;

                    }

                    int b = input.nextInt();

                    if (menu.containsKey(dish)) {

                        flag = 1;

                        int price = menu.get(dish);

                        if (size1.equals("1"))

                            cnt += price;

                       else if (size1.equals("2")) {

                            if (price % 2 == 1)

                                cnt += price / 2 + price + 1;

                            else

                                cnt += price / 2 + price;

                        }

                        else if (size1.equals("3"))

                            cnt += 2 * price;

                    } else {

                        cnt=0;

                        recording[re++] = 0;

                        System.out.println(dish + " does not exist");

                        max++;

                    }

                    if (flag == 1) {

                        cnt *= b;

                        sum += cnt;

                        recording[re++] = cnt;

                        System.out.println(t + " " + dish + " " + cnt);

                        max++;

                    }

                }

                if ((wek == 7 || wek == 6) && !t.equals("0") && (ts >= 9.5) && (ts <= 21))

                    put[cn - 1] = ("table" + " " + num + ": " + sum);

                if (wek == 7 && !t.equals("0") && (ts < 9.5 || ts > 21))

                    put[cn - 1] = ("table" + " " + num + " out of opening hours");

                if ((wek >= 1 && wek <= 5) && !t.equals("0") && (ts >= 17) && (ts <= 20.5))

                    put[cn - 1] = ("table" + " " + num + ": " + Math.round(sum * 0.8));

                if ((wek >= 1 && wek <= 5) && !t.equals("0") && (ts >= 10.5) && (ts <= 14.5))

                    put[cn - 1] = ("table" + " " + num + ": " + Math.round(sum * 0.6));

                if ((wek >= 1 && wek <= 5) && !t.equals("0") && (ts < 10.5 || (ts > 14.5 && ts < 17) || ts > 20.5))

                    put[cn - 1] = ("table" + " " + num + " out of opening hours");

            }

        }

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

            System.out.println(put[j]);

    }

}

 

这次题目对于逻辑能力有着很大的要求,需要花费一定的时间去仔细思考,一步一步的完成,切记不能一蹴而就(如果实力够硬那当我没说),逐步的去完善。

7-5

这次题目是菜单题的最后一次,他是7-3的相关拓展,增加了特色菜和口味的相关设定,对于输入更是有了进一步的加强,所以需要用到正则表达。下面给出我的代码;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.time.DateTimeException;

import java.time.LocalDateTime;

import java.util.*;

 

public class Main {

    /*public static String  taste(String tas,String grade){

        String[]

    }*/

 

    public static boolean isNumeric(String string) {  //判断是否为数字

        try {

            Integer.parseInt(string);

            return true;

        } catch (NumberFormatException e) {

            return false;

        }

    }

    public static boolean searchCall(String threeCall) {

        String []CallNumber=new String[]{"180","181","189","133","135","136"};

        for(int i=0;i<CallNumber.length;i++)

            if(CallNumber[i].equals(threeCall))return false;

        return true;

    }

    public static void main(String[] args) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH/mm/ss");   //时间模板

        Menu menu = new Menu();

        HashMap<String ,String>hash=new HashMap<>();

        String []customer=new String[30];

        int cnt=0;

        ArrayList<Table> tables = new ArrayList<Table>();

        Scanner input = new Scanner(System.in);

        String []zhe=new String[]{"不甜","微甜","稍甜","甜"};

        String []jin=new String[]{"不酸","微酸","稍酸","酸","很酸"};

        String []chuan=new String[]{"不辣","微辣","稍辣","辣","很辣","爆辣"};

        int []tastedGrade=new int [100];

        int []tastedNumber=new int [100];

        String str1 = new String();

        String str2 = new String();

        String str3=new String();

        String time = new String();

        int [][]taste=new int[20][7];

        int table_count = 0;

        int i = 0, flag = 0;

        int portion = 0, number = 0;        //份额,份数

        while (true) {// 输入菜单

            str1 = input.next();

            if (str1.equals("T")) { // 判断上一道菜是否为特价菜

                if(str3.equals(" ")){menu.dishes.remove(menu.dishes.size() - 1);System.out.println("wrong format");continue;}

                menu.dishes.get(menu.dishes.size() - 1).isT = true;

                str1 = input.next();

            }

            if (str1.equals("table"))

                break; // 结束菜谱输入

            if (str1.equals("end"))

                break; // 仅有菜谱的情况

            str2 = input.next();

            str3=" ";

            if(!isNumeric(str2)) {

                str3=str2;

                str2 = input.next();

            }

            // 判断菜谱是否重复输入

            for (i = 0; i < menu.dishes.size(); i++) {

                if (menu.dishes.get(i).name.equals(str1)) {

                    menu.dishes.get(i).price = Integer.parseInt(str2);

                    flag++;

                    break;

                }

            }

            if (flag == 0) {

                menu.dishes.add(menu.addDish(str1, Integer.parseInt(str2),str3));

            }

            flag = 0;

        }

        if(str1.equals("end"))return ;

        while(!str1.equals("end")){

            if(str1.equals("end"))break;

            int chuanGrade=0;

            int chuanNumber=0;

            int zheGrade=0;

            int zheNumber=0;

            int jinGrade=0;

            int jinNumber=0;

            String []a=new String[]{};

            String ord;

            String call;

            String useName;

            Table tab= new Table();

            ord = input.nextLine();

            a=ord.split(" ");

            str2=a[1];

            useName=a[3];

            call=a[4];

            String threeCall=call.substring(0,3);

            if(useName.length()>10||call.length()!=11||searchCall(threeCall)){

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

                break;

            }

            String[] Date = a[5].split("/");

            String[] Time =a[6].split("/");

            tab.num = Integer.parseInt(str2);

            int[] intDate = new int[3];

            int[] intTime = new int[3];

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

                intDate[i] = Integer.parseInt(Date[i]);

                intTime[i] = Integer.parseInt(Time[i]);

            }

            try {

                tab.time = LocalDateTime.of(intDate[0],intDate[1],intDate[2],intTime[0],intTime[1],intTime[2]);

                //时间判断

                tables.add(tab);

                if(!tab.isOpen()) {

                    System.out.println("table " + str2 + " out of opening hours");

                    while(true){

                        str1=input.next();

                        if(str1.equals("end")||str1.equals("table"))break;

                    }

                    continue;

                }

            }catch(DateTimeException e){

                System.out.println( tab.num + " date error");

                break;

            }

            System.out.println("table "+str2+": ");

            while (true) {

                str1 = input.next();

                if (str1.equals("end"))

                    break;

                if (str1.equals("table"))

                    break;

                str2 = input.next();

                // 判断是否为代点

                if (isNumeric(str2)) {

                    //判断代点桌号是否存在

                    boolean exist = false;

                    for(int j=0;j<tables.size();j++) {

                        if(tables.get(j).num==Integer.parseInt(str1)) {

                            exist = true;

                            break;

                        }

                    }

                    //若存在则完成代点

                    if(exist) {

                        System.out.print(Integer.parseInt(str2) + " table " +tables.get(table_count).num + " pay for table "

                                + Integer.parseInt(str1) + " ");

                        String str=str1;

                        Record treat = new Record();

                        str1 = input.next();

                        treat.ds = menu.dishes.get(menu.searchDish(str1));

                        if(menu.dishes.get(menu.searchDish(str1)).isT) {

                            str3 = input.next();

                            tastedGrade[Integer.parseInt(str1)]=Integer.parseInt(str3);

                        }

                        else str3=" ";

                        portion = input.nextInt();

                        number = input.nextInt();

                        tastedNumber[Integer.parseInt(str1)]=number;

                        treat.portion = portion;

                        treat.number = number;

                        System.out.print(treat.getPrice() + "\n");

                        if(menu.dishes.get(menu.searchDish(str1)).taste.equals("晋菜")) {

                            if(Integer.parseInt(str3)<0||Integer.parseInt(str3)>4){

                                System.out.println("acidity num out of range :"+jinGrade);

                                str1=input.nextLine();

                                continue;

                            }

                            taste[Integer.parseInt(str)-1][1] += Integer.parseInt(str3)*number;

                            taste[Integer.parseInt(str)-1][4]+=number;

                        }

                        if(menu.dishes.get(menu.searchDish(str1)).taste.equals("川菜")) {

                            if(Integer.parseInt(str3)<0||Integer.parseInt(str3)>5){

                                System.out.println("spicy num out of range :"+str3);

                                str1=input.nextLine();

                                continue;

                            }

                            taste[Integer.parseInt(str)-1][0] += Integer.parseInt(str3)*number;

                            taste[Integer.parseInt(str)-1][3]+=number;

                        }

                        if(menu.dishes.get(menu.searchDish(str1)).taste.equals("浙菜")) {

                            if(Integer.parseInt(str3)<0||Integer.parseInt(str3)>3){

                                System.out.println("sweetness num out of range :"+str3);

                                str1=input.nextLine();

                                continue;

                            }

                            taste[Integer.parseInt(str)-1][2] += Integer.parseInt(str3)*number;

                            taste[Integer.parseInt(str)-1][5]+=number;

                        }

 

                        tables.get(table_count).add(menu, "代点",str2, str1, portion, number);

                    }

                    //若不存在则输出内容

                    else {

                        System.out.println("Table number :"+Integer.parseInt(str1)+" does not exist");

                    }

                }

                // 若不是代点

                else {

                    // 若不为删除订单,则读入份数和大小

                    if (!str2.equals("delete")) {

                        boolean t=false;

                        if(menu.dishes.get(menu.searchDish(str2)).isT) {

                            hash.put(str1,menu.dishes.get(menu.searchDish(str2)).taste);

                            str3 = input.next();

                            portion = input.nextInt();

                            number = input.nextInt();

                            tastedGrade[Integer.parseInt(str1)]=Integer.parseInt(str3);

                            tastedNumber[Integer.parseInt(str1)]=number;

                            t=true;

                            if(menu.dishes.get(menu.searchDish(str2)).taste.equals("晋菜")) {

                                if(Integer.parseInt(str3)<0||Integer.parseInt(str3)>4){

                                    System.out.println("acidity num out of range :"+str3);

                                    str1=input.nextLine();

                                    continue;

                                }

                                jinGrade += Integer.parseInt(str3)*number;

                                jinNumber+=number;

                            }

                            if(menu.dishes.get(menu.searchDish(str2)).taste.equals("川菜")) {

                                if(Integer.parseInt(str3)<0||Integer.parseInt(str3)>5){

                                    System.out.println("spicy num out of range :"+str3);

                                    str1=input.nextLine();

                                    continue;

                                }

                                chuanGrade += Integer.parseInt(str3)*number;

                                chuanNumber+=number;

                            }

                            if(menu.dishes.get(menu.searchDish(str2)).taste.equals("浙菜")) {

                                if(Integer.parseInt(str3)<0||Integer.parseInt(str3)>3){

                                    System.out.println("sweetness num out of range :"+str3);

                                    str1=input.nextLine();

                                    continue;

                                }

                                zheGrade += Integer.parseInt(str3)*number;

                                zheNumber+=number;

                            }

                        }

                        else str3=" ";

                        if(t==false) {

                            portion = input.nextInt();

                            number = input.nextInt();

                        }

                    }

                    tables.get(table_count).add(menu, str3,str1, str2, portion, number);

                    if(str2.equals("delete")){

                        if(hash.get(str1).equals("晋菜")) {

                            jinGrade -= tastedGrade[Integer.parseInt(str1)];

                            jinNumber -= tastedNumber[Integer.parseInt(str1)];

                        }

                        if(hash.get(str1).equals("川菜")) {

                            chuanGrade -= tastedGrade[Integer.parseInt(str1)];

                            chuanNumber -= tastedNumber[Integer.parseInt(str1)];

                        }

                        if(hash.get(str1).equals("浙菜")) {

                            zheGrade -= tastedGrade[Integer.parseInt(str1)];

                            zheNumber -= tastedNumber[Integer.parseInt(str1)];

                        }

                        hash.put(str1,"");

                    }

                }

 

            }

            taste[table_count][0]=chuanGrade;

            taste[table_count][1]=jinGrade;

            taste[table_count][2]=zheGrade;

            taste[table_count][3]=chuanNumber;

            taste[table_count][4]=jinNumber;

            taste[table_count][5]=zheNumber;

            // 本桌点菜结束,进入下一桌

            tables.get(table_count).getSum();

            int sum=0;

            String str=useName+" "+call+" "+tables.get(table_count).sum;

            String []b=new String[]{};

            if(table_count==0)customer[cnt++]=str;

            else {

                b = str.split(" ");

                boolean r=true;

                for (int l = 0; l < cnt; l++) {

                    String[] c = customer[l].split(" ");

                    if (b[0].equals(c[0])) {

                        sum = Integer.parseInt(c[2]) + Integer.parseInt(b[2]);

                        customer[l] = useName + " " + call + " " + sum;

                        r=false;

                    }

                }

                if(r)customer[cnt++]=str;

            }

            table_count++;

        }

        // 最终输出桌号订单信息

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

            if (tables.get(i).isOpen()) {

                System.out.print("table " + tables.get(i).num + ": " + tables.get(i).origSum+" "+tables.get(i).sum);

                for(int j=0;j<3;j++){

                    if(taste[i][j+3]!=0){

                        if(j==0){

                            System.out.print(" 川菜 "+taste[i][j+3]+" "+chuan[(int) Math.round(1.0*taste[i][j]/taste[i][j+3])]);

                        }

                        if(j==1){

                            System.out.print(" 晋菜 "+taste[i][j+3]+" "+jin[(int) Math.round(1.0*taste[i][j]/taste[i][j+3])]);

                        }

                        if(j==2){

                            System.out.print(" 浙菜 "+taste[i][j+3]+" "+zhe[(int) Math.round(1.0*taste[i][j]/taste[i][j+3])]);

                        }

                    }

                }

                System.out.println();

            } else

                System.out.println("table " + tables.get(i).num + " out of opening hours");

        }

        Arrays.sort(customer,0,cnt);

        for(int j=0;j<cnt;j++) {

            if(j<cnt-1)

                System.out.println(customer[j]);

            else System.out.print(customer[j]);

        }

    }

 

}

class Dish {   //菜单格式

    String name;

    int price;

    String taste;

 

    boolean isT = false;

}

class Record {      //点菜

    int orderNum;  //序号

    Dish ds;   //菜品

    int portion;  //份额

    int number;  //份数

    boolean isDeleted = false;

 

    int getPrice() {

        if (portion == 2)

            return (int) Math.round(1.5 * ds.price) * number;

        else if (portion == 3)

            return 2 * ds.price * number;

        else

            return ds.price * number;

    }

}

class Menu {   //菜单

    ArrayList<Dish> dishes = new ArrayList<Dish>();

 

    int searchDish(String dishName) {

        for (int i = 0; i < dishes.size(); i++) {

            if (dishName.equals(dishes.get(i).name)) {

                return i;

            }

        }

        return -1;

    }

 

    Dish addDish(String dishName, int price,String taste) {

        Dish newDish = new Dish();

        newDish.name = dishName;

        newDish.price = price;

        newDish.taste=taste;

        return newDish;

    }

}

class Order {       //点菜

    ArrayList<Record> records = new ArrayList<Record>();

 

    Record addARecord(String tasteGrade,int orderNum, String dishName, int portion, int number, Menu menu) {

        Record newRecord = new Record();

        newRecord.orderNum = orderNum;

        newRecord.ds = menu.dishes.get(menu.searchDish(dishName));

        newRecord.portion = portion;

        newRecord.number = number;

        if(!tasteGrade.equals("代点"))

            System.out.println(newRecord.orderNum + " " + newRecord.ds.name + " " + newRecord.getPrice());

        return newRecord;

    }

 

    boolean delARecordByOrderNum(int orderNum) {

        int i = 0, flag = 0;

        for (i = 0; i < records.size(); i++) {

            if (records.get(i).orderNum == orderNum) {

                if (records.get(i).isDeleted == false) {

                    records.get(i).isDeleted = true;

                }

                else {

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

                }

                flag++;

            }

        }

        if (flag == 0) {

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

            return false;

        }

        return true;

    }

}

class Table {

    Order order = new Order();

    int num;

    LocalDateTime time;

    long sum = 0;

    long origSum = 0;

    void add(Menu menu, String str3,String str1, String str2, int portion, int number) {     //判断点菜的模式(是否存在,删除,添加)

        if (str2.equals("delete")) {

            order.delARecordByOrderNum(Integer.parseInt(str1));

        } else {

            if (menu.searchDish(str2) != -1) {

                order.records.add(order.addARecord(str3,Integer.parseInt(str1), str2, portion, number, menu));

            } else

                System.out.println(str2 + " does not exist");

        }

    }

 

    void getSum() {

        double ts = time.getHour() + time.getMinute() / 60;

        int wek = time.getDayOfWeek().getValue();

        for (int i = 0; i < order.records.size(); i++) {

            if (!order.records.get(i).isDeleted) {

                origSum += order.records.get(i).getPrice();

                if ((wek == 7 || wek == 6) && (ts >= 9.5) && (ts <= 21))

                    sum += order.records.get(i).getPrice();

                if ((wek >= 1 && wek <= 5) && (ts >= 17) && (ts <= 20.5)) {

                    if(!order.records.get(i).ds.isT)

                        sum += Math.round(order.records.get(i).getPrice() * 0.8);

                    else sum += Math.round(order.records.get(i).getPrice() * 0.7);

                }

                if ((wek >= 1 && wek <= 5) && (ts >= 10.5) && (ts <= 14.5)) {

                    if(!order.records.get(i).ds.isT)

                        sum += Math.round(order.records.get(i).getPrice() * 0.6);

                    else sum += Math.round(order.records.get(i).getPrice() * 0.7);

                }

 

            }

        }

    }

 

    boolean isOpen() {          //判断是否开门

        int weekday = time.getDayOfWeek().getValue();

        if (weekday > 0 && weekday < 6) {

            if (time.getHour() >= 17 && time.getHour() < 20)

                return true;

            if (time.getHour() == 20) {

                if (time.getMinute() <= 30)

                    return true;

            }

            if (time.getHour() > 10 && time.getHour() < 14)

                return true;

            if (time.getHour() == 10) {

                if (time.getMinute() >= 30)

                    return true;

            }

            if (time.getHour() == 14) {

                if (time.getMinute() <= 30)

                    return true;

            }

        } else {

            if (time.getHour() > 9 && time.getHour() < 21)

                return true;

            if (time.getHour() == 9) {

                if (time.getMinute() >= 30)

                    return true;

            }

            if (time.getHour() == 21) {

                if (time.getMinute() <= 30)

                    return true;

            }

        }

        return false;

 

    }

}

 

由于这次题目我在花了大量时间去重构和思考后还是有几个问题没有解决,所以代码任然存在很大的问题,就不给出相关建议了,看看就好。

 

 

 这里我给出代码的类图,以便理解;

 

 

(3)

踩坑心得:

1.对于这一系列菜单题目,从一开始就需要运用面向对象的思想,老老实实的去把对象一个一个建立好,切不能图轻快,而不去使用类和对象,因为这样的搞,我已经吃了很大的亏,后面也不得不花大量的时间去重构,去改进,还白白错失锻炼加强自己对于这方面知识的机会。

2.一定要在确定整体思路后再开始写,慢慢开始构建框架,我一开始就是有了一点点思路就开始写,这也导致我后面改进代码显得异常的痛苦。

3.对于格式也一定要仔细检查,不然你就算是结果是对的,因为格式对不上而白白浪费时间去做一些无用功。

(4)主要困难及改进建议:

在写代码时常因为不知道自己错在哪里而头痛,也没有什么实质性的提醒给出,这就让我像一个无头苍蝇在乱窜,尽做一些无用功,所以我认为出题的人应该将样例给的更加细致一些,再不济也得加一点提示,让我们改代码有一定的方向,不像无头苍蝇那样乱窜。

(5)总结:在经历了这几次题目集后,我的能力得到了很明显的提升,对于面向对象的认知也更加深刻了,动手能力也得到了锻炼,不再是动手废的一个人了。对于这几次题目集我的建议不是很多,只希望老师能够给出更多的样例,时间给的宽裕一些。

posted @ 2023-05-23 20:23  ℳℓʚ龙ɞℓℳ  阅读(20)  评论(0)    收藏  举报