JavaPTA作业4-5及期中知识总结

(2)设计与分析

 

1、第四次作业第一题:

 

 

import java.text.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        Scanner sc = new Scanner(System.in);
        ArrayList<String> List = new ArrayList<>();
        ArrayList<Table> tableList = new ArrayList<>();
        ArrayList<String> sites = new ArrayList<>();
        
        String str ;

        while (true) {
            int k=0;
            k++;
            str = sc.nextLine();
            if(k!=1&&(str.matches("^\\s*\\n"))||str.equals("")) {
               str="wrongFFF";
           }
            if (str.equalsIgnoreCase("end")) {break;}
            sites.add(str);
        }
        Menu menu = main.new Menu();
        menu.dishes = new Dish[0];
        int sFlag = 0;// 订单开始位置
        for (int i = 0; i < sites.size(); i++) {// 处理菜品
            if(sites.get((i)).equalsIgnoreCase("wrongFFF")){
                List.add("wrong format");
                continue;
            }
            String[] strings = sites.get(i).split(" ");
            if (strings.length==0) {
                List.add("wrong format");
                continue;
            }
            if (strings.length > 3&&strings[0].charAt(0)=='t') {
                sFlag = i;
                break;
            }
            String dName = strings[0];
            String dPrice = strings[1];
            int p = Integer.parseInt(dPrice);
            if (p >= 300 || p<= 0) {
                List.add(dName + " price out of range "+p);
                continue;
            }
            if (menu.searthDish(dName) != null) {
                for (int j = 0; j < menu.dishes.length; j++) {
                    if (menu.dishes[j].equals(dName)) {
                        menu.dishes[j].money = p;
                    }
                }
            } else {
                Dish dish = main.new Dish();
                int len2=strings.length;
                if (len2 == 2) {
                    dish.name = dName;
                    dish.money = p;
                } else if (len2 == 3) {
                    dish.name = dName;
                    dish.money= p;
                    dish.T = true;
                }
                menu.addDish(dish);
            }
        }
        ArrayList<Integer> List2 = new ArrayList<>();
        for (int i = sFlag; i < sites.size(); i++) {
            if (sites.get(i).split(" ")[0].length() > 2 && sites.get(i).split(" ")[0].charAt(0) == 't') {
                List2.add(i);
            }
        }
        for (int i = 0; i < List2.size(); i++) {
            Table table = main.new Table();
            Order order = main.new Order();
            order.records = new Record[0];
            table.order = order;
            if(List2.get(i).equals(" ")){
                List.add("wrong format");
                continue;
            }
            int t1 = List2.get(i);
            String[] strings = sites.get(t1).split(" ");
            
            if (strings[0].equals("table")) {
                // 桌号
                if (strings[1].charAt(0) == '0') {
                    List.add("wrong format");continue;
                }
                int Num = Integer.parseInt(strings[1]);
                if (Num > 55||Num < 1) {
                    List.add(Num + " num out of range");continue;
                }
                String nyr =strings[2];
                String sfm = strings[3];
                if(judgeDate(nyr,sfm)) {List.add("wrong format");continue;}
                DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH/mm/ss");
                sdf.setLenient(false);
                Date date;
                try {
                    date = sdf.parse(nyr+ " " + sfm);
                } catch (ParseException e) {
                    List.add(Num + " date error");
                    continue;
                }
                table.date = date;
                table.tableNum = Num;
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/MM/dd");
                SimpleDateFormat sdf2 = new SimpleDateFormat("HH/mm/ss");
                Date date1,date2;
                try {
                    date1 = sdf1.parse("2022/1/1");
                    date2 = sdf1.parse("2023/12/31");
                    if (date1.compareTo(sdf1.parse(strings[2])) > 0 || date2.compareTo(sdf1.parse(strings[2])) < 0) {
                        List.add("not a valid time period");
                        continue;
                    }
                } catch (ParseException e1) {
                    e1.printStackTrace();
                }
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                int dateNum = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                Date bdate=null,start1=null,edate= null,end1=null,start2=null,end2=null,now=null;
                try {
                    bdate = sdf2.parse("9/29/59");
                    edate = sdf2.parse("21/30/00");
                    start1 = sdf2.parse("10/30/00");
                    end1 = sdf2.parse("14/30/00");
                    start2= sdf2.parse("16/59/59");
                    end2 = sdf2.parse("20/30/00");
                    now = sdf2.parse(strings[3]);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                if (dateNum == 6 || dateNum == 0) {
                    if (timeCalendar(now, bdate, edate)) {
                        table.disscount = 1;
                    }
                } else {
                    if (timeCalendar(now,start1, end1)) {// 6折
                        table.disscount = 0.6;
                    } else if (timeCalendar(now, start2, end2)) {// 8折
                        table.disscount = 0.8;
                    } else {
                        List.add("table " + Num + " out of opening hours");
                        continue;
                    }
                }
                List.add("table"+" "+Num + ": ");
                if (t1 == sites.size() - 1) {
                } else {
                    int fNum;
                    if (List2.get(List2.size() - 1) == t1) {
                        fNum = sites.size();
                    } else {
                        fNum = List2.get(i + 1);
                    }
                    ArrayList<Integer> delNumArrayList = new ArrayList<Integer>();
                    int numb = 0;
                    for (int j = t1 + 1; j < fNum; j++) {// 点菜记录
                        String[] strings2 = sites.get(j).split(" ");
                        if (strings2.length==0) {List.add("wrong format");continue;}
                        int Num1;//判断是否开始为菜品
                        try {
                            Num1 = Integer.parseInt(strings2[0]);
                        } catch (Exception e) {
                            List.add("invalid dish");
                            continue;
                        }
                        if (strings2[0].charAt(0)=='0') {
                            List.add("wrong format");
                            continue;
                        }
                        if (strings2.length == 5) {
                            
                        } else if (strings2.length == 2) {
                            if (!strings2[1].equals("delete")) {
                                List.add("wrong format");
                                continue;
                            } else {
                                boolean flag = false;
                                for (Integer n2 : delNumArrayList) {
                                    if (n2 == Num1) {
                                        flag = true;
                                    }
                                }
                                if (flag) {
                                    List.add("deduplication " + Num1);
                                    continue;
                                }
                                if (table.order.findRecord(Num1) == -1) {
                                    List.add("delete error");
                                    continue;
                                }
                                table.order.searchARecord(Num1);
                                delNumArrayList.add(Num1);
                            }
                        } else if (strings2.length == 4) {
                            List.add("wrong format");
                        } else {
                            continue;
                        }
                    }
                }
            } else {
                if (t1 == sites.size() - 1) {
                } else {
                    int fNum;
                    if (List2.get(List2.size() - 1) == t1) {
                        fNum = sites.size();
                    } else {
                        fNum = List2.get(i + 1)+1;
                    }
                    Table table2 = tableList.get(tableList.size()-1);
                    ArrayList<Integer> delNumArrayList = new ArrayList<Integer>();
                    int numb = table2.order.records[table2.order.records.length-1].orderNum;
                    for (int j = t1 + 1; j < fNum; j++) {// 点菜记录
                        String[] strings2 = sites.get(j).split(" ");
                        if (strings2.length==0) {
                            List.add("wrong format");
                            continue;
                        }
                        int Num1 = Integer.parseInt(strings2[0]);
                        if (strings2[0].charAt(0)=='0') {List.add("wrong format");continue;
                        }
                        if (strings2.length == 5) {
                            boolean flag = true;
                            for (int k = 0; k < tableList.size(); k++) {
                                if (tableList.get(k).tableNum == Num1) {
                                    flag = false;
                                }
                            }
                            if (flag) {
                                List.add("Table number :" + Num1 + " does not exist");
                                continue;
                            }

                            if (strings2[3].length() > 1) {
                                List.add("wrong format");
                                continue;
                            } else {
                                String dishName = strings2[2];// 菜名
                                int P= Integer.parseInt(strings2[3]);;
                                int N= Integer.parseInt(strings2[4]);;
                                if (menu.searthDish(dishName) == null) {
                                    List.add(dishName + " does not exist");
                                    continue;
                                }
                                Dish dish = menu.searthDish(dishName);
                                if (dish.T) {
                                    if (P != 1 && P != 3) {
                                        List.add(Num1 + " portion out of range " + P);
                                        continue;
                                    }
                                } else {
                                    if (P != 1 && P != 3 && P != 2) {
                                        List.add(Num1 + " portion out of range " + P);
                                        continue;
                                    }
                                }
                                if (N > 15 || N < 1) {
                                    List.add(Num1 + " num out of range " + N);
                                    continue;
                                }
                                boolean f1 = true;
                                if (f1) {
                                    table2.order.addARecord(Num1, dish, P, N);
                                }
                            }

                        } else if (strings2.length == 2) {
                            if (!strings2[1].equals("delete")) {
                                List.add("wrong format");
                                continue;
                            } else {
                                boolean flag = false;
                                for (Integer n2 : delNumArrayList) {
                                    if (n2 == Num1) {
                                        flag = true;
                                    }
                                }
                                if (table2.order.findRecord(Num1) == -1) {
                                    List.add("delete error");
                                    continue;
                                }
                                table2.order.searchARecord(Num1);
                                delNumArrayList.add(Num1);
                            }

                        } else if (strings2.length == 4) {
                            if (strings2[2].length() > 1) {
                                List.add("wrong format");
                                continue;
                            } else {
                                String dishName = strings2[1];// 菜名
                                int dishP;
                                int dishN;
                                if (strings2[3].charAt(0) == '0') {
                                    List.add("wrong format");
                                    continue;
                                }
                                try {
                                    dishP = Integer.parseInt(strings2[2]);
                                    dishN = Integer.parseInt(strings2[3]);
                                } catch (Exception e) {
                                    List.add("wrong format");
                                    continue;
                                }
                                if (Num1 <= numb) {
                                    List.add("record serial number sequence error");continue;
                                }
                                if (menu.searthDish(dishName) == null) {
                                    List.add(dishName + " does not exist");continue;
                                }
                                Dish dish = menu.searthDish(dishName);
                                if (dish.T) {
                                    if (dishP != 1 && dishP != 3) {
                                        List.add(Num1 + " portion out of range " + dishP);continue;
                                    }
                                } else {
                                    if (dishP != 1 && dishP != 3 && dishP != 2) {
                                        List.add(Num1 + " portion out of range " + dishP);continue;
                                    }
                                }
                                numb = Num1;
                                for (int k = 0; k < table2.order.records.length; k++) {
                                    if (table2.order.records[k].d.name.equals(dishName)
                                            && table2.order.records[k].portion == dishP) {
                                        table2.order.records[k].num = table2.order.records[k].num + dishN;
                                    }
                                }
                            }
                        } else {
                            List.add("wrong format");
                            continue;
                        }
                    }
                }
            }
            if (table.tableNum!=0) {
                tableList.add(table);
            }
        }
        for (int j = 0; j < List.size(); j++) {
            System.out.println(List.get(j));
        }
        for (int j = 0; j < tableList.size()-1; j++) {
            if (tableList.get(0).tableNum<tableList.get(1).tableNum) {
                Table table;
                table = tableList.get(1);
                tableList.set(1, tableList.get(0));
                tableList.set(0, table);
            }
        }
        for (int j = 0; j < tableList.size(); j++) {
            int tableNum = tableList.get(j).tableNum;
            Order order =tableList.get(j).order;
            int sum = 0,finSum = 0;
            for (int i = 0; i < order.records.length; i++) {
                Record record = order.records[i];
                if (record.d.T&&tableList.get(j).disscount!=1.0) {
                    sum +=(int)Math.round(record.getPrice());
                    finSum +=(int)Math.round(record.getPrice()*0.7);
                }else {
                    sum +=record.getPrice();
                    finSum +=(int)Math.round(record.getPrice()*tableList.get(j).disscount);
                }
            }
            if (j==tableList.size()-1) {
                System.out.print("table"+" "+tableNum+":"+" "+sum+" "+finSum);
            }else {
                System.out.println("table"+" "+tableNum+":"+" "+sum+" "+finSum);
            }
        }
    }
    public static  boolean judgeDate(String nyr,String sfm){
        if (nyr.split("/")[0].length() != 4) {return true;}
        if (nyr.split("/")[1].length() > 2 || nyr.split("/")[1].length() < 1) {return true;}
        if (nyr.split("/")[2].length() > 2 || nyr.split("/")[2].length() < 1) {return true;}
        if (sfm.split("/")[0].length() > 2 || sfm.split("/")[0].length() < 1) {return true;}
        if (sfm.split("/")[1].length() > 3 || sfm.split("/")[1].length() < 2) {return true;}
        return sfm.split("/")[2].length() > 2 || sfm.split("/")[2].length() < 1;
    }
    // 菜品类
    class Dish {
        String name;
        int money; 
        boolean T = false;
        int getPrice(int portion) {
            double p = 0;
            switch (portion) {
                case 1:
                    p = 1.0 * this.money;break;
                case 2:
                    p= 1.5 * this.money;break;
                case 3:
                    p = 2.0 * this.money;break;
            }
            return (int) Math.round(p);
        }
    }
    // 菜谱类:对应菜谱,包含饭店提供的所有菜的信息。
    class Menu {
        Dish[] dishes;// 菜品数组,保存所有菜品信息
        Dish searthDish(String dishName) {// 根据菜名在菜谱中查找菜品信息,返回Dish对象。
            for (int i = 0; i < dishes.length; i++) {
                if (dishes[i].name.equals(dishName)) {
                    return dishes[i];
                }
            }
            return null;
        }
        Dish addDish(Dish dish) {// 添加菜品
            Dish[] dishes1 = new Dish[dishes.length + 1];
            for (int i = 0; i < dishes.length; i++) {
                dishes1[i] = dishes[i];
            }
            dishes1[dishes.length] = dish;
            dishes = dishes1;
            return dish;
        }
    }
    class Record {
        int orderNum;
        Dish d;
        int portion;
        int num;
        int getPrice() {
            return (int) Math.round(this.d.getPrice(this.portion) * num);
        }
    }
    class Order {
        Record[] records;
        Record addARecord(int orderNum, Dish dish, int portion, int num) {
            Record record = new Record();
            record.orderNum = orderNum;
            record.d = dish;
            record.portion = portion;
            record.num = num;
            Record[] records1 = new Record[this.records.length + 1];
            for (int i = 0; i < this.records.length; i++) {
                records1[i] = this.records[i];
            }
            records1[this.records.length] = record;
            this.records = records1;
            return record;
        } 
        void searchARecord(int orderNum) { 
            int n = this.findRecord(orderNum);
            Record[] records1 = new Record[this.records.length - 1];
            for (int i = 0; i < this.records.length; i++) {
                if (i == n) {
                    continue;
                }
                if (i > n) {
                    records1[i] = this.records[i-1];
                } else {
                    records1[i] = this.records[i];
                }
            }
            this.records = records1;
        }
        int findRecord(int orderNum) {
            int n = -1;
            for (int i = 0; i < this.records.length; i++) {
                if (this.records[i].orderNum == orderNum) {
                    n = i;
                }
            }
            return n;
        }
    }
    public static boolean timeCalendar(Date nowT, Date beginT, Date endT) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowT);// 当前
        Calendar begin = Calendar.getInstance();// 开始
        begin.setTime(beginT);
        Calendar end = Calendar.getInstance(); //结束
        end.setTime(endT);
        return date.after(begin) && date.before(end);//判断值返回
    }
    class Table {
        int tableNum;
        Order order;
        Date date;
        double disscount = 0;
    }
}

 

 

