一、前言
通过这三周对Java课程的学习及pta大作业的练习,我了解了Java的编译环境如JDK、JRE等等,Java去掉了C++语言的许多功能,是安全的、解释的、高性能的语言,但最主要的还是Java的面向对象性,Java中的类与对象的创建以及类间关系,类与类之间方法属性的调用时常让我头疼,通过pta的练习我也对Java语法知识点更加熟悉并且更进一步了解了Java中的继承关系、抽象类与接口:
(1)继承关系
Java中的继承是一种面向对象编程的重要概念,它允许一个类(称为子类或派生类)继承另一个类(称为父类或基类)的属性和方法。
Java中的继承通过关键字“extends”实现。子类通过“extends”关键字后指定父类的名称来继承父类。例如:
```
public class Animal {
   public void eat() {
      System.out.println("Animal is eating...");
   }
}
public class Cat extends Animal {
   public void meow() {
      System.out.println("Cat is meowing...");
   }
}
```
在上述代码中,Cat类通过“extends”关键字指定Animal类作为其父类,并继承了Animal类的“eat”方法。此外,Cat类还定义了自己的方法“meow”。
接下来,我们可以通过创建Cat类对象来使用“eat”和“meow”方法:
```
public class Main {
   public static void main(String args[]) {
      Cat myCat = new Cat();
      myCat.eat(); // 调用Animal类的eat方法
      myCat.meow(); // 调用Cat类的meow方法
   }
}
```
在上述代码中,我们创建了一个Cat类对象“myCat”,并调用了它的“eat”和“meow”方法。由于Cat类继承Animal类的“eat”方法,因此Cat类对象可以使用Animal类中定义的“eat”方法。
继承允许在不重复编写相同代码的情况下扩展和修改现有代码,从而提高了代码的可重用性和可维护性。
(2)抽象类
在Java中,抽象类是一种不能被实例化的类,它只能作为其他类的父类来使用。抽象类可以包含抽象方法和具体方法,其中抽象方法是只有定义,而没有实现的方法,而具体方法则是有实现的方法。
使用抽象类可以实现一种基于约定的编程方式。抽象类定义了一些方法的接口,要求派生类实现这些方法。因此,抽象类实现了一种行为的规范,而具体的实现则由派生类来完成。
在Java中,使用“abstract”关键字来定义抽象类和抽象方法。例如:
```
public abstract class Shape {
    int x, y;
    public Shape(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public void move(int deltaX, int deltaY) {
        x += deltaX;
        y += deltaY;
    }
    public abstract double getArea();
}
```
在上述代码中,Shape类是一个抽象类,它包含了一个构造函数和两个具体方法“move”和抽象方法“getArea”。
由于Shape类含有抽象方法,因此它不能被实例化。只有定义了这些抽象方法的具体实现的类才能被实例化。例如:
```
public class Rectangle extends Shape {
    int width, height;
    public Rectangle(int x, int y, int width, int height) {
        super(x, y);
        this.width = width;
        this.height = height;
    }
    @Override
    public double getArea() {
        return width * height;
    }
}
```
在上述代码中,Rectangle类扩展了Shape类并实现了抽象方法“getArea”。
抽象类提供了一种抽象层次结构,可以有效地组织和管理程序中的对象类型,并确保代码的可维护性和可扩展性。
(3)接口
在Java中,接口是一种定义方法规范的抽象类型,它定义了一组方法,但没有提供任何实现。接口提供了一种行为的规范,而具体的实现则由实现了接口的类来完成。
使用Java接口可以实现多态性,这意味着相同的接口可以由不同的类来实现,并提供不同的实现方法。
在Java中,使用“interface”关键字来定义接口。例如:
```
public interface Drawable {
    void draw();
}
```
在上述代码中,Drawable是一个接口,它定义了一个抽象方法“draw”。
要实现一个接口,需要使用“implements”关键字。例如:
```
public class Circle implements Drawable {
    private int x, y, radius;
    public Circle(int x, int y, int radius) {
        this.x = x;
        this.y = y;
        this.radius = radius;
    }
    @Override
    public void draw() {
        System.out.println("Drawing a circle at (" + x + ", " + y + ") with radius " + radius);
    }
}
```
在上述代码中,Circle类实现了Drawable接口,并提供了“draw”方法的具体实现。因此,我们可以使用Circle类对象调用“draw”方法。
接口可以被多重继承,这意味着一个接口可以通过继承其他接口来定义更复杂的行为规范。
Java接口提供了一种灵活的机制,可以使程序更易于扩展和维护,并提高代码的可读性和可重用性。
(4)ArrayList
在 Java 中,ArrayList 是一个动态数组,实现了 List 接口。ArrayList 允许在运行时动态添加和删除元素,可以包含重复的元素。
ArrayList 内部使用一个数组来存储数据,当数组元素数量达到容量时,自动增加容量。默认情况下,ArrayList 的容量为 10。
以下是一些常见的 ArrayList 操作:
1. 在尾部添加元素:`list.add(element)`
2. 在指定位置插入元素:`list.add(index, element)`
3. 删除元素:`list.remove(index)` 或 `list.remove(element)`
4. 获取元素:`list.get(index)`
5. 获取元素个数:`list.size()`
6. 判断 ArrayList 是否为空:`list.isEmpty()`
7. 清空 ArrayList:`list.clear()`
二、设计与分析
(1)7-1 菜单计价程序-4
这是菜单类的第四大题,该题涉及到了类与类之间的属性方法的相互调用,该题我一共创建了几个类:主类、菜品类、菜谱类、点菜记录类、订单类、桌类和Worn类;在菜品类中有菜名属性name和菜品单价unit_price和getPrice方法该方法中需传参,将菜品的份额portion传入该方法中,在该方法中用switch语句实现对每一道菜计算价格,在菜单类我创建了菜品数组用来保存所有菜品信息,并在该菜单类中的Dish searthDish(String dishName)方法中对菜单中的菜品信息(菜名、单价)做了初始化操作并且设置了比对功能,如果顾客所点的菜与菜单中的菜不匹配则print该菜does not exist,若匹配成功则返回顾客所点菜的菜名。在点菜记录类中我也创建了菜品类Dish的对象和份额portion属性,该订单中的getPrice()方法与Dish类重名,可是两者内容是不同的,Record类中的getPrice方法是用来计价计算本条记录的价格。订单类中创建了点菜记录类Record的对象,并创建了getTotalPrice()方法用来计算点菜记录的总价,也创建了addARecord(String dishName,int portion)的方法用来实现不同顾客不同点菜记录的需求。在创建好这些必须类的内容后,我们再开始主类内容,该题输入主要是菜名和份额,也就是输入字符型和整形数据,最后输出菜的总价或某某菜不存在,所以创建字符变量a接收输入的字符型数据,整形变量b来接收输入菜名的份额,并创建order类与Menu类对象分别为c和n,只要变量a接收的数据不是end,b变量接收份额,并且c变量也继续接收储存新的点菜记录,这之后还要运用Menu对象n中的方法searthDish(String dishName)与点菜记录Record比对,存在则输出总价,若有不存在的菜名,则输出dishname+does not exist和总价,桌类用来判断本桌订单的信息和判断时间是否正确包括对菜品价格的处理(折扣计算),Worn类的创建是为了更好地对错误格式的判定,考虑所有的错误格式的输入并放在该类中处理。
import java.util.*;
public class Main {
	public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String[] a = new String[50];
    int[] b = new int[50];
    Menu c=new Menu();
    table[] t=new table[20];
    Dish g=new Dish();
    int i=0;//桌号
    int j=0;//菜数
    int k=0;//订单菜数
    int d1=0,d2=0,d3=0;
    int o1=0,o2=0,o3=0,o4=0;
    int lastdish=0;
        int h=0;
    a[0]=in.nextLine();
        b[0] = 0;
        int Scannernum = 0;
    while(!a[Scannernum].equals("end"))
    {
        Scannernum++;
        a[Scannernum]=in.nextLine();
        b[Scannernum]=0;
    }
//非法输入
        Worn w=new Worn();
        w.worninput(a,Scannernum,b);
        
