首页 > 其他分享 >装疯卖傻总结性blog

装疯卖傻总结性blog

时间:2023-04-01 18:13:20浏览次数:34  
标签:总结性 记录 int money blog 菜品 Dish table 装疯卖傻

前言

1.     Scanner in = new Scanner(System.in);

Scanner 是一个类,in是一个实例或者说是对象,new 是一个创建对象的方法

2. (float)(x/0.45359237)

强制数据类型转换的写法

3.String[] race=new String[]{"人类","精灵","兽人","暗精灵"};

创建数组格式like this ,与c不一样要写前面

4. year = a.substring(0,2);

取前两个单位长度为year,substring(a, b)表示截取下标从a开始到b结束的字符,包含第a个字符但是不包含第b个字符,可以看成[a,b)。

5. college.equalsIgnoreCase("01")

equals() 会判断大小写区别,equalsIgnoreCase() 不会判断大小写区别

6. a.charAt(i)=='-'

获取字符串中 i 位置的字符

7. StreamTokenizer re = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

快速输入

8. String[] str = st.split(" ");

将st根据空格分离并存入数组str

9.   Set<Integer> set = new HashSet<Integer>();

set集合,并且集合里面只能储存int类型的数字

HashSet类

hashSet.add("jake");添加

hashSet.remove("jake");删除

10.while(sc.hasNext())

hasNext()返回的是boolean类型而next()返回的是你输入的那个值

11. LinkedHashSet<String> temp = new LinkedHashSet<String>();

 

LinkedHashSet中的元素没有重复

LinkedHashSet中的元素有顺序,维护了添加顺序

LInkedHashSet可以存储null值

12.调用无参构造方法,并通过setter方法进行设值

Student student1 = new Student();

public void setSid(String a) {

        sid = a;

}

public String getSid() {

        return sid;

    }

 调用有参构造方法

Student student2 = new Student(sid2, name2, age2, major2);

public Student(String sid, String name, int age, String major) {

        super();

        this.name = name;

        this.major = major;

        this.sid = sid;

        this.age = age;

}

13. LocalDate   t0 = LocalDate.of(year,month,day);

14. long a =t0.until(t1, ChronoUnit.DAYS);

相差日

15long b =t0.until(t1, ChronoUnit.WEEKS);

相差周

难度:个人认为难度适中,对于知识点的调用能力要求比较高,属于会者不难,难者不会

 

设计与分析

7-1 菜单计价程序-1

分数 30

 

全屏浏览题目

 

切换布局

作者蔡轲

单位南昌航空大学

某饭店提供4种菜,每种菜品的基础价格如下:

西红柿炒蛋 15

清炒土豆丝 12

麻婆豆腐 12

油淋生菜 9

 

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

订单由一条或多条点菜记录组成,每条记录一行,最后以"end"结束

每条点菜记录包含:菜名、份额两个信息。

份额可选项包括:123,分别代表小、中、大份)

 

不同份额菜价的计算方法:

小份菜的价格=菜品的基础价格。

中份菜的价格=菜品的基础价格1.5

小份菜的价格=菜品的基础价格2

如果计算出现小数,按四舍五入的规则进行处理。

 

参考以下类的模板进行设计:

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

Dish {

String name;//菜品名称

int unit_price; //单价

int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小//大份)

}

 

菜谱类:对应菜谱,包含饭店提供的所有菜的信息。

Menu {

Dish[] dishs ;//菜品数组,保存所有菜品信息

Dish searthDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。

}

 

点菜记录类:保存订单上的一道菜品记录

Record {

Dish d;//菜品

int portion;//份额(1/2/3代表小//大份)

int getPrice()//计价,计算本条记录的价格

}

 

订单类:保存用户点的所有菜的信息。

Order {

Record[] records//保存订单上每一道的记录

int getTotalPrice()//计算订单的总价

Record addARecord(String dishName,int portion)

//添加一条菜品信息到订单中。

}

 

输入格式:

每条点菜记录的格式:

菜名+空格(英文)+份额

注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。

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

 

输出格式:

订单上所有菜品的总价(整数数值),每份菜

如果订单中包含不能识别的菜名,则在总价之前输出“** does not exist”,**是不能识别的菜名

 

输入样例:

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

 

麻婆豆腐 2

西红柿炒蛋 3

end

输出样例:

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

 

48

输入样例1:

订单中包含不存在的菜品记录。例如:

 

麻婆豆腐 2

炒脆肚 2

西红柿炒蛋 3

end

输出样例1:

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

 

炒脆肚 does not exist

48

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

 

我的正确代码:

import java.util.*;

public class Main{

 

 

    public static int count(int price, int portion) {

        double money=0;

        switch (portion) {

 

         /*     case 1 -> money = price;

            case 2 -> money = price * 1.5;

            case 3 -> money = price * 2;*/

            case 1: money= price; break;

            case 2 : money = price * 1.5; break;

            case 3:  money = price * 2; break;

 

        }

        return (int) Math.round(money);

 

    }

 

 

   

