首页 > 其他分享 >4-5 + 期中

4-5 + 期中

时间:2023-06-30 18:33:49浏览次数:55  
标签:temp str1 System 期中 time println out

(1)前言:

关于类:

1、类似C++中的结构体struct,构造函数、内置方法(函数 )都比较相似

2、但是无法重载运算符,这就很难受了。

3、尽量避免代码的重复,把private和public的方法搞清晰。

4、把需要解决的问题进行拆分,拆得好就写得快

关于java语法:

1、小心使用运算符,第一次作业用==去比较两个BigInteger调了很久。

2、慎用clone,(我选择手写copy)

3、注意可变类的赋值是引用

其实我学得不太多,理解也不够深,像继承等等东西,也没怎么用过,等以后再来谈这方面吧。

 

(2)设计与分析:

类图:

 

 

 

 

 

源码:

import java.text.ParseException;

import java.time.DateTimeException;

import java.time.Duration;

import java.time.LocalDateTime;

import java.util.ArrayList;

import java.util.Scanner;

 

public class Main {

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

        Menu menu = new Menu();

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

        Scanner input = new Scanner(System.in);

        String str1;

        int i = 0;

        int portion = 0, quota = 0;

        while (true) {

            // 输入菜单

            Dish temp = new Dish();

            int isRepeat = -1;

            str1 = input.nextLine();

            if (str1.matches("[\\S]* [1-9][\\d]*")) {

                String[] token = str1.split(" ");

                temp.name = token[0];

                temp.unit_price = Integer.parseInt(token[1]);

                if (temp.unit_price > 300) {

                    System.out.println(temp.name + " price out of range " + temp.unit_price);

                    continue;

                }

                temp.isT = false;

                isRepeat = menu.searchDish(temp.name);

                if (isRepeat != -1) {

                    menu.dishs.remove(isRepeat);

                }

                menu.dishs.add(temp);

            } else if (str1.matches("[\\S]* [\\d]* T")) {

                String[] token = str1.split(" ");

                temp.name = token[0];

                temp.unit_price = Integer.parseInt(token[1]);

                if (temp.unit_price > 300) {

                    System.out.println(temp.name + " price out of range " + temp.unit_price);

                    continue;

                }

                temp.isT = true;

                if (isRepeat != -1) {

                    menu.dishs.remove(isRepeat);

                }

                menu.dishs.add(temp);

            } else if (str1.equals("end")) {

                break;

            } else if (str1.matches("tab.*")) {

                break;

 

            } else {

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

                continue;

            }

        }

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

            Table temp = new Table();

            boolean isRepeat = false;

            int repeatNum = 0;

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

                if (str1.matches("table [1-9][\\d]* [\\d]*/[\\d][\\d]?/[\\d][\\d]? [\\d][\\d]?/[\\d][\\d]?/[\\d][\\d]?")) {

                    String[] token = str1.split(" ");

                    String[] Date = token[2].split("/");

                    String[] Time = token[3].split("/");

                    int[] intDate = new int[3];

                    int[] intTime = new int[3];

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

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

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

                    }

                    temp.num = Integer.parseInt(token[1]);

                    if (temp.num > 55) {

                        System.out.println(temp.num + " table num out of range");

                        str1 = input.nextLine();

                        continue;

                    }

                    try {

                        temp.time = LocalDateTime.of(intDate[0], intDate[1], intDate[2], intTime[0], intTime[1],

                                intTime[2]);

                        temp.getWeekDay();

                    } catch (DateTimeException e) {

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

                        str1 = input.nextLine();

                        continue;

                    }

                    if (!(temp.time.isAfter(LocalDateTime.of(2022, 1, 1, 0, 0, 0))

                            && temp.time.isBefore(LocalDateTime.of(2024, 1, 1, 0, 0, 0)))) {

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

                        str1 = input.nextLine();

                        continue;

                    }

                    // 判断桌号是否重复

