BLOG-2

一、前言

面向对象程序设计(Object-Oriented Programming,简称OOP)是一种编程范式,它以对象作为程序的基本单元,将数据和操作封装在一起。面向对象程序设计的基本概念包括类、对象、继承、多态等。

  • 类(Class)是面向对象程序设计的基本构建块,它是一种抽象的数据类型,用于描述具有相同属性和行为的对象的集合。类定义了对象的属性(成员变量)和行为(方法)。

  • 对象(Object)是类的实例化结果,它是具体的、有状态的实体。对象可以根据类的定义,拥有自己的属性值,并能执行类中定义的方法。

  • 继承(Inheritance)是一种机制,允许在已有类的基础上创建新类,新类可以继承和扩展已有类的属性和方法。继承能够提供代码的重用性和层次化的组织结构。

  • 多态(Polymorphism)是指同一类型的对象在不同的情况下可以表现出不同的行为。多态性可以通过继承和接口实现,使得程序可以根据上下文选择合适的方法。

面向对象程序设计的优点包括代码的可重用性、可扩展性、易维护性和模块化等。它提供了一种更加灵活和抽象的编程方式,使得程序的设计和实现更加清晰和可管理。在前三次的作业中我主要使用了创建类和对象的知识,并未涉及到继承和多态的有关知识的运用。

 

二、作业分析

 1.菜单计价程序-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)//根据序号查找一条记录
}

代码如下:

import java.util.Scanner;
import java.util.*;

class Dish{
    String name;//菜品名称
    int unit_price;//单价

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

class Menu{
    Dish[] dishs = new Dish[10];//菜品数组,保存所有菜品信息
    int count = 0;

    Dish searthDish(String dishName){//根据菜名在菜谱中查找菜品信息,返回Dish对象。
        Dish temd = null;
        for(int i = count - 1;i >= 0;i--){
            if(dishName.equals(dishs[i].name)){
                temd = dishs[i];
                break;
            }
        }
        if(temd == null)
            System.out.println(dishName + " does not exist");
        return temd;
    }

    Dish addDish(String dishName,int unit_price){//添加一道菜品信息
        Dish dish = new Dish();
        dish.name = dishName;
        dish.unit_price = unit_price;
        count++;
        return dish;
    }
}

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

class Order {
    Record[] records = new Record[10];//保存订单上每一道的记录
    int count = 0;//订单数量
    void addARecord(int orderNum,String dishName,int portion,int num){//添加一条菜品信息到订单中
        records[count] = new Record();
        records[count].d.name = dishName;
        records[count].orderNum = orderNum;
        records[count].portion = portion;
        records[count].num = num;
        count++;
    }

    int delARecordByOrderNum(int orderNum){//根据序号删除一条记录
        if(orderNum > count || orderNum <= 0){
            System.out.println("delete error;");
            return 0;
        }
        else
            return records[orderNum - 1].getPrice();
    }
}

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        Menu menu = new Menu();
        Order order = new Order();
        int j = 0;
        int l = 0;
        Dish dish;
        int count;
        String temp[];
        int prices = 0;
        int a0,a1,a2,a3;
        while(true){
            String str = sc.nextLine();
            temp = str.split(" ");
            if(str.equals("end"))
                break;
            count = temp.length;
            if(count == 2){
                if(temp[1].equals("delete")){
                    a0 = Integer.parseInt(temp[0]);
                    int c = order.delARecordByOrderNum(a0);
                    prices -= c;
                }
                else{
                    a1 = Integer.parseInt(temp[1]);
                    menu.dishs[j] = menu.addDish(temp[0],a1);
                    j++;
                }
            }
            else
                if(count == 4){
                    a0 = Integer.parseInt(temp[0]);
                    a2 = Integer.parseInt(temp[2]);
                    a3 = Integer.parseInt(temp[3]);
                    order.addARecord(a0,temp[1],a2,a3);
                    dish = menu.searthDish(temp[1]);
                    if(dish != null){
                        order.records[l].d = dish;
                        int c = order.records[l].getPrice();
                        System.out.println(order.records[l].orderNum + " " + dish.name + " " + c);
                        prices += c;
                    }
                    l++;
                }
        }
        System.out.println(prices);
    }
}

分析:用string接收输入的一行信息,再根据空格分成string数组中的元素,用if语句判断这个数组中有多少个元素来判断用户想要我们做什么。

 

2.菜单计价系统-3

类设计如下:

菜品类:对应菜谱上一道菜的信息。

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)//根据序号查找一条记录

}

代码如下:

import java.util.Scanner;
import java.util.*;

class Dish{
    String name;//菜品名称
    int unit_price;//单价

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

class Menu{
    Dish[] dishs = new Dish[10];//菜品数组,保存所有菜品信息
    int count = 0;

    Dish searthDish(String dishName){//根据菜名在菜谱中查找菜品信息,返回Dish对象。
        Dish temd = null;
        for(int i = count - 1;i >= 0;i--){
            if(dishName.equals(dishs[i].name)){
                temd = dishs[i];
                break;
            }
        }
        if(temd == null)
            System.out.println(dishName + " does not exist");
        return temd;
    }