    public static void main(String args[]){

        Scanner sc=new Scanner(System.in);

        int sum=0;

        double money=0;

        String name;

        int price=0;

        while(true)  {

            name=sc.next();

 

            if(name.equals("end")){

                break;

            }

 

            int poriton=sc.nextInt();

 

            if(name.equals("西红柿炒蛋")){

                price=15;

               sum=sum+count(price,poriton);

                // count(price,poriton);

               // sum=sum+money;

            }

 

        else if(name.equals("清炒土豆丝")){

                price=12;

                //count(price,poriton);

                sum=sum+count(price,poriton);

              //  sum=sum+money;

            }

 

     else   if(name.equals("麻婆豆腐")){

                price=12;

                //count(price,poriton);

               // sum=sum+money;

                sum=sum+count(price,poriton);

            }

 

     else if(name.equals("油淋生菜")){

 

                price=9;

                sum=sum+count(price,poriton);

               // sum=sum+money;

            }

 

            else  {

                System.out.println(name+" does not exist");

                continue;

 

            }

        }

 

            System.out.println((sum));

 

    }

 

}

踩坑心得

 

我的错误代码:

 

 

 

 

 

import java.util.*;

public class Main{

    double money=0;

 

    public static double count(int price, int portion) {

        double money=0;

        switch (portion) {

 

         /*     case 1 -> money = price;

            case 2 -> money = price * 1.5;

            case 3 -> money = price * 2;*/

            case 1: money= price; break;

            case 2 : money = price * 1.5; break;

            case 3:  money = price * 2; break;

 

        }

        return money;

 

    }

  /*  public static double  crash(double money){

        money

 

 

    }*/

 

    /*  void dish(name){

          if(name.equals("西红柿炒蛋"))

              price=15;

          if(name.equals("清炒土豆丝")

              price=12;

             if(name.equals("麻婆豆腐")

              price=12;

          if(name.equals("油淋生菜")

              price=9;

    else  System.out.println(name+"does not exist"+"\n");

 

      }*/

    public static void main(String args[]){

        Scanner sc=new Scanner(System.in);

        double sum=0;

        double money=0;

        String name;

        int price=0;

        while(true)  {

            name=sc.next();

 

            if(name.equals("end")){

                break;

            }

 

            int poriton=sc.nextInt();

 

            if(name.equals("西红柿炒蛋")){

                price=15;

               sum=sum+count(price,poriton);

                // count(price,poriton);

               // sum=sum+money;

            }

 

        else if(name.equals("清炒土豆丝")){

                price=12;

                //count(price,poriton);

                sum=sum+count(price,poriton);

              //  sum=sum+money;

            }

 

     else   if(name.equals("麻婆豆腐")){

                price=12;

                //count(price,poriton);

               // sum=sum+money;

                sum=sum+count(price,poriton);

            }

 

     else if(name.equals("油淋生菜")){

 

                price=9;

                sum=sum+count(price,poriton);

               // sum=sum+money;

            }

 

            else  {

                System.out.println(name+" does not exist");

                continue;

 

            }

        }

 

            System.out.println((int)sum);

 

    }

}

 

第一次提交的时候发现多桌记录的测试点都没有通过,然后显示是答案错误说明代码本身是没有问题,于是我就一次次测试,后来发现问题出现在四舍五入上面,基本答案算的是大差不差的,于是我就改进算法,将计算钱的方法的返回值的地方做了详细的四舍五入的函数处理:

 

 

这个改进成了

 

 

主要困难以及改进建议:

一直没想到使用了一个四舍五入的函数,一直想着直接使用强制转换,但是忘了使用强制类型转化成整型时,直接去掉小数点后的数,会忽略掉小数的精度。当然,本题老师其实已经给了类只用填写相应内容但是我因为刚接触面向对象的写法,还不是很熟悉,思路还是面向过程那一套,看见题目的时候面向过程的那一套就已经跃然于心头,所以这道题我做下来其实是就没有花太多时间,最大的问题就是没有意识到四舍五入和强制数据转换的差距,当然这种写法对后续填写更多的相关功能内容是没有一点好处的,很难写。

 

7-2 菜单计价程序-2

分数 40

 

全屏浏览题目

 

切换布局

作者蔡轲

单位南昌航空大学

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

 

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

 

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

 

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

 

 

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

点菜记录包含:序号、菜名、份额、份数。

份额可选项包括:123,分别代表小、中、大份。

 

删除记录格式:序号 delete

 

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

 

不同份额菜价的计算方法:

小份菜的价格=菜品的基础价格。

中份菜的价格=菜品的基础价格1.5

小份菜的价格=菜品的基础价格2

如果计算出现小数,按四舍五入的规则进行处理。

 

参考以下类的模板进行设计:

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

 

