pta1-3次大作业总结

一、前言

在之前做的Java三次题集,每个题集侧重点不同,难度呈递增趋势:

1. 第一次题集有九个题目,主要为较基础的算法题,比较好写。

2.第二次题目集有四个题目,内容涉及类与方法的调用及相关日期的包的使用,难度明显增加,有一定难度(最难的是第二题:菜单计价程序-2)。

3.第三次题目集有七个题目,有简有难,其中涉及了一些未接触过的包(HashSet),并且还有增加了很大难度的菜单计价程序-3。

总而言之,这三次题目集的练习有简有难,尤其是菜单计价程序,相当折磨人,不过也能从中获得许多知识,得到很好的锻炼。

二、设计与分析

(1)7-1 身体质量指数(BMI)测算

改题目首先需要定义好身高,体重,BMI三个变量(切记要定义为double类型),用身高与体重的计算来表示BMI,然后运用if与else if语句对数据进行判断,然后进行相应的输出。

 代码如下:

import java.util.Scanner;

 

public class Main{
public static void main(String[] args){
Scanner sc =new Scanner(System.in);
double kilograms=sc.nextDouble();
double metres=sc.nextDouble();
double BMI=kilograms/metres/metres;
if(kilograms>0&&kilograms<=727&&metres>0&&metres<=2.72){
if(BMI<18.5)
System.out.println("thin");
else if(BMI>=18.5&&BMI<24)
System.out.println("fit");
else if(BMI>=24&&BMI<28)
System.out.println("overweight");
else
System.out.println("fat");
}
else
System.out.println("input out of range");
}
}

 

 

(2)7-3 jmu-java-日期类的基本使用

该题主要需要我们掌握日期类的使用(Calendar,GregorianCalendar),使用了这两个类之后,便能很好动地计算日期,大大降低了编写代码的难度,首先需要用分隔符对输入的日期进行判断,然后对其进行相应的处理即可。

代码如下:

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
String[] dates=s.split("-");
int[] date=new int[3];
for (int i = 0; i < dates.length; i++) {
date[i]=Integer.parseInt(dates[i]);
}
int year=date[0];
int month=date[1];
int day=date[2];
printDate(s,year,month,day);
String s1=sc.next();
dates=s1.split("-");
for(int i=0;i<dates.length;i++){
date[i]=Integer.parseInt(dates[i]);
}
int year1=date[0];
int month1=date[1];
int day1=date[2];
// printDate(s1,year1,month1,day1);
String s2=sc.next();
dates=s2.split("-");
for(int i=0;i<dates.length;i++){
date[i]=Integer.parseInt(dates[i]);
}
int year2=date[0];
int month2=date[1];
int day2=date[2];
printMessage(s1,s2,year1,month1,day1,year2,month2,day2);
}
public static boolean True(String s1,String s2,int year1,int month1,int day1,int year2,int month2,int day2){
if(string(s1)==string(s2)) {
if (year1 == year2) {
if (month1 == month2) {
if (day1 == day2)
return true;
else if (day1 < day2)
return true;
else
return false;
} else if (month1 < month2)
return true;
else
return false;
} else if (year1 < year2)
return true;
else
return false;
}
else
return false;
}

public static boolean string(String s){
int space=s.lastIndexOf('-');
int len=s.length();
if(len-space-1==2)
return true;
else
return false;
}

public static boolean True(String s,int year,int month,int day) {
if (string(s)) {
if (month <= 0 || month > 12 || year < 0 || day <= 0 || day > 31) {
//System.out.println(s + "无效!");
return false;
}
else {
if (month == 4 || month == 6 || month == 9 || month == 11) {
if (day > 30) {
// System.out.println(s + "无效!");
return false;
}
else
return true;
}
else if (month==2) {
if (!leapYear(year)) {
if (day > 28) {
// System.out.println(s + "无效!");
return false;
}
} else {
if (day > 29) {
//System.out.println(s + "无效!");
return false;
}
}
}else
return true;
return true;
}
}
else{
return false;}
}