    Dish addDish(String dishName,int unit_price){//添加一道菜品信息
        Dish dish = new Dish();
        dish.name = dishName;
        dish.unit_price = unit_price;
        count++;
        return dish;
    }
}

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

class Order {
    Record[] records = new Record[10];//保存订单上每一道的记录
    int count = 0;//订单数量
    void addARecord(int orderNum,String dishName,int portion,int num){//添加一条菜品信息到订单中
        records[count] = new Record();
        records[count].d.name = dishName;
        records[count].orderNum = orderNum;
        records[count].portion = portion;
        records[count].num = num;
        count++;
    }

    int delARecordByOrderNum(int orderNum){//根据序号删除一条记录
        if(orderNum > count || orderNum <= 0){
            System.out.println("delete error;");
            return 0;
        }
        else
            return records[orderNum - 1].getPrice();
    }
}

class Table{
    int tableNum;
    String tableDtime;
    int year,month,day,week,hh,mm,ss;
    int sum = 0;
    Order odt = new Order();
    float dis = -1;

    void Gettottalprice(){
        if(dis > 0){
            sum = Math.round(sum * dis);
            System.out.println("table " + tableNum + ": " + sum);
        }
        else
            System.out.println("table " + tableNum + " out of opening hours");
    }

    void AheadProcess(String tableDtime){
        this.tableDtime = tableDtime;
        processTime();
        discount();
    }

    void processTime(){
        String[] temp = tableDtime.split(" ");
        tableNum = Integer.parseInt(temp[1]);
        String[] temp1 = temp[2].split("/");
        String[] temp2 = temp[3].split("/");
        year = Integer.parseInt(temp1[0]);
        month = Integer.parseInt(temp1[1]);
        day = Integer.parseInt(temp1[2]);
        Calendar c = Calendar.getInstance();
        c.set(year, (month-1), day);
        week = c.get(Calendar.DAY_OF_WEEK);
        if(week == 1)
            week = 7;
        else
            week--;
        hh = Integer.parseInt(temp2[0]);
        mm = Integer.parseInt(temp2[1]);
        ss = Integer.parseInt(temp2[2]);
    }

    void discount(){
        if(week >= 1 && week <= 5){
            if( (hh >= 17 && hh < 20) ||
                (hh == 20 && mm < 30) ||
                (hh == 20 && mm == 30 && ss == 0))
                dis = 0.8F;
            if( (hh >= 11 && hh <= 13) ||
                (hh == 10 && mm >= 30) ||
                (hh == 14 && mm < 30) ||
                (hh == 14 && mm == 30 && ss == 0))
                dis = 0.6F;
        }
        else{
            if( (hh >= 10 && hh <= 20) ||
                (hh == 9 && mm >= 30) ||
                (hh == 21 && mm < 30) ||
                (hh == 21 && mm == 30 && ss == 0))
                dis = 1.0F;
        }
    }
}

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        Menu menu = new Menu();
        Table[] tablemes = new Table[10];
        int j = 0;
        int l = 0;
        int k = 0;
        Dish dish;
        int cntTable = 0;
        int count;
        String[] temp;
        int a0,a1,a2,a3,a4;
        while(true){
            String st = sc.nextLine();
            temp = st.split(" ");
            if(st.equals("end"))
                break;
            count = temp.length;
            if(count == 2){
                if(temp[1].equals("delete")){
                    a0 = Integer.parseInt(temp[0]);
                    int c = tablemes[cntTable].odt.delARecordByOrderNum(a0);
                    tablemes[cntTable].sum -= c;
                }
                else{
                    a1 = Integer.parseInt(temp[1]);
                    menu.dishs[j] = menu.addDish(temp[0],a1);
                    j++;
                }
            }
            else
                if(count == 4){
                    if(temp[0].equals("table")){
                        cntTable++;
                        l = 0;
                        tablemes[cntTable] = new Table();
                        tablemes[cntTable].AheadProcess(st);
                        System.out.println("table " + cntTable + ": ");
                    }
                    else{
                        a0 = Integer.parseInt(temp[0]);
                        a2 = Integer.parseInt(temp[2]);
                        a3 = Integer.parseInt(temp[3]);
                        tablemes[cntTable].odt.addARecord(a0, temp[1],a2 , a3);
                        dish = menu.searthDish(temp[1]);
                        if(dish != null){
                            tablemes[cntTable].odt.records[l].d = dish;
                            int a = tablemes[cntTable].odt.records[l].getPrice();
                            System.out.println(tablemes[cntTable].odt.records[l].orderNum + " " + dish.name + " " + a );
                            tablemes[cntTable].sum += a;
                        }
                        l++;
                    }
                }
                else
                    if(count == 5){
                        a1 = Integer.parseInt(temp[1]);
                        a3 = Integer.parseInt(temp[3]);
                        a4 = Integer.parseInt(temp[4]);
                        tablemes[cntTable].odt.addARecord(a1,temp[2],a3,a4);
                        dish = menu.searthDish(temp[2]);
                        if(dish != null){
                            tablemes[cntTable].odt.records[l].d.unit_price = dish.unit_price;
                            int b = tablemes[cntTable].odt.records[l].getPrice();
                            System.out.println(temp[1] + " table " + tablemes[cntTable].tableNum + " pay for table " + temp[0] + " " + b);
                            tablemes[cntTable].sum += b;
                        }
                        l++;
                    }
        }
        for(int i = 1;i < cntTable + 1;i++)
            tablemes[i].Gettottalprice();
    }
}