    int p = 0;
    for(p=0;p<Scannernum;p++)
    {
        int flag = 0;
        if(a[p].matches(""))
        {
            System.out.println("wrong format");
            continue;
        }
        String[] arr = a[p].split("    |   |  | ");
       // System.out.println(arr.length+" "+a[p]);
        if(arr.length==4)
        {
        	
            if(arr[0].charAt(0)=='t')
            {
                flag=1;
                lastdish=0;
                table x=new table();
                x.input(a[p],b[p]);
                 if(i!=0)
                 k=t[i-1].searchtime(t,x,i);
                if(p==i)//加新桌
                    {
                        t[i]=new table();
                        t[p]=x;
                        i++;
                        lastdish=0;//初始化
                        if(t[p].flag)
                            System.out.println("table "+t[p].tablenum+": ");
                        //k=0;
                    }
                    else
                    {
                        if(t[p].flag)
                            System.out.println("table "+t[p].tablenum+": ");
                        lastdish=0;
                    }
                if(arr[1].matches("[0-9]{0,}")&&arr[2].matches("^[0-9]{4}/[1-9][0-9]{0,1}/[1-9][0-9]{0,1}$")&&arr[3].matches("^[0-9]{0,2}/[0-9]{0,2}/[0-9]{0,2}$"))
                {
                		k=i;
                        t[i]=new table();
                        t[k]=x;
                      //  i++;
                        lastdish=0;//初始化
                        if(t[k].flag)
                            System.out.println("table "+t[k].tablenum+": ");
                        i++;
                }
                        //k=0;
                   // }
//                     else
//                     {
//                         if(t[k].flag)
//                             System.out.println("table "+t[k].tablenum+": ");
//                         lastdish=0;
//                     }
            }
             else if(arr[0].matches("^[1-9][0-9]{0,1}$")&&i!=0)
                {
            	 if(b[p]==1)
                 {
                     flag=1;
                     if(i!=0&&t[k].flag)
                     {
                         System.out.println("wrong format");
                     }
                     continue;
                 }
                 d1=Integer.parseInt(arr[0]);
                 d2=Integer.parseInt(arr[2]);
                 d3=Integer.parseInt(arr[3]);
                 if(i!=0&&t[k].flag)
                 {
                     if(i!=0)
                     {
                         if(d1<=lastdish)
                         {
                             System.out.println("record serial number sequence error");
                         }
                         else
                         {
                             t[k].selforder.addARecord(0,d1,arr[1],d2,d3);
                             g=c.searthDish(arr[1]);
                             if(g!=null&&t[k].flag)
                             {
                                 if(d2>3&&d2<10||d2<=0&&d2>-10)//情况9
                                 {
                                     //序号+" portion out of range "+份额
                                     System.out.println(d1+" portion out of range "+d2);
                                 }
                                 else if(d3>15)
                                 {
                                     System.out.println(d1+" num out of range "+d3);
                                 }
                                 else
                                 {
                                     t[k].selforder.records[t[k].cs].d=g;
                                     //System.out.println(t[p].selforder.records[]);
                                     int x=t[k].selforder.records[t[k].cs].getPrice();
                                     //2 油淋生菜 27
                                     lastdish=d1;
                                     System.out.println(d1+" "+arr[1]+" "+x);
                                 }
                             }
                             t[k].cs++;
                         }
                     }
                 }
                }
        }
        
        else if(arr.length==5)//代点
        {
            //  1 4 麻婆豆腐 1 1
            if(b[p]==1)
            {
                flag=1;
                if(i!=0&&t[k].flag)
                {
                    System.out.println("wrong format");
                }
                continue;
            }
            o1=Integer.parseInt(arr[0]);
            o2=Integer.parseInt(arr[1]);
            o3=Integer.parseInt(arr[3]);
            o4=Integer.parseInt(arr[4]);
            if(t[k].searthtable(t,o1,k)==true)
            {
              
                t[k].selforder.addARecord(o2, o1, arr[2],o3,o4);
                g=c.searthDish(arr[2]);
                if(g!=null)
                {
                    t[k].selforder.records[t[k].cs].d=g;
                    int x=t[k].selforder.records[t[k].cs].getPrice();
                
                    System.out.println(d2+" table "+t[k].tablenum+" pay for table "+d1+" "+x);
                   
                }
               
                t[k].cs++;
            }
        }
        else if(arr.length==2)
        {
                if(arr[1].equals("delete"))//删菜
                {
                    if(i!=0)
                    {
                        if(b[p]==1)
                        {
                            flag=1;
                            if(i!=0)
                            {
                                System.out.println("wrong format");
                            }
                            continue;
                        }
                        if(t[k].flag)
                            t[k].selforder.delARecordByOrderNum(Integer.parseInt(arr[0]),t[k]);
                    }
                }
                else//加菜
                {
                    if(b[p]==1)
                    {
                        flag=1;
                        System.out.println("wrong format");
                        continue;
                    }
                    //  油淋生菜 9
                    if(i!=0&&t[k].flag)//情况1
                    {
                        System.out.println("invalid dish");
                    }
                    else
                    {
                        if(Integer.parseInt(arr[1])>0&&Integer.parseInt(arr[1])<300)
                        {
                            c.addDish(arr[0], Integer.parseInt(arr[1]),false);
                            j++;
                        }
                        else
                        {
                            System.out.println(arr[0]+" price out of range "+arr[1]);
                        }
                     }
                }
        }
        
        else if(arr.length==3)//特色菜
        {
        	 if(b[p]==1)
             {
                 flag=1;
                 System.out.println("wrong format");
                 continue;
             }
             if(i!=0&&t[k].flag)
             {
                 System.out.println("invalid dish");
             }
             else
             {
                 if(arr[2].equals("T"))
                 {
                     if(Integer.parseInt(arr[1])>0&&Integer.parseInt(arr[1])<300)
                     {
                         c.addDish(arr[0], Integer.parseInt(arr[1]),true);
                         j++;
                     }
                     else
                     {
                         System.out.println(arr[0]+" price out of range "+arr[1]);
                     }
                 }
             }
         }
         	if(flag==0&&b[p]==1||a[p].matches("\n| |  |null"))//其他错误
         	{
         		System.out.println("wrong format");
         	}
        }
    		for(int l=0;l<i;l++)
    			{
    				t[l].getTotalPrice();
    			}
    }
}
//菜品类:对应菜谱上一道菜的信息。
//String name="";//菜品名称
//int unit_price;菜品单价
//int getPrice(int portion)//计算一盘菜价格
class Dish
{
    String name="";//菜品名称
    int unit_price;
    boolean special=false;//特色菜,true为是
    //单价
    int getPrice(int portion)
    {
        int 菜价=0;
        //单菜价格
        switch(portion)
        {
            case 1: 菜价=unit_price;break;
            case 2: 菜价=(int)Math.ceil(1.0*unit_price*3/2);break;
            case 3: 菜价=unit_price*2;break;
        }
        return 菜价;
    }
}
//菜谱类:对应菜谱,包含饭店提供的所有菜的信息。
//Dish[] dishs =new Dish[20];所有菜信息
//int i=0;//用于记录菜品总数
//Dish addDish(String dishName,int unit_price)//添加菜品 名称 价格
//Dish searthDish(String dishName,Menu c) //根据菜名在菜谱中查找菜品信息,返回Dish对象
class Menu
{
	 Dish[] dishs =new Dish[20];
	 int i=0;//用于记录菜品总数
	 void addDish(String dishName,int unit_price,Boolean ts)
	 {
		 dishs[i]=new Dish();
		 dishs[i].name=dishName;
		 dishs[i].unit_price=unit_price;
         dishs[i].special=ts;
		 i++;
	 }
	//菜品数组,保存所有菜品信息
    Dish searthDish(String dishName) //根据菜名在菜谱中查找菜品信息,返回Dish对象。
    {
        int j=0;
        int flag=0;
        for(j=i-1;j>=0;j--)//从末端开始查找比对
        {
            if(dishName.compareTo(dishs[j].name)==0)
            {
                flag=1;
                break;
            }
        }
        if(flag==1)
        return dishs[j];
        else
        {
        	System.out.println(dishName+" does not exist");
            return null;
        }
    }
}
//点菜记录类:保存订单上的一道菜品记录
//Dish d=new Dish();//菜品
//int orderNum=0;//序号
//int portion=0;//份额(1/2/3代表小/中/大份)
//int num=0;//份数
//int getPrice()计算一种菜的价格
class Record 
{
    Dish d=new Dish();//菜品
    int orderNum=0;//序号\
    int portion=0;
    int num=0;//份数
    boolean self=true;
    //份额(1/2/3代表小/中/大份)
    int getPrice()
    {
    	int n=0;
        n=num*d.getPrice(portion);
        return n;
    }//计价,计算本条记录的价格
}
class Order 
{
    Record[] records=new Record[20];
    int j=0;
	//添加一条菜品信息到订单中。
    int[] deletenum=new int[50];
    void addARecord(int tablenum,int orderNum,String dishName,int portion,int num)//添加一条菜品信息到订单中。
    {
        //1 麻婆豆腐 2 2
    	records[j]=new Record();
    	//records[j].tablenum=tablenum;
    	records[j].orderNum=orderNum;
    	records[j].d.name=dishName;
    	records[j].portion=portion;
    	records[j].num=num;
        j++;
    }
    int delARecordByOrderNum(int orderNum,table t)//根据序号删除一条记录
    //试试直接总价减去删除菜
    {
    if(srarchdelete(orderNum)==false)
        {
            for(int i=0;i<j;i++)
            {
                if(t.selforder.records[i].orderNum==orderNum)
                {
                    t.selforder.records[i].num=0;
                }
                //records[orderNum-1].getPrice();
            }
        }
        else//情况4
        {
            System.out.println("deduplication "+orderNum);
        }
        return 0;
    }
      boolean srarchdelete(int orderNum)//找到返回true
    {
        int i=0;
        for(i=0;i<50;i++)
        {
            if(deletenum[i]!=0)
            {
                if(orderNum==deletenum[i])
                {
                    return true;
                }
            }
            else break;
        }
        deletenum[i]=orderNum;
        if(i==0)
        {
            return false;
        }
        return false;
    }
    void srarchrecord(Record g,int k)
    {
        for(int i=0;i<k;i++)
        {
            if(g.d.name.equals(records[i].d.name)&&g.portion==records[i].portion&&records[i].self==true)
            {
                records[i].num+=g.num;
                g.num=0;
                break;
            }
        }
    }
}
class table
{
    int tablenum;//桌号
    String time;//点菜时间
    int year=0,month=0,day=0,ww=0,hh=0,mm=0,ss=0; 
    boolean flag=true;//判断时间是否正确
    double count=0;//折扣
    Order selforder=new Order();//本桌订单
    double Tcount=1;
    //Order alothreorder=new Order();//代点订单
    //int sc=0;//删菜
    int cs=0;//点菜次数
    int sum=0;//计算总价
    int truesum=0;//未打折价
    void input(String time,int b)//预处理
    {
     	//this.time=time;
//     	timechange();
//     	jscount();
//     	pdflag();
    	
        String[] arr1=time.split(" ");
        int blank=0;
        for(int bk=0;bk<time.length();bk++)
        {
            if(time.charAt(bk)==' ')
            {
                blank++;
            }
        }
        //桌号
        //System.out.println(blank+""+arr1[1]+""+arr1.length);
        if(blank!=3||!arr1[1].matches("^[1-9][0-9]{0,}$")||arr1.length!=4)
        {
            System.out.println("wrong format");
        }
        //System.out.println();
        else
        {
            this.time=arr1[2]+" "+arr1[3];
            // System.out.println();
            tablenum=Integer.parseInt(arr1[1]);
            if(tablenum<=55&&tablenum>=1)
            {
                if(this.time.matches("^([0-9]{4}/[0-9]{1,2}/[0-9]{1,2}) ([0-9]{1,2}/[0-9]{1,2}/[0-9]{1,2})$"))
                {
                    if(arr1[2].matches("^(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})/((([0]{0,1}[13578]|1[02])/([0]{0,1}[1-9]|[12][0-9]|3[01]))|(([0]{0,1}[469]|11)/([0]{0,1}[1-9]|[12][0-9]|30))|([0]{0,1}2/([1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})([48]|[0]{0,1}[2468][048]|[0]{0,1}[13579][26])|(([48]|[2468][048]|[3579][26])00))/[0]{0,1}2/29)$")&&arr1[3].matches("^([0-9]|([01][0-9])|(2[0-3]))/([0-9]|([0-5][0-9]))/([0-9]|([0-5][0-9]))$"))
                    {
//                         boolean flag1=judgetime1();
//                         if(flag1==true)//时间有效
//                         {
                    		tablenum=Integer.parseInt(arr1[1]);//桌号
                            timechange();
                            jscount();
                            pdflag();
//                             if(b==1)
//                                 System.out.println("wrong format");
                            //return true;
//                         }
//                         else
//                         {
//                             System.out.println("not a valid time period");
//                         }
                    }
                    else
                        System.out.println(tablenum+" date error");
                }
                else
                    System.out.println("wrong format");
}
            else
            {
                System.out.println(arr1[1] +" table num out of range");
            }
            //return false;
        }
    }
    boolean searthtable(table[]t,int num,int p)
    {
        for(int i=0;i<50;i++)
        {
            if(t[i]!=null&&i!=p)
            {
                if(t[i].tablenum==num&&t[i].flag)
                    return true;
            }
            else
                break;
        }
        //"Table number :"+被点菜桌号+" does not exist"
        System.out.println("Table number :"+num+" does not exist");
        return false;
    }
	void getTotalPrice()//计算桌总价
    {
    	if(flag)
        {
            int j=selforder.j;
            for(int i=j-1;i>=0;i--)
            {
                //if(selforder.records[i].self==true)
                selforder.srarchrecord(selforder.records[i],i);
            }
            for(int i=0;i<j;i++)
            {
                int x=0;
                x=selforder.records[i].getPrice();
                //
                truesum+=x;
//     	    	if(selforder.records[i].self==true)
//     	    	{
                if(selforder.records[i].d.special==true)//特色菜
                {
                    sum+=(int)(x*Tcount+0.5);
                }
                else
                    sum+=(int)(x*count+0.5);
                //}
                //System.out.println();
            }
            System.out.println("table "+tablenum+": "+truesum+" "+sum);
    }
    }
    void jscount()//运用时间计算折扣
    {
    	if(ww>=1&&ww<=5)
    	{
    		Tcount=0.7;
    		if(hh>=17&&hh<20)
    			count=0.8;
   	        else if(hh==20&&mm<30)
                count=0.8;
            else if(hh==20&&mm==30&&ss==0)
                count=0.8;
    		else if(hh>=11&&hh<=13||hh==10&&mm>=30)
    			count=0.6;
            else if(hh==14&&mm<30)
                count=0.6;
            else if(hh==14&&mm==30&&ss==0)
    	        count=0.6;
    	}
    	else 
    	{
    		Tcount=1;
    		if(hh>=10&&hh<=20)
    			count=1.0;
            else if(hh==9&&mm>=30)
                count=1.0;
            else if(hh==21&&mm<30||hh==21&&mm==30&&ss==0)
                count=1.0;
    	}
    		
//    		折扣的计算方法(注:以下时间段均按闭区间计算):
//
//    		周一至周五营业时间与折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余时间不营业。
//
//    		周末全价,营业时间:9:30-21:00
//
//    		如果下单时间不在营业范围内,输出"table " + t.tableNum + " out of opening hours"
    }
    void Tcount()//判断特价菜
    {
        
    }
    void pdflag()//判断时间是否正确
    {
    	if(count==0)
    		flag=false;
    	else
    		flag=true;
    }
    void timechange()//时间转换
    {
    	//table 1 2023/3/22 12/2/3
    	 String[] arr1=time.split(" ");
//    	 System.out.println(time);
    	// 
    	 String[] arr2=arr1[0].split("/");
    	 String[] arr3=arr1[1].split("/");
    	 year=Integer.parseInt(arr2[0]);//时间
    	 month=Integer.parseInt(arr2[1]);
    	 day=Integer.parseInt(arr2[2]);
    	 Calendar c = Calendar.getInstance();
    	 c.set(year,month-1,day);//设置时间
 		 ww=c.get(Calendar.DAY_OF_WEEK);
 		 hh=Integer.parseInt(arr3[0]);//时间
 		 mm=Integer.parseInt(arr3[1]);
 		 ss=Integer.parseInt(arr3[2]);
        if(ww==1)
            ww=7;
        else
            ww--;
    }
    
