PTA4、5、6及期中考试总结

发布时间 2023-11-19 15:23:03作者: GuazhuEastRoad

本次是第二次博客,主要的习题是pta4、5、6以及期中考试,pta的题目主要是延续了之前的菜单题目,相较于之前的菜单增加了许多功能如增加桌号,增加时间,增加了特色菜,增加删除某条记录等,期中考试的题目则是选择题加上大题目,选择题是对Java的一些基础知识的考察,大题目主要考察了类的设计,继承和多态,抽象类与接口。

pta4有四个题目分别是菜单计价程序2、3和判断日期,统计字符次数,菜单系列比较连贯的系列,前面的书写对于后面的菜单有较大的作用但是由于难度较大开始没有写的很好导致后面的菜单计价程序也都写的不行在这次中就写出来了部分题目,判断日期和统计字符次数。

判断日期先后程序,这个对于后面的菜单的计算时间范围内功能有作用

 1 import java.util.Scanner;
 2 import java.text.ParseException;
 3 import java.text.SimpleDateFormat;
 4 import java.util.Date;
 5 
 6 public class Main {
 7     public static void main(String[] args) throws ParseException {
 8         Scanner sc = new Scanner(System.in);
 9         String date1 = sc.next();
10         String date2 = sc.next();
11         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
12         Date d1=sdf.parse(date1);
13         Date d2=sdf.parse(date2);
14         long daysBetween=(d2.getTime()/(60*60*24*1000)-d1.getTime()/(60*60*24*1000));
15         //System.out.println(daysBetween);
16         Integer i = date1.compareTo(date2);
17         long between = Math.abs(daysBetween);
18         if(i<0)
19         {
20             long between1 = between;
21             int week1 = (int)between1/7;
22             System.out.println("第一个日期比第二个日期更早");
23             System.out.println("两个日期间隔"+between1+"天");
24             System.out.println("两个日期间隔"+week1+"周");
25         }
26 
27         if(i>0)
28         {
29             //int between3 = ChronoUnit.DAYS.between(date1,date2);
30             long between2 = between;
31             int week2 = (int)(between2/7);
32             System.out.println("第一个日期比第二个日期更晚");
33             System.out.println("两个日期间隔"+between2+"天");
34             System.out.println("两个日期间隔"+week2+"周");
35                 
36         }
37         
38         
39     }
40 }

pta5就只有一个题目菜单计价程序4,这次是一个综合性的题目但是由于基础不好前面的也没有写的很好,所以导致后面的程序也受影响只能写出部分功能,

相较于第三次,增加了特色菜的功能,以及不同时间范围内不同折数的功能,在菜单计价程序-3的基础上也增加了特色菜的处理以及大量异常输入的处理,由于基础不好,这次写的代码不仅多而且功能也不完善,只能实现部分测试点。

import java.util.Scanner;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;