2、第五次作业第一题

 

 

 

import java.util.*;
import java.util.regex.*;
import java.text.*;

public class Main {
    public static void main(String[] args) throws ParseException {
        Scanner sc = new Scanner(System.in);
        Menu menu = new Menu();
        Order[] orders = new Order[1000];
        Record[] recordss = new Record[1000];
        int t = 0, r = 0, c = 0;
        String temp = null;
        while (true) {
            String s = sc.nextLine();
            String[] strs = s.split(" ");
            if (strs[0].equals("table")) {
                temp = s;
                break;
            } else {
                try {
                    if (strs.length == 2) {
                        if (menu.searthDish(strs[0]) != null)
                            menu.updateDish(strs[0], null, Integer.valueOf(strs[1]), null);
                        else menu.addDish(strs[0], null, Integer.valueOf(strs[1]), null);
                    } else if (strs.length == 4) {
                        if (menu.searthDish(strs[0]) != null)
                            menu.updateDish(strs[0], strs[1], Integer.valueOf(strs[2]), strs[3]);
                        else menu.addDish(strs[0], strs[1], Integer.valueOf(strs[2]), strs[3]);
                    } else {
                        System.out.println("wrong format");
                    }
                } catch(Exception e) {
                }
            }
        }
        String[] oneStrs = temp.split(" ");
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(oneStrs[1]);
        boolean wrongFlag = false;
        if (isNum.matches()) {
            orders[t++] = new Order(Integer.valueOf(oneStrs[1]), oneStrs[3], oneStrs[4], oneStrs[5], oneStrs[6]);
            orders[0].setMenu(menu);
        } else {
            wrongFlag = true;
            orders[t++] = new Order(-1, oneStrs[3], oneStrs[4], oneStrs[5], oneStrs[6]);
            orders[0].setMenu(null);
        }
        String phoneCon = "180、181、189、133、135、136";
        if (oneStrs[3].length() > 10 || oneStrs[4].length() != 11 || !phoneCon.contains(oneStrs[4].substring(0, 3))) {
            wrongFlag = true;
            orders[0].setMenu(null);
        }
        if (wrongFlag) {
            System.out.print("wrong format");
        }
        if (isAvailable(dateToWeek(oneStrs[5]), oneStrs[6]) == 0) {
            if (wrongFlag) {
                System.out.println();
            }
            System.out.println("table " + oneStrs[1] + " out of opening hours");
            orders[0].setMenu(null);
        }
        if (orders[0].menu != null) {
            System.out.println("table " + oneStrs[1] + ": ");
        }
        while (true) {
            String s = sc.nextLine();
            String[] strs = s.split(" ");
            if (s.equals("end")) {
                if (t == 0) return;
                else break;
            } else {
                if (strs[0].equals("table")) {
                    isNum = pattern.matcher(strs[1]);
                    if (wrongFlag) {
                        System.out.println();
                    }
                    wrongFlag = false;
                    if (isNum.matches()) {
                        orders[t++] = new Order(Integer.valueOf(strs[1]), strs[3], strs[4], strs[5], strs[6]);
                        orders[t - 1].setMenu(menu);
                    } else {
                        wrongFlag = true;
                        orders[t++] = new Order(-1, strs[3], strs[4], strs[5], strs[6]);
                        orders[t - 1].setMenu(null);
                    }
                    if (strs[3].length() > 10 || strs[4].length() != 11 || !phoneCon.contains(strs[4].substring(0, 3))) {
                        wrongFlag = true;
                        orders[t - 1].setMenu(null);
                    }
                    if (wrongFlag) {
                        System.out.print("wrong format");
                    }
                    if (isAvailable(dateToWeek(strs[5]), strs[6]) == 0) {
                        System.out.println("table " + strs[1] + " out of opening hours");
                        orders[t - 1].setMenu(null);
                    }
                    if (orders[t - 1].menu != null) {
                        System.out.println("table " + strs[1] + ": ");
                    }
                    r = 0;
                } else {
                    if (orders[t - 1].menu != null) {
                        try {
                            boolean flag = false, replaceFlag = false;
                            if (strs.length == 5)
                                flag = orders[t - 1].addRecord(Integer.valueOf(strs[0]), strs[1], Integer.valueOf(strs[2]), Integer.valueOf(strs[3]), Integer.valueOf(strs[4]), null);
                            else if (strs.length == 4)
                                flag = orders[t - 1].addRecord(Integer.valueOf(strs[0]), strs[1], null, Integer.valueOf(strs[2]), Integer.valueOf(strs[3]), null);
                            else if (strs.length == 6) {
                                replaceFlag = true;
                                flag = orders[t - 1].addRecord(Integer.valueOf(strs[0]), strs[2], Integer.valueOf(strs[3]), Integer.valueOf(strs[4]), Integer.valueOf(strs[5]), new Replace(Integer.valueOf(orders[t - 1].orderNum), Integer.valueOf(strs[1])));
                            } else if (strs.length == 2) {
                                orders[t - 1].deleteRecord(Integer.valueOf(strs[0]));
                                continue;
                            } else {
                                System.out.println("wrong format");
                            }
                            if (flag) {
                                if (replaceFlag)
                                    System.out.println(strs[0] + " table " + orders[t - 1].orderNum + " pay for table " + strs[1] + " " + orders[t - 1].records.get(r++).getPrice());
                                else
                                    System.out.println(strs[0] + " " + strs[1] + " " + orders[t - 1].records.get(r++).getPrice());
                            }
                        } catch(Exception e) {
                            System.out.println("wrong format");
                        }
                    }
                }
            }
        }
        int count = 0;
        while (orders[count] != null) {
            if (orders[count].menu != null) {
                int sum = 0;
                int sumDiscount = 0;
                for (Record record : orders[count].records) {
                    sum += record.getPrice();
                    sumDiscount += record.getDiscountPrice(isAvailable(dateToWeek(orders[count].day), orders[count].time));
                    if (record.getReplace() != null) {
                        recordss[c++] = record;
                    }
                }
                orders[count].sum = sum;
                orders[count].sumDiscount = sumDiscount;
            }
            count++;
        }
        count = 0;
        TreeMap<String, Integer> map = new TreeMap<>();
        while (orders[count] != null) {
            if (orders[count].menu != null) {
                int sumTaste1 = 0, sumTaste2 = 0, sumTaste3 = 0;
                int n1 = 0, n2 = 0, n3 = 0;
                int nn1 = 0, nn2 = 0, nn3 = 0;
                for (Record record : orders[count].records) {
                    if (record.getSpicy() != null) {
                        if ("川菜".equals(record.getDish().getType())) {
                            n1++;
                            nn1 += record.getNum();
                            sumTaste1 += record.getSpicy() * record.getNum();
                        }
                        if ("晋菜".equals(record.getDish().getType())) {
                            n2++;
                            nn2 += record.getNum();
                            sumTaste2 += record.getSpicy() * record.getNum();
                        }
                        if ("浙菜".equals(record.getDish().getType())) {
                            n3++;
                            nn3 += record.getNum();
                            sumTaste3 += record.getSpicy() * record.getNum();
                        }
                    }
                }
                if (count == 0 && wrongFlag) {
                    System.out.println();
                }
                System.out.print("table " + orders[count].orderNum + ": " + orders[count].sum + " " + orders[count].sumDiscount);
                for (Record record : recordss) {
                    if (record != null) {
                        if (record.getReplace().getOriginNum() - 1 == count) {
                            if ("川菜".equals(record.getDish().getType())) {
                                n1--;
                                nn1 -= record.getNum();
                                sumTaste1 -= record.getSpicy() * record.getNum();
                            }
                            if ("晋菜".equals(record.getDish().getType())) {
                                nn2 -= record.getNum();
                                sumTaste2 -= record.getSpicy() * record.getNum();
                            }
                            if ("浙菜".equals(record.getDish().getType())) {
                                n3--;
                                nn3 -= record.getNum();
                                sumTaste3 -= record.getSpicy() * record.getNum();
                            }
                        }
                        if (record.getReplace().getReplaceNum() - 1 == count) {
                            if ("川菜".equals(record.getDish().getType())) {
                                nn1 += record.getNum();
                                sumTaste1 += record.getSpicy() * record.getNum();
                            }
                            if ("晋菜".equals(record.getDish().getType())) {
                                nn2 += record.getNum();
                                sumTaste2 += record.getSpicy() * record.getNum();
                            }
                            if ("浙菜".equals(record.getDish().getType())) {
                                n3++;
                                nn3 += record.getNum();
                                sumTaste3 += record.getSpicy() * record.getNum();
                            }
                        }
                    } else {
                        break;
                    }
                }
                String[] spicyArr = {"不辣", "微辣", "稍辣", "辣", "很辣", "爆辣"};
                String[] acidityArr = {"不酸", "微酸", "稍酸", "酸", "很酸"};
                String[] sweetnessArr = {"不甜", "微甜", "稍甜", "甜"};
                if (n1 != 0)
                    System.out.print(" 川菜 " + nn1 + " " + spicyArr[(int) Math.round((double) sumTaste1 / nn1)]);
                if (n2 != 0)
                    System.out.print(" 晋菜 " + nn2 + " " + acidityArr[(int) Math.round((double) sumTaste2 / nn2)]);
                if (n3 != 0)
                    System.out.print(" 浙菜 " + nn3 + " " + sweetnessArr[(int) Math.round((double) sumTaste3 / nn3)]);
                if (n1 == 0 && n2 == 0 && n3 == 0) {
                    System.out.print(" ");
                }
                System.out.println();
                if (map.containsKey(orders[count].customerName)) {
                    map.put(orders[count].customerName, map.get(orders[count].customerName) + orders[count].sumDiscount);
                } else {
                    map.put(orders[count].customerName, orders[count].sumDiscount);
                }
            }
            count++;
        }
        Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            String e = it.next().getKey();
            String phoneNum = null;
            count = 0;
            while (orders[count] != null) {
                if (e.equals(orders[count].customerName)) {
                    phoneNum = orders[count].customerPhone;
                    break;
                }
                count++;
            }
            if (it.hasNext()) {
                System.out.println(e + " " + phoneNum + " " + map.get(e));
            } else {
                System.out.print(e + " " + phoneNum + " " + map.get(e));
            }
        }
    }

    public static int dateToWeek(String dateTime) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd");
        int[] weekDays = new int[]{7, 1, 2, 3, 4, 5, 6};
        Calendar calendar = Calendar.getInstance();
        Date date = null;
        try {
            date = f.parse(dateTime);
            calendar.setTime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekDays[w];
    }
    public static int isAvailable(int weekDays, String timeNow) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("HH/mm/ss");
        Long time = sdf.parse(timeNow).getTime();
        if (weekDays == 6 || weekDays == 7) {
            Long startTime = sdf.parse("09/30/00").getTime();
            Long endTime = sdf.parse("21/30/00").getTime();
            if (time.compareTo(endTime) <= 0 && time.compareTo(startTime) >= 0) {
                return 3;
            } else {
                return 0;
            }
        } else {
            Long startTime1 = sdf.parse("10/30/00").getTime();
            Long startTime2 = sdf.parse("17/00/00").getTime();
            Long endTime1 = sdf.parse("14/30/00").getTime();
            Long endTime2 = sdf.parse("20/30/00").getTime();
            if (time.compareTo(endTime1) <= 0 && time.compareTo(startTime1) >= 0) {
                return 1;
            } else if (time.compareTo(endTime2) <= 0 && time.compareTo(startTime2) >= 0) {
                return 2;
            } else {
                return 0;
            }
        }
    }
}