    int searchtime(table[]t,table a,int sumtable)//若找到返回对应桌,找不到返回下一个空桌
    {
        //(时段的认定:周一到周五的中午或晚上是同一时段,或者周末时间间隔1小时(不含一小时整,精确到秒)
        for(int i=0;i<sumtable;i++)
        {
            if(a.tablenum==t[i].tablenum)
                if(a.year==t[i].year)
                {
                    if(a.day==t[i].day)
                    {
                        if(a.ww>=1&&a.ww<=5)//周1~5
                        {
                            if(a.hh<=14&&t[i].hh<=14||a.hh>14&&t[i].hh>14)//在同一时段
                            {
return i;
                            }
                        }
                        else//周末
                        {
                            if(Math.abs((a.hh-t[i].hh)*60*60+(a.mm-t[i].mm)*60)+(a.ss-t[i].ss)<60*60)//在同一时段
                            {
                                return i;
                            }
                        }
                    }
                }
        }
        //System.out.println(sumtable);
        return sumtable;
    }
}
class Worn
{
    void worninput(String[]a,int num,int[]b)
    {
        //boolean flag1=true;
        for(int i=0;i<num;i++)
        {
            String[] arr=a[i].split(" ");
            boolean flag=false;
            //麻婆豆腐 12
            if(arr.length>5)
            {
                b[i]=1;
            }
            if(a[i].matches("^[\u4e00-\u9fa5]{1,} ([1-9][0-9]{0,2})$")==true)//普通菜存入
            {
                flag=true;
            }
            //油淋生菜 9 T
            else if(a[i].matches("^[\u4e00-\u9fa5]{1,4} ([1-9][0-9]{0,2}) [T]$")==true)//特色菜存入
            {
                flag=true;
            }
            //table 31 2023/2/1 14/20/00
            else if(arr[0].equals("table")==true)//桌
            {
                int blank=0;
                for(int bk=0;bk<a[i].length();bk++)
                {
                    if(a[i].charAt(bk)==' ')
                    {
                        blank++;
                    }
                }
                if(arr[1].matches("^([1-9][0-9]{0,})$")&&blank==3)
                    flag=true;
                //int blank=0;
            }
            //1 麻婆豆腐 1 16
            else if(a[i].matches("^[1-9][0-9]{0,2} [\u4e00-\u9fa5]{1,} [1-9] ([1-9][0-9]{0,1})$")==true)//自己点菜
            {
                flag=true;
            }
            //1 1 麻婆豆腐 1 16
            else if(a[i].matches("^([1-9][0-9]{0,1}) [1-9][0-9]{0,2} [\u4e00-\u9fa5]{1,} [1-9] ([1-9][0-9]{0,1})$")==true)//待点菜
            {
                flag=true;
            }
            //2 delete
            else if(a[i].matches("^([1-9][0-9]{0,1}) delete$")==true)//删菜
            {
                flag=true;
            }
            else if(a[i].matches("^end$")==true)//结尾
            {
                flag=true;
            }
            if(flag==false)
            {
                b[i]=1;
            }
        }
    }
}


