首页 > 其他分享 >PTA4-5及期中总结

PTA4-5及期中总结

时间:2023-06-29 18:56:28浏览次数:48  
标签:总结 return Point double topLeftPoint 期中 PTA4 public lowRightPoint

(1)前言

  期中考试的题目集总体来说还是很简单的,题目量比较少而且难度偏易,考察的知识点可以说是很基础的面向对象编程知识点,基本上就是在考验我们的基本功扎不扎实,对于知识点很熟悉的同学可以很快的完成大部分题目,但是还有个接口题目,需要使用java自带类,这题先前没有遇到过就会做起来有困难。

  第4次作业,题量只有一题,但是复杂程度还是比较大的,这是相比于前面的菜单计价程序-3的进阶版,增加了很多异常处理以及特色菜这些新功能,难度相较之前有明显的增加,有很多异常情况需要进行判断处理,相对来说挺麻烦的

  第5次题目集,题量不多,但是单个题目耗费的精力还是挺大的,同样是菜单计价程序-3的不同进阶版,相比于之前增加了口味度和特色菜以及折扣计算和代点菜,难度相较来说也是增加不少,要在原来的基础上进行较大的改动,首先考验的就是我们的思维能力,实现这些功能并不简单,需要花费许多精力去构思。还是很有难度的。

(2)设计与分析

  首先对期中考试题目集进行设计分析,首先看到第一个题目,就是一个简单的设计一个java类然后创建对象使用它,考察的就是我们对基本知识的掌握,所以我严格按照一个javabean类的要求设计一个circle 同时将属性按照题目要求私有化,然后通过方法调用属性,以下是代码:

 

import java.util.*;
public class Main {
    public static void main(String[] args){
        Scanner sc= new Scanner(System.in);
        if (sc.hasNextDouble()) {
            double radius = sc.nextDouble();
            if (radius > 0) {
                Circle circle = new Circle();
                circle.setR(radius);
                double area = circle.getArea();
                System.out.println(String.format("%.2f", area));
            } else {
                System.out.println("Wrong Format");
            }
        } else {
            System.out.println("Wrong Format");
        }
    }
}
class Circle{
    private double r;

    public Circle() {
    }

    public Circle(float r) {
        this.r = r;
    }

    public double getR() {
        return r;
    }

    public void setR(double r) {
        this.r = r;
    }
    public double getArea()
    {
        return r*r*Math.PI;
    }
}

 

下面是对代码进行分析:

  1. main方法中,首先通过Scanner对象获取用户输入的半径值。
  2. 然后进行判断,如果用户输入的值是一个有效的双精度浮点数(hasNextDouble()方法返回true),则继续执行。
  3. 在有效输入的情况下,检查输入的半径是否大于0。如果是,则创建一个Circle对象并设置半径值。
  4. 使用getArea方法计算圆的面积,并将结果保留两位小数输出。
  5. 如果输入的值不是有效的双精度浮点数或者半径小于等于0,则输出"Wrong Format"。

 

然后是第二题,同样是对java类的编写,这里编写两个类,其中rectangle里的属性是另一个类point的对象,接下来是代码:

 

import java.util.*;
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        double x1= sc.nextDouble();
        double y1=sc.nextDouble();
        double x2= sc.nextDouble();
        double y2=sc.nextDouble();
            Point topLeftPoint = new Point(x1, y1);
            Point lowRightPoint = new Point(x2, y2);
            Rectangle rectangle = new Rectangle(topLeftPoint, lowRightPoint);
            System.out.println(String.format("%.2f", rectangle.getArea()));
    }

}
class Point{
    double x;
    double y;

    public Point() {
    }
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public void setX(double x) {
        this.x = x;
    }

    public void setY(double y) {
        this.y = y;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }
}
class Rectangle{
    Point topLeftPoint;
    Point lowRightPoint;

    public Rectangle() {
    }

    public Rectangle(Point topLeftPoint, Point lowRightPoint) {
        this.topLeftPoint = topLeftPoint;
        this.lowRightPoint = lowRightPoint;
    }

    public void setTopLeftPoint(Point topLeftPoint) {
        this.topLeftPoint = topLeftPoint;
    }

    public void setLowRightPoint(Point lowRightPoint) {
        this.lowRightPoint = lowRightPoint;
    }

    public Point getTopLeftPoint() {
        return topLeftPoint;
    }

    public Point getLowRightPoint() {
        return lowRightPoint;
    }
    public double getLength()
    {
        return Math.abs(lowRightPoint.getX()-topLeftPoint.getX());
    }
    public double getHeight(){
        return Math.abs(topLeftPoint.getY()-lowRightPoint.getY());
    }
    public double getArea(){
        return getHeight()*getLength();
    }
}

 

这段代码是一个计算矩形面积的程序。下面是对代码进行分析:

  1. main方法中通过Scanner对象获取用户输入的四个坐标值:矩形左上角点的x坐标、y坐标,以及矩形右下角点的x坐标、y坐标。
  2. 通过这四个坐标值创建两个Point对象,分别表示矩形的左上角和右下角。
  3. 使用这两个Point对象创建一个Rectangle对象。
  4. 调用getArea方法计算矩形的面积,并使用String.format将结果保留两位小数输出。

 

然后是题目3,这里考察的则是面向对象里的继承和多态,题目要求设计一个抽象类shape类然后让前面两个题目中的类来继承,同时使用多态来对方法进行调用,以下为代码