class Dish {
protected String name;
protected String type;
protected int unitPrice;
protected boolean isVegetarian;

public Dish(String name, String type, int unitPrice, boolean isVegetarian) {
    this.name = name;
    this.type = type;
    this.unitPrice = unitPrice;
    this.isVegetarian = isVegetarian;
}

public String getName() {
    return name;
}

public String getType() {
    return type;
}

public int getUnitPrice() {
    return unitPrice;
}

public boolean isVegetarian() {
    return isVegetarian;
}

public int getPrice(int portion) {
    if (portion == 1) {
        return unitPrice;
    } else if (portion == 2) {
        return (int) Math.round(unitPrice * 1.5);
    } else if (portion == 3) {
        return unitPrice * 2;
    } else {
        throw new IllegalArgumentException("Invalid portion: " + portion);
    }
}
}

class Menu {
    ArrayList<Dish> dishs = new ArrayList<>();//菜品数组,保存所有菜品信息

    Dish searthDish(String dishName) {
        if (dishName != null) {
            for (Dish dish : dishs) {
                if (dishName.equals(dish.getName())) {
                    return dish;
                }
            }
            return null;
        } else {
            throw new RuntimeException("输入的菜品名称不能为空!");
        }
    }

    boolean updateDish(String dishName, String type, int unit_price, String special) {
        if (dishName != null) {
            for (int i = 0; i < dishs.size(); i++) {
                if (dishName.equals(dishs.get(i).getName())) {
                    boolean flag = false;
                    if (type != null && special != null && special.equals("T")) {
                        flag = true;
                    }
                    dishs.set(i, new Dish(dishName, type, unit_price, flag));
                    return true;
                }
            }
        }
        return false;
    }