分析:用string接收输入的一行信息,再根据空格分成string数组中的元素,用if语句判断这个数组中有多少个元素来判断用户想要我们做什么。和菜单计价系统-2一样,只是多了一个桌号。

 

3.菜单计价系统-4

类设计如下:

菜品类:对应菜谱上一道菜的信息。

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)//根据序号查找一条记录

}

代码如下:

import java.text.ParseException;
import java.time.DateTimeException;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Scanner;

public class Main{
    public static boolean isNumeric(String string){
        int intValue;
        try{
            intValue = Integer.parseInt(string);
            return true;
        }
        catch(NumberFormatException e){
            return false;
        }
    }

    public static void main(String[] args) throws ParseException{
        Menu menu = new Menu();
        ArrayList<Table> tables = new ArrayList<Table>();
        Scanner input = new Scanner(System.in);
        String str1 = new String();
        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[] token = str1.split(" ");
                        portion = Integer.parseInt(token[2]);
                        quota = Integer.parseInt(token[3]);
                        if(temp.order.records.size() > 0){
                            if(Integer.parseInt(
                                    token[0]) <= temp.order.records.get(temp.order.records.size() - 1).orderNum){
                                System.out.println("record serial number sequence error");
                                continue;
                            }
                        }
                        if(menu.searchDish(token[1]) == -1){
                            System.out.println(token[1] + " does not exist");
                            continue;
                        }
                        if(portion > 3 || portion < 1){
                            System.out.println(Integer.parseInt(token[0]) + " portion out of range " + portion);
                            continue;
                        }
                        if(quota > 15){
                            System.out.println(Integer.parseInt(token[0]) + " num out of range " + quota);
                            continue;
                        }
                        temp.od(menu, token[0], token[1], portion, quota);
                    }
                    // 判断是否为删除订单
                    else if(str1.matches("[1-9][\\d]* delete")){
                        String[] token = str1.split(" ");
                        temp.order.delARecordByOrderNum(Integer.parseInt(token[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 if(str1.matches("ta.*")){
                        break;
                        
                    }
                    else{
                        System.out.println("wrong format");
                        continue;
                    }
                }
            }
            else if(str1.matches("t.*")){
                isRepeat = true;
                temp = tables.get(tables.size());
                while(true){
                    str1 = input.nextLine();
                    if(str1.matches("[1-9][\\d]* [\\S]* [\\d] [1-9][\\d]*")){
                        String[] token = str1.split(" ");
                        portion = Integer.parseInt(token[2]);
                        quota = Integer.parseInt(token[3]);
                        // 判断订单号是否由小到大排列
                        if(temp.order.records.size() > 0){
                            if(Integer.parseInt(
                                    token[0]) <= temp.order.records.get(temp.order.records.size() - 1).orderNum){
                                System.out.println("record serial number sequence error");
                                continue;
                            }
                        }
                        if(menu.searchDish(token[1]) == -1){
                            System.out.println(token[1] + " does not exist");
                            continue;
                        }
                        if(portion > 3 || portion < 1){
                            System.out.println(Integer.parseInt(token[0]) + " portion out of range " + portion);
                            continue;
                        }
                        if(quota > 15){
                            System.out.println(Integer.parseInt(token[0]) + " num out of range " + quota);
                            continue;
                        }
                        temp.od(menu, token[0], token[1], portion, quota);
                    }
                    // 判断是否为删除订单
                    else if(str1.matches("[1-9][\\d]* delete")){
                        String[] token = str1.split(" ");
                        temp.order.delARecordByOrderNum(Integer.parseInt(token[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.get(tables.size() - 1).order.records.addAll(temp.order.records);
                }
            }
            else{
                str1 = input.nextLine();
                continue;
            }

            // 本桌点菜结束,进入下一桌
            if(isRepeat){
                tables.remove(repeatNum);
            }
            temp.getSum();
            tables.add(temp);
        }
        // 最终输出桌号订单信息
        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<Dish>();

        int searchDish(String dishName){
            for(int i = 0; i < dishs.size(); i++){
                if(dishName.equals(dishs.get(i).name)){
                    return i;
                }
            }
            return -1;
        }

        Dish addDish(String dishName, int unit_price){
            Dish newDish = new Dish();
            newDish.name = dishName;
            newDish.unit_price = unit_price;
            return newDish;
        }
    }

    static class Order{
//        Record[] records = new Record[20];
        ArrayList<Record> records = new ArrayList<Record>();

        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;
        }

        int searchReocrd(String name){
            for(int i = 0; i < records.size(); i++){
                if(records.get(i).d.name == name){
                    return i;
                }
            }
            return -1;
        }

        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;
        }
    }
}

分析:啊啊啊,好难啊,毁灭吧!

 

4.菜单计价系统-5

类设计如下:

菜品类:对应菜谱上一道菜的信息。

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)//根据序号查找一条记录

}

