首页 > 其他分享 >第二次blog

第二次blog

时间:2023-11-19 17:56:35浏览次数:19  
标签:String int blog str dish table 第二次 name

作业总结

1.1 前言

  • 这几次作业主要是对菜单计价程序的完善,第四次作业中的菜单计价程序2是在菜单计价程序1的基础上进行完善,添加了输入菜单和份额的要求,难度还在可以接受的范围。第四次作业中的菜单计价程序3则是在菜单计价程序2的基础上添加了一系列的要求,包括添加桌号、代点菜等要求等,本次作业相较于在菜单计价程序2难度则是有了很大的增加,且测试的时间限制太短,最终本次作业有一个测试点没有过。第五次作业的菜单计价程序4则是在菜单计价程序3的基础上进行完善,不仅增加了特色菜的概念,更是增加了很多的异常情况,本次作业主要考验的是我们是异常处理情况,难度很大。第六次作业的菜单计价程序5同样是在菜单计价程序3的基础上进行完善,不过是和菜单计价程序4不同的分支,本次作业侧重的是在对特色菜的处理上面,难度与菜单计价程序4的难度相当。这几次作业涉及到的知识点有类的设计,异常处理,集合的使用等等。
  • 第四次题目集整体难度不高,属于过渡性质的训练,有为后面铺垫的菜单计价程序-3,跟前面题目集一样的查找重复数据,7-7还要我们去稍微了解很多之前没接触的类和方法
  • 第五次题目集主要知识点是正则表达式和类图的了解,前几题都是很简单的正则表达式的应用题目,最后两个较长的日期问题前面也都做过,只不过这回要根据类图来编写,很多前面的代码都可以直接照搬,难度并不高。
    第六次题目集虽然只有一题,但却是遇到的最难的题目,是前面菜单计价程序-3的升级版菜单计价程序-4,前面的菜单计价程序-3摸了导致这题花了大量的时间,最后也没有完全做完。要运用前面学到的很多知识点,最重要的是要在编写前理解题目并构建好框架,不然就会面临多次的大规模重写。难度很高。

1.2 程序设计


1.2.1 第四次作业

**7-4 菜单计价-2

题目:设计点菜计价程序,根据输入的信息,计算并输出总价格。

输入内容按先后顺序包括两部分:菜单、订单,最后以"end"结束。

菜单由一条或多条菜品记录组成,每条记录一行

每条菜品记录包含:菜名、基础价格 两个信息。

订单分:点菜记录和删除信息。每一类信息都可包含一条或多条记录,每条记录一行。
点菜记录包含:序号、菜名、份额、份数。
份额可选项包括:1、2、3,分别代表小、中、大份。

删除记录格式:序号 delete

标识删除对应序号的那条点菜记录。

不同份额菜价的计算方法:
小份菜的价格=菜品的基础价格。
中份菜的价格=菜品的基础价格1.5。
小份菜的价格=菜品的基础价格
2。
如果计算出现小数,按四舍五入的规则进行处理。

参考以下类的模板进行设计:
菜品类:对应菜谱上一道菜的信息。

Dish {    
   String name;//菜品名称    
   int unit_price;    //单价    
   int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)    }

菜谱类:对应菜谱,包含饭店提供的所有菜的信息。

Menu {
   Dish[] dishs ;//菜品数组,保存所有菜品信息
   Dish searthDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。
   Dish addDish(String dishName,int unit_price)//添加一道菜品信息
}

点菜记录类:保存订单上的一道菜品记录

Record {
   int orderNum;//序号\
   Dish d;//菜品\
   int portion;//份额(1/2/3代表小/中/大份)\
   int getPrice()//计价,计算本条记录的价格\
}

订单类:保存用户点的所有菜的信息。

Order {
   Record[] records;//保存订单上每一道的记录
   int getTotalPrice()//计算订单的总价
   Record addARecord(int orderNum,String dishName,int portion,int num)//添加一条菜品信息到订单中。
   delARecordByOrderNum(int orderNum)//根据序号删除一条记录
   findRecordByNum(int orderNum)//根据序号查找一条记录
}
输入格式:

菜品记录格式:

菜名+英文空格+基础价格

如果有多条相同的菜名的记录,菜品的基础价格以最后一条记录为准。

点菜记录格式:
序号+英文空格+菜名+英文空格+份额+英文空格+份数
注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。

删除记录格式:序号 +英文空格+delete

最后一条记录以“end”结束。

输出格式:

按顺序输出每条订单记录的处理信息,

每条点菜记录输出:序号+英文空格+菜名+英文空格+价格。其中的价格等于对应记录的菜品*份数,序号是之前输入的订单记录的序号。
如果订单中包含不能识别的菜名,则输出“** does not exist”,**是不能识别的菜名

如果删除记录的序号不存在,则输出“delete error”

最后输出订单上所有菜品的总价(整数数值),

本次题目不考虑其他错误情况,如:菜单订单顺序颠倒、不符合格式的输入、序号重复等。

代码:
import java.util.ArrayList;
import java.util.Scanner;

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

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

class Menu{

    ArrayList<Dish> meaulist;
    Menu(){meaulist = new ArrayList<>();}

    boolean Isdishname(String str)
    {
        for (Dish dish : meaulist) {
            if (dish.name.equals(str))
                return true;
        }
        return false;
    }

    boolean update_meau(String str,int price)
    {
        boolean flag = false;
        int idex = -1;
        for (int i = 0; i < meaulist.size(); i++) {
            if(meaulist.get(i).name.equals(str))
            {
                idex = i;
                flag = true;
                break;
            }
        }
        if(idex != -1)
        {
            meaulist.get(idex).unit_price = price;
        }

        return flag;
    }

    Dish searthDish(String dishName)
    {
        int i;
        for (i = 0; i < meaulist.size(); i++) {
            if(meaulist.get(i).name.equals(dishName))
                break;
        }
        return meaulist.get(i);
    }//根据菜名在菜谱中查找菜品信息,返回Dish对象。

    void addDish(String dishName,int unit_price)
    {
        meaulist.add(new Dish(dishName,unit_price));
    }//添加一道菜品信息

    //判断是否需要更新价格
}

class  Record {
    Record(int orderNum,Dish dish,int portion,int cnt)
    {
        this.orderNum = orderNum;
        this.d = dish;
        this.portion = portion;
        this.cnt = cnt;
    }
    int orderNum;//序号\
    Dish d;//菜品\
    int portion;//份额(1/2/3代表小/中/大份)\
    int cnt;

    boolean iscal = true;  //是否被计算价格

    int getPrice()
    {
        int price = 0;
        price = d.getPrice(this.portion) * this.cnt;
        return price;
    }//计价,计算本条记录的价格\
}