    //添加一道菜品信息
    Dish addDish(String dishName, String type, int unit_price, String special) {
        boolean flag = false;
        if (type != null && special != null && special.equals("T")) {
            flag = true;
        }
        Dish newDish = new Dish(dishName, type, unit_price, flag);
        dishs.add(newDish);
        return newDish;
    }
}


class Record {
protected int recordNum;
protected Dish dish;
protected Integer spicy;
protected int portion;
protected int num;
protected Replace replace;

public Record(int recordNum, Dish dish, Integer spicy, int portion, int num, Replace replace) {
    this.recordNum = recordNum;
    this.dish = dish;
    this.spicy = spicy;
    this.portion = portion;
    this.num = num;
    this.replace = replace;
}

public int getRecordNum() {
    return recordNum;
}

public Dish getDish() {
    return dish;
}

public Integer getSpicy() {
    return spicy;
}

public int getPortion() {
    return portion;
}

public int getNum() {
    return num;
}

public Replace getReplace() {
    return replace;
}

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

public int getDiscountPrice(int discountType) {
    if (dish.isVegetarian()) {
        if (discountType == 1 || discountType == 2) {
            return (int) Math.round(dish.getPrice(portion) * num * 0.7);
        } else if (discountType == 3) {
            return dish.getPrice(portion) * num;
        } else {
            throw new RuntimeException("输入的折扣类型错误!");
        }
    } else {
        if (discountType == 1) {
            return (int) Math.round(dish.getPrice(portion) * num * 0.6);
        } else if (discountType == 2) {
            return (int) Math.round(dish.getPrice(portion) * num * 0.8);
        } else if (discountType == 3) {
            return dish.getPrice(portion) * num;
        } else {
            throw new RuntimeException("输入的折扣类型错误!");
        }
    }
}

public void setNum(int i) {
}
}