代码如下:

import java.util.*;
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        int wrongNum1 = 0;
        boolean flag;
        Scanner scanner = new Scanner(System.in);
        Menu menu = new Menu();
        List<Table> tables = new ArrayList<>();
        Table currentTable = null;
        while(scanner.hasNext()){
            String line = scanner.nextLine();
            if(line.equals("end")){
                break;
            }
            String[] parts = line.split("\\s+");
            if(parts[0].equals("table")){
                if( (parts.length != 7) ||
                    (!Character.isDigit(parts[1].charAt(0))) ||
                    (!parts[2].startsWith(":")) ||
                    (!Character.isLetter(parts[3].charAt(0)))){
                    System.out.println("wrong format");
                }
                int tableNum = Integer.parseInt(parts[1]);
                String user = parts[3];
                String phoneNum = parts[4];
                String date = parts[5];
                String time = parts[6];
                currentTable = new Table(tableNum,user,phoneNum,date,time);
                tables.add(currentTable);
                flag = phoneNum.matches("13[356]|18[019]\\d{8}");
                if(phoneNum.length() != 11 && !flag)
                    wrongNum1++;
            }
            else if(parts[1].equals("delete")){
                int orderNum = Integer.parseInt(parts[0]);
                assert currentTable != null;
                currentTable.getTotalPrice();
                boolean b = currentTable.order.delARecordByOrderNum(orderNum);
                Record record = currentTable.order.addARecord(orderNum,"",0,-1,-1,menu);
                record.dish.unit_price = -1;
                record.isDelOrSue = b;
            }
            else if(parts[0].equals("end")){
                break;
            }
            else{
                if(currentTable == null){
                    if(parts.length == 2){
                        String dishName = parts[0];
                        int unit_price = Integer.parseInt(parts[1]);
                        int Flag = 0;
                        menu.addDish(dishName,Flag,unit_price);
                    }
                    if(parts.length == 4){
                        String dishName = parts[0];
                        int Flag = 0;
                        int unit_price = Integer.parseInt(parts[2]);
                        if(parts[1].equals("川菜"))
                            Flag = 1;
                        if(parts[1].equals("晋菜"))
                            Flag = 2;
                        if(parts[1].equals("浙菜"))
                            Flag = 3;
                        menu.addDish(dishName,Flag,unit_price);
                    }
                }
                else{
                    if(parts.length == 4){
                        int tableNum = currentTable.tableNum;
                        int orderNum = Integer.parseInt(parts[0]);
                        String dishName = parts[1];
                        int portion = Integer.parseInt(parts[2]);
                        int num = Integer.parseInt(parts[3]);
                        for(Table table : tables){
                            if(table.tableNum == tableNum){
                                table.order.addARecord(orderNum,dishName,-1,portion,num,menu);
                                break;
                            }
                        }
                    }
                    else if(parts.length == 5){
                        if(parts[1].length() == 1){
                            int tableNum = currentTable.tableNum;
                            int orderNum = Integer.parseInt(parts[1]);
                            String dishName = parts[2];
                            int portion = Integer.parseInt(parts[3]);
                            int num = Integer.parseInt(parts[4]);
                            for(Table table : tables){
                                if(table.tableNum == tableNum){
                                    Record record = table.order.addARecord(orderNum,dishName,
                                                                           -2,portion,num,menu);
                                    record.payTableId = Integer.parseInt(parts[0]);
                                    record.payOnBehalf = true;
                                    break;
                                }
                            }
                        }
                        if(parts[1].length() != 1){
                            int tableNum = currentTable.tableNum;
                            int orderNum = Integer.parseInt(parts[0]);
                            String dishName = parts[1];
                            int tasteNum = Integer.parseInt(parts[2]);
                            int portion = Integer.parseInt(parts[3]);
                            int num = Integer.parseInt(parts[4]);
                            for(Table table : tables){
                                if(table.tableNum == tableNum){
                                    table.order.addARecord(orderNum,dishName,
                                                           tasteNum,portion,num,menu);
                                    break;
                                }
                            }
                        }
                    }
                    if(parts.length == 6){
                        int tableNum = currentTable.tableNum;
                        int orderNum = Integer.parseInt(parts[1]);
                        String dishName = parts[2];
                        int tasteNum = Integer.parseInt(parts[3]);
                        int portion = Integer.parseInt(parts[4]);
                        int num = Integer.parseInt(parts[5]);
                        for(Table table : tables){
                            if(table.tableNum == tableNum){
                                Record record = table.order.addARecord(orderNum,dishName,
                                                                       tasteNum,portion,num,menu);
                                record.payTableId = Integer.parseInt(parts[0]);
                                record.payOnBehalf = true;
                                break;
                            }
                        }
                        for(Table table : tables){
                            if(table.tableNum == tableNum){
                                Record record = table.order.addARecord(orderNum,dishName,
                                                                       tasteNum,portion,num,menu);
                                record.payTableId = Integer.parseInt(parts[0]);
                                record.payOnBehalf = true;
                                break;
                            }
                        }
                    }
                }
            }
        }

        ArrayList<Table> objects = new ArrayList<>();
        for(Table table : tables){
            int totalPrice = table.getTotalPrice();
            if(totalPrice != 0 && wrongNum1 == 0){
                System.out.println("table " + table.tableNum + ": ");
                for(Record record : table.order.records){
                    if(!record.payOnBehalf){
                        if( record.dish.unit_price != 0 &&
                            record.dish.unit_price != -1 &&
                            record.tasteNum != -10){
                            if(record.dish.Flag == 1 && record.tasteNum >= 6){
                                System.out.println("spicy num out of range :" +
                                                   record.tasteNum);
                            }
                            else if(record.dish.Flag == 2 && record.tasteNum >= 5){
                                System.out.println("acidity num out of range :" +
                                                   record.tasteNum);
                            }
                            else if(record.dish.Flag == 3 && record.tasteNum >= 4){
                                System.out.println("sweetness num out of range :" +
                                                   record.tasteNum);
                            }
                            else
                                System.out.println(record.orderNum + " " +
                                                   record.dish.name + " " +
                                                   record.dish.getPrice(record.portion) *
                                                   record.num);
                        }
                        if(record.dish.unit_price == 0){
                            System.out.println(record.dish.name + " does not exist");
                        }
                        if(record.dish.unit_price == -1){
                            if(!record.isDelOrSue || record.isDel){
                                System.out.println("delete error;");
                            }
                        }
                    }
                    else{
                        System.out.println(record.orderNum + " table " +
                                           table.tableNum + " pay for table " +
                                           record.payTableId + " " +
                                           record.dish.getPrice(record.portion) *
                                           record.num);
                    }
                }
                objects.add(table);
            }
            if(totalPrice == 0 && wrongNum1 == 0){
                System.out.println("table " + table.tableNum +
                                   " out of opening hours");
            }
        }
        for(Table table : objects){
            int totalPrice = table.getTotalPrice();
            int totalPrice_yuan = table.getTotalPrice_yuan();
            if( totalPrice != 0 &&
                table.getNum1() != 0 &&
                table.getNum2() != 0 &&
                table.getNum3() != 0){
                System.out.println("table " + table.tableNum + ": " +
                                   totalPrice_yuan + " " +
                                   totalPrice + " " + "川菜 " +
                                   table.getNum1() + " " +
                                   table.getTaste1() + " " + "晋菜 " +
                                   table.getNum2() + " " +
                                   table.getTaste2() + " " + "浙菜 " +
                                   table.getNum3() + " " +
                                   table.getTaste3());
            }
            if( totalPrice != 0 &&
                table.getNum1() == 0 &&
                table.getNum2() != 0 &&
                table.getNum3() != 0){
                System.out.println("table " + table.tableNum + ": " +
                                   totalPrice_yuan + " " +
                                   totalPrice + " " + "晋菜 " +
                                   table.getNum2() + " " +
                                   table.getTaste2() + " " + "浙菜 " +
                                   table.getNum3() + " " +
                                   table.getTaste3());
            }
            if( totalPrice != 0 &&
                table.getNum1() != 0 &&
                table.getNum2() == 0 &&
                table.getNum3() != 0){
                System.out.println("table " + table.tableNum + ": " +
                                   totalPrice_yuan + " " +
                                   totalPrice + " " + "川菜 " +
                                   table.getNum1() + " " +
                                   table.getTaste1() + " " + "浙菜 " +
                                   table.getNum3() + " " + table.getTaste3());
            }
            if( totalPrice != 0 &&
                table.getNum1() != 0 && table.getNum2() != 0 && table.getNum3() == 0){
                System.out.println("table " + table.tableNum + ": " +
                                   totalPrice_yuan + " " +
                                   totalPrice + " " + "川菜 " +
                                   table.getNum1() + " " +
                                   table.getTaste1() + " " + "晋菜 " +
                                   table.getNum2() + " " +
                                   table.getTaste2());
            }
            if( totalPrice != 0 &&
                table.getNum1() == 0 &&
                table.getNum2() == 0 &&
                table.getNum3() != 0){
                System.out.println("table " + table.tableNum + ": " +
                                   totalPrice_yuan + " " +
                                   totalPrice + " " + "浙菜 " +
                                   table.getNum3() + " " +
                                   table.getTaste3());
            }
            if( totalPrice != 0 &&
                table.getNum1() == 0 &&
                table.getNum2() != 0 &&
                table.getNum3() == 0){
                System.out.println("table " + table.tableNum + ": " +
                                   totalPrice_yuan + " " +
                                   totalPrice + " " + "晋菜 " +
                                   table.getNum2() + " " +
                                   table.getTaste2());
            }
            if( totalPrice != 0 &&
                table.getNum1() != 0 &&
                table.getNum2() == 0 &&
                table.getNum3() == 0){
                System.out.println("table " + table.tableNum + ": " +
                                   totalPrice_yuan + " " +
                                   totalPrice + " " + "川菜 " +
                                   table.getNum1() + " " +
                                   table.getTaste1());
            }
            if( totalPrice != 0 &&
                table.getNum1() == 0 &&
                table.getNum2() == 0 &&
                table.getNum3() == 0){
                System.out.println("table " + table.tableNum + ": " +
                                   totalPrice_yuan + " " +
                                   totalPrice + "");
            }
        }
        int NUM;
        NUM = objects.size();
        objects.get(0).Flag = 0;
        for(int m = 0;m < NUM;m++){
            objects.get(m).price_user = objects.get(m).getTotalPrice();
            objects.get(m).Flag = -1;
        }
        for(int i = 0;i < NUM;i++){
            for(int j =(i + 1);j < NUM;j++){
                if(objects.get(i).Flag == 1){
                    break;
                }
                if( objects.get(i).user.equals(objects.get(j).user)){
                    objects.get(i).price_user += objects.get(j).getTotalPrice();
                    objects.get(j).Flag = 1;
                }
            }
        }
        for(int n = 8;n > 0;n--){
            for(int l = 0;l < NUM;l++){
                if( objects.get(l).user.length() == n &&
                    objects.get(l).Flag == -1){
                    System.out.println(objects.get(l).user + " " +
                                       objects.get(l).phoneNum + " " +
                                       objects.get(l).price_user);
                }
            }
        }
    }
}