class  Order
{
    ArrayList<Record> orderlist;
    Order(){orderlist = new ArrayList<>();}
    int getTotalPrice()//计算订单的总价
    {
        int total_price = 0;
        for (Record record : orderlist) {
            //未被删除计算总价格
            if(record.iscal)
            {
                total_price += record.getPrice();
            }
        }
        return total_price;
    }

    void addARecord(int orderNum,Dish dish,int portion,int num)//添加一条菜品信息到订单中。
    {
        Record record = new Record(orderNum,dish,portion,num);
        orderlist.add(record);
        System.out.println(record.orderNum +" "+ record.d.name+" "+ record.getPrice());
    }

    void delARecordByOrderNum(int orderNum)//根据序号删除一条记录
    {
        orderlist.get(orderNum).iscal = false;    //不被计算
    }

    int findRecordByNum(int orderNum)//根据序号查找一条记录
    {
        int idex = -1;
        for (int i = 0; i < orderlist.size(); i++) {
            if(orderlist.get(i).orderNum == orderNum)
            {
                idex = i;
                break;
            }
        }
        //查找失败
        return idex;
    }
}

class Input_contrast
{
    static String id = "\\d+";//8个0-9的数字
    static String name = "\\S{1,10}";//1到10个非空格(TAB)字符

    static String dish_match = name + " " + id;
    static String meau_match = id + " "+ name + " " + id + " " + id;
    static String delete_match  = id + " " + "delete";


    Order order;
    Menu meau;


    Input_contrast()
    {
        order = new Order();
        meau = new Menu();
    }

    void deal_Dish(String str)
    {
        String[] store =str.split(" ");
        String dish_name = store[0];
        int dish_price = Integer.parseInt(store[1]);

        //查看新输入的菜名是否重复
        if(!meau.update_meau(dish_name, dish_price))
        {
            meau.addDish(dish_name,dish_price);
        }
    }

    void deal_delete(String str)
    {
        String[] store =str.split(" ");
        String dish_id = store[0];

        //在菜单记录中寻找删除的菜品
        int delete_staue = order.findRecordByNum(Integer.parseInt(dish_id));

        //没有查到
        if(delete_staue == -1)
            System.out.println("delete error;");
        else
            order.delARecordByOrderNum(delete_staue);
    }

    void deal_meau(String str)
    {
        String[] store =str.split(" ");
        int dish_id = Integer.parseInt(store[0]);
        String dish_name = store[1];
        int portion = Integer.parseInt(store[2]);
        int dish_cnt = Integer.parseInt(store[3]);

        boolean name_flag = meau.Isdishname(dish_name);
        //判断合法
        if(!name_flag)
        {
            System.out.println(dish_name + " " + "does not exist");
        }
        else
        {
            Dish dish = meau.searthDish(dish_name);
            order.addARecord(dish_id,dish,portion,dish_cnt);
        }
    }

    int Input_match(String str)
    {
        int staue = 0;
        if(str.matches(dish_match))
            staue = 1;
        else if(str.matches(meau_match))
            staue = 2;
        else if(str.matches(delete_match))
            staue = 3;
        return staue;
    }

    void user_Input(String str)
    {
        int deal_flag = Input_match(str);
        switch (deal_flag)
        {
            case 1: deal_Dish(str);break;
            case 2: deal_meau(str);break;
            case 3: deal_delete(str);break;
            default:break;
        }
    }

    void print_total_price()
    {
        //打印总价格
        System.out.print(order.getTotalPrice());
    }
}

public  class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String str = s.nextLine();
        Input_contrast Input = new Input_contrast();

        while(!str.equals("end"))
        {
            Input.user_Input(str);
            str = s.nextLine();
        }

        Input.print_total_price();
    }
}
类图:

**7-1 菜单计价程序-3
输入格式:

桌号标识格式:table + 序号 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)

菜品记录格式:

菜名+英文空格+基础价格

如果有多条相同的菜名的记录,菜品的基础价格以最后一条记录为准。

点菜记录格式:序号+英文空格+菜名+英文空格+份额+英文空格+份数注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。

删除记录格式:序号 +英文空格+delete

代点菜信息包含:桌号+英文空格+序号+英文空格+菜品名称+英文空格+份额+英文空格+分数

最后一条记录以“end”结束。

输入格式:

按输入顺序输出每一桌的订单记录处理信息,包括:

1、桌号,格式:table+英文空格+桌号+“:”+英文空格

2、按顺序输出当前这一桌每条订单记录的处理信息,

每条点菜记录输出:序号+英文空格+菜名+英文空格+价格。其中的价格等于对应记录的菜品*份数,序号是之前输入的订单记录的序号。如果订单中包含不能识别的菜名,则输出“** does not exist”,**是不能识别的菜名

如果删除记录的序号不存在,则输出“delete error”

最后按输入顺序一次输出每一桌所有菜品的总价(整数数值)格式:table+英文空格+桌号+“:”+英文空格+当前桌的总价

本次题目不考虑其他错误情况,如:桌号、菜单订单顺序颠倒、不符合格式的输入、序号重复等,在本系列的后续作业中会做要求。

代码:
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Calendar;
import java.util.Date;

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

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

class Menu{

    ArrayList<Dish> meaulist;
    Menu(){meaulist = new ArrayList<>();}

    boolean Isdishname(String str)
    {
        for (Dish dish : meaulist) {
            if (dish.name.equals(str))
                return true;
        }
        return false;
    }

    boolean update_meau(String str,int price)
    {
        boolean flag = false;
        int idex = -1;
        for (int i = 0; i < meaulist.size(); i++) {
            if(meaulist.get(i).name.equals(str))
            {
                idex = i;
                flag = true;
                break;
            }
        }
        if(idex != -1)
        {
            meaulist.get(idex).unit_price = price;
        }

        return flag;
    }

    Dish searthDish(String dishName)
    {
        int i;
        for (i = 0; i < meaulist.size(); i++) {
            if(meaulist.get(i).name.equals(dishName))
                break;
        }
        return meaulist.get(i);
    }//根据菜名在菜谱中查找菜品信息,返回Dish对象。

    void addDish(String dishName,int unit_price)
    {
        meaulist.add(new Dish(dishName,unit_price));
    }//添加一道菜品信息

    //判断是否需要更新价格
}

class  Record {
    Record(int orderNum,Dish dish,int portion,int cnt,int table_num)
    {
        this.orderNum = orderNum;
        this.d = dish;
        this.portion = portion;
        this.cnt = cnt;
        this.table_num = table_num;
    }
    int orderNum;//序号\
    Dish d;//菜品\
    int portion;//份额(1/2/3代表小/中/大份)\
    int cnt;