class Order {
int orderNum;//序号
String customerName;//用户名
String customerPhone;//手机号码
String day;
String time;
int sum;
int sumDiscount;
Menu menu;//菜单
ArrayList<Record> records = new ArrayList<>();//保存订单上每一道的记录

public Order() {
}

public Order(int orderNum, String customerName, String customerPhone, String day, String time) {
    this.orderNum = orderNum;
    this.customerName = customerName;
    this.customerPhone = customerPhone;
    this.day = day;
    this.time = time;
}

public void setMenu(Menu menu) {
    this.menu = menu;
}

//添加一条菜品信息到订单中。
boolean addRecord(int recordNum, String dishName, Integer spicy, int portion, int num, Replace replace) {
    if (menu != null) {
        Dish dish = menu.searthDish(dishName);
        if (dish != null) {
            if ("川菜".equals(dish.getType()) && (spicy < 0 || spicy > 5)) {
                System.out.println("spicy num out of range :" + spicy);
                return false;
            }
            if ("晋菜".equals(dish.getType()) && (spicy < 0 || spicy > 4)) {
                System.out.println("acidity num out of range :" + spicy);
                return false;
            }
            if ("浙菜".equals(dish.getType()) && (spicy < 0 || spicy > 3)) {
                System.out.println("sweetness num out of ranged :" + spicy);
                return false;
            }
            //遍历记录,避免添加重复的记录
            for (Record record : records) {
                if (record.getDish().getName().equals(dishName)
                        && record.getSpicy() == spicy
                        && record.getPortion() == portion
                        && record.getReplace() == replace) {
                    record.setNum(record.getNum() + num);
                    return true;
                }
            }
            records.add(new Record(recordNum, dish, spicy, portion, num, replace));
            return true;
        } else {
            System.out.println(dishName + " does not exist");
            return false;
        }
    } else {
        return false;
    }
}

public boolean deleteRecord(int recordNum) {
    for (Record record : records) {
        if (record.getRecordNum() == recordNum) {
            records.remove(record);
            return true;
        }
    }
    System.out.println("delete error;");
    return false;
}
}

