首页 > 其他分享 >题目集4-6总结

题目集4-6总结

时间:2023-04-29 20:22:25浏览次数:40  
标签:总结 输出 题目 String int 31 table public

一、前言

本阶段的题目集最开始还是和前面的题目集有些相似的内容,但相较于前阶段的题目集主要是改进了代码所用到的知识点使代码更加简洁与所用内存更加小。这次题目集的难度还可以,并且是逐渐加大难度的,特别是题目集六一道题一百分。这次的题目集主要考察了正则表达式的应用,类与类之间的关系(聚合,关联,依赖等),以及程序设计间的逻辑。

二、设计与分析

1、题目集04的7-2

在一大堆数据中找出重复的是一件经常要做的事情。现在,我们要处理许多整数,在这些整数中,可能存在重复的数据。

你要写一个程序来做这件事情,读入数据,检查是否有重复的数据。如果有,输出“YES”这三个字母;如果没有,则输出“NO”。

输入格式:

你的程序首先会读到一个正整数n,n∈[1,100000],然后是n个整数。

输出格式:

如果这些整数中存在重复的,就输出:

YES

否则,就输出:

NO

输入样例:

 5

1 2 3 1 4

输出样例:

YES

下面是我的代码

import java.util.ArrayList; // 引入 ArrayList 类
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner put=new Scanner(System.in);
        ArrayList<Integer> a = new ArrayList<Integer>();
        int n = put.nextInt();
        int i = 0,j = n-1,k = 0;
        if(n==1){
            System.out.print("NO");
        }
        else {
        if(n>=1&&n<=100000){
        for(i=0;i<n;i++){
            a.add(put.nextInt());
        }
            i=0;
        while(i<n){
                if(a.get(i)==a.get(j)){
                System.out.print("YES");
                break;}
            else i = i+1;j = j-1;
        }
        if(i==n)
            System.out.print("NO");}
        else System.out.print("N");}
        
    }}    

 这个和上一阶段是一样的题,不过上一阶段的题我采用了数组的方法来做,而这道题的测试点更加严格一些,内存要更小,我用了arraylist来做。下面来看一下它的测试点

当时做题的时候被很慢,很慢搞得很烦,不过幸好是做出来了。arraylist可以不用定义数组长度(和动态数组很像),要注意的是定义一个arraylist数组的时候数组类型的写法,例如定义一个整形数组要写成 ArrayList<Integer> a = new ArrayList<Integer>();而不能写成int。向数组内元素赋值直接用.add()就可以。这道题还是比较简单的。

2、题目集04的7-5

Student类具体要求如下:
私有成员变量:学号(sid,String类型),姓名(name,String类型),年龄(age,int类型),专业(major,String类型) 。
提供无参构造和有参构造方法。(注意:有参构造方法中需要对年龄大小进行判定)
普通成员方法:print(),输出格式为“学号:6020203100,姓名:王宝强,年龄:21,专业:计算机科学与技术”。
普通成员方法:提供setXxx和getXxx方法。(注意:setAge()方法中需要对年龄进行判定)
注意:
年龄age不大于0,则不进行赋值。
print()中的“:”和“,”为均为中文冒号和逗号。

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //调用无参构造方法,并通过setter方法进行设值
        String sid1 = sc.next();
        String name1 = sc.next();
        int age1 = sc.nextInt();
        String major1 = sc.next();
        Student student1 = new Student();
        student1.setSid(sid1);
        student1.setName(name1);
        student1.setAge(age1);
        student1.setMajor(major1);
        //调用有参构造方法
        String sid2 = sc.next();
        String name2 = sc.next();
        int age2 = sc.nextInt();
        String major2 = sc.next();
        Student student2 = new Student(sid2, name2, age2, major2);
        //对学生student1和学生student2进行输出
        student1.print();
        student2.print();
    }
}

/* 请在这里填写答案 */

输入格式:

输出格式:

学号:6020203110,姓名:王宝强,年龄:21,专业:计算机科学与技术
学号:6020203119,姓名:张三丰,年龄:23,专业:软件工程

输入样例:

在这里给出一组输入。例如:

6020203110 王宝强 21 计算机科学与技术
6020203119 张三丰 23 软件工程 

输出样例:

在这里给出相应的输出。例如:

学号:6020203110,姓名:王宝强,年龄:21,专业:计算机科学与技术
学号:6020203119,姓名:张三丰,年龄:23,专业:软件工程
我的代码如下
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //调用无参构造方法,并通过setter方法进行设值
        String sid1 = sc.next();
        String name1 = sc.next();
        int age1 = sc.nextInt();
        String major1 = sc.next();
        Student student1 = new Student();
        student1.setSid(sid1);
        student1.setName(name1);
        student1.setAge(age1);
        student1.setMajor(major1);
        //调用有参构造方法
        String sid2 = sc.next();
        String name2 = sc.next();
        int age2 = sc.nextInt();
        String major2 = sc.next();
        Student student2 = new Student(sid2, name2, age2, major2);
        //对学生student1和学生student2进行输出
        student1.print();
        student2.print();
    }
}

/* 请在这里填写答案 */
class Student{
    private String sid;
    private String name;
    private int age;
    private String major;
    public Student(String sid,String name,int age,String major){
        this.sid = sid;
        this.name = name;
        this.age = age;
        this.major = major;
    }
    public Student(){
        this.sid = sid;
        this.name = name;
        this.age = age;
        this.major = major;
    }
    public String getSid(){
        return sid;
    }
    public String getName(){
        return name;
    }
    public int getAge(){
        return age;
    }
    public String getMajor(){
        return major;
    }
    public String setSid(String sid){
        this.sid = sid;
        return sid;
    }
    public String setName(String name){
        this.name = name;
        return name;
    }
    public int setAge(int age){
        this.age = age;
        if(age>0)
        return age;
        
    }
    public String setMajor(String major){
        this.major = major;
        return major;
    }
    public void print(){
        System.out.print("学号:"+sid+","+"姓名"+name+","+"年龄:"+age+","+"专业:"+major);
    }
}

这道题还是很简单的,定义了两个student实例,在student类中的方法是常规的getter和setter。

3、题目集04的7-6

从键盘输入两个日期,格式如:2022-06-18。判断两个日期的先后,并输出它们之间间隔的天数、周数(不足一周按0计算)。

预备知识:通过查询Java API文档,了解Scanner类中nextLine()等方法、String类中split()等方法、Integer类中parseInt()等方法的用法,了解LocalDate类中of()、isAfter()、isBefore()、until()等方法的使用规则,了解ChronoUnit类中DAYS、WEEKS、MONTHS等单位的用法。

输入格式:

输入两行,每行输入一个日期,日期格式如:2022-06-18

输出格式:

第一行输出:第一个日期比第二个日期更早(晚)
第二行输出:两个日期间隔XX天
第三行输出:两个日期间隔XX周

输入样例1:

2000-02-18
2000-03-15

输出样例1:

第一个日期比第二个日期更早
两个日期间隔26天
两个日期间隔3周 

输入样例2:

2022-6-18
2022-6-1

输出样例2:

第一个日期比第二个日期更晚
两个日期间隔17天
两个日期间隔2周
我的代码如下
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
    Scanner put=new Scanner(System.in);
    String a = put.nextLine();
    String b = put.nextLine();
    
    String[] start = a.split("-");
    String[] end = b.split("-");
    
    int startyear = Integer.parseInt(start[0]);
    int startmonth = Integer.parseInt(start[1]);
    int startday = Integer.parseInt(start[2]);
    int endyear = Integer.parseInt(end[0]);
    int endmonth = Integer.parseInt(end[1]);
    int endday = Integer.parseInt(end[2]);
    LocalDate o= LocalDate.of(startyear,startmonth,startday);
    LocalDate t= LocalDate.of(endyear,endmonth,endday);
    
    if(o.isAfter(t))
        System.out.println("第一个日期比第二个日期更晚");
    else
    System.out.println("第一个日期比第二个日期更早");
    
    long c = o.until(t,ChronoUnit.DAYS);//.between(one,two);
    System.out.println("两个日期间隔"+Math.abs(c)+"天");
    System.out.print("两个日期间隔" + Math.abs(ChronoUnit.WEEKS.between(o,t))+"周");
}}

这道题和前面的题目集有些相呼应的效果,之前的日期题是要我们自己写求下几天前几天的逻辑,不允许使用Java里的类,但这道题是允许使用的,也让我更加了解了Java里类的功能的强大。

下面是我的类图

 

4、题目集5的7-2

对输入的字符串中的字符进行排序并输出。

输入格式:

在一行内输入一个字符串。

输出格式:

对该字符串内的字符进行排序后(按ASCII码进行升序排序)输出。

输入样例:

在这里给出一组输入。例如:

h!dy%2dh1
 

输出样例:

在这里给出相应的输出。例如:

!%12ddhhy
下面是我的代码
 
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner put = new Scanner(System.in);
        String a = put.next();
        int i = 0,j = 0;
        char t;
        char[] n = new char[a.length()];
        for(i = 0;i<a.length();i++) {
            n[i] = a.charAt(i);
        }
        for(i = 0;i<a.length()-1;i++) {
            for(j = i+1;j<a.length();j++) {
                if(n[i]>n[j]) {
                    t = n[i];
                    n[i] = n[j];
                    n[j] = t;
                }
            }
        }
        
        System.out.print(n);
    }
 
}

这是题目集五这中的内容,题目集五一共六道题其中四道都是关于正则表达使得考察,正则 表达式对于字符串的判断是非常有帮助的。例如a.matches("^[1-9][0-9]{4,14}$")是判断字符串a的第一个字符是否是y1到9,后面的字符是否是0到9,是否循环了4到十四次,其中^表示字符串的开端&表示字符串的结尾。

下面是我们的重头戏

5、题目集5的7-5

应用程序共测试三个功能:

  1. 求下n天
  2. 求前n天
  3. 求两个日期相差的天数

注意:严禁使用Java中提供的任何与日期相关的类与方法,并提交完整源码,包括主类及方法(已提供,不需修改)

输入格式:

有三种输入方式(以输入的第一个数字划分[1,3]):

  • 1 year month day n //测试输入日期的下n天
  • 2 year month day n //测试输入日期的前n天
  • 3 year1 month1 day1 year2 month2 day2 //测试两个日期之间相差的天数

输出格式:

  • 当输入有误时,输出格式如下:
    Wrong Format
  • 当第一个数字为1且输入均有效,输出格式如下:
    year-month-day
     
  • 当第一个数字为2且输入均有效,输出格式如下:
    year-month-day
     
  • 当第一个数字为3且输入均有效,输出格式如下:
    天数值
     

输入样例1:

在这里给出一组输入。例如:

3 2014 2 14 2020 6 14

输出样例1:

在这里给出相应的输出。例如:

2312

输入样例2:

在这里给出一组输入。例如:

2 1935 2 17 125340

输出样例2:

在这里给出相应的输出。例如:

1591-12-17

输入样例3:

在这里给出一组输入。例如:

1 1999 3 28 6543

输出样例3:

在这里给出相应的输出。例如:

2017-2-24

输入样例4:

在这里给出一组输入。例如:

0 2000 5 12 30

输出样例4:

在这里给出相应的输出。例如:

Wrong Format
下面是我的代码
import java.util.Scanner;

class DateUtil{
    Day day;
    
    public DateUtil(){}
    
    public DateUtil(int d,int m,int y){
        this.day=new Day(d,m,y);
    }
    
    public Day getDay(){
        return day;
    }
   
    public void setDay(Day d){
        this.day=d;
    }

    public boolean checkInputValidity(){
        if(this.getDay().getMonth().getYear().validate()&&this.getDay().getMonth().validate()&&day.validate())//年 月 日 合法检验
            return true;
        else
            return false;
    }
    
    public boolean compareDates(DateUtil date) {//比较date是否大于this
        if(date.getDay().getMonth().getYear().getValue()<this.getDay().getMonth().getYear().getValue())
            return false;
        else if(date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue()&&date.getDay().getMonth().getValue()<this.getDay().getMonth().getValue())
            return false;
        else if(date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue()&&date.getDay().getMonth().getValue()==this.getDay().getMonth().getValue()&&date.getDay().getValue()<this.getDay().getValue())
            //年 月 日逐级比较
            return false;
        else
            return true;
    }
    
    public boolean equalTwoDates(DateUtil date){//比较两个日期是否相等
        if(this.getDay().getValue()==date.getDay().getValue()&&this.getDay().getMonth().getValue()==date.getDay().getMonth().getValue()&& this.getDay().getMonth().getYear().getValue()==date.getDay().getMonth().getYear().getValue())
            return true;
        else
            return false;
    }
    
    public String showDate(){
        return this.getDay().getMonth().getYear().getValue()+"-"+this.getDay().getMonth().getValue()+"-"+this.getDay().getValue();
    }
    
    public int s(DateUtil d){//计算该年的剩余天数
        int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
        int f=0,i;
        if(d.getDay().getMonth().getYear().isLeapYear()){
            a[2] = 29;
        }
        else {
            a[2] = 28;
        }
        f=f+a[d.getDay().getMonth().getValue()]-d.getDay().getValue();
        for(i=d.getDay().getMonth().getValue()+1;i<=12;i++){
            f=f+a[i];
        }
        return f;
    }