    int table_num;   //桌号

    boolean iscal = true;  //是否被计算价格
    int getPrice()
    {
        int price;
        price = d.getPrice(this.portion) * this.cnt;
        return price;
    }//计价,计算本条记录的价格\
}

class  Order
{
    ArrayList<Record> orderlist;
    Order(){orderlist = new ArrayList<>();}

    void addARecord(int orderNum,Dish dish,int portion,int num,int table_num,boolean flag)//添加一条菜品信息到订单中。
    {
        Record record = new Record(orderNum,dish,portion,num,table_num);
        orderlist.add(record);
        if(flag)
            System.out.println(record.orderNum +" "+ record.d.name+" "+ record.getPrice());
    }

    void delARecordByOrderNum(int orderNum)//根据序号删除一条记录
    {
        orderlist.get(orderNum).iscal = false;    //不被计算
    }

    int findRecordByNum(int orderNum,int tablenum)//根据序号查找一条记录
    {
        int idex = -1;
        for (int i = 0; i < orderlist.size(); i++) {
            if(orderlist.get(i).orderNum == orderNum && orderlist.get(i).table_num == tablenum)
            {
                idex = i;
                break;
            }
        }
        //查找失败
        return idex;
    }
}

//存储桌号和折扣
class table
{
    int table_num;   //桌号
    double discount = 1.0;   //折扣
    String date;
    String time;
    boolean Isouttime = false;   //是否超时
    int inweek_day;
    int table_price = 0;

    table(int table_num,String date,String time)
    {
        this.table_num = table_num;
        this.date = date;
        this.time = time;

        //计算周几 和折扣
        String[] store_1 =date.split("/");
        int year = Integer.parseInt(store_1[0]);
        int month = Integer.parseInt(store_1[1]);
        int day = Integer.parseInt(store_1[2]);
        Calendar calendar = Calendar.getInstance();
        Date nowDate = new Date(year,month - 1,day);
        calendar.setTime(nowDate);
        inweek_day = calendar.get(Calendar.DAY_OF_WEEK )-1;
        inweek_day = (inweek_day == 1 ? 7 : inweek_day - 1);

        String[] store_2 = time.split("/");
        int hour = Integer.parseInt(store_2[0]);
        int min = Integer.parseInt(store_2[1]);

        //设置折扣并判断是否超时
        if(inweek_day >=1 && inweek_day <=5)
        {
            if(hour >= 17 && hour <= 20) {
                if(hour == 20 && min > 30)
                    this.Isouttime = true;
                this.discount = 0.8;
            }
            else if(hour >= 10 && hour <= 14) {
                if((hour == 10 && min > 30) || (hour == 14 && min >30))
                    this.Isouttime = true;
                this.discount = 0.6;
            }
            else
                this.Isouttime = true;
        }
        else {
            if(hour >= 9&& hour <= 21)
            {
                if((hour == 9 && min > 30) || (hour == 21 && min > 30))
                    this.Isouttime = true;
            }
            else
                this.Isouttime = true;
        }
    }
}

class Input_contrast
{
    static String id = "\\d+";//8个0-9的数字
    static String name = "\\S{1,10}";//1到10个非空格(TAB)字符

    static String dish_match = name + " " + id;
    static String meau_match = id + " "+ name + " " + id + " " + id;
    static String delete_match  = id + " " + "delete";
    static String time_match = "table\\s\\d+\\s\\d{4}/\\d{1,2}/\\d{1,2}\\s\\d{1,2}/\\d{1,2}/\\d{1,2}";
    static String other_pay = id + " " + id + " " + name + " " + id + " " + id;

    ArrayList<table> tablelist = new ArrayList<>();
    Order order;
    Menu meau;
    int table_num;
    String date;
    String time;

    Input_contrast()
    {
        order = new Order();
        meau = new Menu();
    }

    void deal_Dish(String str)
    {
        String[] store =str.split(" ");
        String dish_name = store[0];
        int dish_price = Integer.parseInt(store[1]);

        //查看新输入的菜名是否重复
        if(!meau.update_meau(dish_name, dish_price))
        {
            meau.addDish(dish_name,dish_price);
        }
    }

    void deal_delete(String str,int table_num)
    {
        String[] store =str.split(" ");
        String dish_id = store[0];

        //在菜单记录中寻找删除的菜品
        int delete_staue = order.findRecordByNum(Integer.parseInt(dish_id),table_num);

        //没有查到
        if(delete_staue == -1)
            System.out.println("delete error;");
        else
            order.delARecordByOrderNum(delete_staue);
    }

    void deal_meau(String str)
    {
        String[] store =str.split(" ");
        int dish_id = Integer.parseInt(store[0]);
        String dish_name = store[1];
        int portion = Integer.parseInt(store[2]);
        int dish_cnt = Integer.parseInt(store[3]);

        boolean name_flag = meau.Isdishname(dish_name);
        //判断合法
        if(!name_flag)
        {
            System.out.println(dish_name + " " + "does not exist");
        }
        else
        {
            Dish dish = meau.searthDish(dish_name);
            order.addARecord(dish_id,dish,portion,dish_cnt,table_num,true);
        }
    }

    void deal_time(String str)
    {
        String[] store =str.split(" ");
        table_num = Integer.parseInt(store[1]);
        date = store[2];
        time = store[3];
        //存储点菜桌号并计算折扣
        tablelist.add(new table(table_num,date,time));
        //打印信息
        System.out.println("table" + " " + table_num + ":");
    }

    void deal_otherpay(String str)
    {
        String[] store =str.split(" ");
        int pay_table_num = Integer.parseInt(store[0]);
        int order_num = Integer.parseInt(store[1]);
        String dishname = store[2];
        int portion = Integer.parseInt(store[3]);
        int dish_cnt = Integer.parseInt(store[4]);

        if(meau.Isdishname(dishname))
        {
            Dish dish = meau.searthDish(dishname);
            order.addARecord(order_num,dish,portion,dish_cnt,table_num,false);
            double discount = 1.0;
            for (table table : tablelist) {
                if (table.table_num == table_num) {
                    discount = table.discount;
                    break;
                }
            }
            int idex = order.orderlist.size()-1;
            int print_price =(int)Math.round(discount * order.orderlist.get(idex).getPrice());
            System.out.println(order_num + " " + "table" + " " + table_num  + " " + "pay for table "
                    + pay_table_num + " " + print_price);
        }
        else
            System.out.println(dishname + " " + "does not exist");
    }