public class Main {
    public static void main(String[] args) {
        
        Main main = new Main();
        Scanner scanner = new Scanner(System.in);
        ArrayList<String> messageList = new ArrayList<String>();
        ArrayList<String> arrayList = new ArrayList<String>();
        ArrayList<Table> tArrayList = new ArrayList<Main.Table>();
        String string = "";
        
        while (true) {
            string = scanner.nextLine();
            if ("end".equals(string)) {
                break;
            }
            arrayList.add(string);
        }
        Menu menu = main.new Menu();
        menu.dishs = new Dish[0];
        int n = 0;
        for (int i = 0; i < arrayList.size(); i++) {
            String[] strings = arrayList.get(i).split(" ");
            if (strings.length==0) {
                messageList.add("wrong format");
                break;
            }
            if (strings.length > 3&&strings[0].charAt(0)=='t') {
                n = i;
                break;
            }else if(strings.length > 3){
                messageList.add("wrong format");
                continue;
            }
            String dName = strings[0];
            String dPrice = strings[1];
            if (dPrice.charAt(0) == '0') {
                messageList.add("wrong format");
                continue;
            }
            int hp = 0;
            try {
                hp = Integer.parseInt(dPrice);
            } catch (Exception e) {
                messageList.add("wrong format");
                continue;
            }
            if (hp >= 300 || hp <= 0) {
                messageList.add(dName + " price out of range "+hp);
                continue;
            }
            if (menu.searthDish(dName) != null) {
                for (int j = 0; j < menu.dishs.length; j++) {
                    if (menu.dishs[j].equals(dName)) {
                        menu.dishs[j].unit_price = hp;
                    }
                }
            } else {
                Dish dish = main.new Dish();
                if (strings.length == 2) {
                    dish.name = dName;
                    dish.unit_price = hp;
                } else if (strings.length == 3) {
                    dish.name = dName;
                    dish.unit_price = hp;
                    dish.isT = true;
                }
                menu.addDish(dish);
            }
        }
        ArrayList<Integer> arrayLista = new ArrayList<Integer>();// 桌号开始下标
        for (int i = n; i < arrayList.size(); i++) {
            if (arrayList.get(i).split(" ")[0].length() > 2 && arrayList.get(i).split(" ")[0].charAt(0) == 't') {
                arrayLista.add(i);
            }
        }
        
        for (int i = 0; i < arrayLista.size(); i++) {
            Table table = main.new Table();
            Order order = main.new Order();
            order.records = new Record[0];
            table.order = order;
            int t1 = arrayLista.get(i);
            String[] strings = arrayList.get(t1).split(" ");
            if (strings.length==0) {
                messageList.add("wrong format");
                continue;
            }
            if (strings[0].equals("table")) {
                if (strings[1].charAt(0) == '0') {
                    messageList.add("wrong format");
                    continue;
                }
                int tNum = 0;
                try {
                    tNum = Integer.parseInt(strings[1]);
                } catch (Exception e) {
                    messageList.add("wrong format");
                    continue;
                }
                if (tNum > 55 && tNum <= 0) {
                    messageList.add(tNum + " num out of range");
                    continue;
                }
                if (strings[2].split("/")[0].length() != 4) {// year
                    messageList.add("wrong format");
                    continue;
                }
                if (strings[2].split("/")[1].length() > 2 || strings[2].split("/")[1].length() < 1) {// month
                    messageList.add("wrong format");
                    continue;
                }
                if (strings[2].split("/")[2].length() > 2 || strings[2].split("/")[2].length() < 1) {// day
                    messageList.add("wrong format");
                    continue;
                }
                if (strings[3].split("/")[0].length() > 2 || strings[3].split("/")[0].length() < 1) {// hour
                    messageList.add("wrong format");
                    continue;
                }
                if (strings[3].split("/")[1].length() > 2 || strings[3].split("/")[1].length() < 1) {// minute
                    messageList.add("wrong format");
                    continue;
                }
                if (strings[3].split("/")[2].length() > 2 || strings[3].split("/")[2].length() < 1) {// moment
                    messageList.add("wrong format");
                    continue;
                }
                DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH/mm/ss");
                sdf.setLenient(false);
                Date date = null;
                try {
                    date = sdf.parse(strings[2] + " " + strings[3]);
                } catch (ParseException e) {
                    messageList.add(tNum + " date error");
                    continue;
                }
                table.date = date;
                table.tableNum = tNum;
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/MM/dd");
                SimpleDateFormat sdf2 = new SimpleDateFormat("HH/mm/ss");
                Date date1 = null;
                Date date2 = null;
                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) {
                        messageList.add("not a valid time period");
                        continue;
                    }
                } catch (ParseException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                int dayNum = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                Date bdate = null;
                Date edate = null;
                Date bdate1 = null;
                Date edate1 = null;
                Date bdate2 = null;
                Date edate2 = null;
                Date nowDate = null;
                try {
                    bdate = sdf2.parse("9/29/59");
                    edate = sdf2.parse("21/30/00");
                    bdate1 = sdf2.parse("10/30/00");
                    edate1 = sdf2.parse("14/30/00");
                    bdate2 = sdf2.parse("16/59/59");
                    edate2 = sdf2.parse("20/30/00");
                    nowDate = sdf2.parse(strings[3]);
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (dayNum == 6 || dayNum == 0) {// 周末
                    if (timeCalendar(nowDate, bdate, edate)) {// 全价
                        table.dicount = 1.0;
                    } else {
                        messageList.add("table " + tNum + " out of opening hours");
                        continue;
                    }
                } else {// 工作日
                    if (timeCalendar(nowDate, bdate1, edate1)) {// 6折
                        table.dicount = 0.6;
                    } else if (timeCalendar(nowDate, bdate2, edate2)) {// 8折
                        table.dicount = 0.8;
                    } else {
                        messageList.add("table " + tNum + " out of opening hours");
                        continue;
                    }
                }
                messageList.add("table"+" "+tNum + ": ");
                if (t1 == arrayList.size()-1) {
                } else {
                    int fNum;
                    if (arrayLista.get(arrayLista.size()-1) == t1) {
                        fNum = arrayList.size();
                    } else {
                        fNum = arrayLista.get(i + 1);
                    }
                    ArrayList<Integer> delNumArrayList = new ArrayList<Integer>();
                    int numb = 0;
                    for (int j = t1 + 1; j < fNum; j++) {// 点菜记录
                        String[] strings2 = arrayList.get(j).split(" ");
                        if (strings2.length==0) {
                            messageList.add("wrong format");
                            continue;
                        }
                        int Num1;
                        try {
                            Num1 = Integer.parseInt(strings2[0]);
                        } catch (Exception e) {
                            messageList.add("invalid dish");
                            continue;
                        }
                        if (strings2[0].charAt(0)=='0') {
                            messageList.add("wrong format");
                            continue;
                        }
                        if (strings2.length == 5) {// 代点菜
                            boolean flag = false;

                                if (tArrayList.get(0).tableNum == Num1) 
                                    flag = true;
                            if (flag) {
                                messageList.add("Table number :" + Num1 + " does not exist");
                                continue;
                            }
                            if (strings2[3].length() > 1) {
                                messageList.add("wrong format");
                                continue;
                            } else {
                                String dishName = strings2[2];// 菜名
                                int dishP;
                                int dishN;
                                if (strings2[4].charAt(0) == '0') {
                                    messageList.add("wrong format");
                                    continue;
                                }
                                try {
                                    dishP = Integer.parseInt(strings2[3]);
                                    dishN = Integer.parseInt(strings2[4]);
                                } catch (Exception e) {
                                    messageList.add("wrong format");
                                    continue;
                                }
                                if (menu.searthDish(dishName) == null) {
                                    messageList.add(dishName + " does not exist");
                                    continue;
                                }
                                Dish dish = menu.searthDish(dishName);
                                if (dish.isT) {
                                    if (dishP != 1 && dishP != 3&& dishP != 2) {
                                        messageList.add(Num1 + " portion out of range " + dishP);
                                        continue;
                                    }
                                } else {
                                    if (dishP != 1 && dishP != 3 && dishP != 2) {
                                        messageList.add(Num1 + " portion out of range " + dishP);
                                        continue;
                                    }
                                }
                                if (dishN > 15 || dishN < 1) {
                                    messageList.add(Num1 + " num out of range " + dishN);
                                    continue;
                                }
                                boolean f1 = true;
                                if (f1) {
                                    table.order.addARecord(Num1, dish, dishP, dishN);
                                }
                            }
                        } else if (strings2.length == 2) {
                            if (!strings2[1].equals("delete")) {
                                messageList.add("wrong format");
                                continue;
                            } else {
                                boolean flag = false;
                                for (Integer n2 : delNumArrayList) {
                                    if (n2 == Num1) {
                                        flag = true;
                                    }
                                }
                                if (flag) {
                                    messageList.add("deduplication " + Num1);
                                    continue;
                                }
                                if (table.order.findRecordByNum(Num1) == -1) {
                                    messageList.add("delete error");
                                    continue;
                                }
                                table.order.delARecordByOrderNum(Num1);
                                delNumArrayList.add(Num1);
                            }
                        } else if (strings2.length == 4) {
                            if (strings2[2].length() > 1) {
                                messageList.add("wrong format");
                                continue;
                            } else {
                                String dishName = strings2[1];// 菜名
                                int dishP;
                                int dishN;
                                if (strings2[3].charAt(0) == '0') {
                                    messageList.add("wrong format");
                                    continue;
                                }
                                try {
                                    dishP = Integer.parseInt(strings2[2]);
                                    dishN = Integer.parseInt(strings2[3]);
                                } catch (Exception e) {
                                    messageList.add("wrong format");
                                    continue;
                                }
                                if (Num1 <= numb) {
                                    messageList.add("record serial number sequence error");
                                    continue;
                                }
                                if (menu.searthDish(dishName) == null) {
                                    messageList.add(dishName + " does not exist");
                                    continue;
                                }
                                Dish dish = menu.searthDish(dishName);
                                if (dish.isT) {
                                    if (dishP != 1 && dishP != 3&& dishP != 2) {
                                        messageList.add(Num1 + " portion out of range " + dishP);
                                        continue;
                                    }
                                } else {
                                    if (dishP != 1 && dishP != 3 && dishP != 2) {
                                        messageList.add(Num1 + " portion out of range " + dishP);
                                        continue;
                                    }
                                }
                                if (dishN > 15 || dishN < 1) {
                                    messageList.add(Num1 + " num out of range " + dishN);
                                    continue;
                                }
                                numb = Num1;
                                boolean f1 = true;
                                for (int k = 0; k < table.order.records.length; k++) {
                                    if (table.order.records[k].d.name.equals(dishName)
                                            && table.order.records[k].portion == dishP) {
                                        table.order.records[k].num = table.order.records[k].num + dishN;
                                        f1 = false;
                                    }
                                }
                                if (f1) {
                                    table.order.addARecord(Num1, dish, dishP, dishN);
                                    messageList.add(Num1 + " " + dishName + " " + dish.getPrice(dishP) * dishN);
                                }
                            }
                        } else {
                            messageList.add("wrong format");
                            continue;
                        }
                    }
                }
            } else {
                messageList.add("wrong format");
                if (t1 == arrayList.size()-1) {
                } else {
                    int fNum;
                    if (arrayLista.get(arrayLista.size() - 1) == t1) {
                        fNum = arrayList.size();
                    } else {
                        fNum = arrayLista.get(i + 1)+1;
                    }
                    Table table2 = tArrayList.get(tArrayList.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 = arrayList.get(j).split(" ");
                        if (strings2.length==0) {
                            messageList.add("wrong format");
                            continue;
                        }
                        int Num1;
                        try {
                            Num1 = Integer.parseInt(strings2[0]);
                        } catch (Exception e) {
                            messageList.add("invalid dish");
                            continue;
                        }
                        if (strings2[0].charAt(0)=='0') {
                            messageList.add("wrong format");
                            continue;
                        }
                        if (strings2.length == 5) {// 代点菜
                            boolean flag = true;
                            for (int k = 0; k < tArrayList.size(); k++) {
                                
                                if (tArrayList.get(k).tableNum == Num1) {
                                    flag = false;
                                }
                            }
                            if (flag) {
                                messageList.add("Table number :" + Num1 + " does not exist");
                                continue;
                            }
                            if (strings2[3].length() > 1) {
                                messageList.add("wrong format");
                                continue;
                            } else {
                                String dishName = strings2[2];// 菜名
                                int dishP;
                                int dishN;
                                if (strings2[4].charAt(0) == '0') {
                                    messageList.add("wrong format");
                                    continue;
                                }
                                try {
                                    dishP = Integer.parseInt(strings2[3]);
                                    dishN = Integer.parseInt(strings2[4]);
                                } catch (Exception e) {
                                    messageList.add("wrong format");
                                    continue;
                                }
                                if (menu.searthDish(dishName) == null) {
                                    messageList.add(dishName + " does not exist");
                                    continue;
                                }
                                Dish dish = menu.searthDish(dishName);
                                if (dish.isT) {
                                    if (dishP != 1 && dishP != 3) {
                                        messageList.add(Num1 + " portion out of range " + dishP);
                                        continue;
                                    }
                                } else {
                                    if (dishP != 1 && dishP != 3 && dishP != 2) {
                                        messageList.add(Num1 + " portion out of range " + dishP);
                                        continue;
                                    }
                                }
                                if (dishN > 15 || dishN < 1) {
                                    messageList.add(Num1 + " num out of range " + dishN);
                                    continue;
                                }
                                boolean f1 = true;
                                if (f1) {
                                    table2.order.addARecord(Num1, dish, dishP, dishN);
                                }
                            }
                        } else if (strings2.length == 2) {
                            if (!strings2[1].equals("delete")) {
                                messageList.add("wrong format");
                                continue;
                            } else {
                                boolean flag = false;
                                for (Integer n2 : delNumArrayList) {
                                    if (n2 == Num1) {
                                        flag = true;
                                    }
                                }
                                if (flag) {
                                    messageList.add("deduplication " + Num1);
                                    continue;
                                }
                                if (table2.order.findRecordByNum(Num1) == -1) {
                                    messageList.add("delete error");
                                    continue;
                                }
                                table2.order.delARecordByOrderNum(Num1);
                                delNumArrayList.add(Num1);
                            }
                        } else if (strings2.length == 4) {
                            if (strings2[2].length() > 1) {
                                messageList.add("wrong format");
                                continue;
                            } else {
                                String dishName = strings2[1];// 菜名
                                int dishP;
                                int dishN;
                                if (strings2[3].charAt(0) == '0') {
                                    messageList.add("wrong format");
                                    continue;
                                }
                                try {
                                    dishP = Integer.parseInt(strings2[2]);
                                    dishN = Integer.parseInt(strings2[3]);
                                } catch (Exception e) {
                                    messageList.add("wrong format");
                                    continue;
                                }
                                if (Num1 <= numb) {
                                    messageList.add("record serial number sequence error");
                                    continue;
                                }
                                if (menu.searthDish(dishName) == null) {
                                    messageList.add(dishName + " does not exist");
                                    continue;
                                }
                                Dish dish = menu.searthDish(dishName);
                                if (dish.isT) {
                                    if (dishP != 1 && dishP != 3) {
                                        messageList.add(Num1 + " portion out of range " + dishP);
                                        continue;
                                    }
                                } else {
                                    if (dishP != 1 && dishP != 3 && dishP != 2) {
                                        messageList.add(Num1 + " portion out of range " + dishP);
                                        continue;
                                    }
                                }
                                if (dishN > 15 || dishN < 1) {
                                    messageList.add(Num1 + " num out of range " + dishN);
                                    continue;
                                }
                                numb = Num1;
                                boolean f1 = true;
                                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;
                                        f1 = false;
                                    }
                                }
                                if (f1) {
                                    table2.order.addARecord(Num1, dish, dishP, dishN);
                                    messageList.add(Num1 + " "+ dishName+" " + dish.getPrice(dishP) *dishN);
                                }
                            }
                        } else {
                            messageList.add("wrong format");
                            continue;
                        }
                    }
                }
            }
            if (table.tableNum!=0) {
                tArrayList.add(table);
            }
        }
        for (int j = 0; j < messageList.size(); j++) {
            System.out.println(messageList.get(j));
        }
        for (int j = 0; j < tArrayList.size()-1; j++) {
            if (tArrayList.get(0).tableNum<tArrayList.get(1).tableNum) {
                Table table = main.new Table();
                table = tArrayList.get(1);
                tArrayList.set(1, tArrayList.get(0));
                tArrayList.set(0, table);
            }
        }
        for (int j = 0; j < tArrayList.size(); j++) {
            int tableNum = tArrayList.get(j).tableNum;
            Order order =tArrayList.get(j).order;
            int sum = 0;
            int sumP = 0;
            for (int i = 0; i < order.records.length; i++) {
                Record record = order.records[i];
                if (record.d.isT&&tArrayList.get(j).dicount!=1.0) {
                    sum +=(int)Math.round(record.getPrice());
                    sumP +=(int)Math.round(record.getPrice()*0.7);
                }else {
                    sum +=record.getPrice();
                    sumP +=(int)Math.round(record.getPrice()*tArrayList.get(j).dicount);
                }
            }
            if (j==tArrayList.size()-1) {
                System.out.print("table"+" "+tableNum+":"+" "+sum+" "+sumP);
            }else {
                System.out.println("table"+" "+tableNum+":"+" "+sum+" "+sumP);
            }
        }
    }

    // 菜品类
    class Dish {
        String name;// 菜品名称
        int unit_price; // 单价
        boolean isT = false;

        int getPrice(int portion) {// 计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
            double money = 0;
            switch (portion) {
            case 1:// 小份
                money = this.unit_price * 1.0;
                break;
            case 2:// 中份
                money = 1.5 * this.unit_price;
                break;
            case 3:// 大份
                money = this.unit_price * 2.0;
                break;
            }
            return (int) Math.round(money);
        }
    }

    // 菜谱类:对应菜谱,包含饭店提供的所有菜的信息
    class Menu {

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

        Dish searthDish(String dishName) {
            for (int i = 0; i < dishs.length; i++) {
                if (dishs[i].name.equals(dishName)) {
                    return dishs[i];
                }
            }
            return null;
        }

        Dish addDish(Dish dish) {// 添加一道菜品信息
            Dish[] dishs1 = new Dish[dishs.length + 1];
            
            for (int i = 0; i < dishs.length; i++) {
                dishs1[i] = dishs[i];
            }
            dishs1[dishs.length] = dish;
            dishs = dishs1;
            return dish;
        }
    }

    // 点菜记录类:保存订单上的一道菜品记录
    class Record {

        int orderNum;// 序号
        Dish d;// 菜品
        int portion;// 份额(1/2/3代表小/中/大份)
        int num;// 份数

        int getPrice() {// 计价,计算本条记录的价格
            return (int) Math.round(this.d.getPrice(this.portion) * num);
        }

    }

    // 订单类:保存用户点的所有菜的信息
    class Order {
        Record[] records;// 保存订单上每一道的记录
        
        int getTotalPrice() {// 计算订单的总价
            int sum = 0;
            for (int i = 0; i < records.length; i++) {
                sum += records[i].getPrice();
            }
            return sum;
        }

        // 添加一条菜品信息到订单中
        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 delARecordByOrderNum(int orderNum) {
            int n = this.findRecordByNum(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 findRecordByNum(int orderNum) {
            int n = -1;
            for (int i = 0; i < this.records.length; i++) {
                if (this.records[i].orderNum == orderNum) {
                    n = i;
                }
            }
            return n;
        }
    }

    class Table {
        int tableNum;
        Order order;
        Date date;
        double dicount = 0;
    }

    public static boolean timeCalendar(Date nowTime, Date beginTime, Date endTime) {
        Calendar date = Calendar.getInstance();// 设置当前时间
        date.setTime(nowTime);
        Calendar begin = Calendar.getInstance();// 设置开始时间
        begin.setTime(beginTime);// 开始时间
        Calendar end = Calendar.getInstance();// 设置结束时间
        end.setTime(endTime);// 上午结束时间
        
        if ((date.after(begin) && date.before(end))) {// 判断是否处于开始时间之后和结束时间之前
            return true;
        } else {
            return false;
        }
    }
}

pta6也是菜单计价程序,菜单计价程序5相较于4增加的功能并不多,但对于我个人也难,还是不能写出全部只有部分功能

 

这次增加了口味类型以及多桌点菜记录,特色菜有川菜、晋菜、浙菜之分,而且增加了口味度,对每桌菜订单数据的输出增加了按英文字典顺序输出。首先处理人名、电话数据,由于人名和电话是和订单桌号相联系又对应关系的,人名和电话应该属于table类中的属性,只需要在table类中加入人名、电话号属性即可

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
    public static void main(String[] args) throws ParseException {
        Menu menu = new Menu();

        // 读入菜单信息
        Scanner sc = new Scanner(System.in);

        String menuLine = sc.nextLine();
        while (!menuLine.startsWith("table")) {
            String[] menuInfo = menuLine.split(" ");

            if (menuInfo.length == 2) {
                String name = menuInfo[0];
                int unit_price = Integer.parseInt(menuInfo[1]);
                if (menu.searchDish(name) == null) {
                    menu.addDish(name, unit_price);
                }
            } else if (menuInfo.length == 4 && menuLine.endsWith("T")) {
                String name = menuInfo[0];
                String type = menuInfo[1];
                int unit_price = Integer.parseInt(menuInfo[2]);

                Map<String, String> map = new HashMap<String, String>() {
                    {
                        put("川菜", "Chuan");
                        put("晋菜", "Jin");
                        put("浙菜", "Zhe");
                    }
                };

                DishType dishType = DishType.valueOf(map.get(type));

                if (menu.searchDish(name) == null) {
                    menu.addDish(name, unit_price, dishType);
                }
            } else {
                System.out.println("wrong format");
            }

            menuLine = sc.nextLine();
        }

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

        // 读入订单信息
        int tableId = 0;
        String name = null;
        String phone = null;
        Date date = null;
        Date time = null;
        boolean propertime = true;
        boolean legalformat = true;

        String orderLine = menuLine;
        while (!orderLine.equals("end")) {
            String[] orderInfo = orderLine.split(" ");

            // 解析桌号标识
            if (orderLine.startsWith("table")) {
                legalformat = true;
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
                SimpleDateFormat timeFormat = new SimpleDateFormat("HH/mm/ss");

                tableId = Integer.parseInt(orderInfo[1]);
                name = orderInfo[3];
                phone = orderInfo[4];
                try {
                    date = dateFormat.parse(orderInfo[5]);
                    time = timeFormat.parse(orderInfo[6]);
                } catch (Exception e) {
                    legalformat = false;
                    System.out.println("wrong format");
                    orderLine = sc.nextLine();
                    continue;
                }

                String regex = "^1(80|81|89|33|35|36)\\d{8}$";

                Table table = new Table(tableId, name, phone, date, time);
                tables.add(table);

                if (name.length() > 10 || !phone.matches(regex)) {
                    legalformat = false;
                    System.out.println("wrong format");
                    orderLine = sc.nextLine();
                    continue;
                }

                if (!names.contains(name)) {
                    names.add(name);
                }

                if (table.getCoefficient(true) == 0) {
                    propertime = false;
                    System.out.println("table " + table.tableId + " out of opening hours");
                } else {
                    System.out.println(table.printId());
                }
            } else {
                if (legalformat) {
                    int orderNum;

                    try {
                        orderNum = Integer.parseInt(orderInfo[0]);
                    } catch (Exception e) {
                        System.out.println("wrong format");
                        orderLine = sc.nextLine();
                        continue;
                    }

                    if (orderLine.endsWith("delete")) {
                        if (!tables.get(tableId - 1).delRECORDByOrderNum(orderNum)) {
                            System.out.println("delete error");
                        }
                    } else {
                        if (orderInfo.length == 4) {
                            String dishName = orderInfo[1];
                            int portion = Integer.parseInt(orderInfo[2]);
                            int quantity = Integer.parseInt(orderInfo[3]);

                            Dish dish = menu.searchDish(dishName);

                            if (dish == null) {
                                System.out.println(dishName + " does not exist");
                                orderLine = sc.nextLine();
                                continue;
                            }

                            RECORD record = new RECORD(tableId, orderNum, dish, portion, quantity);

                            tables.get(tableId - 1).addRECORD(record);

                            if (propertime) {
                                System.out.println(record.print(tableId));
                            }
                        } else if (orderInfo.length == 5) {
                            String dishName = orderInfo[1];
                            int level = Integer.parseInt(orderInfo[2]);
                            int portion = Integer.parseInt(orderInfo[3]);
                            int quantity = Integer.parseInt(orderInfo[4]);

                            Dish dish = menu.searchDish(dishName);

                            if (dish == null) {
                                System.out.println(dishName + " does not exist");
                                orderLine = sc.nextLine();
                                continue;
                            }

                            RECORD record = new RECORD(tableId, orderNum, dish, level, portion, quantity);

                            tables.get(tableId - 1).addRECORD(record);

                            if (propertime) {
                                System.out.println(record.print(tableId));
                            }
                        } else if (orderInfo.length == 6) {
                            int givenId = Integer.parseInt(orderInfo[1]);
                            String dishName = orderInfo[2];
                            int level = Integer.parseInt(orderInfo[3]);
                            int portion = Integer.parseInt(orderInfo[4]);
                            int quantity = Integer.parseInt(orderInfo[5]);

                            Dish dish = menu.searchDish(dishName);

                            if (dish == null) {
                                System.out.println(dishName + " does not exist");
                                orderLine = sc.nextLine();
                                continue;
                            }

                            RECORD record1 = new RECORD(givenId, orderNum, dish, level, portion, quantity);
                            RECORD record2 = new RECORD(givenId, orderNum, dish, level, 0, quantity);

                            tables.get(tableId - 1).addRECORD(record1);
                            tables.get(givenId - 1).addRECORD(record2);

                            if (propertime) {
                                System.out.println(record1.print(tableId));
                            }
                        } else {
                            System.out.println("wrong format");
                        }
                    }
                }
            }

            // 读入下一个桌号标识
            orderLine = sc.nextLine();
        }

        sc.close();

        for (Table table : tables) {
            if (table.flag && table.getTotalPrice() != 0) {
                System.out.println(table.printInfo());
            }
        }

        names.sort(new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                return s1.compareTo(s2);
            }
        });

        for (String costumName : names) {
            int sum = 0;
            String costumPhone = null;
            for (Table table : tables) {
                if (table.name.equals(costumName)) {
                    sum += table.getCheckedPrice();
                    costumPhone = table.phone;
                }
            }

            if (sum != 0) {
                System.out.println(costumName + " " + costumPhone + " " + sum);
            }
        }
    }
}