Dish {   

   String name;//菜品名称   

   int unit_price;    //单价   

   int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小//大份)    }

菜谱类:对应菜谱,包含饭店提供的所有菜的信息。

 

Menu {

   Dish[] dishs ;//菜品数组,保存所有菜品信息

   Dish searthDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。

   Dish addDish(String dishName,int unit_price)//添加一道菜品信息

}

点菜记录类:保存订单上的一道菜品记录

 

Record {

   int orderNum;//序号\

   Dish d;//菜品\

   int portion;//份额(1/2/3代表小//大份)\

   int getPrice()//计价,计算本条记录的价格\

}

订单类:保存用户点的所有菜的信息。

 

Order {

   Record[] records//保存订单上每一道的记录

   int getTotalPrice()//计算订单的总价

   Record addARecord(int orderNum,String dishName,int portion,int num)//添加一条菜品信息到订单中。

   delARecordByOrderNum(int orderNum)//根据序号删除一条记录

   findRecordByNum(int orderNum)//根据序号查找一条记录

}

输入格式:

菜品记录格式:

 

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

 

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

 

点菜记录格式:

序号+英文空格+菜名+英文空格+份额+英文空格+份数

注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。

 

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

 

 

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

 

输出格式:

按顺序输出每条订单记录的处理信息,

 

每条点菜记录输出:序号+英文空格+菜名+英文空格+价格。其中的价格等于对应记录的菜品*份数,序号是之前输入的订单记录的序号。

如果订单中包含不能识别的菜名,则输出“** does not exist”,**是不能识别的菜名

 

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

 

最后输出订单上所有菜品的总价(整数数值),

 

本次题目不考虑其他错误情况,如:菜单订单顺序颠倒、不符合格式的输入、序号重复等,在本系列的下一次作业中会做要求。

 

输入样例:

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

 

麻婆豆腐 12

油淋生菜 9

1 麻婆豆腐 2 2

2 油淋生菜 1 3

end

输出样例:

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

 

1 麻婆豆腐 36

2 油淋生菜 27

63

输入样例1:

订单中包含删除记录。例如:

 

麻婆豆腐 12

油淋生菜 9

1 麻婆豆腐 2 2

2 油淋生菜 1 3

1 delete

end

输出样例1:

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

 

1 麻婆豆腐 36

2 油淋生菜 27

27

输入样例2:

订单中包含不存在的菜品记录。例如:

 

麻婆豆腐 12

油淋生菜 9

1 麻婆豆腐 2 2

2 油淋生菜 1 3

3 麻辣鸡丝 1 2

end

输出样例2:

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

 

1 麻婆豆腐 36

2 油淋生菜 27

麻辣鸡丝 does not exist

63

输入样例3:

订单中包含删除信息以及不存在的菜品记录。例如:

 

麻婆豆腐 12

油淋生菜 9

1 麻婆豆腐 2 2

2 油淋生菜 1 3

3 麻辣鸡丝 1 2

1 delete

7 delete

end

输出样例3:

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

 

1 麻婆豆腐 36

2 油淋生菜 27

麻辣鸡丝 does not exist

delete error;

27

输入样例4:

订单中包含删除信息以及不存在的菜品记录。例如:

 

麻婆豆腐 12

油淋生菜 9

1 麻婆豆腐 2 2

2 油淋生菜 1 3

3 麻辣鸡丝 1 2

5 delete

7 delete

end

输出样例4:

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

 

1 麻婆豆腐 36

2 油淋生菜 27

麻辣鸡丝 does not exist

delete error;

delete error;

63

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

我的代码:

import java.util.Scanner;

public class Main {

 

    //菜谱

    //点菜

    //订单

    public static void main(String[] args)

    {

        int flag=0;//判断是否开始订单

        int i=0,e=0;

        Scanner in=new Scanner(System.in);

        Menu menu=new Menu();

        Order order=new Order();

        while(true)

        {

            String a=in.nextLine();

            if(a.equals("end"))

                break;

            String[] b=a.split(" ");//cut in

            if(b[0].equals("1"))

                flag=1;

            if(flag==0)

            {

                menu.dishs[i]=new Dish();

                if(menu.searthDish(b[0],i)==null)

                {

                    menu.dishs[i]=menu.addDish(b[0],Integer.parseInt(b[1]));

                    i++;

                }

                else

                    menu.searthDish(b[0],i).money=Integer.parseInt(b[1]);

            }

            else

            {

                if(menu.searthDish(b[1],i)!=null)

                {

                    e=Integer.parseInt(b[0]);

                    order.history[e]=new Record();

                    order.history[e]=order.add(e,menu.searthDish(b[1],i),Integer.parseInt(b[2]),Integer.parseInt(b[3]));

                    System.out.println(order.history[e].orderNum+" "+order.history[e].d.name+" "+order.history[Integer.parseInt(b[0])].getPrice());

                }

                else if(b[1].equals("delete"))

                {   if(Integer.parseInt(b[0])>e&&Integer.parseInt(b[0])<1)

                        System.out.println("delete error;");

                    else{

                      if(order.history[Integer.parseInt(b[0])].n==0)

                        order.delete(Integer.parseInt(b[0]));

                    else

                        System.out.println("delete error;");

                }

                }

                else

                {

                    e=Integer.parseInt(b[0]);

                    order.history[e]=new Record();

                    Dish dish=new Dish();

                    dish.name=dish.name+b[1];

                    dish.money=0;

                    order.history[e]=order.add(e,dish,Integer.parseInt(b[2]),Integer.parseInt(b[3]));

                    System.out.println(b[1]+" does not exist");

                }

            }

        }

        System.out.println(order.getTotalPrice(e));

    }

}

class Order {

    Record[]  history=new Record[100];//保存订单上每一道的记录

    int getTotalPrice(int number)//计算订单的总价

    {

        int i,s=0;

        for(i=1;i<=number;i++)

        {

            if(history[i].n==0)

                s=s+history[i].getPrice();

        }

        return s;

    }Record add(int orderNumber,Dish d,int portion,int num)//cut in

    {

        Record re=new Record();

        re.d=new Dish();

        re.orderNum=orderNumber;

        re.d=d;

        re.portion=portion;

        re.num=num;

        return re;

    }

    void delete(int orderNum)

    {

        history[orderNum].n=1;

    }

 

 

}

 

 

class Dish

{

    String name="";

    int money;

    int getPrice(int portion)

    {

        double p=1;

        switch(portion)

        {

            case 1:p=money*1;break;

            case 2:p=money*1.5;break;

            case 3:p=money*2;break;

        }

        return (int)Math.round(p);

    }

}

class Menu

{

    Dish[] dishs=new Dish[10] ;//菜品数组,保存所有菜品信息

    Dish searthDish(String dishName,int j)//根据菜名在菜谱中查找菜品信息,返回Dish对象。

    {

        int i;

        for(i=0;i<j;i++)

        {

            if(dishName.equals(dishs[i].name))

                return dishs[i];

        }

        return null;

    }

    Dish addDish(String dishName,int money)//添加一道菜品信息

    {

        Dish dish=new Dish();

        dish.name=dishName.substring(0);

        dish.money=money;

        return dish;

    }

}

class Record

{

    int orderNum;//序号

    Dish d;//菜品

    int portion;//份额(1/2/3代表小//大份)

    int num;//分量

    int n=0;//标记是否被删除

    int getPrice()//计价,计算本条记录的价格

    {

        double p=1;

        int i;

        return (int)Math.round(d.getPrice(portion)*num);

 

}}

二.设计与分析

类图如下:

 

 菜谱类:对应菜谱,包含饭店提供的所有菜的信息。

点菜记录类:保存订单上的一道菜品记录

订单类:保存用户点的所有菜的信息。

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

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

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


订单分:点菜记录和删除信息。每一类信息都可包含一条或多条记录,每条记录一行。
点菜记录包含:序号、菜名、份额、份数。
份额可选项包括:1、2、3,分别代表小、中、大份。

删除记录格式:序号 delete

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

踩坑心得:

 

 

 

 

 

 

 

 

 

 

 

 

基本上前三次提交都是忘了}或者因为一行太长落了哪个(那阵我电脑拿去修了没有编译器所以都是拿pta写的报错就很容易出现这种问题,有时候多删了这种);导致语句无法正常进行,或者是因为命名的问题,因为变量太多导致他们的名字个人感觉太相像了导致我造成记忆和观感上的混淆,于是我就临时起意更换名字但是忘了他们关联在了很多地方,没有及时的改进以至于编译器无法正确匹配从而报错。