    int Input_match(String str)
    {
        int staue = 0;
        if(str.matches(dish_match))
            staue = 1;
        else if(str.matches(meau_match))
            staue = 2;
        else if(str.matches(delete_match))
            staue = 3;
        else if(str.matches(time_match))
            staue = 4;
        else if(str.matches(other_pay))
            staue = 5;
        return staue;
    }

    void user_Input(String str)
    {
        int deal_flag = Input_match(str);
        switch (deal_flag)
        {
            case 1: deal_Dish(str);break;
            case 2: deal_meau(str);break;
            case 3: deal_delete(str,table_num);break;
            case 4: deal_time(str);break;
            case 5: deal_otherpay(str);break;
            default:break;
        }
    }

    void print_total_price()
    {
        for (int i = 0; i < tablelist.size(); i++) {
            int num = tablelist.get(i).table_num;
            if(tablelist.get(i).Isouttime)
                System.out.println("table"+" "+ num + " "+ "out of opening hours");
            else
            {
                for (int j = 0; j < order.orderlist.size(); j++) {
                    if(order.orderlist.get(j).table_num == num && order.orderlist.get(j).iscal)
                    {
                        tablelist.get(i).table_price += order.orderlist.get(j).getPrice();
                    }
                }
                int printprice = (int)Math.round(tablelist.get(i).table_price * tablelist.get(i).discount);

                System.out.println("table"+ " " + tablelist.get(i).table_num + ": " + printprice);
            }
        }
    }
}

public  class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String str = s.nextLine();
        Input_contrast Input = new Input_contrast();

        while(!str.equals("end"))
        {
            Input.user_Input(str);
            str = s.nextLine();
        }

        Input.print_total_price();
    }
}

1.2.2 第五次作业


**7-1 菜单计价-4

​ 这道题在菜单计价程序-3的基础上增加了特色菜的处理以及大量异常输入的处理,对于该题我发现原有的代码主体部分不足以满足要求,不好进行增加特色菜和异常处理的代码更改,于是我进行了一定的调整。首先是特色菜的处理。由于特色菜与普通菜都是属于菜品,属于菜品的数据处理工作,所以我在处理菜品的类:Dish类中增加了Boolean T来达到合并的效果,一旦处理数据为特色菜,则T为true,反之为false,这样既避免了多加一个类来处理,又方便了数据的使用。

class Dish{
    String Name;      //菜品名
    int odd_Prive
;   //单价
    boolean T;        //是否为特色菜

    public int cal_price(int portion){
        int price=0;
        double pRice;
        if(portion==1)
            price=odd_Prive
;
        if(portion==2){
            pRice=odd_Prive *1.5;
            price=(int)Math.round(pRice);
        }
        if(portion==3)
            price=odd_Prive *2;
        return  price;
    }
}

​ 这样后续的处理区分特色菜和普通菜,并不用花费什么力气,只需要判断Dish类中的T即可。

由于该次作业含大量的异常输入,我第一想到的就是刚学的try-catch来处理。通过含参构造将输入的数据存入Record类或Menu类中或对Record类中的数据进行处理,由于每一种正确输入的格式都是确定且不变的,所以每次输入的数据一旦为异常输入,则不满足方法中参数的格式,从而进行相应的报错处理,这时可以通过try-catch来对异常数据进行相应的处理。在此之前我学了try-catch的使用方法,而try-catch对我的感觉就像是特殊的if-else,通俗地来说就是if报错,则else,也就是一旦try中抛出错误,则catch抓住错误,再通过catch中的代码进行更正。由于try中抛出的错误种类有很多,catch抓住的错误也就要进行一定的处理,比如时间错误:DateTimeException,一旦抛出时间错误,就要用catch(DateTimeException w)来抓住,再通过catch中的代码进行更正,这是对特定错误的处理,而对于一般广泛的无差别错误处理,则可用Exception来抓住所有错误,这是因为其实Java中报错的也是由类来完成,而Exception类是所有报错的类的父类。另外如特殊需要,也可以通过throw new ArithmeticException()语句强行抛出错误。有了以上的基础,我将main类中输入的数据分三个模块处理。
		
	对时间错误抛出的捕捉及处理,一旦时间输入不和要求则完成相应的异常输入处理

1.2.3 第六次作业

7-1 菜单计价-6
增加:

1、菜单输入时增加特色菜,特色菜的输入格式:菜品名+英文空格+口味类型+英文空格+基础价格+"T"

例如:麻婆豆腐 川菜 9 T

菜价的计算方法:

周一至周五 7折, 周末全价。

特色菜的口味类型:川菜、晋菜、浙菜

川菜增加辣度值:辣度0-5级;对应辣度水平为:不辣、微辣、稍辣、辣、很辣、爆辣;

晋菜增加酸度值,酸度0-4级;对应酸度水平为:不酸、微酸、稍酸、酸、很酸;

浙菜增加甜度值,甜度0-3级;对应酸度水平为:不甜、微甜、稍甜、甜;

例如:麻婆豆腐 川菜 9 T

输入订单记录时如果是特色菜,添加口味度(辣/酸/甜度)值,格式为:序号+英文空格+菜名+英文空格+口味度值+英文空格+份额+英文空格+份数

例如:1 麻婆豆腐 4 1 9

单条信息在处理时,如果口味度超过正常范围,输出"spicy/acidity/sweetness num out of range : "+口味度值,spicy/acidity/sweetness(辣度/酸度/甜度)根据菜品类型择一输出,例如:

acidity num out of range : 5

输出一桌的信息时,按辣、酸、甜度的顺序依次输出本桌菜各种口味的口味度水平,如果没有某个类型的菜,对应的口味(辣/酸/甜)度不输出,只输出已点的菜的口味度。口味度水平由口味度平均值确定,口味度平均值只综合对应口味菜系的菜计算,不做所有菜的平均。比如,某桌菜点了3份川菜,辣度分别是1、3、5;还有4份晋菜,酸度分别是,1、1、2、2,辣度平均值为3、酸度平均值四舍五入为2,甜度没有,不输出。

一桌信息的输出格式:table+英文空格+桌号+:+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价+英文空格+"川菜"+数量+辣度+英文空格+"晋菜"+数量+酸度+英文空格+"浙菜"+数量+甜度。

如果整桌菜没有特色菜,则只输出table的基本信息,格式如下,注意最后加一个英文空格:

table+英文空格+桌号+:+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价+英文空格

例如:table 1: 60 36 川菜 2 爆辣 浙菜 1 微甜

计算口味度时要累计本桌各类菜系所有记录的口味度总和(每条记录的口味度乘以菜的份数),再除以对应菜系菜的总份数,最后四舍五入。

注:本题要考虑代点菜的情况,当前桌点的菜要加上被其他桌代点的菜综合计算口味度平均值。