import java.util.*;
public class Main {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        int choice = input.nextInt();
        switch(choice) {
            case 1://Circle
                if (input.hasNextDouble()) {
                    double radius = input.nextDouble();
                    if (radius > 0) {
                        Circle circle = new Circle();
                        circle.setR(radius);
                        double area = circle.getArea();
                        System.out.println(String.format("%.2f", area));
                    } else {
                        System.out.println("Wrong Format");
                    }
                } else {
                    System.out.println("Wrong Format");
                }
                break;
            case 2://Rectangle
                double x1 = input.nextDouble();
                double y1 = input.nextDouble();
                double x2 = input.nextDouble();
                double y2 = input.nextDouble();

                Point leftTopPoint = new Point(x1,y1);
                Point lowerRightPoint = new Point(x2,y2);

                Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);

                printArea(rectangle);
                break;
        }
    }
    public static void printArea(Shape shape){
        System.out.println(String.format("%.2f",shape.getArea()));
    }
}
abstract class Shape{
    public Shape(){

    }
    abstract double getArea();
}
class Circle extends Shape{
    private double r;

    public Circle() {
    }

    public Circle(float r) {
        this.r = r;
    }

    public double getR() {
        return r;
    }

    public void setR(double r) {
        this.r = r;
    }
    public double getArea()
    {
        return r*r*Math.PI;
    }
}
class Point{
    double x;
    double y;

    public Point() {
    }
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public void setX(double x) {
        this.x = x;
    }

    public void setY(double y) {
        this.y = y;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }
}
class Rectangle extends Shape{
    Point topLeftPoint;
    Point lowRightPoint;

    public Rectangle() {
    }

    public Rectangle(Point topLeftPoint, Point lowRightPoint) {
        this.topLeftPoint = topLeftPoint;
        this.lowRightPoint = lowRightPoint;
    }

    public void setTopLeftPoint(Point topLeftPoint) {
        this.topLeftPoint = topLeftPoint;
    }

    public void setLowRightPoint(Point lowRightPoint) {
        this.lowRightPoint = lowRightPoint;
    }

    public Point getTopLeftPoint() {
        return topLeftPoint;
    }

    public Point getLowRightPoint() {
        return lowRightPoint;
    }
    public double getLength()
    {
        return Math.abs(lowRightPoint.getX()-topLeftPoint.getX());
    }
    public double getHeight(){
        return Math.abs(topLeftPoint.getY()-lowRightPoint.getY());
    }
    public double getArea(){
        return getHeight()*getLength();
    }
}

 

下面对代码进行分析:

  1. main方法中,通过Scanner对象获取用户输入的选择(choice),根据选择执行相应的操作。
  2. 如果选择为1,则表示计算圆形的面积。代码首先检查输入是否为有效的double类型数据,然后创建一个Circle对象,设置半径(r)并计算面积。最后使用String.format方法格式化输出面积。
  3. 如果选择为2,则表示计算矩形的面积。代码依次获取用户输入的四个坐标(x1, y1, x2, y2),并使用这些坐标创建两个Point对象,分别表示矩形的左上角和右下角。然后使用这两个点创建一个Rectangle对象,并调用printArea方法输出面积。
  4. Shape是一个抽象类,代表图形,其中包含一个抽象方法getArea
  5. Circle类继承自Shape类,表示圆形,具有半径(r)属性,并实现了getArea方法用于计算圆形的面积。
  6. Point类表示一个点,具有x坐标和y坐标属性。
  7. Rectangle类继承自Shape类,表示矩形,包含左上角和右下角两个点属性,并实现了getArea方法用于计算矩形的面积。

接下来是对题目4进行设计分析,这一题相比第3题多了一个排序,但是按照题目要求是需要使用一个java自带接口去实现,所以如果没有查找过资料的话,只能自己用冒泡或者选择排序法进行排序,以下为代码:

 

import java.util.*;
public class Main {
public static void main(String[] args) {
       // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        ArrayList<Shape> list = new ArrayList<>();
        int choice = input.nextInt();
        while(choice != 0) {
            switch(choice) {
                case 1://Circle
                    double radiums = input.nextDouble();
                    Shape circle = new Circle((float) radiums);
                    list.add(circle);
                    break;
                case 2://Rectangle
                    double x1 = input.nextDouble();
                    double y1 = input.nextDouble();
                    double x2 = input.nextDouble();
                    double y2 = input.nextDouble();
                    Point leftTopPoint = new Point(x1,y1);
                    Point lowerRightPoint = new Point(x2,y2);
                    Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);
                    list.add(rectangle);
                    break;
            }
            choice = input.nextInt();
        }
        list.sort(Comparator.naturalOrder());//正向排序
        for(int i = 0; i < list.size(); i++) {
            System.out.print(String.format("%.2f", list.get(i).getArea()) + " ");
        }
    }
}
abstract class Shape implements Comparable<Shape>{
    public Shape(){
    }
    abstract double getArea();
    public int compareTo(Shape other) {
        return Double.compare(this.getArea(), other.getArea());
    }
}
class Circle extends Shape{
    private double r;
    public Circle() {
    }
    public Circle(float r) {
        this.r = r;
    }
    public double getR() {
        return r;
    }
    public void setR(double r) {
        this.r = r;
    }
    public double getArea()
    {
        return r*r*Math.PI;
    }
}
class Point{
    double x;
    double y;
    public Point() {
    }
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
    public void setX(double x) {
        this.x = x;
    }
    public void setY(double y) {
        this.y = y;
    }
    public double getX() {
        return x;
    }
    public double getY() {
        return y;
    }
}
class Rectangle extends Shape{
    Point topLeftPoint;
    Point lowRightPoint;
    public Rectangle() {
    }
    public Rectangle(Point topLeftPoint, Point lowRightPoint) {
        this.topLeftPoint = topLeftPoint;
        this.lowRightPoint = lowRightPoint;
    }
    public void setTopLeftPoint(Point topLeftPoint) {
        this.topLeftPoint = topLeftPoint;
    }
    public void setLowRightPoint(Point lowRightPoint) {
        this.lowRightPoint = lowRightPoint;
    }
    public Point getTopLeftPoint() {
        return topLeftPoint;
    }
    public Point getLowRightPoint() {
        return lowRightPoint;
    }
    public double getLength()
    {
        return Math.abs(lowRightPoint.getX()-topLeftPoint.getX());
    }
    public double getHeight(){
        return Math.abs(topLeftPoint.getY()-lowRightPoint.getY());
    }
    public double getArea(){
        return getHeight()*getLength();
    }
}

 