class Dish{
    String name;
    int Flag;
    int unit_price;

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

    public int getPrice(int portion){
        double price = 0.0;
        if(portion == 1) price = unit_price;
        else if(portion == 2) price = unit_price * 1.5;
        else if(portion == 3) price = unit_price * 2.0;
        return(int) Math.round(price);
    }
}

class Menu{
    Map<String,Dish> dishMap = new HashMap<>();
    public Dish searchDish(String dishName){
        return dishMap.get(dishName);
    }
    public Dish addDish(String dishName,int Flag,int unit_price){
        Dish dish = new Dish(dishName,Flag,unit_price);
        dishMap.put(dishName,dish);
        return dish;
    }
}

class Record{
    int orderNum;
    Dish dish;
    int tasteNum;
    int portion;
    int num;
    boolean isDel = false;
    boolean isDelOrSue = false;
    boolean payOnBehalf = false;
    int payTableId;

    public Record(int orderNum,Dish dish,int tasteNum,int portion,int num){
        this.orderNum = orderNum;
        this.dish = dish;
        this.portion = portion;
        this.num = num;
        this.tasteNum = tasteNum;
    }

    public int getPrice(){
        return !isDel?dish.getPrice(portion) * num:0;
    }

    public int getTasteNum(){
        return !isDel?tasteNum:-1;
    }
}