2、考虑客户订多桌菜的情况,输入时桌号时,增加用户的信息:

格式:table+英文空格+桌号+英文空格+":"+英文空格+客户姓名+英文空格+手机号+日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)

例如:table 1 : tom 13670008181 2023/5/1 21/30/00

约束条件:客户姓名不超过10个字符,手机号11位,前三位必须是180、181、189、133、135、136其中之一。

输出结果时,先按要求输出每一桌的信息,最后按字母顺序依次输出每位客户需要支付的金额。不考虑各桌时间段的问题,同一个客户的所有table金额都要累加。

输出用户支付金额格式:

用户姓名+英文空格+手机号+英文空格+支付金额

代码:
import java.util.*;

class  user_Input
{
    Order order = new Order();
    Menu menu = new Menu();

    boolean timeflag = false;
    String time = "";

    void deal_Input(String s)
    {
        int flag = 0;
        boolean F4 = s.matches("\\d* \\d* [\\S]* \\d \\d*");//带点菜
        boolean F5 = s.matches("\\d* delete");
        boolean F6 = s.matches("[\\S]* [\\S]* \\d* T");
        boolean F7 = s.matches("\\d* [\\S]* \\d* \\d \\d*");
        boolean F8 = s.matches("\\d* \\d* [\\S]* \\d* \\d \\d*");//带点菜
        boolean F1 = s.matches("[\\S]* \\d*");
        boolean F2 = s.matches("table \\d* : [\\S]{1,10} (180||181||189||133||135||136)\\d{8} \\d{4}/\\d{1,2}/\\d{1,2} \\d{1,2}/\\d{1,2}/\\d{1,2}");
        boolean F3 = s.matches("\\d* [\\S]* \\d \\d*");
        String[] str = s.split(" ");
        if(F1||F6){
            String name = str[0];
            if (!F6) {
                int qian = Integer.parseInt(str[1]);
                menu.addDish(name, qian, "F","nomal");
            } else {
                int qian = Integer.parseInt(str[2]);
                String vary = str[1];
                menu.addDish(name, qian, "T",vary);
            }
        }
        else if(F2){
            time = s;
            String[] ASHFUIKHS = time.split(" ");
            Table table = new Table();
            if(!order.Istim(time)){
                System.out.println(ASHFUIKHS[0] + " " + ASHFUIKHS[1] + " out of opening hours");
            }
            else {
                timeflag = true;
                table.tabNum = time;
                table.Pricncsd = -1;
                table.Lastoo = -1;
                order.tables.add(table);
                System.out.println(ASHFUIKHS[0] + " " + ASHFUIKHS[1] + ": ");
            }
        }
        else if(F4 ||F8){
            if(timeflag) {
                flag = Integer.parseInt(str[0]);
                int orderNum = Integer.parseInt(str[1]);
                String name = str[2];
                int por;
                int num;
                if (!F8) {
                    por = Integer.parseInt(str[3]);
                    num = Integer.parseInt(str[4]);
                    if (menu.searthDish(name) != null) {
                        order.addARecord(orderNum, name, por, num, menu, time, flag, -1);
                    } else {
                        System.out.println(name + " does not exist");
                    }
                } else {
                    int level = Integer.parseInt(str[3]);
                    por = Integer.parseInt(str[4]);
                    num = Integer.parseInt(str[5]);
                    if (menu.searthDish(name) != null) {
                        Dish dish = menu.searthDish(name);
                        if (dish.isTastRight(dish.abc, level)) {
                            order.addARecord(orderNum, name, por, num, menu, time, flag, level);
                        }
                    } else {
                        System.out.println(name + " does not exist");
                    }
                }
            }
        }
        else if(F5){
            int ordernum = Integer.parseInt(str[0]);
            order.delARecordByOrderNum(ordernum,time);
        }
        else if(F3||F7){
            if(timeflag) {
                int orderNum = Integer.parseInt(str[0]);
                String name = str[1];
                if (!F7) {
                    int por = Integer.parseInt(str[2]);
                    int num = Integer.parseInt(str[3]);
                    if (menu.searthDish(name) != null) {
                        order.addARecord(orderNum, name, por, num, menu, time, flag, -1);
                    } else {
                        System.out.println(name + " does not exist");
                    }
                } else {
                    int level = Integer.parseInt(str[2]);
                    int por = Integer.parseInt(str[3]);
                    int num = Integer.parseInt(str[4]);
                    if (menu.searthDish(name) != null) {
                        Dish dish = menu.searthDish(name);
                        if (dish.isTastRight(dish.abc, level)) {
                            order.addARecord(orderNum, name, por, num, menu, time, flag, level);
                        }
                    } else {
                        System.out.println(name + " does not exist");
                    }
                }
            }
        }
        else {
            System.out.println("wrong format");
        }
    }

    void user_print()
    {
        order.tables = order.printTOAL();
        String[] chuan = {"不辣","微辣","稍辣","辣","很辣","爆辣"};
        String[] jin = {"不酸","微酸","稍酸","酸","很酸","爆辣"};
        String[] zhe = {"不甜","微甜","稍甜","甜"};
        for (Table table : order.tables) {
            String[] ASHFUIKHS = table.tabNum.split(" ");
            if(table.Pricncsd!=-1) {
                System.out.print(ASHFUIKHS[0] +" " +ASHFUIKHS[1]+ ": " + table.Pricncsd + " " + table.Lastoo);
                if(table.csdcdc!=0){
                    System.out.print(" 川菜 " + table.csdcdc);
                    System.out.print(" " + chuan[table.scsac]);
                }
                if(table.vfvrteew!=0){
                    System.out.print(" 晋菜 " + table.vfvrteew);
                    System.out.print(" " + jin[table.dfdsaf]);
                }
                if(table.dsfrgftrg!=0){
                    System.out.print(" 浙菜 " + table.dsfrgftrg);
                    System.out.print(" " + zhe[table.sfggggh]);
                }
                if(table.csdcdc==0&&table.vfvrteew==0&&table.dsfrgftrg==0){
                    System.out.print(" ");
                }
                System.out.println();
            }
        }
        order.Lastoo();
    }
}

public class Main{
    public static void main(String[] args) {

        user_Input user_iput = new user_Input();
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        while(!s.equals("end"))
        {
            user_iput.deal_Input(s);
            s = sc.nextLine();
        }
        user_iput.user_print();
    }
}

class Dish {
    String name;//菜品名称
    int unit_qian; //单价
    String abc;
    String T;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getUnit_qian() {
        return unit_qian;
    }

    public void setUnit_qian(int unit_qian) {
        this.unit_qian = unit_qian;
    }