enum DishType {
    Chuan,
    Jin,
    Zhe,
}

class Dish {
    public String name;
    public int unit_price;
    public DishType type;

    public Dish(String name, int unit_price, DishType type) {
        this.name = name;
        this.unit_price = unit_price;
        this.type = type;
    }

    public Dish(String name, int unit_price) {
        this.name = name;
        this.unit_price = unit_price;
    }

    @Override
    public String toString() {
        return name;
    }
}

class Menu {
    public ArrayList<Dish> dishs = new ArrayList<>();

    public Dish searchDish(String dishName) {
        for (Dish dish : dishs) {
            if (dish.name.equals(dishName)) {
                return dish;
            }
        }

        return null;
    }

    void addDish(String dishName, int unit_price) {
        dishs.add(new Dish(dishName, unit_price));
    }

    void addDish(String dishName, int unit_price, DishType type) {
        dishs.add(new Dish(dishName, unit_price, type));
    }
}

class RECORD {
    int orderNum;
    Dish dish;
    int portion;
    int quantity;
    int level;
    boolean flag;
    int givenId;

    boolean check_level() {
        switch (dish.type) {
            case Chuan:
                if (level > 5 || level < 0) {
                    return false;
                } else {
                    return true;
                }
            case Jin:
                if (level > 4 || level < 0) {
                    return false;
                } else {
                    return true;
                }
            case Zhe:
                if (level > 3 || level < 0) {
                    return false;
                } else {
                    return true;
                }
            default:
                return true;
        }
    }