下面对代码进行分析:

  1. main方法中,通过Scanner对象获取用户输入的选择(choice),根据选择执行相应的操作。
  2. 如果选择为1,则表示计算圆形的面积。代码首先获取用户输入的半径(radius),然后创建一个Circle对象,并将半径设置为用户输入的值。接着将该对象添加到list集合中。
  3. 如果选择为2,则表示计算矩形的面积。代码依次获取用户输入的四个坐标(x1, y1, x2, y2),并使用这些坐标创建两个Point对象,分别表示矩形的左上角和右下角。然后使用这两个点创建一个Rectangle对象,并将该对象添加到list集合中。
  4. Shape是一个抽象类,代表图形,其中包含一个抽象方法getArea用于计算图形的面积。该类还实现了Comparable接口,重写了compareTo方法,用于对图形进行排序。
  5. Circle类继承自Shape类,表示圆形,具有半径(r)属性,并实现了getArea方法用于计算圆形的面积。
  6. Point类表示一个点,具有x坐标和y坐标属性。
  7. Rectangle类继承自Shape类,表示矩形,包含左上角和右下角两个点属性,并实现了getArea方法用于计算矩形的面积。

 

接下来就是对菜单计价程序进行设计与分析,由于本人能力有限所以菜单计价程序-4未能在规定时间内将代码完成,导致最后测试点都没过,只能直接输出“Wrong Format”来通过一些测试点,代码如下:

public class Main{
    public static void main(String [] args)
    {
        System.out.println("wrong format");
    }
}

对于菜单计价程序-5的设计,首先按照题目要求,相比于-3这里多了用户和各个菜系以及口味度,有部分类可以承接之前的-3代码中的类可以继续使用,但是还是有许多地方需要进行修改,如我添加了一个custom类用来记录用户消费,然后在table类里面做了很大的修改,以及点菜记录record里也进行了对菜系口味度的修改,最后的主函数main则按照相应的输入要求重新进行编写,根据输入的字符串不同的长度以及一些特定的标识来判断输入的是什么信息,然后进入相应入口进行操作创建相应的对象,将数据存入,最后计算得到结果。以下为代码:

import java.time.*;
import java.util.Scanner;
public class Main {
    public static void main(String [] args)
    {
        Scanner sc=new Scanner(System.in);
        Menu menu=new Menu();
        Table[] table=new Table[10];
        Customer[] customer=new Customer[10];
        String data=sc.nextLine();
        int j=0;
        int r=0;
        while (!data.equals("end"))
        {
            String[] fen=data.split(" ");
            if(fen.length==4&&fen[3].equals("T"))
            {
                menu.addDish(fen[0],fen[1],Integer.parseInt(fen[2]),true);
            } else if(fen.length==2&&fen[1].equals("delete")==false)
            {
                menu.addDish(fen[0],null,Integer.parseInt(fen[1]),false);
            } else if(fen.length==3&&fen[2].equals("T")==true)
            {
                System.out.println("wrong format");
            }
            if(fen.length==7&&fen[0].equals("table")&&judgeValidName(fen[3])&&judgeValidNumber(fen[4]))
            {
                j++;
                table[j]=new Table();
                customer[r]=new Customer();
                table[j].order=new Order();
                table[j].time=new Time();
                table[j].num=Integer.parseInt(fen[1]);
                table[j].customer=fen[3];
                table[j].phoneNumber=fen[4];
                table[j].time.time1=fen[5];
                table[j].time.time2=fen[6];
                if(r==0)
                {

                    customer[r].name=fen[3];
                    customer[r].phoneNumber=fen[4];
                    customer[r].addTable(table[j]);
                    r++;
                } else
                {
                    int c=0;
                    for(int g=0;g<r;g++)
                    {
                        if(fen[3].equals(customer[g].name))
                        {
                            customer[g].addTable(table[j]);
                            c=1;
                        }
                    }
                    if(c==0)
                    {
                        customer[r].name=fen[3];
                        customer[r].phoneNumber=fen[4];
                        customer[r].addTable(table[j]);
                        r++;
                    }
                }
                if(judgeIsWork(table[j].time)==false)
                {
                    System.out.println("table "+table[j].num+" out of opening hours");
                    System.exit(0);
                }
                System.out.println("table "+Integer.parseInt(fen[1])+": ");
            } else if(fen.length==7&&fen[0].equals("table")&&(!judgeValidName(fen[3])||!judgeValidNumber(fen[4])))
            {
                System.out.println("wrong format");
                System.exit(0);
            }
            if(fen.length==5&&menu.searthDish(fen[1])!=null)
            {
                table[j].order.addARecord(Integer.parseInt(fen[0]),fen[1],Integer.parseInt(fen[3]),Integer.parseInt(fen[4]),Integer.parseInt(fen[2]));
                Record record=new Record();
                record=table[j].order.findRecordByNum(Integer.parseInt(fen[0]));
                record.d=menu.searthDish(fen[1]);
                if(record.judgeTaste()==false)
                {
                    if(record.d.typedef.equals("川菜"))
                    {
                        System.out.println("spicy num out of range :"+fen[2]);
                    }
                    if(record.d.typedef.equals("晋菜"))
                    {
                        System.out.println("acidity num out of range :"+fen[2]);
                    }
                    if(record.d.typedef.equals("浙菜"))
                    {
                        System.out.println("sweetness num out of range :"+fen[2]);
                    }
                    table[j].order.delARecordByOrderNum(Integer.parseInt(fen[0]));
                } else
                {
                    System.out.println(fen[0]+" "+fen[1]+" "+(int)record.d.getPrice(Integer.parseInt(fen[3]))*Integer.parseInt(fen[4]));
                }
                if(record.d.special)
                {
                    if(record.judgeTaste()==true)
                    {
                        if (record.d.typedef.equals("川菜")) {
                            table[j].order.chuanNum += record.num;
                            table[j].order.chuanTotalNum += record.num * record.taste;
                        }
                        if (record.d.typedef.equals("晋菜")) {
                            table[j].order.jinNum += record.num;
                            table[j].order.JinTotalNum += record.num * record.taste;
                        }
                        if (record.d.typedef.equals("浙菜")) {
                            table[j].order.zheNum += record.num;
                            table[j].order.zheTotalNum += record.num * record.taste;
                        }
                    }
                }
            } else if(fen.length==4&&menu.searthDish(fen[1])!=null&&fen[3].equals("T")==false)
            {
                table[j].order.addARecord(Integer.parseInt(fen[0]),fen[1],Integer.parseInt(fen[2]),Integer.parseInt(fen[3]));
                Record record=new Record();
                record=table[j].order.findRecordByNum(Integer.parseInt(fen[0]));
                record.d=menu.searthDish(fen[1]);
                System.out.println(fen[0]+" "+fen[1]+" "+(int)record.d.getPrice(Integer.parseInt(fen[2]))*Integer.parseInt(fen[3]));
            } else if(fen.length==5&&menu.searthDish(fen[1])==null)
            {
                System.out.println(fen[1]+" does not exist");
            } else if(fen.length==4&&menu.searthDish(fen[1])==null&&!fen[3].equals("T"))
            {
                System.out.println(fen[1]+" does not exist");
            }
            if(fen.length==2&&fen[1].equals("delete"))
            {
                if(table[j].order.findRecordByNum(Integer.parseInt(fen[0]))!=null)
                {
                    table[j].order.delARecordByOrderNum(Integer.parseInt(fen[0]));
                } else
                {
                    System.out.println("delete error;");
                }
            }
            data=sc.nextLine();
        }
        for(int a=1;a<=j;a++) {
            table[a].getPrice();
        }
        for(int number=0;number<r;number++) {
            int price=0;
            for(int b=0;b<customer[number].tableIndex;b++) {
                price+=customer[number].tables[b].getPrice1();
            }
            System.out.println(customer[number].name+" "+customer[number].phoneNumber+" "+price);
        }
        sc.close();
    }