import java.util.*;
import java.util.Scanner;
public class Main {
   public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
       String a;
       Menu c=new Menu();
       table[] t=new table[20];
       people[] p=new people[10];
       Dish g=new Dish();
       int i=0;//桌号
       int j=0;//菜数
       int k=0;//订单菜数
       int y=0;//当前处理人
       int dc1=0,dc2=0,dc3=0,dc4=0,dd1=0,dd2=0,dd3=0,dd4=0,dd5=0;
       int Tc=0;//计算川菜总的口味度
       int Tj=0;//计算晋菜总的口味度
       int Tz=0;//计算浙菜总的口味度
       int s1=0;//计算川菜数量
       int s2=0;//计算晋菜数量
       int s3=0;//计算浙菜数量
       a=in.nextLine();
       int s;
       while(!a.equals("end"))
       {
           //分割字符
           String[] arr = a.split(" ");
           //判断情况
           if(arr.length==4)
           {
               if(arr[3].equals("T"))//特色菜录入菜单
               {
                   s=Integer.parseInt(arr[2]);
                   //	c.dishs[j]=new Dish();
                   c.addDish(a);
                   j++;
               }
               else if(t[i-1].flag)//自己点菜
               {
                   //  1 麻婆豆腐 2 2
                   //System.out.println(a);
                   dc1=Integer.parseInt(arr[0]);
                   dc2=Integer.parseInt(arr[2]);
                   dc3=Integer.parseInt(arr[3]);
                   t[i-1].selforder.addARecord(t[i-1].tablenum,dc1,arr[1],dc2,dc3,0);
                   g=c.searthDish(arr[1],dc2);
                   if(g!=null)
                   {
                       t[i-1].selforder.records[k].d=g;
                       t[i-1].selforder.records[k].flag=true;
                       int x=t[i-1].selforder.records[k].getPrice(false,0);
                       //2 油淋生菜 27
                      // System.out.println(dc1+" "+arr[1]+" "+x);
                      
                   }
                   k++;
               }
           }
           else if(arr.length==6&&(t[i-1].flag))
           {
//        	   1 1 醋浇羊肉 0 1 2
        	   //(int tablenum,int orderNum,String dishName,int portion,int num,int kw)
               dd1=Integer.parseInt(arr[0]);
               dd2=Integer.parseInt(arr[1]);
               dd3=Integer.parseInt(arr[3]);
               dd4=Integer.parseInt(arr[4]);
               dd5=Integer.parseInt(arr[5]);
               t[i-1].selforder.addARecord(t[i-1].tablenum,dd1, arr[2], dd4, dd5,dd3);
               g=c.searthDish(arr[2],dd3);
               if(g!=null)
               {
                   t[i-1].selforder.records[k].d=g;
                   int x=t[i-1].selforder.records[k].getPrice(true,dd2);
                   t[i-1].selforder.records[k].flag=false;
                   //4 table 2 pay for table 1 12
                 //  System.out.println(dd2+" table "+i+" pay for table "+dd1+" "+x);
                   for(int l=0;l<i-1;l++)
                   {
                	   if(t[l].tablenum==dd2)
                	   {
                		   t[l].otherorder.addARecord(t[l].tablenum,dd1, arr[3], dd4, dd5,dd3);
                		  // System.out.println( t[l].otherorder.j);
                		   t[l].otherorder.records[t[l].otherorder.j-1].d=g;
                		   break;
                	   }
                   }
                 
               }
               k++;
           }
           else if(arr.length==7)
           {
               t[i]=new table();
               t[i].input(a);
               //input(String time)p
               if(t[i].flag) {
                   y = people.newpeople(arr[3], arr[4], p);
                   p[y].addtable(i);
               }
               i++;
               k=0;
               if(!arr[4].matches( "^((18[0,1,9])|(13[3,5,6]))\\d{8}$")||arr[4].length()!=11||arr[3].length()>10)
                   System.out.println("wrong format");
           }
           else if(arr.length==2)
           {
               if(arr[1].equals("delete")&&(t[i-1].flag))//删菜
               {
                   t[i-1].selforder.delARecordByOrderNum(Integer.parseInt(arr[0]),t,i-1);
               }
               else//加菜
               {
                   //  油淋生菜 9
                   c.addDish(a);
                   j++;
               }
           }
           else if(arr.length==5&&(t[i-1].flag))//点特色菜
           {
        	   if(arr[1].matches("^[1-9][0-9]{0,}$"))
        	   {
        		   //1 1 醋浇羊肉 1 2
        		   //(int tablenum,int orderNum,String dishName,int portion,int num,int kw)
        	   
        		   dd1=Integer.parseInt(arr[0]);
                   dd2=Integer.parseInt(arr[1]);
                   dd3=Integer.parseInt(arr[3]);
                   dd4=Integer.parseInt(arr[4]);
                  // dd5=Integer.parseInt(arr[5]);
                   t[i-1].selforder.addARecord(t[i-1].tablenum,dd1, arr[3], dd3, dd4,0);
                   g=c.searthDish(arr[2],dd3);
                   if(g!=null)
                   {
                       t[i-1].selforder.records[k].d=g;
                       t[i-1].selforder.records[k].flag=false;
                       int x=t[i-1].selforder.records[k].getPrice(true,dd2);
                       for(int l=0;l<i-1;l++)
                       {
                    	   if(t[l].tablenum==dd2)
                    	   {
                    		   t[l].otherorder.addARecord(t[l].tablenum,dd1, arr[3], dd3, dd4,0);
                    		   t[l].otherorder.records[t[l].otherorder.j-1].d=g;
                    		   break;
                    	   }
                       }
                       //4 table 2 pay for table 1 12
                     //  System.out.println(dd2+" table "+i+" pay for table "+dd1+" "+x);
                     
                   }
        	   }
        	   else
        	   {
        		   dc1=Integer.parseInt(arr[0]);
        		   dc2=Integer.parseInt(arr[3]);
        		   dc3=Integer.parseInt(arr[4]);
        		   dc4=Integer.parseInt(arr[2]);//口味
        		   t[i-1].selforder.addARecord( t[i-1].tablenum,dc1,arr[1],dc2,dc3,dc4);
        		   g=c.searthDish(arr[1],dc4);
        		   if(g!=null)
        		   {
	                   t[i-1].selforder.records[k].d=g;
	                   t[i-1].selforder.records[k].flag=true;
	                   int x=t[i-1].selforder.records[k].getPrice(false,0);
                   //2 油淋生菜 27
                //   System.out.println(dc1+" "+arr[1]+" "+x);
              
        		   }
        		   
        	   }
        	   k++;
           }
           a=in.nextLine();
       }
       for(int l=0;l<i;l++)
       {
           t[l].getTotalPrice();
       }
       Arrays.sort(p, 0, people.j, new cmp());
       for(int l=0;l<people.j;l++)
       {
           p[l].ttprice(t);
       }
   }
}
class cmp implements Comparator<people> {
	@Override
	public int compare(people a, people b) ///降序排序
	{
		return a.name.compareTo(b.name);
	}
} 
//菜品类:对应菜谱上一道菜的信息。
//String name="";//菜品名称
//int unit_price;菜品单价
//int getPrice(int portion)//计算一盘菜价格
class people
{
   static int j=0;
   String name="";
   String phone_number="";
   int[]tab=new int[5];
   int t=0;
   static int search(String name,String phone_number,people[]p)
   {
       int i=0;
       for(i=0;i<j;i++)
       {
           if(p[i].name.equals(name)&&p[i].phone_number.equals(phone_number))
           {
               return i;
           }
       }
       return i;
   }
   static int newpeople(String name,String phone_number,people[] p)
   {
       int i=search(name, phone_number, p);
       if(i==j)
       {
           p[i]=new people();
           p[i].name=name;
           p[i].phone_number=phone_number;
           j++;
       }
       return i;
   }
   void addtable(int i)
   {
       tab[t]=i;
       t++;
   }
   void ttprice(table[]tt)
   {
       int sum=0;
       for(int i=0;i<t;i++)
       {
           sum+=tt[tab[i]].truesum1;
       }
       //tom 13605054400 115
       System.out.println(name+" "+phone_number+" "+sum);
   }
}
class Dish
{
   String name="";//菜品名称
   int unit_price;//单价
   int 菜系;
   boolean special=false;
   int getPrice(int portion)
   {
       int 菜价=0;
       //单菜价格
       switch(portion)
       {
           case 1: 菜价=unit_price;break;
           case 2: 菜价=(int)Math.ceil(1.0*unit_price*3/2);break;
           case 3: 菜价=unit_price*2;break;
       }
       return 菜价;
   }
}
//菜谱类:对应菜谱,包含饭店提供的所有菜的信息。
//Dish[] dishs =new Dish[20];所有菜信息
//int i=0;//用于记录菜品总数
//Dish addDish(String dishName,int unit_price)//添加菜品 名称 价格
//Dish searthDish(String dishName,Menu c) //根据菜名在菜谱中查找菜品信息,返回Dish对象
class Menu
{
   Dish[] dishs =new Dish[20];
   int i=0;//用于记录菜品总数
   void addDish(String a)
   {
       String[] arr=a.split(" ");
       // Dish dish1=new Dish();
       if(arr.length==2)//普通菜
       {
           dishs[i]=new Dish();
           dishs[i].name=arr[0];
           dishs[i].unit_price=Integer.parseInt(arr[1]);
       }
       if(arr.length==4)//特色菜
       {
           dishs[i]=new Dish();
           dishs[i].special=true;
           dishs[i].name=arr[0];
           dishs[i].unit_price=Integer.parseInt(arr[2]);
           if(arr[1].equals("川菜"))
               dishs[i].菜系=1;
           else if(arr[1].equals("晋菜"))
               dishs[i].菜系=2;
           else if(arr[1].equals("浙菜"))
               dishs[i].菜系=3;
       }
       i++;
   }
   //菜品数组,保存所有菜品信息
   Dish searthDish(String dishName,int kw) //根据菜名在菜谱中查找菜品信息,返回Dish对象。
   {
       int j=0;
       int flag=0;
       for(j=i-1;j>=0;j--)//从未末端开始查找比对
       {
           //System.out.println(j);
           if(dishName.compareTo(dishs[j].name)==0)
           {
               flag=1;
               break;
           }
       }
       if(flag==1)
       {
    	   if(dishs[j].special)
    	   {
    		   if(dishs[j].菜系==1)
    		   {
    			   if(kw>5||kw<0)
    			   {
    				   //spicy/acidity/sweetness num out of range : "+口味度值
    				   System.out.println("spicy num out of range :"+kw);
    				   flag=0;
    				   return null;
    			   }
    		   }
    		   else if(dishs[j].菜系==2)
    		   {
    			   if(kw>4||kw<0)
    			   {
    				   System.out.println("acidity num out of range :"+kw);
    				   flag=0;
    				   return null;
    			   }
    		   }
    		   else if(dishs[j].菜系==3)
    		   {
    			   if(kw>3||kw<0)
    			   {
    				   System.out.println("sweetness num out of range :"+kw);
    				   flag=0;
    				   return null;
    			   }
    		   }
    	   }
       }
       if(flag==0)
       {
           //麻辣鸡丝 does not exist
           System.out.println(dishName+" does not exist");
           return null;
       }
       else
       {
    	   return dishs[j];
       }
      
   }
}
//点菜记录类:保存订单上的一道菜品记录
//Dish d=new Dish();//菜品
//int orderNum=0;//序号
//int portion=0;//份额(1/2/3代表小/中/大份)
//int num=0;//份数
//int getPrice()计算一种菜的价格
class Record
{
   Dish d=new Dish();//菜品
   int orderNum=0;//序号\
   int portion=0;
   int num=0;//份数
   int tablenum=0;//桌号//没什么用
   int kw=0;
   int price;
   boolean flag=false;
   //份额(1/2/3代表小/中/大份)
   boolean flag1=false;
//       int n=0;
//       n=num*d.getPrice(portion);
       //2 油淋生菜 27
	   int getPrice(boolean flag1,int t)//判断是否为带点菜,true为是,t表示自己桌号
	    {
	    	int n=0;
	    	if(d!=null)
	    	{
	    		n=num*d.getPrice(portion);
	        //2 油淋生菜 27
	    		price=n;
	    		if(flag1==false)
	    		{
	    			if(flag1==false)
	    			{
	    				System.out.println(orderNum+" "+d.name+" "+n);
	    			}
	    			else
	    			{
	    				System.out.println(orderNum+" table "+tablenum+" pay for table "+t+" "+n);
	    			}
	    			flag1=true;
	    		}
	    	}
	        return n;
	    }//计价,计算本条记录的价格
}
class Order
{
   Record[] records=new Record[20];
   int j=0;
   //保存订单上每一道的记录
//    int getTotalPrice()
//    //计算订单的总价
//    {
//        int sum=0,i=0;
//        for(i=0;i<j;i++)
//        {
//            sum+=records[i].getPrice();
//        }
//        return sum;
//    }
   //添加一条菜品信息到订单中。
   void addARecord(int tablenum,int orderNum,String dishName,int portion,int num,int kw)//添加一条菜品信息到订单中。
   {
       //1 麻婆豆腐 2 2
       records[j]=new Record();
       records[j].tablenum=tablenum;
       records[j].orderNum=orderNum;
       records[j].d.name=dishName;
       records[j].portion=portion;
       records[j].num=num;
       records[j].kw=kw;
       //System.out.println(num);
       j++;
       //     return r;
   }
   void delARecordByOrderNum(int orderNum,table[]t,int tablen)//根据序号删除一条记录
   //试试直接总价减去删除菜
   {
	   boolean flag=false;
       if(t[tablen].flag)
       {
           for(int i=0;i<t[tablen].selforder.j;i++)
           {
        	   if(t[tablen].selforder.records[i].orderNum==orderNum)
        	   {
        		   flag=true;
        		   t[tablen].selforder.records[i].num=0;
        	   }
           }
           
       }
       //不存在
       if(flag==false)
       {
           System.out.println("delete error;");
           
       }
   }
}
class table
{
   int tablenum;//桌号
   String time;//点菜时间
   int year=0,month=0,day=0,ww=0,hh=0,mm=0,ss=0;
   boolean flag=true;//判断时间是否正确
   double count=0;//折扣
   double specialcount=1;
   Order selforder=new Order();//本桌订单
   Order otherorder=new Order();
   //Order alothreorder=new Order();//代点订单
   //int sc=0;//删菜
   int sum=0;//计算折后总价
   int truesum1=0;
   double yuanjia=0;//计算原价
   int average=0;
   int Tc=0;//计算川菜总的口味度
   int Tj=0;//计算晋菜总的口味度
   int Tz=0;//计算浙菜总的口味度
   int s1=0;//计算川菜数量
   int s2=0;//计算晋菜数量
   int s3=0;//计算浙菜数量
   void input(String time)//预处理
   {
       String[] arr1=time.split(" ");
       tablenum=Integer.parseInt(arr1[1]);//桌号
       this.time=arr1[5]+" "+arr1[6];
       timechange();
       jscount();
       pdflag();
       if(flag)
       {
    	   System.out.println("table "+tablenum+": ");
       }
   }
   void getTotalPrice()//计算桌总价
   {
       if(flag)
       {
//           yuanjia=sum;
//           sum=(int)(sum*count+0.5);
//           System.out.print("table "+tablenum+": "+yuanjia+" "+sum+" "+"");
           //table 1: 63
    	 //  System.out.println(yuanjia);
    	 int i=0;
  	     for(i=0;i<selforder.j;i++)//自己的订单
  	     {
  	    	 
  	    	 if(selforder.records[i].d!=null)
  	    	 {
  	    		 int n=selforder.records[i].price;
  	    		 sum+=n;
  	    		// System.out.println(selforder.records[i].flag);
  	    		 if(selforder.records[i].d.special)
  	    		 {
  	    			 yuanjia+=(int)(n*specialcount*1.0+0.5);
//  	    			 if(selforder.records[i].tablenum==0)
//  	    			 {
  	    			 if(selforder.records[i].flag)
  	    			 {
  	    				if(selforder.records[i].d.菜系==1)
  	    			 	{   
  	    				 	s1+=selforder.records[i].num;
  	    				 	Tc+=selforder.records[i].kw*selforder.records[i].num;
  	    			 	}
  	    			 	else if(selforder.records[i].d.菜系==2)
  	    			 	{
  	    				 	s2+=selforder.records[i].num;
  	    				 	Tj+=selforder.records[i].kw*selforder.records[i].num;
  	    			 	}
  	    			 	else if(selforder.records[i].d.菜系==3)
  	    			 	{
  	    				 	s3+=selforder.records[i].num;
  	    				 	Tz+=selforder.records[i].kw*selforder.records[i].num;
  	    			 	}
  	    			 }
  	    			// }
  	    		 }
  	    		 else
  	    			 yuanjia+=(int)(n*count*1.0+0.5);
  	    	 }
  	     }
  	   //  System.out.println(otherorder.j);
  	    	 for(i=0;i<otherorder.j;i++)//别桌带自己点的订单
      	     {
  	    		 
      	    	 if(otherorder.records[i].d!=null)
      	    	 {
      	    		 if(otherorder.records[i].d.special)
      	    		 {
      	    			 if(otherorder.records[i].d.菜系==1)
      	    			 {   
      	    				 s1+=otherorder.records[i].num;
      	    				 Tc+=otherorder.records[i].kw*otherorder.records[i].num;
      	    			 }
      	    			 else if(otherorder.records[i].d.菜系==2)
      	    			 {
      	    				 
      	    				 s2+=otherorder.records[i].num;
      	    				 Tj+=otherorder.records[i].kw*otherorder.records[i].num;
      	    			 }
      	    			 else if(otherorder.records[i].d.菜系==3)
      	    			 {
      	    				 s3+=otherorder.records[i].num;
      	    				 Tz+=otherorder.records[i].kw*otherorder.records[i].num;
      	    			 }
      	    		 }
      	    	 }
  	     }
  	     truesum1=(int)(yuanjia+0.5);//四舍五入
  		System.out.print("table "+tablenum+": "+sum+" "+truesum1);
  		//System.out.print(s1+" "+s2+" "+s3);
  		if(s1==0&&s2==0&&s3==0)
  			System.out.print(" ");
  		if(s1!=0)
  		{
  			Tc=(int)(Tc*1.0/s1+0.5);
  			System.out.print(" 川菜 "+s1);
  			switch(Tc)
  			{
  				case 0:System.out.print(" 不辣");break;
  				case 1:System.out.print(" 微辣");break;
  				case 2:System.out.print(" 稍辣");break;
  				case 3:System.out.print(" 辣");break;
  				case 4:System.out.print(" 很辣");break;
  				case 5:System.out.print(" 爆辣");break;
  				
  			}
  		}
  		if(s2!=0)
  		{
  			Tj=(int)(Tj*1.0/s2+0.5);
  			System.out.print(" 晋菜 "+s2);
  			switch(Tj)
  			{
  				case 0:System.out.print(" 不酸");break;
  				case 1:System.out.print(" 微酸");break;
  				case 2:System.out.print(" 稍酸");break;
  				case 3:System.out.print(" 酸");break;
  				case 4:System.out.print(" 很酸");break;
  				
  			}
  		}
  		if(s3!=0)
  		{
  			Tz=(int)(Tz*1.0/s3+0.5);
  			System.out.print(" 浙菜 "+s3);
  			switch(Tz)
  			{
  				case 0:System.out.print(" 不甜");break;
  				case 1:System.out.print(" 微甜");break;
  				case 2:System.out.print(" 稍甜");break;
  				case 3:System.out.print(" 甜");	break;		
  			}
  		
  		}
  		System.out.print("\n");
       }
       else
           System.out.println("table "+tablenum+" out of opening hours");
   }
   void jscount()//运用时间计算折扣
   {
       if(ww>=1&&ww<=5)
       {
    	   specialcount=0.7;
           if(hh>=17&&hh<20)
               count=0.8;
           else if(hh==20&&mm<30)
               count=0.8;
           else if(hh==20&&mm==30&&ss==0)
               count=0.8;
           else if(hh>=11&&hh<=13||hh==10&&mm>=30)
               count=0.6;
           else if(hh==14&&mm<30)
               count=0.6;
           else if(hh==14&&mm==30&&ss==0)
               count=0.6;
       }
       else
       {
    	   specialcount=1;
           if(hh>=10&&hh<=20)
               count=1.0;
           else if(hh==9&&mm>=30)
               count=1.0;
           else if(hh==21&&mm<30||hh==21&&mm==30&&ss==0)
               count=1.0;
       }
//   		折扣的计算方法(注:以下时间段均按闭区间计算):
//
//   		周一至周五营业时间与折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余时间不营业。
//
//   		周末全价,营业时间:9:30-21:00
//
//   		如果下单时间不在营业范围内,输出"table " + t.tableNum + " out of opening hours"
   }
   void pdflag()//判断时间是否正确
   {
       if(count==0)
           flag=false;
       else
           flag=true;
   }
   void timechange()//时间转换
   {
       //table 1 2023/3/22 12/2/3
       String[] arr1=time.split(" ");
       String[] arr2=arr1[0].split("/");
       String[] arr3=arr1[1].split("/");
       year=Integer.parseInt(arr2[0]);//时间
       month=Integer.parseInt(arr2[1]);
       day=Integer.parseInt(arr2[2]);
       Calendar c = Calendar.getInstance();
       c.set(year,month-1,day);//设置时间
       ww=c.get(Calendar.DAY_OF_WEEK);
       hh=Integer.parseInt(arr3[0]);//时间
       mm=Integer.parseInt(arr3[1]);
       ss=Integer.parseInt(arr3[2]);
       if(ww==1)
           ww=7;
       else
           ww--;
   }
}