    public DateUtil getNextNDays(int n){//求下n天
        int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
        int y=0,m=0,d=0;
        /*int f=0,i,j;
        if(d.getDay().getMonth().getYear().isLeapYear()){
            a[2] = 29;
        }
        else {
            a[2] = 28;
        }
        f=f+a[d.getDay().getMonth().getValue()]-d.getDay().getValue();
        for(i=d.getDay().getMonth().getValue()+1;i<=12;i++){
            f=f+a[i];
        }*/
        int i,j,e;
        int b=s(this);//当前类剩余天数
        if(b>n){//该年剩余天数大于n
            //y=this.getDay().getMonth().getYear().getValue();
            if(this.getDay().getMonth().getYear().isLeapYear()){//如果是闰年
                a[2]=29;
            }
            e=a[this.getDay().getMonth().getValue()]-this.getDay().getValue();//本月剩余的天数
            //e=e-this.getDay().getValue();//本月剩余的天数
            if(e>=n){//如果n天后在本月
                m=this.getDay().getMonth().getValue();
                d=n+this.getDay().getValue();
            }
            else{//如果n天后不在本月
                n=n-e;
                m=this.getDay().getMonth().getValue()+1;
                while(n-a[m]>0&&m<=12){
                    n=n-a[m];
                    m++;
                }
                d=n;
            }
        }
        else{//剩余天数小于n
            n=n-b;//减去剩余天数
            y=this.getDay().getMonth().getYear().getValue()+1;//年加一
            int c=365;//平年天数
            if(new Year(y).isLeapYear()){//闰年天数加一
                c++;
            }
            while(n-c>0){
                n=n-c;
                y++;
                c=365;
                if(new Year(y).isLeapYear())
                    c++;
            }
            m=1;
            while(n-a[m]>0&&m<=12){
                n=n-a[m];
                m++;
            }
            d=n;
        }
        return new DateUtil(y,m,d);
    }
   
    public DateUtil getPreviousNDays(int n){ //求前n天
        int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
        int y=0,m=0,d=0;
        int i,b;
        b=365-s(this);//该年过了多少天
        if(this.getDay().getMonth().getYear().isLeapYear()){//如果是闰年
           b++;
        }
        if (b>n){//如果前n天在该年
            if( this.getDay().getMonth().getYear().isLeapYear()){
                a[2]=29;
            }
            int e=this.getDay().getValue();//本月已经过的天数
            if(e>n){//缩小范围看是否在本月
                m=this.getDay().getMonth().getValue();
                d=e-n;
            }
            else{//前n天不在本月
                n=n-e;
                m=this.getDay().getMonth().getValue()-1;
                while(n-a[m]>0&&m>=0){
                    n=n-a[m];
                    m--;
                }
                d=a[m]-n;
            }
        }
        else{//如果前n天不在该年
            n=n-b;
            y=this.getDay().getMonth().getYear().getValue()-1;
            int f=365;
            if(new Year(y).isLeapYear()){
                f++;
            }
            while(n-f>0){
                n=n-f;
                y--;
                f=365;
                if(new Year(y).isLeapYear())
                    f++;
            }
            m=12;//从后往前算
            while(n-a[m]>0&&m>=0){
                n=n-a[m];
                m--;
            }
            if(this.getDay().getMonth().getYear().isLeapYear()){//如果是闰年
                a[2]=29;
            }
            d=a[m]-n;
        }
        return new DateUtil(y,m,d);
    }
    
    public int getDaysofDates(DateUtil date){//求两个日期之间的天数
        DateUtil c = this;
        DateUtil d=date;
        int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
        int i,j,e=0;
        if(c.equalTwoDates(date)){//如果两天的日期相等
            return 0;
        }
        else if(c.compareDates(date)){//c比d大
            d = this;
            c=date;
        }
        for(i=c.getDay().getMonth().getYear().getValue()+1;i<d.getDay().getMonth().getYear().getValue();i++){//计算跨年情况,逐级递减
            e=e+365;
            if(new Year(i).isLeapYear())
                e++;
        }
        if(c.getDay().getMonth().getYear().getValue()==d.getDay().getMonth().getYear().getValue()&&this.getDay().getMonth().getValue()==d.getDay().getMonth().getValue()){//年份相同,月份相同,日不同
            e=d.getDay().getValue()-this.getDay().getValue();
        }
        else if(c.getDay().getMonth().getYear().getValue()==d.getDay().getMonth().getYear().getValue()&&this.getDay().getMonth().getValue()!=d.getDay().getMonth().getValue()){//年份相同,月份不同
            if(c.getDay().getMonth().getYear().isLeapYear())//是闰年
                a[2]=29;
            e=e+a[c.getDay().getMonth().getValue()]-c.getDay().getValue();
            e=e+d.getDay().getValue();
            for(j=c.getDay().getMonth().getValue()+1;j<=d.getDay().getMonth().getValue()-1;j++)
                e = e+a[j];
        }
        else if(c.getDay().getMonth().getYear().getValue()!=d.getDay().getMonth().getYear().getValue()){//年份不同
            e=e+a[c.getDay().getMonth().getValue()]-c.getDay().getValue();
            e=e+d.getDay().getValue();//先加上天数在算月数
            for(j=c.getDay().getMonth().getValue()+1;j<=12;j++)
                e=e+a[j];
            for(j=d.getDay().getMonth().getValue()-1;j>0;j--)//过了多少月
                e=e+a[j];
            if(c.getDay().getMonth().getYear().isLeapYear()&&c.getDay().getMonth().getValue()<=2)
                e++;
            if(d.getDay().getMonth().getYear().isLeapYear()&&d.getDay().getMonth().getValue()>2)
                e++;
        }
        return e;
    }}

class Day{
    int value;//相当于day的数值
    Month month;//month类作为数据域
    int [] mon_maxnum=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
    
    public Day(){}
    
    public Day(int yearValue,int monthValue,int dayValue){
        this.month=new Month(yearValue,monthValue);
        this.value=dayValue;
    }
    
    public int getValue(){
        return value;
    }
    
    public Month getMonth(){
        return month;
    }
   
    public void setValue(int value){
        this.value=value;
    }
    public void setMonth(Month value){
        this.month=value;
    }
    
    public void resetMin(){
        value=1;
    }
   
    public void resetMax(){
        value=mon_maxnum[month.getValue()];
    }
    
    public boolean validate(){
        if(this.getMonth().getYear().isLeapYear())
            mon_maxnum[2]=29;
        if(value>=1&&value<=mon_maxnum[month.getValue()])
            return true;
        else
            return false;
    }
    
    public void dayIncrement() {
        value=value+1;
    }
    
    public void dayReduction() {
        value=value-1;
    }
}

class Month{
    int value;//“值”相当于month
    Year year;
   
    public Month(){}
    
    public Month(int yearValue,int monthValue){
        this.year=new Year(yearValue);
        this.value=monthValue;
    }
    
    public int getValue(){
        return value;
    }
    
     public void setValue(int value){
        this.value=value;
    }
    
    public Year getYear(){
        return year;
    }
    
    public void setYear(Year year){
        this.year=year;
    }
   
    public void resetMin(){//月份复位
        value=1;
    }
    