    public static boolean judgeValidName(String str) {
        if(str.length()<=10&&str.length()>=0)
        {
            return true;
        }
        return false;
    }
    public static boolean judgeValidNumber(String str) {

        if(str.length()==11&&(str.substring(0,3).equals("180")||str.substring(0,3).equals("181")||str.substring(0,3).equals("189")||str.substring(0,3).equals("133")||str.substring(0,3).equals("135")||str.substring(0,3).equals("136")))
        {
            return true;
        }
        return false;
    }
    public static boolean judgeIsWork(Time time1) {
        time1.getDay();
        time1.getYear();
        time1.getWeekday();
        if(time1.weekday<=5&&time1.weekday>=1) {
            if((time1.hour>=17&&time1.hour<20)||(time1.hour==20&&time1.minute<=30)) {
                return true;
            } else if((time1.hour==10&&time1.minute>=30)||(time1.hour>=11&&time1.hour<14)||(time1.hour==14&&time1.minute<=30))
            {
                return true;
            } else return false;
        }
        if(time1.weekday==6||time1.weekday==7) {
            if((time1.hour==9&&time1.minute>=30)||(time1.hour>9&&time1.hour<21)||(time1.hour==21&&time1.minute<=30)) {
                return true;
            } else return false;
        }
        return true;
    }
}
class Dish{
    String name;
    boolean special=false;
    int unit_price;
    String typedef;
    Dish() {
    }
    Dish(String name,int price, String typedef,boolean special) {
        this.name=name;
        this.unit_price = price;
        this.special=special;
        this.typedef=typedef;
    }
    float getPrice (int portion) {
        float b1[]={1,1.5f,2};
        return Math.round(unit_price*b1[portion-1]);
    }
}
class Menu {
    public static Dish[] dishs =new Dish[20];//菜品数组,保存所有菜品信息
    static int x=0;
    int m=0;
    Dish searthDish(String dishName)
    {
        if(dishs==null)
            return null;
        for(int r=0;r<Menu.x;r++)
        {
            if(dishs[r].name.equals(dishName))
                return dishs[r];
        }
        return null;
    }
    public void addDish(String dishName,String typedef,int unit_price,boolean special)
    {
        m=1;
        for(int j=0;j<x;j++)
        {
            if(dishs[j].name.equals(dishName))
            {
                dishs[j].unit_price=unit_price;
                m=0;
                x--;
                break;
            }
        }
        if(dishs[x]==null&&m==1)
        {
            dishs[x]=new Dish();
            dishs[x].name=dishName;
            dishs[x].unit_price=unit_price;
            dishs[x].typedef=typedef;
            dishs[x].special=special;
            x++;
        }
    }
}
class Record {
    int orderNum;//序号
    int num; //份数
    int delete=1;
    Dish d=new Dish();//菜品
    int portion;//份额(1/2/3代表小/中/大份)\\
    int table;
    int taste;
    boolean judgeTaste()
    {
        if(this.d.typedef.equals("川菜"))
        {
            if(this.taste<6)
                return true;
            else return false;
        }
        if(this.d.typedef.equals("晋菜"))
        {
            if(this.taste<5)
                return true;
            else return false;
        }
        if(this.d.typedef.equals("浙菜"))
        {
            if(this.taste<4)
                return true;
            else return false;
        }
        return false;
    }
    int getPrice() {
        double price;
        price=d.getPrice(portion)*num*delete;
        return (int)Math.round(price);
    }
}
class Order{
    public static Record [] records=new Record[30];
    static int i=0;
    int chuanNum=0;
    int chuanTotalNum=0;
    int jinNum=0;
    int JinTotalNum=0;
    int zheNum=0;
    int zheTotalNum=0;
    Record addARecord(int orderNum,String dishName,int portion,int num,int taste) {
        records[i]=new Record();
        records[i].d=new Dish();
        this.records[i].orderNum = orderNum;
        this.records[i].d.name=dishName;
        this.records[i].portion=portion;
        this.records[i].num=num;
        this.records[i].taste=taste;
        this.i++;
        return null;
    }
    Record addARecord(int orderNum,String dishName,int portion,int num) {
        records[i]=new Record();
        records[i].d=new Dish();
        this.records[i].orderNum = orderNum;
        this.records[i].d.name=dishName;
        this.records[i].portion=portion;
        this.records[i].num=num;
        this.i++;
        return null;
    }
    void delARecordByOrderNum(int orderNum)//根据序号删除一条记录
     {
        int m=0;
        for(int j=0;j<i;j++)
        {
            if(records[j].orderNum==orderNum)
            {
                records[j].delete=0;
                m=1;
            }
        }
        if(m==0)
            System.out.println("delete error;");
    }
    Record findRecordByNum(int orderNum)
    {
        if(orderNum>0&&orderNum<=i)
        {
            for (int temp = 0; temp <= i; temp++)
            {
                if(records[temp].delete==1)
                    if (records[temp].orderNum == orderNum)
                        return records[temp];
            }
        }
        return null;
    }
    int getSpecialPrice()
    {
        int k,s=0;
        for(k=0;k<i;k++)
        {
            if(records[k].delete==1)
            {
                if(records[k].d.special)
                    s+=records[k].getPrice();
            }
        }
        return  s;
    }
    int getNormalPrice()
    {
        int k,s=0;
        for(k=0;k<i;k++)
        {
            if(records[k].delete==1)
            {
                if(!records[k].d.special)
                    s+=records[k].getPrice();
            }
        }
        return  s;
    }

}
class Time{
    String time1;
    String time2;
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int weekday;
    void getWeekday()
    {
        this.weekday=LocalDateTime.of(this.year,this.month,this.day,this.hour,this.minute).getDayOfWeek().getValue();
    }
    void getYear()
    {
        String [] k=time1.split("\\/");
        year=Integer.parseInt(k[0]);
        month=Integer.parseInt(k[1]);
        day=Integer.parseInt(k[2]);
    }
    void getDay()
    {
        String[] k = time2.split("\\/");
        hour=Integer.parseInt(k[0]);
        minute=Integer.parseInt(k[1]);
    }
}
class Table{
    String customer;
    String phoneNumber;
    int num;
    Time time=new Time();
    Order order=new Order();
    int tablePrice;
    int orderNum=0;