class Order{
    List<Record> records = new ArrayList<>();
    public int getNum1(){
        int Num1 = 0;
        for(Record record : records){
            if( record.dish.Flag == 1 &&
                record.tasteNum <= 5 &&
                record.getTasteNum() >= 0){
                Num1 += record.num;
            }
        }
        return Num1;
    }
    public int getNum2(){
        int Num2 = 0;
        for(Record record : records){
            if( record.dish.Flag == 2 &&
                record.tasteNum <= 4 &&
                record.getTasteNum() >= 0){
                Num2 += record.num;
            }
        }
        return Num2;
    }

    public int getNum3(){
        int Num3 = 0;
        for(Record record : records){
            if( record.dish.Flag == 3 &&
                record.tasteNum <= 3 &&
                record.getTasteNum() >= 0){
                Num3 += record.num;
            }
        }
        return Num3;
    }

    public int getTasteNum1(){
        int Num1 = 0;
        double getTasteNum1 = 0;
        for(Record record : records){
            if( record.dish.Flag == 1 &&
                record.tasteNum <= 5 &&
                record.getTasteNum() >= 0){
                Num1 += record.num;
                getTasteNum1 += record.getTasteNum() * record.num;
            }
        }
        getTasteNum1 = getTasteNum1 / Num1;
        return (int)Math.round(getTasteNum1);
    }

    public int getTasteNum2(){
        int Num2 = 0;
        double getTasteNum2 = 0;
        for(Record record : records){
            if( record.dish.Flag == 2 &&
                record.tasteNum <= 4 &&
                record.getTasteNum() >= 0){
                Num2 += record.num;
                getTasteNum2 += record.getTasteNum() * record.num;
            }
        }
        getTasteNum2 = getTasteNum2 / Num2;
        return (int)Math.round(getTasteNum2);
    }

    public int getTasteNum3(){
        int Num3 = 0;
        double getTasteNum3 = 0;
        for(Record record : records){
            if( record.dish.Flag == 3 &&
                record.tasteNum <= 3 &&
                record.getTasteNum() >= 0){
                Num3 += record.num;
                getTasteNum3 += record.getTasteNum() * record.num;
            }
        }
        getTasteNum3 = getTasteNum3 / Num3;
        return (int)Math.round(getTasteNum3);
    }