我把他们三个归到一起是因为都是硬核错误暂时没有发现代码或者答案哪里不对劲。、

第四次提交,发现测试点就最后三个没有通过,发现是删除异常的问题,其实当时是没有时间再继续写的,所以我就放下了去写写到一半的第三题,但是我后来有时间之后还是将第二题跟进,将正确的代码写出来的,因为距离我写已经过了很一段时间,记得是在if判断语句那里少写了几种情况,即异常删除的输出和异常菜,反正放进代码里面就是少写了3else如果没有记错的话,都是输出WRONG的结果,对应代码大概是如下位置,这个地方少写了三种情况吧大概(因为是三个测试点)

 

 

 

 

在后来有时间改进之后编写出来的正确代码如下:

 

import java.util.Scanner;

public class Main {

 

    //菜谱

    //点菜

    //订单

    public static void main(String[] args)

    {

        int flag=0;//判断是否开始订单

        int i=0,e=0;

        Scanner in=new Scanner(System.in);

        Menu menu=new Menu();

        Order order=new Order();

        while(true)

        {

            String a=in.nextLine();

            if(a.equals("end"))

                break;

            String[] b=a.split(" ");//cut in

            if(b[0].equals("1"))

                flag=1;

            if(flag==0)

            {

                menu.dishs[i]=new Dish();

                if(menu.searthDish(b[0],i)==null)

                {

                    menu.dishs[i]=menu.addDish(b[0],Integer.parseInt(b[1]));

                    i++;

                }

                else

                    menu.searthDish(b[0],i).money=Integer.parseInt(b[1]);

            }

            else

            {

                if(menu.searthDish(b[1],i)!=null)

                {

                    e=Integer.parseInt(b[0]);

                    order.history[e]=new Record();

                    order.history[e]=order.add(e,menu.searthDish(b[1],i),Integer.parseInt(b[2]),Integer.parseInt(b[3]));

                    System.out.println(order.history[e].orderNum+" "+order.history[e].d.name+" "+order.history[Integer.parseInt(b[0])].getPrice());

                }

                else if(b[1].equals("delete"))

                {

                    if(Integer.parseInt(b[0])<=e&&Integer.parseInt(b[0])>=1)

                    {

                        if(order.history[Integer.parseInt(b[0])].n==0)

                            order.delete(Integer.parseInt(b[0]));

                        else

                            System.out.println("delete error;");

                    }

                    else

                        System.out.println("delete error;");

                }

                else

                {

                    e=Integer.parseInt(b[0]);

                    order.history[e]=new Record();

                    Dish dish=new Dish();

                    dish.name=dish.name+b[1];

                    dish.money=0;

                    order.history[e]=order.add(e,dish,Integer.parseInt(b[2]),Integer.parseInt(b[3]));

                    System.out.println(b[1]+" does not exist");

                }

            }

        }

        System.out.println(order.getTotalPrice(e));

    }

}