    String getChuanTaste()
    {
        int temp=(int)Math.round(1.0*order.chuanTotalNum/order.chuanNum);
        if(temp==0)
            return "不辣";
        else if(temp==1)
            return "微辣";
        else if(temp==2)
            return "稍辣";
        else if(temp==3)
            return "辣";
        else if(temp==4)
            return "很辣";
        else if(temp==5)
            return "爆辣";
        return null;
    }
    String getJinTaste()
    {
        int temp=(int)Math.round(1.0*order.JinTotalNum/order.jinNum);
        if(temp==0)
            return "不酸";
        else if(temp==1)
            return "微酸";
        else if(temp==2)
            return "稍酸";
        else if(temp==3)
            return "酸";
        else if(temp==4)
            return "很酸";
        return null;
    }
    String getZheTaste()
    {
        int temp=(int)Math.round(1.0*order.zheTotalNum/order.zheNum);
        if (temp==0)
            return "不甜";
        else if(temp==1)
            return "微甜";
        else if(temp==2)
            return "稍甜";
        else if(temp==3)
            return "甜";
        return null;
    }
    void getPrice()
    {
        time.getDay();
        time.getYear();
        time.getWeekday();
        if(time.weekday<=5&&time.weekday>=1)
        {
            if((time.hour>=17&&time.hour<20)||(time.hour==20&&time.minute<=30))
            {
                int oriTablePrice=(int)Math.round(order.getSpecialPrice()+order.getNormalPrice());
                tablePrice=(int)Math.round(order.getNormalPrice()*0.8+order.getSpecialPrice()*0.7);
                System.out.print("table "+this.num+": "+oriTablePrice+" "+this.tablePrice+" ");
                if(order.chuanNum!=0)
                {
                    System.out.println("川菜"+" "+order.chuanNum+" "+this.getChuanTaste());
                }
                if(order.jinNum!=0)
                {
                    System.out.println("晋菜"+" "+order.jinNum+" "+this.getJinTaste());
                }
                if(order.zheNum!=0)
                {
                    System.out.println("浙菜"+" "+order.zheNum+" "+this.getZheTaste());
                }
            }
            else if((time.hour==10&&time.minute>=30)||(time.hour>=11&&time.hour<14)||(time.hour==14&&time.minute<=30))
            {
                int oriTablePrice=(int)Math.round(order.getSpecialPrice()+order.getNormalPrice());
                tablePrice=(int)Math.round(order.getNormalPrice()*0.6+order.getSpecialPrice()*0.7);
                System.out.print("table "+this.num+": "+oriTablePrice+" "+this.tablePrice+" ");
                if(order.chuanNum!=0)
                {
                    System.out.println("川菜"+" "+order.chuanNum+" "+getChuanTaste());
                }
                if(order.jinNum!=0)
                {
                    System.out.println("晋菜"+" "+order.jinNum+" "+getJinTaste());
                }
                if(order.zheNum!=0)
                {
                    System.out.println("浙菜"+" "+order.zheNum+" "+getZheTaste());
                }
            }
        }
        if(time.weekday==6||time.weekday==7)
        {
            if((time.hour==9&&time.minute>=30)||(time.hour>9&&time.hour<21)||(time.hour==21&&time.minute<=30))
            {
                tablePrice=(int)Math.round(order.getSpecialPrice()+order.getNormalPrice());
                System.out.print("table "+this.num+": "+this.tablePrice+" ");
                if(order.chuanNum!=0)
                {
                    System.out.println("川菜"+" "+" "+getChuanTaste());
                }
                if(order.jinNum!=0)
                {
                    System.out.println("晋菜"+" "+order.jinNum+" "+getJinTaste());
                }
                if(order.zheNum!=0)
                {
                    System.out.println("浙菜"+" "+order.zheNum+" "+getZheTaste());
                }
            }
        }
    }
    int getPrice1()
    {
        time.getDay();
        time.getYear();
        time.getWeekday();
        if(time.weekday<=5&&time.weekday>=1)
        {
            if((time.hour>=17&&time.hour<20)||(time.hour==20&&time.minute<=30))
            {
                int oriTablePrice=(int)Math.round(order.getSpecialPrice()+order.getNormalPrice());
                tablePrice=(int)Math.round(order.getNormalPrice()*0.8+order.getSpecialPrice()*0.7);
                return this.tablePrice;
            }
            else if((time.hour==10&&time.minute>=30)||(time.hour>=11&&time.hour<14)||(time.hour==14&&time.minute<=30))
            {
                int oriTablePrice=(int)Math.round(order.getSpecialPrice()+order.getNormalPrice());
                tablePrice=(int)Math.round(order.getNormalPrice()*0.6+order.getSpecialPrice()*0.7);
                return this.tablePrice;
            }
        }
        if(time.weekday==6||time.weekday==7)
        {
            if((time.hour==9&&time.minute>=30)||(time.hour>9&&time.hour<21)||(time.hour==21&&time.minute<=30))
            {
                tablePrice=(int)Math.round(order.getSpecialPrice()+order.getNormalPrice());
                return this.tablePrice;
            }
        }
        return this.tablePrice;
    }
}
class Customer
{
    String name;
    String phoneNumber;
    Table[] tables=new Table[20];
    static int tableIndex =0;
    public void addTable(Table t)
    {
        tables[tableIndex] = t;
        tableIndex++;
    }
}

以下为代码类图:

 接下来进行分析

它通过命令行输入来获取用户的点餐和服务操作,并将数据保存到相应的类对象中。