class Replace {
protected int originNum;
protected int replaceNum;

public Replace(int originNum, int replaceNum) {
    this.originNum = originNum;
    this.replaceNum = replaceNum;
}

public int getOriginNum() {
    return originNum;
}

public int getReplaceNum() {
    return replaceNum;
}
}

 

 

 无bug。

 

3.期中第一题

 

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        double W = input.nextDouble();
        double H = input.nextDouble();
        if(W<=0 || W>727 || H<=0 || H>2.72)
        {
            System.out.println("input out of range");
            return;
        }
        double temp = W/(H*H);
        if(temp<18.5)
            System.out.println("thin");
        if(temp>=18.5 && temp<24)
            System.out.println("fit");
        if(temp>=24 && temp<28)
            System.out.println("overweight");
        if(temp>=28)
            System.out.println("fat");
    }
}

 

 

4、期中第二题

 

 

 

 

 

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        int Wt = 0;
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();
        int fang = input.nextInt();
        int ping = input.nextInt();
        double mian = input.nextDouble();
        double qi=0.0, ying=0.0, jiao=0.0, che=0.0;
        if(num == 1)
        {
            if(mian <= 90)
            {
                qi = 0.01 * ping * 10000;
            }
            if(mian > 90 && mian <= 144)
            {
                qi = 0.015 * ping * 10000;
            }
            if(mian > 144)
            {
                qi = 0.03 * ping * 10000;
            }
            ying = 0.0005 * fang * 10000;
            jiao = 3 * mian;
            che = 1.36 * mian;
        }
        else
        {
            qi = 0.03 * ping * 10000;
            ying = 0.0005 * fang * 10000;
            jiao = 3 * mian;
            che = 1.36 * mian;
        }
        System.out.println((float)qi+" "+(float)ying+" "+(float)jiao+" "+(float)che);
    }
}

 

 

 

 