    public void resetMax(){
        value=12;
    }
    
    public boolean validate(){
        if(value>=1&&value<=12)
            return true;
        else
            return false;
    }
    
    public void dayIncrement(){
        value=value+1;
    }
    
    public void dayReduction(){
        value=value-1;
    }
}

class Year{
    int value;
    
    public Year(){
    }
    
    public Year(int value){
        this.value=value;
    }
    
    public int getValue(){
        return value;
    }
    
    public void setValue(int value){
        this.value=value;
    }
    
    public  boolean isLeapYear(){
        if((value%4==0&&value%100!=0)||value%400==0)
            return true;
        else
            return false;
    }
  
    public boolean validate(){//判断日期是否合法年份
        if(value>=1900&&value<=2050)
            return true;
        else
            return false;
    }
    
    public void yearIncrement(){
        value=value+1;
    }
    
    public void yearReduction(){
        value=value-1;
    }
}


public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int year = 0;
        int month = 0;
        int day = 0;

        int choice = input.nextInt();

        if (choice == 1) { // test getNextNDays method
            int m = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            DateUtil date = new DateUtil(year, month, day);

            if (!date.checkInputValidity()) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            m = input.nextInt();

            if (m < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            //System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:");
            System.out.println(date.getNextNDays(m).showDate());
        } else if (choice == 2) { // test getPreviousNDays method
            int n = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            DateUtil date = new DateUtil(year, month, day);

            if (!date.checkInputValidity()) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            n = input.nextInt();

            if (n < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            //System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:");
            System.out.println(date.getPreviousNDays(n).showDate());
        } else if (choice == 3) {    //test getDaysofDates method
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            int anotherYear = Integer.parseInt(input.next());
            int anotherMonth = Integer.parseInt(input.next());
            int anotherDay = Integer.parseInt(input.next());

            DateUtil fromDate = new DateUtil(year, month, day);
            DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);

            if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
                System.out.print(//"The days between " + fromDate.showDate() + 
                       // " and " + toDate.showDate() + " are:"
                        //+
                    fromDate.getDaysofDates(toDate));
            } else {
                System.out.println("Wrong Format");
                System.exit(0);
            }
        }
        else{
            System.out.println("Wrong Format");
            System.exit(0);
        }        
    }
}

这道题我的通过了的,不过我的代码可以在判断语句上再进行一些简化,像是判断两个日期是否是同一天,判断两个日期之间的先后这两个部分的判断语句可以进行一些合并。

下面是这道题题目上所要求的的类图

 下面是日期类面向对象(聚合二)题目上的类图和我的代码

 

import java.util.Scanner;

class DateUtil{
Year year;
Month month;
Day day;
int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

public DateUtil(){}
public DateUtil(int d,int m,int y){
this.day = new Day(d);
this.month = new Month(m);
this.year = new Year(y);
}

public Year getYear(){
return year;
}
public void getYear(Year year){
this.year = year;
}

public Month getMonth(){
return month;
}
public void setMonth(Month month){
this.month = month;
}

public Day getDay(){
return day;
}
public void setDay(Day day){
this.day=day;
}

public void setDayMin(){
this.day.value = 1;
}

public void setDayMax(){
this.day.value = a[month.getValue()];
}

public boolean checkInputValidity(){
if(this.day.getValue()>=1&&this.day.getValue()<=a[month.getValue()]&&this.month.validate()&&this.year.validate())//年 月 日 合法检验
return true;
else
return false;
}

public boolean compareDates(DateUtil date) {//判断日期先后
if(date.getYear().getValue()<this.getYear().getValue())//先比年
return false;
else if(date.getYear().getValue()==this.getYear().getValue()&&date.getMonth().getValue()<this.getMonth().getValue())
return false;
else if(date.getYear().getValue()==this.getYear().getValue()&&date.getMonth().getValue()==this.getMonth().getValue()&&date.getDay().getValue()<this.getDay().getValue())
//年 月 日逐级比较
return false;
else
return true;
}

public boolean equalTwoDates(DateUtil date){//比较两个日期是否相等
if(this.getDay().getValue()==date.getDay().getValue()&&this.getMonth().getValue()==date.getMonth().getValue()&& this.getYear().getValue()==date.getYear().getValue())
return true;
else
return false;
}

public String showDate(){
return this.getYear().getValue()+"-"+this.getMonth().getValue()+"-"+this.getDay().getValue();
}

public int s(DateUtil d){//计算该年的剩余天数
int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int f=0,i;
if(d.getYear().isLeapYear()){
a[2] = 29;
}
else {
a[2] = 28;
}
f=f+a[d.getMonth().getValue()]-d.getDay().getValue();
for(i=d.getMonth().getValue()+1;i<=12;i++){
f=f+a[i];
}
return f;
}

public DateUtil getNextNDays(int n){//求下n天
int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int y=0,m=0,d=0;
/*int f=0,i,j;
if(d.getDay().getMonth().getYear().isLeapYear()){
a[2] = 29;
}
else {
a[2] = 28;
}
f=f+a[d.getDay().getMonth().getValue()]-d.getDay().getValue();
for(i=d.getDay().getMonth().getValue()+1;i<=12;i++){
f=f+a[i];
}*/
int i,j;
int b=s(this);//剩余天数
if(b>n){//该年剩余天数大于n
//y=this.getYear().getValue();
if(this.getYear().isLeapYear()){//如果是闰年
a[2]=29;
}
int e=a[this.getMonth().getValue()];//本月的天数
e=e-this.getDay().getValue();//本月剩余的天数
if(e>=n){//如果n天后在本月
m=this.getMonth().getValue();
d=n+this.getDay().getValue();
}
else{//如果n天后不在本月
n=n-e;
m=this.getMonth().getValue()+1;
//i=m;
while(n-a[m]>0&&m<=12){
n=n-a[m];
m++;
//i++;
}
d=n;
}
}
else{//剩余天数小于n
n=n-b;//减去剩余天数
y=this.getYear().getValue()+1;//年加一
int c=365;//平年天数
if(new Year(y).isLeapYear()){//闰年天数加一
c++;
}
while(n-c>0){
n=n-c;
y++;
c=365;
if(new Year(y).isLeapYear())
c++;
}
m=1;
while(n-a[m]>0&&m<=12){
n=n-a[m];
m++;
}
// m=i;
d=n;
}
return new DateUtil(y, m, d);
}

public DateUtil getPreviousNDays(int n){ //求前n天
int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int y=0,m=0,d=0;
int i,b;
b=365-s(this);//该年过了多少天
if(this.getYear().isLeapYear()){//如果是闰年
b++;
}
if (b>n){//如果前n天在该年
y=this.getYear().getValue();
if(new Year(y).isLeapYear()){
a[2]=29;
}
int e=this.getDay().getValue();//本月已经过的天数
if(e>n){//缩小范围看是否在本月
m=this.getMonth().getValue();
d=e-n;
}
else{//前n天不在本月
n=n-e;
m=this.getMonth().getValue()-1;
//i=m;
while(n-a[m]>0&&m>=0){
n=n-a[m];
m--;
//i--;
}
d=a[m]-n;
/*if(new Year(y).isLeapYear()&&m==2){
d++;
}*/
}
}
else{//如果前n天不在该年
n=n-b;
y=this.getYear().getValue()-1;
int f=365;
if(new Year(y).isLeapYear()){
f++;
}
while(n-f>0){
n=n-f;
y--;
f=365;
if(new Year(y).isLeapYear())
f++;
}
i=12;
while(n-a[i]>0&&i>=0){
n=n-a[i];
i--;
}
m=i;
d=a[i]-n;
if(new Year(f).isLeapYear()&&m==2){
d++;
}
}
return new DateUtil(y, m, d);
}

public int getDaysofDates(DateUtil date){//求两个日期之间的天数
DateUtil c=this;
DateUtil d=date;
int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int i,j,e=0;
if(this.equalTwoDates(date)){//如果两天的日期相等
return 0;
}
else if(!this.compareDates(date)){//c比d大
c=date;
d=this;
}
for(i=c.getYear().getValue()+1;i<d.getYear().getValue();i++){//计算跨年情况,逐级递减
e=e+365;
if(new Year(i).isLeapYear())
e++;
}
if(c.getYear().getValue()==d.getYear().getValue()&&c.getMonth().getValue()==d.getMonth().getValue()){
e=d.getDay().getValue()-c.getDay().getValue();
}
if(c.getYear().getValue()==d.getYear().getValue()&&c.getMonth().getValue()!=d.getMonth().getValue()){
if(c.getYear().isLeapYear())//是闰年
a[2]=29;
e=e+a[c.getMonth().getValue()]-c.getDay().getValue();
e=e+d.getDay().getValue();
for(j=c.getMonth().getValue()+1;j<=d.getMonth().getValue()-1;j++)
e = e+a[j];
}
else if(c.getYear().getValue()!=d.getYear().getValue()){//年份不同
e=e+a[c.getMonth().getValue()]-c.getDay().getValue();
e=e+d.getDay().getValue();//先加上天数在算月数
for(j=c.getMonth().getValue()+1;j<=12;j++)
e=e+a[j];
for(j=d.getMonth().getValue()-1;j>0;j--)//过了多少月
e=e+a[j];
if(c.getYear().isLeapYear()&&c.getMonth().getValue()<=2)
e++;
if(d.getYear().isLeapYear()&&d.getMonth().getValue()>2)
e++;
}
return e;
}}