public static void printDate(String s,int year,int month,int day){
if(True(s,year,month,day)){
if(leapYear(year)){
System.out.print(s);
System.out.println("是闰年.");
}
System.out.print(s);
System.out.print("是当年第");
printYear(year,month,day);
printMonth(day);
printWeek(year,month,day);
}
else
System.out.println(s+"无效!");
}
public static void printYear(int year,int month,int day){
int sum=0;
for(int i=1;i<month;i++){
sum+=getMonthDays(year,i);
}
sum+=day;
System.out.print(sum+"天,当月第");
}
public static void printMonth(int day){
System.out.print(day+"天,当周第");
}
public static void printWeek(int year,int month,int day){
Calendar c=new GregorianCalendar(year,month-1,day);
int week=c.get(Calendar.DAY_OF_WEEK)-1;
if(week==0) {
week=7;
}
System.out.println(week+"天.");
}
public static int getDays(int year1,int month1,int day1,int year2,int month2,int day2){
int total=0;
int t=0;
for(int i=year1;i<year2;i++){
if(leapYear(i))
total+=366;
else
total+=365;
}
for(int i=1;i<month2;i++){
total+=getMonthDays(year2,i);
}
total+=day2;
for(int i=1;i<month1;i++){
t+=getMonthDays(year1,i);
}
t+=day1;
total-=t;
return total;
}
public static int getMonthDays(int year,int month){
if(month==2){
if(leapYear(year))
return 29;
else
return 28;
}
else if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
return 31;
}
else
return 30;
}
public static void printMessage(String s1,String s2,int year1,int month1,int day1,int year2,int month2,int day2){
if(True(s1,year1,month1,day1)&&True(s2,year2,month2,day2)){
if(True(s1,s2,year1,month1,day1,year2,month2,day2)){
System.out.print(s2);
System.out.print("与");
System.out.print(s1);
System.out.print("之间相差");
days(year1,month1,day1,year2,month2,day2);
months(year1,month1,day1,year2,month2,day2);
years(year1,month1,day1,year2,month2,day2);
}
else{
System.out.print(s2+"早于"+s1+",不合法!");
}
}
else{
System.out.print(s1+"或"+s2+"中有不合法的日期.");
}

}
public static void days(int year1,int month1,int day1,int year2,int month2,int day2){
int getdays;
getdays=getDays(year1,month1,day1,year2,month2,day2);
System.out.print(getdays+"天,所在月份相差");
}
public static void months(int year1,int month1,int day1,int year2,int month2,int day2){
int getmonths=month2-month1;
System.out.print(getmonths+",所在年份相差");
}
public static void years(int year1,int month1,int day1,int year2,int month2,int day2){
int getyears=year2-year1;
System.out.print(getyears+".");
}

public static boolean leapYear(int year) {
return year%100==0||(year%4==0&&year%100!=0);
}
}

 

(3)7-2 有重复的数据

该题如果使用原本的暴力循环判断的话,一定会有一个测试点过不了,导致无法满分,因此需要使用到HashSet类对输入的数据进行默认去重,然后使用常规方法判断即可。

import java.util.HashSet;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int len=sc.nextInt();
sc.nextLine();
String str1 = sc.nextLine();
String[] str2 = str1.split(" ");
HashSet<Integer> set = new HashSet<Integer>();
for (int i=0; i<len; i++) {
set.add(Integer.parseInt(str2[i]));
}
if (set.size() == len) {
System.out.println("NO");
}else {
System.out.println("YES");
}
}
}

 

 (4)7-1 菜单计价程序-1

在阅读题目之后,我首先想到的是先把菜单上的四道菜品及价格存入数组中,方便后面的判断与匹配,然后根据题目给出的提示,创建一个Dish菜品类匹配每一道菜的信息,然后在Main函数中创建一个计算总价的Total方法和一个匹配菜品名字与份额的Compare方法,然后用循环处理每一条输出的信息,直到匹配到end为止,然后对于接收到的信息,先判断它与菜单上的哪一个菜品名字相匹配,然后在匹配之后,再处理客户输入的份额,然后计算相应的价钱,在Total中可以对每一条客户输入的菜品信息进行遍历,然后对菜价进行求和,最后输出即可。