    public RECORD(int givenID, int orderNum, Dish dish, int portion, int quantity) {
        this.orderNum = orderNum;
        this.dish = dish;
        this.portion = portion;
        this.quantity = quantity;
        this.level = -1;
        this.flag = true;
        this.givenId = givenID;
    }

    public RECORD(int givenId, int orderNum, Dish dish, int level, int portion, int quantity) {
        this.orderNum = orderNum;
        this.dish = dish;
        this.portion = portion;
        this.quantity = quantity;
        this.level = level;
        this.flag = check_level();
        this.givenId = givenId;
    }

    int getPrice() {
        if (!flag)
            return 0;

        double cocount = 0;

        switch (portion) {
            case 1:
                cocount = 1;
                break;
            case 2:
                cocount = 1.5;
                break;
            case 3:
                cocount = 2;
                break;
        }

        int price = (int) Math.round(dish.unit_price * cocount) * quantity;

        return price;
    }

    int getCheckedPrice(Double cocount) {
        return (int) Math.round(getPrice() * cocount);
    }

    public String print(int tableId) {
        if (flag == false) {
            switch (dish.type) {
                case Chuan:
                    return "spicy num out of range :" + level;
                case Jin:
                    return "acid num out of range :" + level;
                case Zhe:
                    return "sweet num out of range :" + level;
                default:
                    return null;
            }
        } else {
            if (givenId == tableId) {
                return orderNum + " " + dish.toString() + " " + getPrice();
            }

            return orderNum + " table " + tableId + " pay for table " + givenId + " " + getPrice();
        }
    }