    public String getVaries() {
        return abc;
    }

    public void setVaries(String abc) {
        this.abc = abc;
    }

    public String getType() {
        return T;
    }

    public void setType(String T) {
        this.T = T;
    }

    int getPrice(int por){
        double[] arr = {0,1,1.5,2};
        return (int) Math.round(this.unit_qian*arr[por]);
    }

    boolean isTastRight(String vary,int level){
        if(vary.equals("川菜") && level >= 0&& level <= 5){
            return true;
        }
        else if(vary.equals("川菜")){
            System.out.println("spicy num out of range :" + level);
            return false;
        }
        else if(vary.equals("晋菜") && level >= 0&& level <= 4){
            return true;
        }
        else if(vary.equals("晋菜")){
            System.out.println("acidity num out of range :" + level);
            return false;
        }
        else if(vary.equals("浙菜") && level >= 0&& level <= 3){
            return true;
        }
        else if(vary.equals("浙菜")){
            System.out.println("sweetness num out of range :" + level);
            return false;
        }
        return false;
    }

}

//菜谱类:对应菜谱,包含饭店提供的所有菜的信息。
class Menu {
    ArrayList<Dish> dishs = new ArrayList<>();//菜品数组,保存所有菜品信息
    Dish searthDish(String dishName){
        for (int i = 0; i < this.dishs.size(); i++) {
            if (this.dishs.get(i).name.equals(dishName)){
                return this.dishs.get(i);
            }
        }
        return null;
    }//根据菜名在菜谱中查找菜品信息,返回Dish对象。
    int searthDishIndix(String dishName){
        for (int i = 0; i < this.dishs.size(); i++) {
            if (this.dishs.get(i).name.equals(dishName)){
                return i;
            }
        }
        return -1;
    }//根据菜名在菜谱中查找菜品信息,返回Dish对象。
    Dish addDish(String dishName,int unit_qian,String T,String vary){
        Dish dish = new Dish();
        dish.name = dishName;
        dish.unit_qian = unit_qian;
        dish.T = T;
        dish.abc = vary;
        if(this.searthDish(dishName)==null) {
            dishs.add(dish);
        }
        else {
            int flag = this.searthDishIndix(dishName);
            dishs.set(flag,dish);
        }
        return dish;
    }
}
//        点菜记录类:保存订单上的一道菜品记录
class Record {
    String time;
    int orderNum;
    Dish d;//菜品
    int por;//份额(1/2/3代表小/中/大份)
    int num;
    int Leve;
    int getPrice(){
        return d.getPrice(this.por)*this.num;
    }//计价,计算本条记录的价格

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public int getOrderNum() {
        return orderNum;
    }

    public void setOrderNum(int orderNum) {
        this.orderNum = orderNum;
    }

    public Dish getD() {
        return d;
    }

    public void setD(Dish d) {
        this.d = d;
    }

    public int getPortion() {
        return por;
    }

    public void setPortion(int por) {
        this.por = por;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public int getLeve() {
        return Leve;
    }

    public void setLeve(int leve) {
        Leve = leve;
    }
}

//        订单类:保存用户点的所有菜的信息。
class Order {
    double zhekou1;
    double zhekou2;
    ArrayList<Record> records=new ArrayList<>();//保存订单上每一道的记录
    ArrayList<Table> tables = new ArrayList<>();
    ArrayList<Table> printTOAL(){
        for (int i = 0; i < tables.size(); i++) {
            int qian;
            int qian1 = 0;
            int qian2 = 0;
            int firstqian = 0;
            Istim(tables.get(i).tabNum);
            for (Record record : records){
                if(record.time.equals(tables.get(i).tabNum)){
                    firstqian +=record.getPrice();
                    if(!record.d.abc.equals("nomal")){
                        qian1+=(int)Math.round(record.getPrice()*this.zhekou2);
                        if(record.d.abc.equals("川菜")){
                            tables.get(i).csdcdc += record.num;
                            tables.get(i).scsac+=(record.Leve*record.num);
                        }
                        else if(record.d.abc.equals("浙菜")){
                            tables.get(i).dsfrgftrg += record.num;
                            tables.get(i).sfggggh+=(record.Leve*record.num);
                        }
                        else if(record.d.abc.equals("晋菜")){
                            tables.get(i).vfvrteew += record.num;
                            tables.get(i).dfdsaf+=(record.Leve*record.num);
                        }
                    }
                    else {
                        qian2+=(int)Math.round(record.getPrice() * this.zhekou1);
                    }
                }
            }
            qian = qian1 +qian2;
            Table table = new Table();
            table.tabNum = tables.get(i).tabNum;
            table.Pricncsd = firstqian;
            table.Lastoo = qian;
            table.csdcdc = tables.get(i).csdcdc;
            table.vfvrteew = tables.get(i).vfvrteew;
            table.dsfrgftrg = tables.get(i).dsfrgftrg;
            table.scsac = (int)Math.round(tables.get(i).scsac/( table.csdcdc *1.0000));
            table.dfdsaf = (int)Math.round(tables.get(i).dfdsaf/(table.vfvrteew *1.0000));
            table.sfggggh = (int)Math.round(tables.get(i).sfggggh/(table.dsfrgftrg  *1.0000));
            tables.set(i,table);
        }
        return this.tables;
    }
    
    //根据序号删除一条记录
    void delARecordByOrderNum(int orderNum,String time){
        for (int i = 0; i < records.size(); i++) {
            if(records.get(i).time.equals(time)) {
                if (records.get(i).orderNum == orderNum) {
                    records.remove(i);
                    return;
                }
            }
        }
        System.out.println("delete error;");
    }

    //添加一条菜品信息到订单中。
    void addARecord(int orderNum,String dishName,int por,int num,Menu menu,String time,int flag,int level){
        String[] ASHFUIKHS = time.split(" ");
        Record record = new Record();
        Dish dish = menu.searthDish(dishName);
        record.Leve = level;
        record.orderNum=orderNum;
        record.d = dish;
        record.por=por;
        record.num=num;
        record.time = time;
        records.add(record);
        if(flag!=0){
            int tn = Integer.parseInt(ASHFUIKHS[1]);
            System.out.println(record.orderNum + " " + ASHFUIKHS[0] + " " + ASHFUIKHS[1] + " pay for table " + flag + " " + record.getPrice());
        }
        else {
            System.out.println(record.orderNum + " " + record.d.name + " " + record.getPrice());
        }
    }