                    if (temp.isOpen()) {

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

                            // 有重复的桌号

                            if (temp.num == tables.get(i).num && tables.get(i).isOpen()) {

                                Duration duration = Duration.between(temp.time, tables.get(i).time);

                                // 同一天

                                if (duration.toDays() == 0) {

                                    // 在周一到周五

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

                                        // 在同一时间段

                                        if (temp.time.getHour() < 15 && tables.get(i).time.getHour() < 15) {

                                            temp = tables.get(i);

                                            isRepeat = true;

                                            repeatNum = i;

                                            break;

                                        }

                                    }

                                    // 在周末

                                    else {

                                        // 时间相差小于一小时

                                        if (duration.toHours() < 3600) {

                                            temp = tables.get(i);

                                            repeatNum = i;

                                            isRepeat = true;

                                            break;

                                        }

                                    }

                                }

                            }

                        }

                    }

                    if (!isRepeat) {

                        System.out.println("table " + temp.num + ": ");

                    }

 

                } else {

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

                    str1 = input.nextLine();

                    continue;

                }

                // 本桌开始点菜

                while (true) {

                    str1 = input.nextLine();

                    if (str1.matches("[1-9][\\d]* [\\S]* [\\d] [1-9][\\d]*")) {

                        String[] inform = str1.split(" ");

                        portion = Integer.parseInt(inform[2]);

                        quota = Integer.parseInt(inform[3]);

                        if (temp.order.records.size() > 0) {

                            if (Integer.parseInt(inform[0]) <= temp.order.records.get(temp.order.records.size() - 1).orderNum) {

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

                                continue;

                            }

                        }

                        if (menu.searchDish(inform[1]) == -1) {

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

                            continue;

                        }

                        if (portion > 3 || portion < 1) {

                            System.out.println(Integer.parseInt(inform[0]) + " portion out of range " + portion);

                            continue;

                        }

                        if (quota > 15) {

                            System.out.println(Integer.parseInt(inform[0]) + " num out of range " + quota);

                            continue;

                        }

                        temp.od(menu, inform[0], inform[1], portion, quota);

                    }

                    // 判断是否为删除订单

                    else if (str1.matches("[1-9][\\d]* delete")) {

                        String[] delete = str1.split(" ");

                        temp.order.delARecordByOrderNum(Integer.parseInt(delete[0]));

                    }

                    // 判断是否为夹杂菜单

                    else if (str1.matches("[\\S]* [\\d]*")) {

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

                        continue;

                    } else if (str1.matches("[\\S]* [\\d]* T")) {

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

                        continue;

                    }

                    else if (str1.equals("end")) {

                        break;

                    } else if (str1.matches("ta.*")) {

                        break;

                    } else {

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

                        continue;

                    }

                }

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

                if (isRepeat) {

                    tables.remove(repeatNum);

                }

                temp.getSum();

                tables.add(temp);

            } else if (str1.matches("t.*")) {

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

                isRepeat = true;

                if (tables.size() != 0) {

                    temp = tables.get(tables.size() - 1);

                }

                while (true) {

                    str1 = input.nextLine();

                    if (str1.matches("[1-9][\\d]* [\\S]* [\\d] [1-9][\\d]*")) {

                        String[] inform = str1.split(" ");

                        portion = Integer.parseInt(inform[2]);

                        quota = Integer.parseInt(inform[3]);

                        // 判断订单号是否由小到大排列

                        if (temp.order.records.size() > 0) {

                            if (Integer.parseInt(inform[0]) <= temp.order.records.get(temp.order.records.size() - 1).orderNum) {

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

                                continue;

                            }

                        }

                        if (menu.searchDish(inform[1]) == -1) {

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

                            continue;

                        }

                        if (portion > 3 || portion < 1) {

                            System.out.println(Integer.parseInt(inform[0]) + " portion out of range " + portion);

                            continue;

                        }

                        if (quota > 15) {

                            System.out.println(Integer.parseInt(inform[0]) + " num out of range " + quota);

                            continue;

                        }

                        temp.od(menu, inform[0], inform[1], portion, quota);

                    }

                    // 判断是否为删除订单

                    else if (str1.matches("[1-9][\\d]* delete")) {

                        String[] delete = str1.split(" ");

                        temp.order.delARecordByOrderNum(Integer.parseInt(delete[0]));

                    }

                    // 判断是否为夹杂菜单

                    else if (str1.matches("[\\S]* [\\d]*")) {

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

                        continue;

                    } else if (str1.matches("[\\S]* [\\d]* T")) {

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

                        continue;

                    }

                    // 判断是否为代点

                    else if (str1.matches("[\\d]* [\\d]* [\\S]* [\\d] [1-9][\\d]*")) {

                        String[] token = str1.split(" ");

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

                        boolean exist = false;

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

                            if (tables.get(j).num == Integer.parseInt(token[0])) {

                                exist = true;

                                break;

                            }

                        }

                        if (exist) {

                            System.out.print(Integer.parseInt(token[1]) + " table " + temp.num + " pay for table " + Integer.parseInt(token[0]) + " ");

                            Record treat = new Record();

                            treat.d = menu.dishs.get(menu.searchDish(token[2]));

                            portion = Integer.parseInt(token[3]);

                            quota = Integer.parseInt(token[4]);

                            treat.portion = portion;

                            treat.quota = quota;

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

                            temp.sum += treat.getPrice();

                        }

                        // 若不存在则输出内容

                        else {

                            System.out.println("Table number :" + Integer.parseInt(token[0]) + " does not exist");

                        }

 

                    } else if (str1.equals("end")) {

                        break;

                    } else {

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

                        continue;

                    }

                }

                if (tables.size() != 0) {

                    tables.remove(tables.size() - 1);

                    temp.sum = 0;

                    temp.origSum = 0;

                    temp.getSum();

                    tables.add(temp);

                }

            } else {

                str1 = input.nextLine();

            }

 

 

        }

 

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

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

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

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

            } else

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

        }

    }

 

    static class Dish {

        String name;

        int unit_price;

        boolean isT = false;

    }

 

    static class Record {

        int orderNum;

        Dish d;

        int portion;

        int quota;

        boolean isDeleted = false;

 

        int getPrice() {

            if (portion == 2)

                return (int) Math.round(1.5 * d.unit_price) * quota;

            else if (portion == 3)

                return 2 * d.unit_price * quota;

            else

                return d.unit_price * quota;

        }

    }

 

    static class Menu {

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

 

        int searchDish(String dishName) {

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

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

                    return i;

                }

            }

            return -1;

        }

    }

 

    static class Order {

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

 

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

            Record newRecord = new Record();

            newRecord.orderNum = orderNum;

            newRecord.d = menu.dishs.get(menu.searchDish(dishName));

            newRecord.portion = portion;

            newRecord.quota = quota;

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

            return newRecord;

        }

 

 

        boolean delARecordByOrderNum(int orderNum) {

            int i = 0, flag = 0;

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

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

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

                        records.get(i).isDeleted = true;

                    } else {

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

                    }

                    flag++;

                }

            }

            if (flag == 0) {

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

                return false;

            }

            return true;

        }

    }

 

    static class Table {

        Order order = new Order();

        int num;

        LocalDateTime time;

        int weekday;

        long sum = 0;

        long origSum = 0;

 

        void od(Menu menu, String str1, String str2, int portion, int quota) {

            {

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

 

            }

        }

 

        void getWeekDay() {

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

        }

 

        void getSum() {

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

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

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

                    if (order.records.get(i).d.isT) {

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

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

                        } else {

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

                        }

                    } else {

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

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

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

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

                                if (time.getMinute() <= 30)

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

                            }

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

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

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

                                if (time.getMinute() <= 30)

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

                            }

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

                    }

                }

            }

 

        }

 

        boolean isOpen() {

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

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

                    return true;

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

                    if (time.getMinute() <= 30)

                        return true;

                }

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

                    return true;

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

                    if (time.getMinute() >= 30)

                        return true;

                }

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

                    if (time.getMinute() <= 30)

                        return true;

                }

            } else {

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

                    return true;

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

                    if (time.getMinute() >= 30)

                        return true;

                }

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

                    if (time.getMinute() <= 30)

                        return true;

                }

            }

            return false;

        }

    }

}

 

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        Scanner input = new Scanner(System.in);

       

        int choice = input.nextInt();

       

        switch(choice) {

        case 1://Circle

            double radiums = input.nextDouble();

            Shape circle = new Circle(radiums);

            printArea(circle);

            break;

        case 2://Rectangle

            double x1 = input.nextDouble();

            double y1 = input.nextDouble();

            double x2 = input.nextDouble();

            double y2 = input.nextDouble();

           

            Point leftTopPoint = new Point(x1,y1);

            Point lowerRightPoint = new Point(x2,y2);

           

            Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);

           

            printArea(rectangle);

            break;

        }

       

}

 

 