class Order {

    Record[]  history=new Record[100];//保存订单上每一道的记录

    int getTotalPrice(int number)//计算订单的总价

    {

        int i,s=0;

        for(i=1;i<=number;i++)

        {

            if(history[i].n==0)

                s=s+history[i].getPrice();

        }

        return s;

    }

    Record add(int orderNumber,Dish d,int portion,int num)//cut in

    {

        Record re=new Record();

        re.d=new Dish();

        re.orderNum=orderNumber;

        re.d=d;

        re.portion=portion;

        re.num=num;

        return re;

    }

    void delete(int orderNum)

    {

        history[orderNum].n=1;

    }

 

 

}

class Record

{

    int orderNum;//序号

    Dish d;//菜品

    int portion;//份额(1/2/3代表小//大份)

    int num;//分量

    int n=0;//标记是否被删除

    int getPrice()//计价,计算本条记录的价格

    {

        double p=1;

        int i;

        return (int)Math.round(d.getPrice(portion)*num);

 

    }

}

class Menu

{

    Dish[] dishs=new Dish[10] ;//菜品数组,保存所有菜品信息

    Dish searthDish(String dishName,int j)//根据菜名在菜谱中查找菜品信息,返回Dish对象。

    {

        int i;

        for(i=0;i<j;i++)

        {

            if(dishName.equals(dishs[i].name))

                return dishs[i];

        }

        return null;

    }

    Dish addDish(String dishName,int money)//添加一道菜品信息

    {

        Dish dish=new Dish();

        dish.name=dishName.substring(0);

        dish.money=money;

        return dish;

    }

}

class Dish

{

    String name="";

    int money;

    int getPrice(int portion)

    {

        double p=1;

        switch(portion)

        {

            case 1:p=money*1;break;

            case 2:p=money*1.5;break;

            case 3:p=money*2;break;

        }

        return (int)Math.round(p);

    }

}

补上了之前没来得及改的那部分(就是判断那部分的问题)。

主要困难以及改进建议:

其实一开始我还是设想过用第一题面向过程的写法继续写,即接着第一题继续写下去,但是发现实在是太麻烦的,就尝试用老师给出的类开始面向过程的代码编写,所以时间消耗特别大,但是其实思路给出来之后也挺好写的,其实在现在学了父类子类之后感觉代码在相同的属性调用上面可以采取子类父类的写法可以降低耦合,让代码更简单一点,现在看感觉还是写的过于繁琐,主要苦难在于设计每个类之前的关联和调用上面,以及完成题目增加的删减上面,相当于在类和方法上面真的挺头疼让我,不知道放哪里。而且类一多调用的时候真的要连很长一串真的很不方便,如果要改进的话,可能就会选择用子类父类,更加灵活的使用类型匹配。其他地方我感觉已经很简洁了

7-1 菜单计价程序-3

分数 30

 

 

 

 

作者 蔡轲

单位 南昌航空大学

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

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

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

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

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

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

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

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

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

删除记录格式:序号 delete

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

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

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

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

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

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

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

周一至周五营业时间与折扣:晚上(1700-20308折,周一至周五中午(10:30--14:306折,其余时间不营业。

周末全价,营业时间: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)//根据序号查找一条记录

}

### 输入格式:

桌号标识格式: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+英文空格+桌号+“:”+英文空格+当前桌的总价

本次题目不考虑其他错误情况,如:桌号、菜单订单顺序颠倒、不符合格式的输入、序号重复等,在本系列的后续作业中会做要求。

输入格式:

桌号标识格式: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
table 1 2023/3/22 12/2/3
1 麻婆豆腐 2 2
2 油淋生菜 1 3
end

输出样例:

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

table 1: 
1 麻婆豆腐 36
2 油淋生菜 27
table 1: 38

输入样例1:

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

麻婆豆腐 12
油淋生菜 9
table 1 2023/3/22 17/0/0
1 麻婆豆腐 2 2
2 油淋生菜 1 3
1 delete
end

输出样例1:

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

table 1: 
1 麻婆豆腐 36
2 油淋生菜 27
table 1: 22

输入样例2:

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

麻婆豆腐 12
油淋生菜 9
table 1 2023/3/22 16/59/59
1 麻婆豆腐 2 2
2 油淋生菜 1 3
1 delete
end

输出样例2:

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

table 1: 
1 麻婆豆腐 36
2 油淋生菜 27
table 1 out of opening hours

输入样例3:

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

麻婆豆腐 12
油淋生菜 9
table 1 2022/12/5 15/03/02
1 麻婆豆腐 2 2
2 油淋生菜 1 3
3 麻辣鸡丝 1 2
5 delete
7 delete
table 2 2022/12/3 15/03/02
1 麻婆豆腐 2 2
2 油淋生菜 1 3
3 麻辣鸡丝 1 2
7 delete
end