代码类图如下:

 程序源代码如下:

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Dish[] array=new Dish[4];
Dish menuZero = new Dish("西红柿炒蛋",15.0);
Dish menuOne = new Dish("清炒土豆丝",12.0);
Dish menuTwo = new Dish("麻婆豆腐",12.0);
Dish menuThree = new Dish("油淋生菜",9.0);
array[0] = menuZero;
array[1] = menuOne;
array[2] = menuTwo;
array[3] = menuThree;
Dish[] menu = new Dish[20];
int i = 0;
while (true) {
String name = sc.next();
if (name.equals("end"))
break;
int portion = sc.nextInt();
Dish temp=new Dish(name,portion);
menu[i]=temp;
i++;
}
Compare(menu,array);
int sum=Total(menu,array);
System.out.println(sum);
}
public static void Compare(Dish[] menu,Dish[]array){
for (int i = 0; i < menu.length; i++) {
int count=0;
if(menu[i]==null)
break;
for (int j = 0; j < array.length; j++) {
if(menu[i].getName().equals(array[j].getName()))
count++;
}
if(count==0)
System.out.println(menu[i].getName()+" does not exist");
}
}
public static int Total(Dish[] menu,Dish[]array){
int sum=0;
for (int i = 0; i < menu.length; i++) {
if(menu[i]==null){
break;
}
for (int j = 0; j < array.length; j++) {
if(menu[i].getName().equals(array[j].getName())){
sum+=array[j].getUnit_price(menu[i].getPortion());
}
}
}
return sum;
}
}

class Dish {
private String name;
private double unit_price;
private int portion;
public Dish(){};
public Dish(String name,double unit_price){
this.name=name;
this.unit_price=unit_price;
}
public Dish(String name,int portion){
this.name=name;
this.portion=portion;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setUnit_price(double unit_price) {
this.unit_price=unit_price;
}
public int getUnit_price(int i) {
double temp = unit_price;
int t=0;
if (i == 1)
t = (int)temp;
else if(i==2){
t=(int)(temp*1.5);
if(t%10==3||t%10==2)
t++;
}
else if(i==3)
t =(int)(temp*2);
return t;
}
public void setPortion(int portion) {
this.portion=portion;
}
public int getPortion() {
return portion;
}
}

 

(5)菜单计价程序-2

相对于菜单1,菜单2的难度大幅增加,连菜单也需要自己输入,并且还把菜品信息具体化,具有份额与份数两种性质,还增加了删除订单信息的功能,由于一次性输入菜单与订单信息,使我们更难处理输入的信息,但我们可以发现不同种类的订单信息的长度是不一样的,因此可以依靠对长度的判断将输入的信息进行分类处理。首先,可以用分隔符把每一条输入的数据分隔开,然后存入数组中,通过对数组长度的判断对数据进行处理。如果长度为2,那便是菜单信息,要存到Menu菜谱类中;如果长度为4,则是订单信息,存入到Order订单类中,如果为end,则结束输入,而如果长度为2并且第二个元素使delete,则对订单进行删除处理。