三、踩坑心得
(1)在7-1 菜单计价程序-4中,由于程序算法中循环结构过多,导致很多测试点报非零返回的错误,后来我更改了算法结构减少了循环结构的使用,很多的测试点非零返回的错误又不报错


四、主要困难及改进建议
在解题过程中我会由于对知识点的不熟悉导致无从下手,得反复翻看Java书本内容才能上手,尤其是7-1 菜单计价程序-4、7-1 菜单计价程序-5,在类与类之间属性与方法之间的相互调用会让我弄不清,例如:
class Menu{
Dish[] dishs = new Dish(); //菜品数组,保存所有菜品信息 int t = 0;//统计录入的菜的数量 Dish searthDish(String dishName){//根据菜名在菜谱中查找菜品信息,返回Dish对象。
int flag = 0;
int i;
for(i=t-1;i>=0;i--){
if(dishName.equals(dishs[i].name)==true){
flag = 1;
break;
}
}
if(flag==1)
return dishs[i];
else
{
return null;
}
}
Dish addDish(String dishName,int unit_price)
{//添加一道菜品信息
//int j=0;
Dish dish1 =new Dish();
dish1.name = dishName;
dish1.unit_price = unit_price;
t++;
return dish1;
}
}
在Menu类中还有Dish类的对象,并且该菜品对象还是用数组来表示更让我头疼,我认为还是我对Java本质面向对象性了解的不深入,无法灵活地理清楚各个类之间的关系,每个类中属性、方法存在的意义。
改进建议:我们可以直接在Menu类中创建Dish[] dishs = new Dish[10];再int i=0;这样每个菜dish中的属性方法都储存于Dish[10]数组中,用i++来录入。
五、总结
Java 中的继承、抽象类和接口都是面向对象编程中非常重要的概念,以下是我对它们的总结和学习心得:
1. 继承:继承是指一个类从另一个类继承(拥有)其属性和方法。继承是实现类之间的 IS-A 关系,使得代码的复用性更高。继承可以通过 `extends` 关键字来实现。在继承的过程中,子类可以通过覆盖或重写方法来改变父类的行为。
学习心得:继承是 Java 中的一个重要概念,熟练掌握继承关系可以提高代码的重用性和可维护性。在实际编程中,需要谨慎设计类之间的继承关系,遵循开闭原则等设计原则。
2. 抽象类:抽象类是一种通过声明抽象方法和具体方法来定义接口和形式的类。抽象类不能被实例化,只能作为子类的基类存在。抽象类可以通过 `abstract` 关键字来定义。抽象类强制子类必须提供实现的方法,从而使得具体子类能够符合规范,更加可靠。
学习心得:抽象类在代码重用和约束子类方面非常有用,可以明确子类必须要实现的方法接口。在实际开发中,需要注意抽象类和接口的使用场景区别,避免不当使用。
3. 接口:接口是一种具有一组抽象方法和常量的数据类型,可以看作是一个协议或规范。接口提供了一种与实现独立的一致性方式,允许不同的类实现相同的方法,实现了类之间的行为规范,使得程序的可扩展性更强。接口可以通过 `interface` 关键字来定义,实现类可以通过 `implements` 关键字来实现接口。
学习心得:接口提供了一种非常灵活的方式来定义行为的规范,适用于一些需要保证不同类的一致性但具体实现不同的场景。在实际开发中,需要设计合理的接口,避免不必要的继承和抽象类的使用。
总之,继承、抽象类和接口都是 Java 中重要且基础的概念。深入理解这些概念的含义和使用场景,可以更好地编写出高质量的Java程序。
 
                    
                 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号