输出样例3:

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

table 1: 
1 麻婆豆腐 36
2 油淋生菜 27
麻辣鸡丝 does not exist
delete error;
delete error;
table 2: 
1 麻婆豆腐 36
2 油淋生菜 27
麻辣鸡丝 does not exist
delete error;
table 1 out of opening hours
table 2: 63

输入样例4:

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

麻婆豆腐 12
油淋生菜 9
table 1 2022/12/3 19/5/12
1 麻婆豆腐 2 2
2 油淋生菜 1 3
3 麻辣鸡丝 1 2
table 2 2022/12/3 15/03/02
1 麻婆豆腐 2 2
2 油淋生菜 1 3
3 麻辣鸡丝 1 2
1 4 麻婆豆腐 1 1
7 delete
end

输出样例4:

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

table 1: 
1 麻婆豆腐 36
2 油淋生菜 27
麻辣鸡丝 does not exist
table 2: 
1 麻婆豆腐 36
2 油淋生菜 27
麻辣鸡丝 does not exist
4 table 2 pay for table 1 12
delete error;
table 1: 63
table 2: 75

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

踩坑心得:

 

首先,是我的错误代码:

import java.time.LocalDate;

import java.time.temporal.ChronoField;

import java.util.Objects;

import java.util.Scanner;

public class Main {

 

    //菜谱

    //点菜

    //订单

    public static void main(String[] args)

    {    int year;

        int month;

        int day;

        int shi;

        int fen;

        int miao;

        int flag=0;//判断是否开始订单

        int i=0,e=0;

        Scanner in=new Scanner(System.in);

        Menu menu=new Menu();

        Order order=new Order();

        Table table = new Table();

        Dish dish = new Dish();

        while(true)

        {

            String a=in.nextLine();

 

              String[] tb=a.split(" ");

              if(tb[0].equals("table")){

              if(Objects.equals(tb[0], "table"))  table.setTable(Integer.valueOf(tb[1]));

              String[] j=tb[2].split("/");

              year=Integer.valueOf(j[0]);

              month=Integer.valueOf(j[1]);

              day=Integer.valueOf(j[2]);

 

              String[] k=tb[3].split("/");

              shi = Integer.valueOf(k[0]);

              fen=Integer.valueOf(k[1]);

              miao=Integer.valueOf(k[2]);

              LocalDate t1=LocalDate.of(year,month,day);

              if(t1.get(ChronoField.DAY_OF_WEEK)==7||t1.get(ChronoField.DAY_OF_WEEK)==6){

                     dish.setCutoff(1);

                     if((shi>21)||(shi<9)||(shi==9&&fen<30)||(shi==21&&fen>30))

                         System.out.println("table " + table.getTable() + " out of opening hours");

              }else{

if((shi>=17&&shi<20)||(shi==20&&fen<30)||(shi==20&&fen==30&&miao==0))

{ dish.setCutoff(0.8);}

else if((shi==10&&fen>=30)||(shi>=11&&shi<14)||(shi==14&&fen<30)||(shi==14&&fen==30&&miao==0))

                   dish.setCutoff(0.6);

else{

    System.out.println("table " + table.getTable() + " out of opening hours");

}

              }

              System.out.println("table " + table.getTable()+": ");

          }

 

            if(a.equals("end"))

                break;

            String[] b=a.split(" ");//cut in

            if(b[0].equals("1"))

                flag=1;

            if(flag==0)

            {

                menu.dishs[i]=new Dish();

                if(menu.searthDish(b[0],i)==null)

                {

                    menu.dishs[i]=menu.addDish(b[0],Integer.parseInt(b[1]));

                    i++;

                }

                else

                    menu.searthDish(b[0],i).money=Integer.parseInt(b[1]);

            }

            else

            {

                if(menu.searthDish(b[1],i)!=null)

                {

                    e=Integer.parseInt(b[0]);

                    order.history[e]=new Record();

                    order.history[e]=order.add(e,menu.searthDish(b[1],i),Integer.parseInt(b[2]),Integer.parseInt(b[3]));

                    System.out.println(order.history[e].orderNum+" "+order.history[e].d.name+" "+order.history[Integer.parseInt(b[0])].getPrice());

                }

                else if(b[1].equals("delete"))

                {

                    if(Integer.parseInt(b[0])<=e&&Integer.parseInt(b[0])>=1)

                    {

                        if(order.history[Integer.parseInt(b[0])].n==0)

                            order.delete(Integer.parseInt(b[0]));

                        else

                            System.out.println("delete error;");

                    }

                    else

                        System.out.println("delete error;");

                }

                else

                {

                    e=Integer.parseInt(b[0]);

                    order.history[e]=new Record();

                  //  Dish dish=new Dish();

                    dish.name=dish.name+b[1];

                    dish.money=0;

                    order.history[e]=order.add(e,dish,Integer.parseInt(b[2]),Integer.parseInt(b[3]));

                    System.out.println(b[1]+" does not exist");

                }

            }

        }

        System.out.println("table "+table.getTable()+": "+order.getTotalPrice(e));

    }

}

 

class Order {

    //String table;

    Dish dish=new Dish();