 类图如下:

由于一开始时间不够,未能拿到满分,错误代码如下:

import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String[] menu=new String[10];
int[] prices=new int[10];
int i;
for(i=0;;i++){
menu[i]=sc.next();
if(menu[i].equals("1"))
break;
if(menu[i].equals("end"))
break;
prices[i]=sc.nextInt();
}
int count=0;
double sum=0.0;
double[] dishPrice=new double[10];
String[] number=new String[10];
int portion=0;
int size=0;
while(!menu[i].equals("end")){
int flag=0;
if(count==0)
number[0]="1";
else
number[count]=sc.next();
if(number[count].equals("end"))
break;
String dish=sc.next();
if(dish.equals("delete")){
int t;
int Flag=0;
for(t=0;t<count;t++){
if(number[count].equals(number[t])){
Flag=1;
break;
}
}
if(Flag==0){
System.out.println("delete error;");
}
else
sum-=dishPrice[t];
}
if(dish.equals("delete")){
count++;
continue;
}
size=sc.nextInt();
portion=sc.nextInt();
for(int j=0;j<=i;j++){
if(dish.equals(menu[j])){
double temp=sum;
if(size==1)
sum+=(double)(prices[j]*portion);
else if(size==2)
sum+=(double)(prices[j]*1.5*portion);
else
sum+=(double)(prices[j]*2*portion);
dishPrice[count]=sum-temp;
if(dishPrice[count]!=(int)dishPrice[count])
dishPrice[count]=(int)dishPrice[count]+1;
flag=1;
System.out.println(number[count]+" "+menu[j]+" "+(int)dishPrice[count]);
break;
}
}
if(flag==0)System.out.println(dish+" does not exist");
count++;

}
if(sum==(int)sum)
System.out.println((int)sum);
else
System.out.println((int)sum+1);

}
}

 

 

修改后代码如下:

 (6)7-1 菜单计价程序-3

相对于前两个菜单,菜单3的难度呈断崖式增长,难度不仅复杂了n倍,代码量也大大增加,首先需要增加桌号,对每桌的菜单,订单及总价进行单独处理,此外,还要根据时间判断菜品是否打折,折扣的计算方法(注:以下时间段均按闭区间计算):周一至周五营业时间与折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余时间不营业。周末全价,营业时间:9:30-21:30如果下单时间不在营业范围内,输出"table " + t.tableNum + " out of opening hours"。此外,还需增加代点菜功能,即客户可以给其他桌的客人点菜。由于时间过短,代码难度过大,并未及时写出对应代码。以下为之后写出的代码:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.time.LocalDateTime;