    @Override
    public String toString() {
        return "RECORD [orderNum=" + orderNum + ", dish=" + dish + ", portion=" + portion + ", quantity=" + quantity
                + ", level=" + level + ", flag=" + flag + ", givenId=" + givenId + "]";
    }
}

class Table {
    ArrayList<RECORD> records = new ArrayList<>();
    int tableId;
    String name;
    String phone;
    Date date;
    Date time;
    boolean flag;

    public Table(int tableId, String name, String phone, Date date, Date time) {
        this.name = name;
        this.phone = phone;
        this.date = date;
        this.time = time;
        this.tableId = tableId;
        this.flag = true;
    }

    double getCoefficient(boolean Special) throws ParseException {
        double cocount = 0;

        SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm");

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

        // 营业时间
        if (dayOfWeek == 1 || dayOfWeek == 7) {
            if (time.after(sdfTime.parse("9:29")) && time.before(sdfTime.parse("21:31"))) {
                cocount = 1;
            }
        } else {
            if (time.after(sdfTime.parse("16:59")) && time.before(sdfTime.parse("20:31"))) {
                if (Special) {
                    cocount = 0.7;
                } else {
                    cocount = 0.8;
                }
            } else if (time.after(sdfTime.parse("10:29")) && time.before(sdfTime.parse("14:31"))) {
                if (Special) {
                    cocount = 0.7;
                } else {
                    cocount = 0.6;
                }
            }
        }

        if (cocount == 0) {
            flag = false;
        }

        return cocount;
    }