5.期中第三题 

 

 

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int zhongZu = input.nextInt();
        int zhiYe = input.nextInt();
        if(zhongZu == 1){
            if(zhiYe == 1)
            System.out.print("人类"+" "+"战士");
        else if(zhiYe == 2)
            System.out.print("人类"+" "+"法师");
        else if(zhiYe == 3)
            System.out.print("人类"+" "+"射手");
        else{
            System.out.print("Wrong Format");
            return;
        }
        }
        else if(zhongZu == 2){
            if(zhiYe == 1)
            System.out.print("精灵"+" "+"战士");
        else if(zhiYe == 2)
            System.out.print("精灵"+" "+"法师");
        else if(zhiYe == 3)
            System.out.print("精灵"+" "+"射手");
        else{
            System.out.print("Wrong Format");
            return;
        }
        }
        else if(zhongZu == 3){
            if(zhiYe == 1)
            System.out.print("兽人"+" "+"战士");
        else if(zhiYe == 2)
            System.out.print("兽人"+" "+"法师");
        else if(zhiYe == 3)
            System.out.print("兽人"+" "+"射手");
        else{
            System.out.print("Wrong Format");
            return;
        }
        }
        else if(zhongZu == 4){
            if(zhiYe == 1)
                System.out.print("暗精灵"+" "+"战士");
            else if(zhiYe == 2)
                System.out.print("暗精灵"+" "+"法师");
            else if(zhiYe == 3)
                System.out.print("暗精灵"+" "+"射手");
            else{
                System.out.print("Wrong Format");
                return;
            }
        }
        else{
            System.out.print("Wrong Format");
            return;
        }
    }
}

 

 