    boolean Istim(String time){
        String[] str = time.split(" ");
        Date time1 = new Date(str[5]);
        Calendar safcas = Calendar.getInstance();
        safcas.setTime(time1);
        int[] arr = {7,1,2,3,4,5,6};
        int week = arr[safcas.get(Calendar.DAY_OF_WEEK)-1];

        String time2 = str[6];
        String[] str1 = time2.split("/");
        int hour = Integer.parseInt(str1[0]);
        int min = Integer.parseInt(str1[1]);
        if(week<6){
            if(hour<10||(hour>14&&hour<17)||hour>20){
                return false;
            }
            else if((hour==10&&min<30) ||(hour==14&&min>30)||(hour==20&&min>30)){
                return false;
            }
            else {
                this.zhekou2 = 0.7;
                if(hour<=14){
                    this.zhekou1 = 0.6;
                }
                else {
                    this.zhekou1 = 0.8;
                }
            }
        }
        else{
            if(hour<9||hour>21){
                return false;
            }
            else if((hour==9&&min<30) ||(hour==21&&min>30)){
                return false;
            }
            this.zhekou1 =1;
            this.zhekou2 =1;
        }
        return true;
    }

    public double getZhekou1() {
        return zhekou1;
    }

    public void setZhekou1(double zhekou1) {
        this.zhekou1 = zhekou1;
    }

    public double getZhekou2() {
        return zhekou2;
    }

    public void setZhekou2(double zhekou2) {
        this.zhekou2 = zhekou2;
    }

    public ArrayList<Record> getRecords() {
        return records;
    }

    public void setRecords(ArrayList<Record> records) {
        this.records = records;
    }

    public ArrayList<Table> getTables() {
        return tables;
    }

    public void setTables(ArrayList<Table> tables) {
        this.tables = tables;
    }

    ArrayList<Table> Lastoo(){
        for (int i = 0; i < tables.size()-1; i++) {
            String[] ASHFUIKHS = tables.get(i).tabNum.split(" ");
            for (int j = i + 1; j < tables.size(); j++) {
                String[] ASHFUIKHS1 = tables.get(j).tabNum.split(" ");
                if(ASHFUIKHS1[3].equals(ASHFUIKHS[3]) && ASHFUIKHS1[4].equals(ASHFUIKHS[4])){
                    Table table = new Table(tables.get(i).tabNum,tables.get(i).Pricncsd+tables.get(j).Pricncsd,tables.get(i).Lastoo+tables.get(j).Lastoo);
                    Table table1 = new Table(tables.get(i).tabNum,-1,-1);
                    tables.set(i,table);
                    tables.set(j,table1);
                }
            }
        }
        Collections.sort(tables, new Comparator<Table>() {
            @Override
            public int compare(Table o1, Table o2) {
                String[] string1 = o1.tabNum.split(" ");
                String[] string2 = o2.tabNum.split(" ");
                return string1[3].compareTo(string2[3]);
            }
        });
        for (Table table : tables) {
            String[] ASHFUIKHS = table.tabNum.split(" ");
            if(table.Pricncsd>-1) {
                System.out.println(ASHFUIKHS[3] + " " + ASHFUIKHS[4] + " " + table.Lastoo);
            }
        }
        return tables;
    }
}

class Table{
    String tabNum;
    int Pricncsd;
    int Lastoo;
    int csdcdc;
    int scsac;
    int vfvrteew;
    int dfdsaf;
    int dsfrgftrg;
    int sfggggh;

    public Table() {
    }

    public Table(String tabNum, int Pricncsd, int Lastoo) {
        this.tabNum = tabNum;
        this.Pricncsd = Pricncsd;
        this.Lastoo = Lastoo;
    }

    public String getTabNum() {
        return tabNum;
    }

    public void setTabNum(String tabNum) {
        this.tabNum = tabNum;
    }

    public int getPricncsd() {
        return Pricncsd;
    }

    public void setPricncsd(int pricncsd) {
        Pricncsd = pricncsd;
    }

    public int getLastoo() {
        return Lastoo;
    }

    public void setLastoo(int lastoo) {
        Lastoo = lastoo;
    }

    public int getCsdcdc() {
        return csdcdc;
    }

    public void setCsdcdc(int csdcdc) {
        this.csdcdc = csdcdc;
    }

    public int getScsac() {
        return scsac;
    }

    public void setScsac(int scsac) {
        this.scsac = scsac;
    }

    public int getVfvrteew() {
        return vfvrteew;
    }

    public void setVfvrteew(int vfvrteew) {
        this.vfvrteew = vfvrteew;
    }

    public int getDfdsaf() {
        return dfdsaf;
    }

    public void setDfdsaf(int dfdsaf) {
        this.dfdsaf = dfdsaf;
    }

    public int getDsfrgftrg() {
        return dsfrgftrg;
    }

    public void setDsfrgftrg(int dsfrgftrg) {
        this.dsfrgftrg = dsfrgftrg;
    }

    public int getSfggggh() {
        return sfggggh;
    }

    public void setSfggggh(int sfggggh) {
        this.sfggggh = sfggggh;
    }
}


部分测试点没有过:

1.2.4 期中考试

7-3 测验3-继承与多态
类图:

代码
import org.w3c.dom.UserDataHandler;

import java.util.Scanner;

class  Shape
{
    Shape(){

    }
    double getarea(){
        return 0;
    }
}

class  Circle extends Shape
{
    private double  r;

    Circle(double r)
    {
        this.r = r;
    }

    public double getarea()
    {
        return Math.PI * Math.pow(r, 2);
    }
}

class Point
{
    double x1;
    double y1;
    Point(double x1,double y1)
    {
        this.x1 = x1;
        this.y1 = y1;
    }
}

class  Rectangle extends Shape
{
    Point x1;
    Point x2;

    Rectangle(Point x1,Point x2)
    {
        this.x1 = new Point(x1.x1,x1.y1);
        this.x2 = new Point(x2.x1, x2.y1);
    }

    public double getarea()
    {
        return Math.abs((this.x2.x1 - this.x1.x1) * (this.x2.y1-this.x1.y1));
    }
}

public class Main {
    static void printArea(Shape shape)
    {
        System.out.printf("%.2f\n",shape.getarea());
    }

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

        int choice = input.nextInt();

        switch(choice) {
            case 1://Circle
                double radiums = input.nextDouble();
                if(radiums <= 0)
                    System.out.println("Wrong Format");
                else{
                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;
    }
}}
7-4 测验4-抽象类与接口
代码:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

class Shape implements Comparable<Shape>
{
    double s;
    Shape(){

    }
    double getarea()
    {
        return 0;
    }

    public int compareTo(Shape otherPerson) {
        // 按照年龄大小进行排序
        return Double.compare(this.getarea(), otherPerson.getarea());
    }
}

class  Circle extends Shape
{
    private double  r;

    Circle(double r)
    {
        this.r = r;
    }