该程序使用了以下类和对象:

  • Menu:表示菜单,包含了菜品信息和添加菜品的方法。
  • Table:表示餐桌,包含了餐桌号、顾客信息、订单信息和时间信息等。
  • Customer:表示顾客,包含了顾客姓名、手机号和所订餐桌的信息等。
  • Order:表示订单,包含了点餐记录和计算价格的方法。
  • Time:表示时间,包含了用于判断是否在营业时间内的方法。
  • Record:表示点餐记录,包含了菜品信息和数量等。

下面是对主程序的执行流程进行分析:

  1. 创建一个菜单对象 menu、餐桌数组 table 和顾客数组 customer
  2. 通过 Scanner 对象获取用户的输入数据。
  3. 使用 split(" ") 方法将输入的字符串按空格分割为数组 fen
  4. 根据不同的输入情况执行不同的操作,例如:循环执行上述步骤直到输入为 "end"。
    • 如果输入长度为4且第四个元素为 "T",则表示为添加菜品操作,调用 menu.addDish() 方法添加菜品。
    • 如果输入长度为2且第二个元素不为 "delete",则表示为添加菜品操作,调用 menu.addDish() 方法添加菜品。
    • 如果输入长度为3且第三个元素为 "T",则输出错误格式信息。
    • 如果输入以 "table" 开头且满足一定条件,表示为创建餐桌和顾客的操作,创建相应的对象并保存相关信息。
    • 如果以上条件均不满足,表示为点餐操作,根据菜品是否存在和口味是否合理进行相应处理,并输出点餐结果。
    • 如果输入长度为2且第二个元素为 "delete",表示为删除点餐记录的操作,根据订单号进行相应处理。
  5. 循环执行上述步骤直到输入为 "end"。
  6. 计算所有餐桌的价格并输出。
  7. 计算每个顾客消费总额并输出。
  8. 关闭 Scanner 对象。

以上即为简单的分析,接下来是样例结果:

 

 

 自此样例通过实验完成。

(3)踩坑心得

  在期中考试的题目集中,还是遇到很多问题,比如在第一题中我遇到的问题是最后输出结果说错误数据,于是对代码结果进行排除,发现是我没有在输入的时候判断输入的数据是否符合要求,所以我添加了一个判断sc.hasNextDouble(),来判断输入的是否是字符型,这样就使得测试点通过了,在第二题时也遇到了有一个测试点没有通过,提示我正常值那里答案错误,于是我试了很多测试点,发现问题所在,就是当我在求长和宽的时候,可能会出现负数的情况我没有考虑进去最后导致得到的结果也是负数,所以我在那些方法返回值那里加上了math.abs来使结果为正数。而第三题同样也遇到了问题,题目没有将全部错误判断描述清楚,它承接了第一题的错误判断,所以在输入圆的半径时同样也要进行和第一题一样的错误判断。而最后一题则考察的是对java自带接口的使用,没有学过的话很难写出来,只能靠常规的排序方式完成题目,需要去网上查找资料。

  然后就是菜单计价程序,这里踩的坑还是比较多的,在一开始时要想着如何在原来的类里面添加新属性,并且如何设计才能使那些方法更方便调用,而且在编写代码过程中,因为当前能力还不够,对于代码的结构其实自己也不是很清楚所以写着写着思路就乱了,代码就出现了一些问题,比如之前的nullpointexpection问题依旧出现了,而且测出当输入的数据过多时它存进对象里出现了问题,所以就是我的代码上有错误,所以我就得对其进行修改,这里耗费的时间还是很多的,而且在最后输出结果时得按照它给出的格式输出,所以我的代码就得跟着进行修改做出很大变动,一直在添加修改添加修改,然后导致更多的代码出错,说明我写的代码不够灵活,不方便以后修改。

(4)改进建议

  这几次题目感觉可以改进的地方很多,比如说在菜单计价程序当中,当我编写完成代码后,回过头发现我的代码写的很长很乱,看上去不是很清晰明了,而且有很多冗杂重复的代码可以缩减,而且整个代码的架构也不是很合理,感觉可变性不高导致后面代码修改起来很困难,所以还是要在写之前想清楚怎么设计,在整理代码结构时,可以将相关的方法放在一起,提高可读性,在接收用户输入之前,应该进行一些基本的输入数据验证,例如检查输入是否为空、是否符合预期格式等,以避免因为无效的输入而导致程序崩溃或产生错误结果,在出现错误或异常情况时,应该给出明确的错误信息,并采取相应的错误处理措施。例如,在输出错误格式信息时,直接退出可能不是最好的选择,可以通过提示错误并继续接收下一轮输入。

  然后就是对于期中考试的题目

 

  如果需要进一步改进代码,可以考虑以下几点:

 

  1. 添加输入验证的逻辑,确保用户输入的值是有效的。
  2. Point类中可以添加距离计算的方法,用于计算两个点之间的距离。
  3. Rectangle类中可以添加判断矩形是否为正方形的方法。
  4. 可以添加输入验证的逻辑,确保用户输入的值是有效的。
  5. 可以在Shape类中定义一个printArea方法,用于统一输出面积结果,避免重复代码。
  6. 考虑使用面向对象设计原则(如单一职责、开放封闭等)和设计模式来优化代码结构和可维护性,提高代码的重用性和扩展性

 