无bug。

 

6.期中第四题

 

 

 

import java.util.*;
import java.text.*;
public class Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        String[] nums = null;
        nums = input.nextLine().split("");
        int[] bianHao = new int[nums.length];
        if(nums.length != 8)
        {
            System.out.println("Wrong Format");
            return;
        }
        for(int i=0; i<nums.length; i++)
        {
            bianHao[i] = Integer.valueOf(nums[i]);
        }
        int nianFen=0, xueYuan=0, banJi=0, xueHao=0;
        nianFen = 2000 + bianHao[0]*10 + bianHao[1];
        xueYuan = bianHao[2]*10 + bianHao[3];
        banJi = bianHao[4]*10 + bianHao[5];
        xueHao = bianHao[6]*10 + bianHao[7];
        if(xueYuan != 1 && xueYuan != 3 && xueYuan != 2 && xueYuan != 20)
        {
            System.out.println("Wrong Format");
            return;
        }
        else
        {
            System.out.println("入学年份:"+nianFen+"年");
            if(xueYuan == 1)
                System.out.println("学院:材料学院");
            if(xueYuan == 2)
                System.out.println("学院:机械学院");
            if(xueYuan == 3)
                System.out.println("学院:外语学院");
            if(xueYuan == 20)
                System.out.println("学院:软件学院");
//             NumberFormat nf_banji = new DecimalFormat("00");
//             NumberFormat nf_xuehao = new DecimalFormat("00");
//             String str_banji = nf_banji.format(banji);
//             String str_xuehao = nf_xuehao.format(xuehao);
            System.out.println("班级:" + String.format("%02d", banJi));
            System.out.println("学号:" + String.format("%02d", xueHao));
        }
    }
}

 

 

我在之后的学习中需要更加注意培养面向对象程序设计思维,提高逻辑思维能力,避免出现看见一大堆题目要求时茫然且不知如何下手,或者编写很多语句相互嵌套之后出现bug而无法发现错误的情况。

 

 

posted @ 2023-06-29 21:21  Mikofa  阅读(51)  评论(1)    收藏  举报