    int getTotalPrice() {
        int sum = 0;

        for (RECORD record : records) {
            sum += record.getPrice();
        }

        return sum;
    }

    int getCheckedPrice() throws ParseException {
        int sum = 0;

        for (RECORD record : records) {
            if (record.level != -1) {
                sum += record.getCheckedPrice(getCoefficient(true));
            } else {
                sum += record.getCheckedPrice(getCoefficient(false));
            }
        }

        return sum;
    }

    String getAveLevel(DishType type) {
        String[] spicy = { "不辣", "微辣", "稍辣", "辣", "很辣", "爆辣" };
        String[] acid = { "不酸", "微酸", "稍酸", "酸", "很酸" };
        String[] sweet = { "不甜", "微甜", "稍甜", "甜" };

        double sum = 0;
        double num = 0;

        for (RECORD record : records) {
            if (record.dish.type == type) {
                if (record.flag && tableId == record.givenId) {
                    num += record.quantity;
                    sum += record.level * record.quantity;
                }
            }
        }

        if (num == 0) {
            return "";
        }

        int ave = (int) Math.round(sum / num);

        switch (type) {
            case Chuan:
                return " 川菜 " + (int) num + " " + spicy[ave];
            case Jin:
                return " 晋菜 " + (int) num + " " + acid[ave];
            case Zhe:
                return " 浙菜 " + (int) num + " " + sweet[ave];
            default:
                return null;
        }
    }