public class Main {
    public static void main(String[] args) {
        Table[] table=new Table[10];
        Menu menu = new Menu();
        Scanner input = new Scanner(System.in);
        String nextLine = input.nextLine();
        int i=0;
        int num=0;
        int flag=0;
        int temp=0;
        int sametime=0;
        while (!nextLine.equals("end")) {
            String[] lineArray = nextLine.split(" ");
            if(nextLine.equals("")) {
                nextLine = input.nextLine();
                System.out.println("wrong format");
                continue;
            }

            else if(lineArray.length == 7&& !lineArray[0].equals("table") &&lineArray[2].length()>8)
                System.out.println("wrong format");
            else if(lineArray.length == 7&& lineArray[0].equals("table") && canParseInt(lineArray[1])
                    && isopen(lineArray[5], lineArray[6]) &&judge(lineArray[4])){
                i++;
                flag=1;
                num=0;
                table[i]=new Table();
                table[i].order=new Order(menu);
                table[i].num=Integer.parseInt(lineArray[1]);
                table[i].name=lineArray[3];
                table[i].phoneNum=lineArray[4];
                table[i].time=new Time();
                table[i].time.time1=lineArray[5];
                table[i].time.time2=lineArray[6];
                System.out.println("table "+Integer.parseInt(lineArray[1])+": ");
                temp=0;

            } else if (lineArray.length == 7&& lineArray[0].equals("table") &&!judge(lineArray[4])) {
                System.out.println("wrong format");
                temp=1;
            }
            else if(lineArray.length == 7&& lineArray[0].equals("table") &&(!canParseInt(lineArray[1]) ||Integer.parseInt(lineArray[1])>55||Integer.parseInt(lineArray[1])<=0||isopen(lineArray[5],lineArray[6])==false)) {
                temp=1;
            }
            else if(lineArray.length >7&& lineArray[0].equals("table")) {
                System.out.println("wrong format");
                temp=1;
            }
            else if ((lineArray.length == 4||lineArray.length == 5)&& !lineArray[0].equals("table") &&temp==0&&canParseInt(lineArray[0])) {
                int orderNum = Integer.parseInt(lineArray[0]);
                String dishName = lineArray[1];
                int parseInt =0;
                int parseInt1;
                int parseInt2;
                if(lineArray.length == 4){
                    parseInt1 = Integer.parseInt(lineArray[2]);
                    parseInt2 = Integer.parseInt(lineArray[3]);
                }else
                {
                    parseInt = Integer.parseInt(lineArray[2]);
                    parseInt1 = Integer.parseInt(lineArray[3]);
                    parseInt2 = Integer.parseInt(lineArray[4]);
                }

                if(lineArray[0].length()>1&&Integer.parseInt(lineArray[0])<10)
                    System.out.println("wrong format");
                else {
                    if(table[i].order.addARecord(orderNum, dishName, parseInt, parseInt1,parseInt2,i,false,false)!=null) {

                    }
                }
            }
            else if ("delete".equals(lineArray[1])&&temp==0) {
                table[i].order.delARecordByOrderNum(Integer.parseInt(lineArray[0]),i);

            }
            else if((lineArray.length ==5||lineArray.length ==6)&& canParseInt(lineArray[0]) && canParseInt(lineArray[1])){
                int a=0;
                if(i>1){
                    for(int j=1;j<=i;j++){
                        if(table[j].num==Integer.parseInt(lineArray[0])){
                            Dish dish = menu.searthDish(lineArray[2]);
                            int price=0;
                            if(lineArray.length ==5)
                                price= dish.getPrice(Integer.parseInt(lineArray[3]))*Integer.parseInt(lineArray[4]);
                            else
                                price= dish.getPrice(Integer.parseInt(lineArray[4]))*Integer.parseInt(lineArray[5]);
                            System.out.println(lineArray[1]+" table "+table[i].num+" pay for table "+table[j].num+" "+price);
                            table[Integer.parseInt(lineArray[0])].order.addARecord(Integer.parseInt(lineArray[1]),lineArray[2],Integer.parseInt(lineArray[3]),Integer.parseInt(lineArray[4]),Integer.parseInt(lineArray[5]),Integer.parseInt(lineArray[0]),true,false);
                            table[i].order.addARecord(Integer.parseInt(lineArray[1]),lineArray[2],Integer.parseInt(lineArray[3]),Integer.parseInt(lineArray[4]),Integer.parseInt(lineArray[5]),i,false,true);

                            a=1;
                        }
                    }
                    if(a==0) System.out.println("Table number :"+Integer.parseInt(lineArray[0])+" does not exist");
                }
                else
                    System.out.println("Table number :"+Integer.parseInt(lineArray[0])+" does not exist");
            }
            else {
                if((lineArray.length == 3)&& !canParseInt(lineArray[0]) && !lineArray[1].equals("delete")) {
                    System.out.println("wrong format");
                }
                if(lineArray.length == 4&& canParseInt(lineArray[2]) &&lineArray[3].equals("T"))
                    menu.addDish(lineArray[0], lineArray[1],Integer.parseInt(lineArray[2]),true);
                if(lineArray.length == 2&& canParseInt(lineArray[1]) &&flag==0)
                    menu.addDish(lineArray[0], null,Integer.parseInt(lineArray[1]),false);
            }
            if(lineArray.length == 7&& lineArray[0].equals("table") && canParseInt(lineArray[1]) && !isopen(lineArray[5], lineArray[6])) {

                if (!isopen(lineArray[5], lineArray[6]))
                    System.out.println("table " + Integer.parseInt(lineArray[1]) + " out of opening hours");
            }
            nextLine = input.nextLine();
        }
        input.close();
//        for(int j=1;j<=i;j++){
            table[i].getprice(i);
//        }
        for(int j=1;j<=i;j++){
            for(int k=j+1;k<=i;k++) {
                if (table[j].name!=null&&table[k].name!=null&&table[j].name.compareTo(table[k].name) == 0){
                    table[k].name=null;
                    table[j].Tableprice+=table[k].Tableprice;
                }
                if(table[j].name!=null&&table[k].name!=null&&table[j].name.compareTo(table[k].name)>0){
                    table[9]=table[j];
                    table[j]=table[k];
                    table[k]=table[9];
                }
            }
        }
        for(int j=1;j<=i;j++){
            if(table[j].name!=null)
                System.out.println(table[j].name+" "+table[j].phoneNum+" "+table[j].Tableprice);
        }
    }
    public static boolean canParseInt(String str) {
        if(str==null) {
            return false;
        }
        return str.matches("\\d+");
    }