    Record[]  history=new Record[100];//保存订单上每一道的记录

    int getTotalPrice(int number)//计算订单的总价

    {

        int i,s=0;

        for(i=1;i<=number;i++)

        {

            if(history[i].n==0)

                s=s+history[i].getPrice();

        }

        return (int)(Math.round(s*dish.getCutoff()));

 

    }

    Record add(int orderNumber,Dish d,int portion,int num)//cut in

    {

        Record re=new Record();

        re.d=new Dish();

        re.orderNum=orderNumber;

        re.d=d;

        re.portion=portion;

        re.num=num;

        return re;

    }

    void delete(int orderNum)

    {

        history[orderNum].n=1;

    }

 

 

}

class Record

{

    int orderNum;//序号

    Dish d;//菜品

    int portion;//份额(1/2/3代表小//大份)

    int num;//分量

    int n=0;//标记是否被删除

    int getPrice()//计价,计算本条记录的价格

    {

       // double p=1;

       // int i;

        return (int)Math.round(d.getPrice(portion)*num);

 

    }

}

class Menu

{

    Dish[] dishs=new Dish[10] ;//菜品数组,保存所有菜品信息

    Dish searthDish(String dishName,int j)//根据菜名在菜谱中查找菜品信息,返回Dish对象。

    {

        int i;

        for(i=0;i<j;i++)

        {

            if(dishName.equals(dishs[i].name))

                return dishs[i];

        }

        return null;

    }

    Dish addDish(String dishName,int money)//添加一道菜品信息

    {

        Dish dish=new Dish();

        dish.name=dishName.substring(0);

        dish.money=money;

        return dish;

    }

}

 class Dish {

    String name;

    private static double cutoff = 1;

    int money;

    double p;

    public void setCutoff(double a) {

        cutoff = a;

    }

 

    public double getCutoff() {

        return cutoff;

    }

 

    int getPrice(int portion) {

      double p=1;

        switch(portion)

        {

            case 1:p=money*1;break;

            case 2:p=money*1.5;break;

            case 3:p=money*2;break;

        }

        return (int)Math.round(p);//getCutoff()

    }

 

 

}

 class Table {

    private int table;

 

    public void setTable(int a) {

        table = a;

    }

 

    public int getTable() {

 

        return table;

 

    }

 

}

二.设计与分析:

 

 

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

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

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

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

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

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

 table:里面是存储tablename的,方便调用编号,get是输出用的

 

 

 

 

 

 

 

 

 

 

踩坑心得:

第一次提交的时候只拿了七分,因为我知道我只写了table的编号和时间判定,所以我就知道是我的时间划分上面出现了问题导致答案错误。所以错误在了这里:

if(t1.get(ChronoField.DAY_OF_WEEK)==7||t1.get(ChronoField.DAY_OF_WEEK)==6){

                     dish.setCutoff(1);

                     if(shi>=21||(shi<=9&&fen<=30))

                         System.out.println("table " + table.getTable() + " out of opening hours");

              }else{

if((shi>=17&&shi<=20)||(shi==20&&fen<30)||(shi==20&&fen==30&&miao==0))

{ dish.setCutoff(0.8);}

else if((shi==10&&fen>=30)||(shi>=11&&shi<=14)||(shi==14&&fen<=30))

                   dish.setCutoff(0.6);

else{

    System.out.println("table " + table.getTable() + " out of opening hours");

}

              }

              System.out.println("table " + table.getTable()+": ");

 

          }

这里错误是因为忽略了秒的作用,我本来也在纳闷给了秒但是根本用不上,但其实我这里的时间判断以偏概全了,不如八点30是营业的但是八点3001是不营业的所以要补充上当整点时候的情况即秒的判定改完的时间段判定是这样的:

if((shi>21)||(shi<9)||(shi==9&&fen<30))

                         System.out.println("table " + table.getTable() + " out of opening hours");

              }else{

if((shi>=17&&shi<=20)||(shi==20&&fen<30)||(shi==20&&fen==30&&miao==0))

{ dish.setCutoff(0.8);}

else if((shi==10&&fen>=30)||(shi>=11&&shi<=14)||(shi==14&&fen<30)||(shi==14&&fen==30&&miao==0))

                   dish.setCutoff(0.6);

else{

    System.out.println("table " + table.getTable() + " out of opening hours");

}

              }然后就通过了测试点。

 

 

 