    public Record addARecord(int orderNum,String dishName,int tasteNum,int portion,int num,Menu menu){
        Dish dish = menu.searchDish(dishName);
        if(dish == null || dish.unit_price == -1){
            dish = menu.addDish(dishName,0,0);
        }
        Record record = new Record(orderNum,dish,tasteNum,portion,num);
        records.add(record);
        return record;
    }

    public boolean delARecordByOrderNum(int orderNum){
        for(Record record1 : records){
            if( record1.orderNum == orderNum){
                record1.isDel = true;
                record1.isDelOrSue = true;
                return true;
            }
        }
        System.out.println("delete error;");
        return false;
    }
}

class Table{
    int Flag;
    int price_user;
    int tableNum;
    String user;
    String phoneNum;
    String date;
    String time;
    Order order = new Order();

    public Table(int tableNum,String user,String phoneNum,String date,String time){
        this.tableNum = tableNum;
        this.user = user;
        this.phoneNum = phoneNum;
        this.date = date;
        this.time = time;
    }

    public String getTaste1(){
        int TasteNum1 = order.getTasteNum1();
        if(TasteNum1 == 0){
            return "不辣";
        }
        if(TasteNum1 == 1){
            return "微辣";
        }
        if(TasteNum1 == 2){
            return "稍辣";
        }
        if(TasteNum1 == 3){
            return "辣";
        }
        if(TasteNum1 == 4){
            return "很辣";
        }
        if(TasteNum1 == 5){
            return "爆辣";
        }
        return date;
    }

    public String getTaste2(){
        int TasteNum2 = order.getTasteNum2();
        if(TasteNum2 == 0){
            return "不酸";
        }
        if(TasteNum2 == 1){
            return "微酸";
        }
        if(TasteNum2 == 2){
            return "稍酸";
        }
        if(TasteNum2 == 3){
            return "酸";
        }
        if(TasteNum2 == 4){
            return "很酸";
        }
        return date;
    }

    public String getTaste3(){
        int TasteNum3 = order.getTasteNum3();
        if(TasteNum3 == 0){
            return "不甜";
        }
        if(TasteNum3 == 1){
            return "微甜";
        }
        if(TasteNum3 == 2){
            return "稍甜";
        }
        if(TasteNum3 == 3){
            return "甜";
        }
        return date;
    }

    public int getNum1(){
        return order.getNum1();
    }

    public int getNum2(){
        return order.getNum2();
    }

    public int getNum3(){
        return order.getNum3();
    }

    public double getDiscount1(){
        double discount1 = 1.0;
        String weekday = getWeekday(date);
        if(time.contains("/")){
            time = time.replaceAll("/",":");
            String[] timeParts = time.split(":");
            if(timeParts[0].length() < 2){
                timeParts[0] = "0" + timeParts[0];
            }
            time= String.join(":",timeParts);
        }
        if(weekday.equals("Saturday") ||
           weekday.equals("Sunday")){
            if(!isOpeningTime(time)){
                return 0;
            }
        }
        else if(weekday.equals("Monday") ||
                  weekday.equals("Tuesday") ||
                  weekday.equals("Wednesday") ||
                  weekday.equals("Thursday") ||
                  weekday.equals("Friday")){
            if(isOpeningTime(time)){
                if( time.compareTo("10:30:00") >= 0 &&
                    time.compareTo("14:30:00") <= 0){
                    discount1 = 0.6;
                }
                else if(time.compareTo("17:00:00") >= 0 &&
                        time.compareTo("20:30:00") <= 0){
                        discount1 = 0.8;
                }
                else{
                    return 0;
                }
            }
        }
        return discount1;
    }

    public double getdiscount2(){
        double discount2 = 1.0;
        String weekday = getWeekday(date);
        if(time.contains("/")){
            time = time.replaceAll("/",":");
            String[] timeParts = time.split(":");
            if(timeParts[0].length() < 2){
                timeParts[0] = "0" + timeParts[0];
            }
            time= String.join(":",timeParts);
        }
        if( weekday.equals("Saturday") ||
            weekday.equals("Sunday")){
            if(!isOpeningTime(time)){
                return 0;
            }
        }
        else if(weekday.equals("Monday") ||
                weekday.equals("Tuesday") ||
                weekday.equals("Wednesday") ||
                weekday.equals("Thursday") ||
                weekday.equals("Friday")){
            if(isOpeningTime(time)){
                if( time.compareTo("10:30:00") >= 0 &&
                    time.compareTo("14:30:00") <= 0){
                    discount2 = 0.7;
                }
                else if(time.compareTo("17:00:00") >= 0 &&
                        time.compareTo("20:30:00") <= 0){
                        discount2 = 0.7;
                }
                else{
                    return 0;
                }
            }
        }
        return discount2;
    }