第四次:

本次作业中对时间范围进行了界定,运用Date类的format将字符串转化成整数后进行判断即可;

以及table格式的判断,最好的方法就是使用正则表达式,将正确格式正则化,其余格式均不做输入操作,并输出“Wrong Format”;

 

第五次:

本次作业中往Dish中添加了口味度,最后根据口味度判断该table的酸/辣/甜度,

同样考察了正则表达式;

 

期中:

期中考察难度比较基础,题目循序渐进,基本上完成了第二题就算完成了大部分题目,考察了抽象接口和抽象类的使用,同时还有内部类的考察;

 

(3)采坑心得:

我认为写程序和人体的免疫一样,有三道防线:

优秀的架构与细节设计。优秀的含义很广泛,清晰、可读、易维护、易扩展的设计往往是鲁棒的,这样的设计往往很难出严重的纰漏,因为很多错误已经被规避掉了,即使有错误,发现、纠正的难度也不会太大

内陷的异常、错误与断言。这些人为指定的运行行为是最容易帮助我们发现并定位潜在问题的

充分的测试,黑箱测、白箱测、压力测、让老妈帮你测,总之测就完事儿了。

出现bug往往是因为这三道防线被全部突破,无险可守了。

首先就是正则表达式,包括正则的效率问题。然后就是程序的鲁棒性,实际工程中程序对

种不同状况做出的反应都应该符合用户需求。最最重要的就是面向对象的思维。

 