class Day{
int value;//相当于day的数值
// int [] mon_maxnum=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};

public Day(){}
public Day(int Value){
this.value=Value;
}

public int getValue(){
return value;
}
public void setValue(int value){
this.value=value;
}

/* public void resetMin(){
value=1;
}

public void resetMax(){
value=mon_maxnum[month.getValue()];
}

public boolean validate(){
if(this.getMonth().getYear().isLeapYear())
mon_maxnum[2]=29;
if(value>=1&&value<=mon_maxnum[month.getValue()])
return true;
else
return false;
}*/

public void dayIncrement() {
value=value+1;
}
public void dayReduction() {
value=value-1;
}
}

class Month{
int value;//“值”相当于month

public Month(){}
public Month(int value){
this.value=value;
}

public int getValue(){
return value;
}
public void setValue(int value){
this.value=value;
}

public void resetMin(){//月份复位
value=1;
}
public void resetMax(){
value=12;
}

public boolean validate(){
if(value>=1&&value<=12)
return true;
else
return false;
}

public void dayIncrement(){
value=value+1;
}

public void dayReduction(){
value=value-1;
}
}

class Year{
int value;

public Year(){}
public Year(int value){
this.value=value;
}

public int getValue(){
return value;
}
public void setValue(int value){
this.value=value;
}

public boolean isLeapYear(){
if((value%4==0&&value%100!=0)||value%400==0)
return true;
else
return false;
}

public boolean validate(){//判断日期是否合法年份
if(value>=1820&&value<=2020)
return true;
else
return false;
}

public void yearIncrement(){
value=value+1;
}
public void yearReduction(){
value=value-1;
}
}


public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int year = 0;
int month = 0;
int day = 0;

int choice = input.nextInt();

if (choice == 1) { // test getNextNDays method
int m = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

DateUtil date = new DateUtil(year, month, day);

if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}

m = input.nextInt();

if (m < 0) {
System.out.println("Wrong Format");
System.exit(0);
}

System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:");
System.out.println(date.getNextNDays(m).showDate());
} else if (choice == 2) { // test getPreviousNDays method
int n = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

DateUtil date = new DateUtil(year, month, day);

if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}

n = input.nextInt();

if (n < 0) {
System.out.println("Wrong Format");
System.exit(0);
}

System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:");
System.out.println(date.getPreviousNDays(n).showDate());
} else if (choice == 3) { //test getDaysofDates method
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

int anotherYear = Integer.parseInt(input.next());
int anotherMonth = Integer.parseInt(input.next());
int anotherDay = Integer.parseInt(input.next());

DateUtil fromDate = new DateUtil(year, month, day);
DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);

if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
System.out.print("The days between " + fromDate.showDate() +
" and " + toDate.showDate() + " are:"
+
fromDate.getDaysofDates(toDate));
} else {
System.out.println("Wrong Format");
System.exit(0);
}
}
else{
System.out.println("Wrong Format");
System.exit(0);
}
}
}

下面是测试点

很遗憾最后还是没有完全通过这道题,但是在pta上测试案例全是对的,我也不是很清楚自己究竟哪里没写对,不过估计也就是日期间测试的逻辑问题。聚合一和聚合二的不同是聚合一的年月日类之间是逐级聚合而聚合二的年月日类都聚合到dateutil里了。

题目集六

本体大部分内容与菜单计价程序-3相同,增加的部分用加粗文字进行了标注。

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

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

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

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

订单分:桌号标识、点菜记录和删除信息、代点菜信息。每一类信息都可包含一条或多条记录,每条记录一行或多行。

桌号标识独占一行,包含两个信息:桌号、时间。

桌号以下的所有记录都是本桌的记录,直至下一个桌号标识。

点菜记录包含:序号、菜名、份额、份数。份额可选项包括:1、2、3,分别代表小、中、大份。

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

删除记录格式:序号 delete

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

如果序号不对,输出"delete error"

代点菜信息包含:桌号 序号 菜品名称 份额 分数

代点菜是当前桌为另外一桌点菜,信息中的桌号是另一桌的桌号,带点菜的价格计算在当前这一桌。

程序最后按输入的桌号从小到大的顺序依次输出每一桌的总价(注意:由于有代点菜的功能,总价不一定等于当前桌上的菜的价格之和)。

每桌的总价等于那一桌所有菜的价格之和乘以折扣。如存在小数,按四舍五入规则计算,保留整数。