    void addRECORD(RECORD record) {
        records.add(record);
    }

    boolean delRECORDByOrderNum(int orderNum) {
        return records.removeIf(record -> record.orderNum == orderNum);
    }

    RECORD findRECORDByOrderNum(int orderNum) {
        for (RECORD record : records)
        {
            if (record.orderNum == orderNum)
            {
                return record;
            }
        }
        return null;
    }

    public String printId() {
        return "table " + tableId + ": ";
    }

    public String printInfo() throws ParseException {
        
        String jin = getAveLevel(DishType.Jin);
        String zhe = getAveLevel(DishType.Zhe);
        String chuan = getAveLevel(DishType.Chuan);
        if (chuan == "" && jin == "" && zhe == "")
        {
            return "table " + tableId + ": " + getTotalPrice() + " " + getCheckedPrice() + " ";
        } 
        else
        {
            return "table " + tableId + ": " + getTotalPrice() + " " + getCheckedPrice() + chuan + jin + zhe;
        }
    }
}

最后是期中考试,选择题就是对java基础语法的知识了解,这些可以通过对书籍和视频的学习来了解,其次就是大题目,通过类的设计来实现继承接口多态等功能,

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

 

import java.util.Scanner;

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

        int shapeType = scanner.nextInt();

        double area = 0.0;

        if (shapeType == 1) {
            double radius = scanner.nextDouble();
            if(radius<=0){
            System.out.println("Wrong Format");
            return;
        }
            area = calculateCircleArea(radius);
        } else if (shapeType == 2) {
        double x1 = scanner.nextDouble();
        double y1 = scanner.nextDouble();

        double x2 = scanner.nextDouble();
        double y2 = scanner.nextDouble();
            double length = Math.abs(x1-x2);
            double width = Math.abs(y1-y2);
            area = calculateRectangleArea(length, width);
        } else {
            System.out.println("Wrong Format");
            return;
        }

        System.out.println(String.format("%.2f", area));
    }

    public static double calculateCircleArea(double radius) {
        return Math.PI * radius * radius;
    }

    public static double calculateRectangleArea(double length, double width) {
        return length * width;
    }
}