    public double getarea()
    {
        s = Math.PI * Math.pow(r, 2);
        return s;
    }
}

class Point
{
    double x1;
    double y1;
    Point(double x1,double y1)
    {
        this.x1 = x1;
        this.y1 = y1;
    }
}

class  Rectangle extends Shape
{
    Point x1;
    Point x2;

    Rectangle(Point x1,Point x2)
    {
        this.x1 = new Point(x1.x1,x1.y1);
        this.x2 = new Point(x2.x1, x2.y1);
    }

    public double getarea()
    {
        s = Math.abs((this.x2.x1 - this.x1.x1) * (this.x2.y1-this.x1.y1));
        return s;
    }
}

public class Main {
    static void printArea(Shape shape)
    {
        System.out.printf("%.2f\n",shape.getarea());
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        ArrayList<Shape> list = new ArrayList<>();

        int choice = input.nextInt();

        while(choice != 0) {
            switch(choice) {
                case 1://Circle
                    double radiums = input.nextDouble();
                    Shape circle = new Circle(radiums);
                    list.add(circle);
                    break;
                case 2://Rectangle
                    double x1 = input.nextDouble();
                    double y1 = input.nextDouble();
                    double x2 = input.nextDouble();
                    double y2 = input.nextDouble();
                    Point leftTopPoint = new Point(x1,y1);
                    Point lowerRightPoint = new Point(x2,y2);
                    Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);
                    list.add(rectangle);
                    break;
            }
            choice = input.nextInt();
        }

        Collections.sort(list);

        for(int i = 0; i < list.size(); i++) {
            System.out.print(String.format("%.2f", list.get(i).getarea()) + " ");
        }
    }
}

1.3 总结


这几次题目集最麻烦的就是菜单计价程序-4,题目本身并不需要用什么新方法,但是题目要求的东西非常多,错误类型多种多样,测试点有45个之多,开始写代码前要把各种错误情况和测试点的提示都看一遍。我就是没有弄明白就开写导致三次大规模重写。像把输出放最后;像写到后面桌子不知道怎么处理只能推倒前面的又创建新的类。复杂的程序一定要先把结构和功能想好再动手

类的设计可能并没有做到最合理,更加合理的类设计可能可以大大的简化代码量;对各种异常的处理比较分散,如何能够做到对所有的异常类进行统一处理可能不仅能简化代码,还能通过原来没通过的测试点。对方法的设计不够完美,每当添加了新的要求时,都不可避免的会对方法的变量、内容进行修改,如果能进行更好的方法设计,或许就能解决这些问题。

标签:String,int,blog,str,dish,table,第二次,name
From: https://www.cnblogs.com/swc2003/p/17842328.html

相关文章

  • PTA题目集4、5、6以及期中考试的总结性Blog
    一.前言    大三上学期开始,我们开始接触java这门语言,Java具有大部分编程语言所共有的一些特征,被特意设计用于互联网的分布式环境。Java具有类似于C++语言的形式和感觉,但它要比C++语言更易于使用,而且在编程时彻底采用了一种以对象为导向的方式。    pta已经写了六......
  • BLOG2
    一、前言:这三次大作业难度都很大,且逐渐递增!!!期中考试难度也挺大的,选择题几乎都不太会,编程题倒是好一些给了类图,但是对于继承关系和对于接口的使用还是不太熟练,琢磨了很久。二、设计与分析:7-4菜单计价程序-2设计点菜计价程序,根据输入的信息,计算并输出总价格。输入内容按先后顺......
  • 第二次blog-对菜单系统和期中考试的总结
     一、前言这三次菜单的更迭,基本每次都是在前一次的基础上增加部分功能,总体改动不是特别大,越到后期菜单系统越完善时功能修改的速度也更快。主要问题在于一开始的框架没有建好,输入信息后对信息的相关处理没有采取一个清晰地任务分割,而是堆砌在了主函数中,大量ifelse语句增......
  • PTA题目集4、5、6以及期中考试的总结性Blog
    第二次博客一、前言​ 对于PTA题目集4.5.6都是对于菜单题目的不断迭代,难度也是越来越高,对于第四次pta第三题也就是菜单二中main函数代码还是很少的,到了菜单三中的一下关键的逻辑代码就放到了主函数中,到后面不断的迭代主函数的内容也是在不断地增加。还是在类设计方面没有做的很......
  • BLOG-2
    1、前言此次博客主要讲述PTA中的第四次、第五次、第六次以及期中考试的心得体会。其中第四次共有4个题目,包括7-1菜单计价程序-3,7-2单词统计与排序,7-3判断两个日期的先后,计算间隔天数、周数,7-4菜单计价程序-2。这次作业整体来讲不是太难,下面将分析关于菜单题目的我的思路......
  • BLOG2
    一、作业总结该次博客作业是对第4次PTA、菜单计价程序-4、菜单计价程序-5、期中测试的一次总结。这次的菜单计价程序在之前的基础上进行了更完善的更改升级,菜单计价程序-4在菜单计价程序-3的基础上增加了异常情况的处理,菜单计价程序-5在菜单计价程序-3的基础上增加了特色菜的深入......
  • blog2
     一、前言随着对java学习的越来越深入,需要学习的东西也越来越多,第四五次pta题目集主要还是以菜单计价系统为主,相较于以前的菜单计价系统,增加了异常情况的处理,以及特色菜,口味度等功能,使这个菜单计价系统越来越与现实生活相关联,当然与之同时题目的难度当然也是大幅度提高了。虽......
  • blog2
    一、前言第四次题目集,主要是菜单计价程序,难度逐渐提高,难度不是很高。第五次题目集,只有一道菜单计价程序4,这道题是在菜单计价程序3的基础上添加了时间处理,使得程序整体难度提升很大第六次题目集,也只有一道菜单计价程序5,这道题也是以菜单计价程序3为基础,添加了特色菜的处理,难度相......
  • blog2
    前言菜单计价程序-3作为计价4和计价5的基础,做不了3就不用谈作为延伸拓展的4和5,期中考试难度一般。主要是菜单4涉及到了异常的处理机制,难度方面还是菜单3比较难,菜单4,5,都是在3的基础上增加内容,难度逐渐上升7-4菜单计价程序-2分数40全屏浏览题目切换布局作者......
  • Blog-2
    前言题目集4整体难度不高,但菜单3时间不足,没有完整写出来。题目集5和题目集6虽然都只有一道题,但分别是菜单4和菜单5,都是在菜单3的基础上进行迭代,难度较大,因为要求有很多,所以也要花费大量时间。期中考试题目难度适中,但前面的选择题花费了太久时间,导致最后一个编程题没有来得及写。......