主要困难以及改进建议:这是我的代码挂掉的几个测试点,其实,这次作业,是我懒了,我本来想等老师讲的因为我实在是太懒了,我只编写了桌子编号和营业时间和打折的相关代码,至于table的带点餐和多组数据的相关内容我其实是一点也没有写的,我承认是我懒了,我就将编号存入使用时候调用数据,拿前两组单数据的分,但是在我周二结束之后也在想该怎么写table这个类,做到在第二次作业的前提上添加功能,不改太多代码,也能正确实行,我是这么想的:用一个table类,然后里面要记录一下那个table的编号。然后我是在table类里面完成时间的处理,然后根据时间的那个星期一到星期五以及周末,然后完成计价,最后输出。即将计价功能挪到table中,将table写成一个数组类,但是我在输出的时候报错了不知道为什么,但是代码本身是不会报错的(因为报错我就想再写一个换个方法什么的,但是当时的代码已经被我删了没有截图)当录入数据后报错的理由大概是因为超出了数组的长度,但是我也不知道为什么,反正就是一个table类然后将他定义为一个数组,每次读取编号之后存入table数组相应位置从而每次计价只用调用对应的table数组内的位置加点(.)调用即可,理论其实也是可行的,但是不知道为什么说我数组超出长度,其实我平时也经常遇见这种情况,我也没有找到过原因,我会改进方法,至于改进的正确代码至今我也没有打出来过,因为我的table数组理论可行但是输出不出来任何东西很让人头疼,大概就是将传入的int类型改为数组形式,table类里面的setget都相应的改变,在传入的时候会增加一个I 代表当前table数组里面的第几组数据从而实现调用输出,当然我也打算把时间判断从主函数里面挪出来挪到类里面当方法调用,这样子耦合会低一点吧

总结

经过三次的作业,学到了基本的输入输出,循环,类型数据转换,或者格式之前的转换,时间类如calendrlocaldate类的使用,如计算时间差周差日差或者获取当前时间和改变格式,非常便捷,还有set类的使用,如添加删除,排重查重,还有最重要的是面向对象的逻辑思维和类的设计和相互的交互的思想,我感觉我想把我的代码改的更简洁一点,然后能更多的利用Java中给出过的固有函数去做题,面向对象思考而不是过程,将每一节课的知识点落实到每一行代码中,拒绝死知识的输入而是活学活用,课后再花时间把自己总是发现写出的数组会超出长度而无法正常输出的问题解决并将第三道大题写出来,当然也希望老师能多讲一点思路以扩宽自己的思维逻辑路径,取其精华,取其糟粕。

 

标签:总结性,记录,int,money,blog,菜品,Dish,table,装疯卖傻
From: https://www.cnblogs.com/cyzcxhm/p/17279022.html

相关文章

  • 题目集1~3的总结性Blog
    1、前言:前三次题目集主要涉及到java的编程基础知识和有关类的简单知识。(1)其中涉及了Java中的面向对象编程、控制语句、输入输出流、类和对象等基础知识点。具体包括类的定义、构造函数、成员变量和成员方法、访问权限修饰符、条件语句、循环语句、Scanner类的使用、System.out.p......
  • 钟7权BLOG1
    前言:总结三次作业的知识点,内容,难度。 显而易见,此三次的PTA作业所考察的知识点以及难度呈现不断变大的趋势。PTA1作业难度简单,虽然题目量是最多的,但却是最简单的,是我三个题目里面唯一一次全对的,PTA2比PTA1难度有较大的提升,知识点从PTA1的循环结构直接变成了数组和面向对象,当然里......
  • BLOG-1
     (1) 前言:第一次作业:第一次作业的七道题相对来说比较基础,因为这是我们第一次写关于Java的代码,现在回看第一次作业,虽然比较基础,但是对于我来说,第一次作业是学到了比较多的东西的,我也记了一些笔记比如:1..next()和.nextLine()的区别:.next()如果输入了一串字符,到了有空格的时候就......
  • 博客系统——VBLOG_项目工程框架搭建
    VBLOG_项目工程框架搭建一、架构设计采用前后端分离架构设计:api:后端接口服务golang开发的restful接口使用mysql做数据存储web:vue3前端框架前端框架:vue3,vue-routerui组件:arco.design(头条开源组件库)二、接口设计2.1、管理员2.1.1、文章上传接口......
  • 题目集1~3的总结性Blog
    一、前言本学期开展了面向对象程序设计这门课程,开始了Java语言的学习。目前,我们已完成三次pta大作业,让我收获了很多,慢慢从上个学期C语言面向过程的编程思想转变为面向对象程序设计的思想。现对三次作业做概括分析:1.第一次作业二、设计与分析1.第一次作业共九道题目,难度一般,均......
  • weblogic-SSRF
    Weblogic中存在一个SSRF漏洞,利用该漏洞可以发送任意HTTP请求,进而攻击内网中redis、fastcgi等脆弱组件。1、启动环境 访问`http://your-ip:7001/uddiexplorer/`,无需登......
  • 题目集1~3的总结性blog
    一.前言学期伊始,面向对象的程序设计课程的老师就利用PTA平台陆续发布了三次训练集。这三次训练集所涉及知识点与课上所学知识点有关,具体知识点如下:训练集01:此训练集所......
  • oop题目集1~3的总结性Blog
    目录一、前言训练集1训练集2训练集3二、设计分析(1)7-3定义日期类(2)7-4日期类设计三、踩坑心得四、改进建议五、总结一、前言从题目集1-3的训练后对java的基本语法有了一......
  • 题目集1~3的总结性Blog
    目录1.前言2.设计与分析3.踩坑心得4.改进建议5.总结 1.前言题目集1:(主要初次了解Java的一些用法)1、计算年利率2、身体质量指数测算3、九九乘法表(双循环)4、快递......
  • 题目集1~3的总结性Blog
    题目集1:1、计算年利率2、身体质量指数测算3、九九乘法表(双循环)4、快递运费5、去掉重复字符6、统计一个子串在整串中出现的次数7、有重复数据8、从一个字符串中移除......