(5)总结

  通过这几次作业,我代码中使用了Java的基本语法,例如类、对象、方法、条件语句、循环等。通过分析代码,可以了解这些基础知识在实际场景中的应用方法,而且代码中使用了面向对象的编程思想,通过创建不同的类和对象来表示餐厅、菜单、餐桌、订单等实体和操作。您可以学习如何将实际问题抽象为类和对象,并建立它们之间的关系,代码中使用了数组和对象来存储和管理数据。您可以了解如何选择适当的数据结构和算法来组织和处理数据,以提高程序的效率和性能。然后是对用户的输入进行了一定程度的验证和处理,以确保输入的有效性并给出相应的错误提示。您可以学习如何验证和处理用户输入,以及如何在程序中处理错误和异常情况,再是通过分析代码,您可以了解如何组织和结构化代码,使其易于理解、维护和扩展。从代码中可以学习到一些良好的编码实践,例如命名规范、模块化设计等。代码中没有给出明确的测试部分,但学习这段代码也可以让您意识到编写适当的测试用例的重要性。通过测试和调试,可以找出潜在的错误和边界情况,并提高代码的质量和稳定性。同时也对面向对象编程有了进一步学习和理解,但是还是存在很多可以提升的地方,比如加强代码的可读性,编写代码时应该头脑清醒,逻辑清晰,然后就是对于java基础知识点需要多加学习掌握。

标签:总结,return,Point,double,topLeftPoint,期中,PTA4,public,lowRightPoint
From: https://www.cnblogs.com/lztnchu/p/17514662.html

相关文章

  • BLOG_OOP_期中考试
    前言涉及知识点1.对于创建对象和类的初步实践;如构建圆类和矩形类;1.对于抽象类和继承与多态的认识;如构建shape类;题量不多,可以完成。难度不大,可以完成。设计与分析题目源码如下importjava.util.*;publicclassMain{publicstaticvoidmain(String[]......
  • python打包exe总结 pyinstaller py2exe
    Python打包exe有挺多可以用的如pyinstallerpy2exe cx_Freezenuitkapy2apppy0xidizer cx_Freeze和nuitka没用过py2app是打包Mac程序的py0xidizer是打包嵌入式的占用空间少感兴趣可以自行了解 这篇文章记录一下pyinstaller和py2exe的用法 以便以后查找  注:以下都......
  • pta题目集4~5及期中考试总结性blog
    一、前言总结三次题目集的知识点、题量、难度等情况第四次题目集主要更新了各种异常情况,是对代码正确度的深入考察,涉及时间的格式问题,信息的格式问题各种格式问题等等,涉及到hashset、面向对象编程的封装性、BigDecimal类关于精确小数的运算以及了解Scanner类中nextLine()等方法......
  • 面向对象程序编程PTA题目集4、5以及期中考试的总结性Blog
    1.对之前发布的PTA题目集4、5以及期中考试的总结性Blog,内容要求如下:(1)前言:总结之前所涉及到的知识点、题量、难度等情况期中考试作业:知识点:主要就是考了对类的使用,和不同类间的相互配合使用,还有对于一个程序的不断循环使用(相比之前更加灵活,可以自定义输入的个数),正则表达(可用可不......
  • 面试总结(一)
    为什么MySQL索引更适合B+树而不是二叉树、B树一数据库为什么使用B+树 与二叉树相比二叉树相比于顺序查找的确减少了查找次数,但是在最坏情况下,二叉树有可能退化为顺序查找。而且就二叉树本身来说,当数据库的数据量特别大时,其层数也将特别大。二叉树的高度一般是log_2^n,B树的高度......
  • 对之前发布的PTA题目集4、5以及期中考试的总结性
    一、前言:总结之前所涉及到的知识点、题量、难度等情况知识点:输入和输出:根据输入的菜单和订单信息,计算每桌的总价,并按顺序输出每一桌的总价。字符串处理和分割:需要将输入的字符串进行适当的处理和分割,提取所需的信息。条件判断和计算:根据不同的情况进行条件判断和计算,......
  • 脑电信号采集模块方案的技术阶段总结简析
    原理 脑电图(electroencephalogram,EEG)是通过精密的仪器从头皮上将脑补的大脑皮层的自发性生物电位加以放大记录而获得的图形,是通过电极记录下来的脑细胞群的自发性、节律性电活动。这种电活动是以电位作为纵轴,时间为横轴,从而记录下来的电位与时间相互关系的平面图。脑电波的频率(......
  • leetcode动态规划题目总结
     ref:https://leetcode.cn/circle/article/2Xxlw3/ 这是一篇我在leetcode.com上撰写的文章DynamicProgrammingSummary,就不翻回中文了,直接copy过来了。Helloeveryone,IamaChinesenoobprogrammer.Ihavepracticedquestionsonleetcode.comfor2years.During......
  • C#中的using用法总结
      using一般有两个作用:  1、作为语句,用于定义一个范围,在此范围的末尾将释放对象(IDisposable和IAsyncDisposable接口)  2、作为指令,用于引入命名空间或者类型,或者为引入的命名空间或者类型定义别名  using语句  using语句应该都很熟悉了吧,从最早的ADO.net,或者对文件、......
  • 《IT项目管理》总结:项目沟通管理
    10/21/200910:33:15PM沟通失败常常是项目——特别是IT项目——成功的最大的威胁。沟通是保持项目顺利进行的润滑剂。沟通计划编制包括信息发送、绩效报告和管理收尾,它需要确定项目干系人的信息和沟通需求。沟通管理计划应该是为所有项目创建的。项目沟通中的项目干系人分析,有助于......