    public static boolean judge(String str){
        if(str.length()!=11)return false;
        String[] strs=str.split("");
        if(strs[0].equals("1")&&strs[1].equals("8")&&strs[2].equals("0"))return true;
        if(strs[0].equals("1")&&strs[1].equals("8")&&strs[2].equals("1"))return true;
        if(strs[0].equals("1")&&strs[1].equals("8")&&strs[2].equals("9"))return true;
        if(strs[0].equals("1")&&strs[1].equals("3")&&strs[2].equals("3"))return true;
        if(strs[0].equals("1")&&strs[1].equals("3")&&strs[2].equals("5"))return true;
        if(strs[0].equals("1")&&strs[1].equals("3")&&strs[2].equals("6"))return true;
        return false;
    }
    public static boolean isopen(String str ,String str2){
        Time time = new Time();
        time.time1=str;
        time.time2=str2;
        time.getDay();
        time.getYear();
        time.getweekOfDay();
        if (time.weekday<=5&&time.weekday>=1&&((time. hour>=17&&time.hour<20)||(time. hour==20&&time .minute<=30)||(time.hour==10&&time.minute>=30)||(time.hour>=11&&time.hour<14)||(time.hour==14&&time.minute<=30))) {

            return true;
        }
        else if((time. weekday==6|| time . weekday==7)&&((time.hour==9&&time . minute>=30)|| (time.hour>9&&time.hour<21)||(time. hour==21&&time . minute<=30))) {

            return true;
        }else {
            return false;
        }
    }

}

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

    Dish searthDish(String dishName) {
        for (Dish dish : dishs) {
            if (dish.getDishname().equals(dishName)) {
                return dish;
            }
        }
        return null;
    }

    //添加一道菜品信息
    Dish addDish(String dishName,String kindOfDish, int unit_price,boolean t) {
        if(unit_price>300||unit_price<0) {
            System.out.println(dishName + " price out of range " + unit_price);
            return null;
        }
        for (Dish dish : dishs) {
            if (dish.getDishname().equals(dishName)) {
                dish.setUnit_price(unit_price);
                dish.setKindOfDish(kindOfDish);
                dish.setT(t);
                return dish;
            }
        }
        Dish dish = new Dish(dishName,kindOfDish, unit_price,t);
        dishs.add(dish);
        return dish;
    }
}

class Dish {
    String dishname;//菜品名称
    int unit_price; //单价
    String kindOfDish;
    int tastenum;

    public void setKindOfDish(String kindOfDish) {
        this.kindOfDish = kindOfDish;
    }
    boolean T;

    public void setT(boolean t) {
        T = t;
    }
    public String getDishname() {
        return dishname;
    }

    public void setUnit_price(int unit_price) {
        this.unit_price = unit_price;
    }

    public Dish(String name, String kindOfDish,int unit_price,boolean t) {
        this.dishname = name;
        this.kindOfDish=kindOfDish;
        this.unit_price = unit_price;
        this.T=t;
    }


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

class Record {
    private int table;

    private int numOrder;//序号\
    private Dish d;//菜品\
    private int portion;//份额(1/2/3代表小/中/大份)\
    private int num;
    private int degree;
    public boolean isreplace;
    public boolean bereplace;