折扣的计算方法(注:以下时间段均按闭区间计算):

周一至周五营业时间与折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余时间不营业。

周末全价,营业时间:9:30-21:30

如果下单时间不在营业范围内,输出"table " + t.tableNum + " out of opening hours"

参考以下类的模板进行设计(本内容与计价程序之前相同,其他类根据需要自行定义):

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

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

}

本次课题比菜单计价系列-3增加的异常情况:

1、菜谱信息与订单信息混合,应忽略夹在订单信息中的菜谱信息。输出:"invalid dish"

2、桌号所带时间格式合法(格式见输入格式部分说明,其中年必须是4位数字,月、日、时、分、秒可以是1位或2位数),数据非法,比如:2023/15/16 ,输出桌号+" date error"

3、同一桌菜名、份额相同的点菜记录要合并成一条进行计算,否则可能会出现四舍五入的误差。

4、重复删除,重复的删除记录输出"deduplication :"+序号。

5、代点菜时,桌号不存在,输出"Table number :"+被点菜桌号+" does not exist";本次作业不考虑两桌记录时间不匹配的情况。

6、菜谱信息中出现重复的菜品名,以最后一条记录为准。

7、如果有重复的桌号信息,如果两条信息的时间不在同一时间段,(时段的认定:周一到周五的中午或晚上是同一时段,或者周末时间间隔1小时(不含一小时整,精确到秒)以内算统一时段),此时输出结果按不同的记录分别计价。

8、重复的桌号信息如果两条信息的时间在同一时间段,此时输出结果时合并点菜记录统一计价。前提:两个的桌号信息的时间都在有效时间段以内。计算每一桌总价要先合并符合本条件的饭桌的点菜记录,统一计价输出。

9、份额超出范围(1、2、3)输出:序号+" portion out of range "+份额,份额不能超过1位,否则为非法格式,参照第13条输出。

10、份数超出范围,每桌不超过15份,超出范围输出:序号+" num out of range "+份数。份数必须为数值,最高位不能为0,否则按非法格式参照第16条输出。

11、桌号超出范围[1,55]。输出:桌号 +" table num out of range",桌号必须为1位或多位数值,最高位不能为0,否则按非法格式参照第16条输出。

12、菜谱信息中菜价超出范围(区间(0,300)),输出:菜品名+" price out of range "+价格,菜价必须为数值,最高位不能为0,否则按非法格式参照第16条输出。

13、时间输入有效但超出范围[2022.1.1-2023.12.31],输出:"not a valid time period"

14、一条点菜记录中若格式正确,但数据出现问题,如:菜名不存在、份额超出范围、份数超出范围,按记录中从左到右的次序优先级由高到低,输出时只提示优先级最高的那个错误。

15、每桌的点菜记录的序号必须按从小到大的顺序排列(可以不连续,也可以不从1开始),未按序排列序号的输出:"record serial number sequence error"。当前记录忽略。(代点菜信息的序号除外)

16、所有记录其它非法格式输入,统一输出"wrong format"

17、如果记录以“table”开头,对应记录的格式或者数据不符合桌号的要求,那一桌下面定义的所有信息无论正确或错误均忽略,不做处理。如果记录不是以“table”开头,比如“tab le 55 2023/3/2 12/00/00”,该条记录认为是错误记录,后面所有的信息并入上一桌一起计算。

本次作业比菜单计价系列-3增加的功能:

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

例如:麻婆豆腐 9 T

菜价的计算方法:

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

注意:不同的四舍五入顺序可能会造成误差,请按以下步骤累计一桌菜的菜价:

计算每条记录的菜价:将每份菜的单价按份额进行四舍五入运算后,乘以份数计算多份的价格,然后乘以折扣,再进行四舍五入,得到本条记录的最终支付价格。

最后将所有记录的菜价累加得到整桌菜的价格。

输入格式:

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

菜品记录格式:

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

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

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

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

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

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

输出格式:

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

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

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

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

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

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

输入样例:

在这里给出一组输入。例如:

麻婆豆腐 12
油淋生菜 9 T
table 31 2023/2/1 14/20/00
1 麻婆豆腐 1 16
2 油淋生菜 1 2
2 delete
2 delete
end
 

输出样例:

在这里给出相应的输出。例如:

table 31: 
1 num out of range 16
2 油淋生菜 18
deduplication 2
table 31: 0 0
 

输入样例1:

份数超出范围+份额超出范围。例如:

麻婆豆腐 12
油淋生菜 9 T
table 31 2023/2/1 14/20/00
1 麻婆豆腐 1 16
2 油淋生菜 4 2
end
 

输出样例1:

份数超出范围+份额超出范围。例如:

table 31: 
1 num out of range 16
2 portion out of range 4
table 31: 0 0
 

输入样例2:

桌号信息错误。例如:

麻婆豆腐 12
油淋生菜 9 T
table a 2023/3/15 12/00/00
1 麻婆豆腐 1 1
2 油淋生菜 2 1
end
 

输出样例2:

在这里给出相应的输出。例如:

wrong format
 

输入样例3:

混合错误:桌号信息格式错误+混合的菜谱信息(菜谱信息忽略)。例如:

麻婆豆腐 12
油淋生菜 9 T
table 55 2023/3/31 12/000/00
麻辣香锅 15
1 麻婆豆腐 1 1
2 油淋生菜 2 1
end
 

输出样例3:

在这里给出相应的输出。例如:

wrong format
 

输入样例4:

错误的菜谱记录。例如:

麻婆豆腐 12.0
油淋生菜 9 T
table 55 2023/3/31 12/00/00
麻辣香锅 15
1 麻婆豆腐 1 1
2 油淋生菜 2 1
end
 

输出样例4:

在这里给出相应的输出。例如:

wrong format
table 55: 
invalid dish
麻婆豆腐 does not exist
2 油淋生菜 14
table 55: 14 10
 

输入样例5:

桌号格式错误(以“table”开头)+订单格式错误(忽略)。例如:

麻婆豆腐 12
油淋生菜 9 T
table a 2023/3/15 12/00/00
1 麻婆 豆腐 1 1
2 油淋生菜 2 1
end
 

输出样例5:

在这里给出相应的输出。例如:

wrong format
 

输入样例6:

桌号格式错误,不以“table”开头。例如:

麻婆豆腐 12
油淋生菜 9 T
table 1 2023/3/15 12/00/00
1 麻婆豆腐 1 1
2 油淋生菜 2 1
tab le 2 2023/3/15 12/00/00
1 麻婆豆腐 1 1
2 油淋生菜 2 1
end
 

输出样例6:

在这里给出相应的输出。例如:

table 1: 
1 麻婆豆腐 12
2 油淋生菜 14
wrong format
record serial number sequence error
record serial number sequence error
table 1: 26 17
下面是我的代码
import java.util.Scanner;
import java.util.Calendar;
public class Main {