在测验3的题目基础上,重构类设计,实现列表内图形的排序功能(按照图形的面积进行排序)。
提示:题目中Shape类要实现Comparable接口。

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

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

        List<Double> areas = new ArrayList<>();

        int shapeType;

        do {


            shapeType = scanner.nextInt();

            if (shapeType == 1) {
                double radius = scanner.nextDouble();
            if(radius<=0){
            System.out.println("Wrong Format");
            return;
        }
                double area = calculateCircleArea(radius);
                areas.add(area);
            } else if (shapeType == 2) {
            double x1 = scanner.nextDouble();
            double y1 = scanner.nextDouble();
            double x2 = scanner.nextDouble();
            double y2 = scanner.nextDouble();
            double length = Math.abs(x1-x2);
            double width = Math.abs(y1-y2);
                double area = calculateRectangleArea(length, width);
                areas.add(area);
            } else if (shapeType != 0) {
                System.out.println("Wrong Format");
            }
        } while (shapeType != 0);

        Collections.sort(areas);

        for (double area : areas) {
            System.out.print(String.format("%.2f", area) + " ");
        }
    }

    public static double calculateCircleArea(double radius) {
        return Math.PI * radius * radius;
    }

    public static double calculateRectangleArea(double length, double width) {
        return length * width;
    }
}

以上就是第二次blog总结对于个人来说,java知识量多,个人学习时间练习时间不多,导致对于许多的java题目都不会,今后还需要花更多的时间联练习。