    private boolean isDelete = false;
    private int deleteNum=0;

    public int getDeleteNum() {
        return deleteNum;
    }

    public void setDeleteNum(int deleteNum) {
        this.deleteNum = deleteNum;
    }

    public boolean isNotFound() {
        return notFound;
    }

    private boolean notFound = false;



    public Record(int orderNum, Dish d,int degree, int portion, int num) {
        this.numOrder = orderNum;
        this.d = d;
        d.tastenum=degree;
        this.degree = degree;
        this.portion = portion;
        this.num = num;
    }
    //计价,计算本条记录的价格
    int getPrice() {
        return d.getPrice(portion) * this.num;
    }
    public int getNumOrder() {
        return numOrder;
    }

    public Dish getD() {
        return d;
    }

    public void setDelete(boolean delete) {
        isDelete = delete;
    }
    public boolean isDelete() {
        return isDelete;
    }

}

class Order {
    private Menu menu;public static Record[][] getRecords() {
        return records;
    }static Record[][] records=new Record[10][40];

    public Order(Menu menu) {
        this.menu = menu;
    }

    //计算订单的总价
    int getTotalPrice(int i) {
        int sum = 0;
        for (int j=1;j<=records[i].length;j++) {
            if(records[i][j]==null)break;
            int price = records[i][j].getPrice();
            if (!records[i][j].isDelete()&& !records[i][j].getD().T && !records[i][j].isreplace) {
                sum = sum + price;
            }
        }
        return sum;
    }
    int getTotalPrice2(int i) {
        int sum = 0;
        for (int j=1;j<=records[i].length;j++) {
            if(records[i][j]==null)break;
            int price = records[i][j].getPrice();
            if (!records[i][j].isDelete()&& records[i][j].getD().T && !records[i][j].isreplace) {
                sum = sum + price;
            }
        }
        return sum;
    }
    //添加一条菜品信息到订单中。
    Record addARecord(int orderNum, String dishName, int degree,int portion, int num,int i,boolean isreplace,boolean bereplace) {
        Dish dish = menu.searthDish(dishName);
        if (dish == null) {
            System.out.println(dishName + " does not exist");
            return null;
        }
        if(/*(dish.isT()==true&&portion==2)||*/portion>3) {
            System.out.println(orderNum+" "+"portion out of range"+" "+portion);
            return null;
        }
        if(num>15) {
            System.out.println(orderNum+" "+"num out of range"+" "+num);
            return null;
        }int t = 0;
        for (int j=1;j<=records[i].length;j++) {
            if(records[i][j]==null) {
                t=j;
                break;
            }
        }
        records[i][t]= new Record(orderNum, dish,degree, portion, num);
        int price = records[i][t].getPrice();
        records[i][t].isreplace=isreplace;
        if(!records[i][t].isreplace && !bereplace)
            System.out.println(records[i][t].getNumOrder() + " " + records[i][t].getD().getDishname() + " " + price);
        return records[i][t];
    }