    public static void main(String[] args) {
        Scanner p = new Scanner(System.in);
        Menu menu = new Menu();
        Order order = new Order();
        Table[] table = new Table[55];
        int j = 1;//菜单数
        int l = 0;//订单数
        int k = 0;//代点菜数
        Dish dish=new Dish();
        int tablenum = 0;//桌号
        int count;
        String[] t;
        String s;
        int a1=0,a2=0,a3=0,a4=0,a5=0;
        
        while (true) {
            String a = p.nextLine();
            t = a.split(" ");//输入信息通过空格分开
            if(a.equals("end"))
                break;
            count = t.length;
            
            if (count == 2) {//一个空格,输入菜单或删除信息
                                                               //String[] temp1 = st.split(" ");
                if (t[1].equals("delete")) {
                    a1 = Integer.parseInt(t[0]);
                    int c = table[/*tablenum*/a2].order.delARecordByOrderNum(a1);//table[/*tablenum*/a2].
                    table[/*tablenum*/a2].sum=table[/*tablenum*/a2].sum-c;
                } else {//菜单添加
                    a1 = Integer.parseInt(t[1]);          //将字符串表示的价格转换为整数
                    s = t[0];
                    /*menu.dishes[j] = */menu.addDish(s, a1);
                    j++;
                    //System.out.println(menu.dishes[0].name);
                }
                else System.out.println("wrong format");
            }
            
            else if (count == 4) {//四个元素  桌号,菜名,份额,份数
                if (t[0].equals("table")) {//桌号   table打错的情况没考虑            
                    a2 = Integer.parseInt(t[1]);
                    table[/*tablenum*/a2] = new Table();
                    table[/*tablenum*/a2].AheadProcess(a);//时间
                    System.out.println("table " + a2 + ": ");
                } else {//增加订单的情况;//四个元素  桌号,菜名,份额,份数
                    l++;
                    a3 =Integer.parseInt(t[0]);//桌号
                    s = t[1];//菜名
                    a4 = Integer.parseInt(t[2]);//份额
                    a5=Integer.parseInt(t[3]);//份数
                   // table[a2].order.addARecord(a3, t[1],a4 , a5);   
                    if(a4!=1&&a4!=2&&a4!=3) {
                        System.out.println("portion out of range "+a4);
                    }
                    else {
                        order.addARecord(a3,s,a4, a5);//将桌号,菜名,份额,份数传入订单
                    }
                    dish = menu.searthDish(s);                   
                    if (dish != null) {
                        table[/*tablenum*/a2].order.records[0].d = dish;
                        int f = table[/*tablenum*/a2].order.records[l].getPrice();//
                        System.out.println(table[tablenum].order.records[/*tablenum*/a2].orderNum + " " + dish.name + " " +f );
                        table[/*tablenum*/a2].sum +=f;
                    }
                    
                }
               
            }

            else if (count == 5) {//代点菜
                //String[] temp3 = st.split(" ");
                a1 = Integer.parseInt(t[1]);
                a2 = Integer.parseInt(t[3]);
                a3 = Integer.parseInt(t[4]);
                table[/*tablenum*/a2].order.addARecord( a1, t[2], a2, a3);
                dish = menu.searthDish(t[2]);
                if (dish != null) {
                    table[/*tablenum*/a2].order.records[/*tablenum*/a2].d.unit_price = dish.unit_price;
                    int b = table[/*tablenum*/a2].order.records[/*tablenum*/a2].getPrice();
                    System.out.println(t[/*tablenum*/a2] + " table " + table[/*tablenum*/a2].tablenum + " pay for table " + t[/*tablenum*/a2] + " " + b);
                    table[/*tablenum*/a2].sum += b;
                }
                l++;
            }
        }//while括号
        for (int i = 1; i < tablenum + 1; i++) {
            table[i].Gettottalprice();
        }}}
class Menu {

    //Dish[] dishs ;//菜品数组,保存所有菜品信息
    Dish[] dishes = new Dish[55];
    /*static*/ int i = 0;
    public Menu() {
        
    }

    public /*boolean*/ Dish searthDish(String dishName){//根据菜名在菜谱中查找菜品信息,返回Dish对象。
        int j = 0;
        Dish t = null;//new Dish();
        //t=null;
        for(j=1;j<i;j++) {
            if(dishName.equals(dishes[j].name)) {
                t = dishes[j];
                break;
            }}
         if(t==null){
                System.out.println(dishName+" does not exist");//存不进菜品
            }
            return t;
    
    }
    public Dish addDish(String dishName,int unit_price/*,int orderNum*/){//添加一道菜品信息
        
        //for(i=0;i<dishs.length;i++) {
        dishes[i] = new Dish();
         dishes[i].name = dishName;
         dishes[i].unit_price = unit_price;
         i++;
        //}
        return dishes[i];
        
    }}
 class Order {
    //ArrayList<Record> records = new ArrayList<>();//保存订单上每一道的记录
    Record[] records=new Record[55];//保存订单上每一道的记录
    int i = 0;
    public int getTotalPrice(){//计算订单的总价
        int j = 0;
        for(i=0;i<records.length;i++) {
            j = j + records[i].getPrice();
        }
        return j;        
    }

    public /*Record*/void addARecord(int orderNum,String dishName,int portion,int copies/*,int Price*/){//添加一条菜品信息到订单中。num是第几次订单
        records[orderNum] = new Record();
        //records[orderNum].d[orderNum] = new Dish();
        //Dish d = new Dish();
        records[orderNum].orderNum = orderNum;
        records[orderNum].d.name = dishName;
        //records[orderNum].d[orderNum].unit_price = Price;
        records[orderNum].portion = portion;
        records[orderNum].copies = copies;
        i++;
        //return records[orderNum];
        
        
    }
    /*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++;
    }//添加一条菜品信息到订单中。*/

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

    /*public findRecordByNum(int orderNum){//根据序号查找一条记录
        
    }*/
}
class Record {
    int orderNum; //序号桌号
    //Dish[] d = new Dish[55];     //菜品,调用类
    Dish d = new Dish();
    int portion; //份额(1/2/3代表小/中/大份)
    int copies;//点菜份数
    public Record() {
        
    }
    public int getPrice(){//计价,计算本条记录的价格
        //d[orderNum] = new Dish();
        
        int h = 0;
        //h = d[orderNum].getPrice(portion) ;
        h = d.getPrice(portion)*copies;
        return h;
    }
}
class Table {    
        int tablenum;
        String tableDtime;
        int year,month,day,week,hous,mm,ss;
        int sum = 0;//一桌价格 ;
        Order order = new Order();
        float discnt = -1;
        
       public void Gettottalprice(){//求总价
            if(discnt>0){
                sum = Math.round(sum*discnt);//四舍五入
                System.out.println("table " + tablenum + ": " + sum);
            }else {
                System.out.println("table " + tablenum + " out of opening hours");
            }
        }
       public void AheadProcess(String tableDtime){//改时间
            this.tableDtime = tableDtime;
            processTime();
            discount();
        }