(4)改进建议:

难度建议不要太大,可以设计一些课外的知识,通过自学掌握,但类于类之间的关系不要太复杂,例如第五次作业第一题难度较大,第七次作业难度较合适,题量也合适。希望老师可以讲解PTA或者我们自己写过的作业,因为自己写的作业自己思考过,每个人知道自己的问题在哪里,带着问题去学习相比于课堂上的书本上的代码样例可能更能带来触动,并留下更深的印象!或者讲解题目涉及的课外知识等!

 

(5)总结:

类的设计尽量做到,简单,复用性强,每个类的作用都明确切少。类的设计符合JAVA类设计的五大原则,做到独立可复用,类与类之间的关系也做到,简单明了,符合JAVA的三大特性。

 

标签:temp,str1,System,期中,time,println,out
From: https://www.cnblogs.com/qingzhichichenyi/p/17517584.html

相关文章

  • Java PTA第4~5次题目集总结以及期中考试总结
    一.前言1.第四次题目集的知识点涉及Time类以及前面学的各种知识点;题量很少只有一题;难度比较大。2.第五次题目集的知识点主要是Time类、异常处理等等;题量很少只有一题;难度比较大。3.期中考试的知识点涉及类、继承与多态、接口等等;题量不多,一共4题;整体难度不高。二.设计与分析7......
  • 题目集4-5及期中期末考试
    一、前言:总结三次题目集的知识点、题量、难度等情况对于我来说第4、5次题目集难度较大,题量适中。期中考试难度一般,题量适中。知识点考察的是接口,对编程中时间信息的处理,类,继承等等。二、设计与分析没怎么写出来菜单题,分析不了源码。分析一下就是自己的态度有问题,没有迎难而......
  • 4-5-期中
    前言:关于类:1、类似C++中的struct,构造函数、内置方法(函数)都比较相似2、但是无法重载运算符,这是比较难受的一点。3、尽量避免代码的重复,把private和public的方法搞清晰。4、把我们需要解决的问题进行拆分,拆得好就写得快关于java语法:1、小心使用运算符,第一次作业用==去比较两个Big......
  • BLOG2-PTA题目集4、5以及期中考试
    (1)前言本次博客主要涵盖了Java题目的几个主要知识点,包括:1.面向对象的基础知识:这部分主要包括了类和对象的基本概念,构造方法,访问权限和成员变量的相关内容。在面向对象编程中,对这些基础知识的理解至关重要。2.面向对象的设计原则:这个题目强调了两个重要的设计原则,即继承和组......
  • 4、5及期中考试总结
    一.前言Java编程语言是当今最流行的编程语言之一,由于其跨平台性、面向对象性和安全性等特点,受到广泛的应用。作为一名计算机专业的学生,在学习Java编程语言时,我们需要完成多个作业来巩固所学知识。在前三次Java作业中,我们已经学习了Java的基础知识和常用技术,通过完成这些作业,我们......
  • 菜单4、5以及期中考试总结-BLOG-PTA-4、5
    22201612-刘健涛目录(1)前言(2)设计与分析(3)踩坑心得(4)改进建议(5)总结正文(1)前言  (2)设计与分析(3)踩坑心得(4)改进建议(5)总结......
  • 4-5次及期中考试PTA题目总结
    前言 第四次和第五次pta每次都只有一道题目,分别是菜单计价4和菜单计价5,没有了其他小题pta拿起分来变得困难了许多。不过好在题目要求上菜单计价4和菜单计价5只是菜单计价3的两个分支,没有太多需要叠加的功能,避免的题目过于的复杂。 第四次的菜单计价在菜单计价三的基础上没有加......
  • PTA4-5及期中总结
    (1)前言期中考试的题目集总体来说还是很简单的,题目量比较少而且难度偏易,考察的知识点可以说是很基础的面向对象编程知识点,基本上就是在考验我们的基本功扎不扎实,对于知识点很熟悉的同学可以很快的完成大部分题目,但是还有个接口题目,需要使用java自带类,这题先前没有遇到过就会做......
  • BLOG_OOP_期中考试
    前言涉及知识点1.对于创建对象和类的初步实践;如构建圆类和矩形类;1.对于抽象类和继承与多态的认识;如构建shape类;题量不多,可以完成。难度不大,可以完成。设计与分析题目源码如下importjava.util.*;publicclassMain{publicstaticvoidmain(String[]......
  • pta题目集4~5及期中考试总结性blog
    一、前言总结三次题目集的知识点、题量、难度等情况第四次题目集主要更新了各种异常情况,是对代码正确度的深入考察,涉及时间的格式问题,信息的格式问题各种格式问题等等,涉及到hashset、面向对象编程的封装性、BigDecimal类关于精确小数的运算以及了解Scanner类中nextLine()等方法......