    public int getTotalPrice(){
        int totalPrice;
        int totalPrice1 = 0;
        int totalPrice2 = 0;
        for(Record record : order.records){
            if(    record.dish.Flag == 0){
                totalPrice1 += (int)Math.round(record.getPrice()*getDiscount1());
            }
            if( (record.dish.Flag == 1 && record.tasteNum <= 5) ||
                (record.dish.Flag == 2 && record.tasteNum <= 4) ||
                (record.dish.Flag == 3 && record.tasteNum <= 3)){
                totalPrice2 += (int)Math.round(record.getPrice()*getdiscount2());
            }
        }
        totalPrice = totalPrice1 + totalPrice2;
        return totalPrice;
    }

    public int getTotalPrice_yuan(){
        int totalPrice_yuan;
        int totalPrice1_yuan = 0;
        int totalPrice2_yuan = 0;
        for(Record record : order.records){
            if( record.dish.Flag == 0){
                totalPrice1_yuan  += Math.round(record.getPrice());
            }
            if( (record.dish.Flag == 1 && record.tasteNum <=5 ) ||
                (record.dish.Flag == 2 && record.tasteNum <=4 ) ||
                (record.dish.Flag == 3 && record.tasteNum <=3)){
                totalPrice2_yuan += Math.round(record.getPrice());
            }
        }
        totalPrice_yuan = totalPrice1_yuan + totalPrice2_yuan;
        return totalPrice_yuan;
    }

    private String getWeekday(String date){
        String[] parts = date.split("/");
        int year = Integer.parseInt(parts[0]);
        int month = Integer.parseInt(parts[1]);
        int day = Integer.parseInt(parts[2]);
        Calendar calendar = Calendar.getInstance();
        calendar.set(year,month - 1,day);
        int weekdayIndex = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        String[] weekdays = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
        return weekdays[weekdayIndex];
    }

    private boolean isOpeningTime(String time){
        return time.compareTo("09:30:00") >= 0 && time.compareTo("21:30:00") <= 0;
    }
}

分析:桌号识别格式那个快疯了,用if语句写考虑的东西太多了,放弃了。

 

5.期中考试

(1)测验1-圆类设计

代码如下:

import java.util.Scanner;
import java.lang.Math;

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

        double r = sc.nextDouble();

        if(r <= 0){
            System.out.println("Wrong Format");
            return;
        }
        else{
            double s = Math.PI * Math.pow(r,2);
            System.out.println(String.format("%.2f",s));
        }
    }
}

分析:没有用类,π我一开始是自己打的数字,输入多少位都有一个答案不对,最后用math函数才对的。

 

(2)测验2-类结构设计

 代码如下:
import java.util.Scanner;
import java.lang.Math;

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

        double x1 = sc.nextDouble();
        double y1 = sc.nextDouble();
        double x2 = sc.nextDouble();
        double y2 = sc.nextDouble();

        double s = (x2 - x1) * (y2 - y1);
        System.out.println(String.format("%.2f",Math.abs(s)));
    }
}

分析:也没有用类,就是一开始没想到要用绝对值,导致有几个答案错误,后面加上绝对值就过了。

 

(3)测验3-继承与多态

代码如下:

import java.util.Scanner;
import java.lang.Math;

public class Main{
    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();
            if(radiums <= 0){
                System.out.println("Wrong Format");
                return;
            }
            else{
                double s = Math.PI * Math.pow(radiums,2);
                System.out.println(String.format("%.2f",s));
            }
        case 2://Rectangle
            double x1 = input.nextDouble();
            double y1 = input.nextDouble();
            double x2 = input.nextDouble();
            double y2 = input.nextDouble();
            
            double s = (x2 - x1) * (y2 - y1);
            System.out.println(String.format("%.2f",Math.abs(s)));
            break;
        }
        
    }
}

分析:还是没有用类,题目上还给了部分代码,把上两题的部分代码复制过来就直接过了。

 

(4)抽象类与接口

代码如下:

import java.util.Scanner;
import java.lang.Math;

public class Main{
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        
        int choice = input.nextInt();
        while(true){
            int choice = input.nextInt();
            switch(choice) {
                case 0:return;break;
            case 1://Circle
                double radiums = input.nextDouble();
                if(radiums <= 0){
                    System.out.println("Wrong Format");
                }
                else{
                    double s = Math.PI * Math.pow(radiums,2);
                    System.out.println(String.format("%.2f",s));
                }
            case 2://Rectangle
                double x1 = input.nextDouble();
                double y1 = input.nextDouble();
                double x2 = input.nextDouble();
                double y2 = input.nextDouble();
            
                double s = (x2 - x1) * (y2 - y1);
                System.out.println(String.format("%.2f",Math.abs(s)));
                break;
            }
        }
    }
}

分析:编译没过。当时时间还剩二十多分钟,但我的电脑只剩10%的电了,急忙改了一下,思路是用while函数循环接收choice并执行switch语句,用double数组记录结果,循环结束后对double数组中的元素进行排序然后输出。没错,还是没用类(doge)。

 

三、总结:

  通过完成这三次pta的作业,我发现我写代码的时候还是惯用C语言的思维,并且是面向结果编程而不是面向对象编程,这是个很大的问题。

posted @ 2023-11-19 14:18  zasmaxes  阅读(22)  评论(0)    收藏  举报