       public void processTime(){//处理时间
            String[] t = tableDtime.split(" ");
            tablenum = Integer.parseInt(t[1]);
            String[] q = t[2].split("/");//年月日
            String[] r = t[3].split("/");//时分秒

            year = Integer.parseInt(q[0]);
            month = Integer.parseInt(q[1]);
            day = Integer.parseInt(q[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--;
            hous = Integer.parseInt(r[0]);
            mm = Integer.parseInt(r[1]);
            ss = Integer.parseInt(r[2]);

        }
        public void discount(){
            if(week>=1&&week<=5)
            {
                if(hous>=17&&hous<20)
                    discnt=0.8F;
                else if(hous==20&&mm<30)
                    discnt=0.8F;
                else if(hous==20&&mm==30&&ss==0)
                    discnt=0.8F;
                else if(hous>=11&&hous<=13||hous==10&&mm>=30)
                    discnt=0.6F;
                else if(hous==14&&mm<30)
                    discnt=0.6F;
                else if(hous==14&&mm==30&&ss==0)
                    discnt=0.6F;
            }
            else
            {
                if(hous>=10&&hous<=20)
                    discnt= 1.0F;
                else if(hous==9&&mm>=30)
                    discnt= 1.0F;
                else if(hous==21&&mm<30||hous==21&&mm==30&&ss==0)
                    discnt= 1.0F;
            }
        }
    }
    
    
class Dish {
    String name;//菜品名称
    int unit_price; //单价
    
        public Dish() {
        
    }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getUnit_price() {
            return unit_price;
        }
        public void setUnit_price(int unit_price) {
            this.unit_price = unit_price;
        }
        
        public int getPrice(int portion){    //计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)        
            double a = 1;
            switch(portion) {
            
            case 1:a = unit_price;break;
            case 2:a = unit_price * 1.5;break;
            case 3:a = unit_price *2.0;break;
            
            }
            a = a +0.5;//四舍五入
            int b = (int)a;

                return b;
        }

        
}


    

这道题在前三次题目集里其实出现过它的简单版本,但当时也没太搞懂,最后这道题我也就拿了十五分。很无奈。

三、踩坑心得

通过这几次pta,我深刻了解到点式学习Java的弊端,面对一道题时,我总是去寻找题里所要的知识点而没有全面的将每一个知识点语法联系起来,导致做每一道题都习惯性看书,缺乏自我思考,所以对于Java的学习我认为我应该学的全面且连贯来弥补自己的不足。

四、总结

这一阶段的题目集使我更加了解了类与类之间的关系,以及怎样去用类与类的关系,还学会了正则表达式。还是希望老师可以把一些相对来说较难的题目代码发布一下,这样也好让我们知道自己哪里不足,帮我们解答疑惑。

标签:总结,输出,题目,String,int,31,table,public
From: https://www.cnblogs.com/LYXOY/p/17359520.html

相关文章

  • OOP 4-6题目集总结
    目录前言设计与分析踩坑心得改进建议总结一、前言      (1)第四次题目集           本次题目集一共有7道题目,题量适中,但第一题难度较大,其余题目难度适中。考察的知识点有类与类之间的关系调用、对象数组的使用、排序、String类方法的使......
  • 4-6次题目集总结
    前言:4-6次pta实验相较于之前三次难度有所提升,主要是为了训练我们对于java类的设计以及类内方法的设计,以及很多语法知识,是正式进入java的过程。题目集四:主要知识点是一些语法的使用,类的设计,以及类的方法体,需要考虑输出格式和算法设计,如正则表达式,LinkedHashSet去重等,题目难度不低......
  • PTA题目集4~6的总结
    1.前言题目集4题目集4题目量适中,整体难度中偏易题目7-1要求厘清类与类间的关系,能对题目所给的要求作出准确的设计,难度中偏上题目7-2~7-7考察基本的算法,对Java中集合框架的使用以及对LocalDate类的使用,总体上难度偏易题目集5......
  • Java题目集4~6的总结
    1.前言第四次作业主要涉及的知识点有通过查询JavaAPI文档,了解Scanner类中nextLine()等方法、String类中split()等方法、Integer类中parseInt()等方法的用法,了解LocalDate类中of()、isAfter()、isBefore()、until()等方法的使用规则,了解ChronoUnit类中DAYS、WEEKS、MONTHS等单位......
  • Codeforces Round 854 补题总结
    CodeforcesRound854补题总结前言昨天做这套题很不顺,今天补完题大概总结一下。总结RecentActions按题意模拟即可,考虑到前\(n\)个数一定始终在后\(m\)个数的前面,所以说当当前队列中如果没有出现\(x\)而在第\(i\)轮放进了\(x\),那么当前在队首的编号小于\(n\)的数......
  • oo第二次博客总结
      目录1.前言2.设计与分析3.踩坑心得4.改进建议5.总结一:前言题目集四:1,菜单计价程序-32,有重复的数据3,去掉重复的数据4.单词的统计与排序5.面向对象编程(封装性)6.GPS测绘中度分秒转换7.判断两个日期的先后,计算间隔天数、周数.题目集五:1.正则表达式训练-QQ号校......
  • pta第二部分总结oop训练集05-06
    (1)前言训练集05:(题量适中,难度适中)7-5:多个类的互相调用对于日期类的整体优化,聚合初体验。7-6:对7-5的优化,加强聚合训练。训练集06:(题量少,难度大)7-1:多需求的实现。(完成的不好,编程能力还不够)(2)设计与分析7-5:类图: 源码:importjava.util.Scanner;publicclassMain{......
  • 集训总结
    集训总结前言“吹散记忆的蒲公英,散落碧空;回望过往的风景,尤存风味。”离开初中生活,总会幻想回到过去,回到以前的老师同学身边,羞怯而带有稚气。结束了本蒟蒻的第一次NOIP,第一次自己在外地参加集训。内心却也充满无法言表的激动。而今总结半个月的回忆,内心充满了话,却不知从何开......
  • 题目集4-6
     第二次blog作业1.前言作为一个java和面向对象的初学者,在写题目这方面确实是处处碰壁,但学习最重要的是坚持,希望我能够坚持下去。对于这三次的题目集,就难度而言是比较难的,难在一些测试点需要经过深层次的思考,还要通过大量的测试测试出来,必须承认的是考到的一些知识在题目中都有......
  • IPv6地址总结
    一、IPv6特点地址空间更大,地址长度128位,更便于路由汇总;无需NAT;保留单播,组播,新加入的任意播取代广播;二、IPv6地址分类1.单播地址:和IPv4一样单播地址:除FF00::/8之外的全部IPv6地址可以在启用IPv6接口下自动生成(1)链路本地地址(Link-LocalAddress)链路本地地址是自动生成的,链路本地地......