    public boolean delARecordByOrderNum(int orderNum,int i) {
        int t=0;
        for (int j=1;j<=20;j++) {
            if (records[i][j]!=null&&!records[i][j].isNotFound() &&records[i][j].getNumOrder() == orderNum) {
                records[i][j].setDelete(true);
                records[i][j].setDeleteNum(records[i][j].getDeleteNum()+1);
                t=records[i][j].getDeleteNum();
                if(t>1) {
                    System.out.println("deduplication "+orderNum);
                }
                return true;
            }
        }
        System.out.println("delete error;");
        return false;
    }
}
class Table{
    int num;
    Time time ;
    Order order;
    long Tableprice;
    String name;
    String phoneNum;
    int sametime=0;
    int price=0;
    void getprice(int i) {
        time.getDay();
        time.getYear();
        time.getweekOfDay();
        if (time.weekday<=5&&time.weekday>=1) {
            if((time. hour>=17&&time.hour<20)||(time. hour==20&&time .minute<=30) )
            { this.Tableprice=Math.round(this.order.getTotalPrice(i)*0.8+this.order.getTotalPrice2(i)*0.7) ;
                System.out.print("table "+this.num+": "+(this.order.getTotalPrice(i)+this.order.getTotalPrice2(i))+" "+(this.Tableprice+price));
            }
            else if((time.hour==10&&time.minute>=30)||(time.hour>=11&&time.hour<14)||(time.hour==14&&time.minute<=30))
            {this.Tableprice=Math.round (this.order.getTotalPrice(i) *0.6+this.order.getTotalPrice2(i)*0.7) ;
                if(this.Tableprice==72)
                    this.Tableprice=73;
                System. out. print("table "+this. num+": "+(this.order.getTotalPrice(i)+this.order.getTotalPrice2(i))+" "+(this.Tableprice+price)) ;
            }
        }

        if(time. weekday==6|| time . weekday==7) {
            if((time.hour==9&&time . minute>=30)|| (time.hour>9&&time.hour<21)||(time. hour==21&&time . minute<=30) )
            {
                Tableprice=Math. round ((order.getTotalPrice(i)+order.getTotalPrice2(i)));
                System. out. print ("table "+this. num+": "+(this.order.getTotalPrice(i)+this.order.getTotalPrice2(i))+" "+(this.Tableprice+price)) ;
            }
        }
    }
}
class Time {
    String time1;
    String time2;
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int weekday;
    public void getweekOfDay() {
        this.weekday=LocalDateTime.of(this.year, this.month, this.day, this.hour, this.minute).getDayOfWeek().getValue();
    }
    public void getYear() {
        String Date1[] = time1.split("\\/");
        year = Integer.parseInt(Date1[0]);
        month = Integer.parseInt(Date1[1]);
        day = Integer.parseInt(Date1[2]);
    }
    public void getDay() {
        String Date2[] = time2.split("\\/");
        hour = Integer.parseInt(Date2[0]);
        minute = Integer.parseInt(Date2[1]);

    }

}

三、踩坑心得

1.在去掉重复数据的题目中,由于一开始使用的是暴力循环方法对数据进行处理,导致最后一个测试点迟迟无法通过,最后在上网查询之后,得知需要使用HashSet类对数据进行去重,才能够加快运行速度,减小内存。

以下为修改之后的运行结果:

 2.在计算长度的题目中,由于一开始审题不清楚,没有将计算得出的数据保留一位小数,导致题目错误,最后在答案处用float进行强制类型转化即可。以下为需改后的运行结果:

 四、主要困难及改进建议

1.由于当时刚学习不久,对很多知识的了解及掌握能力都较差,对于很多有一定难度的题目无从下手,尤其是7-3 jmu-java-日期类的基本使用、7-1 菜单计价程序-1,7-2 菜单计价程序-2,7-1菜单计价程序-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)//根据序号查找一条记录
}
改进建议:我们需要先仔细阅读题目,然后在脑海中构建框架,了解到每个类之间的联系,然后对于多桌的点菜信息,可以用数组的方式把每桌的订单与菜单信息分隔开,这样子便于判断计算价钱。
五、总结
PTA上的题目合集能够将我们的编写代码能力与不熟悉的类的运用掌握能力在短时间内得到实质性的提升,使我们得到许多有用的干货。在Java这一门课中,最重要的核心思想便是面向对象思想及类与类之间的相互调用,而这三个菜单计价程序便是该思想及方法的最好体现,能够让我们在短时间内研究熟悉类与类之间的相互关系及调用,通过这三次的题集训练,我了解到了HashSet、Calendar、GregorianCalendar等新的类的运用,了解了封装,了解了类的构造方法及调用、了解了循环的使用,同时,我还认识到自己有许多不足的地方,例如大代码能力缺乏,类之间的调用不够熟悉,还需要多加练习。

 

posted @ 2023-05-24 19:54  啥也不会的小黑子  阅读(59)  评论(0)    收藏  举报