前言:这次博客分析两个菜单和一个期中考试,在这次期中考试里重点考察了接口以及我们对抽象类和接口之间的差别理解,以及对list包的理解和使用,比如ArrayList<Shape> list = new ArrayList<>();这里考的list的面积排序,就是用接口的办法来解决面积大小的排序。这里接口:Comparable接口 接口中的方法为int compareTo(Shape);接口传入shape类而shape类里面只有面积getarea来进行面积排序。而两个菜单分别考察对错误情况的检查,主要是对正则表达式的熟练应用来区分输入的错误和如何将他输出出来在哪里输出,另外一个则考察多了一个用户,主要对数据的存储和使用进行考察。
设计与分析:
import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.regex.Pattern; import java.util.stream.Collectors; public class Main { static SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH/ss"); static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d"); static LocalDate startDate = LocalDate.parse("2022/1/1", formatter); static LocalDate endDate = LocalDate.parse("2023/12/31", formatter); public static float checkTime(String dateTimeStr) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH/ss"); try { Date dateTime = sdf.parse(dateTimeStr); int weekDay = dateTime.getDay(); // 获取星期几(0表示周日,1表示周一,2表示周二,以此类推) if (weekDay >= 1 && weekDay <= 5) { // 周一至周五 Date startTime1 = sdf.parse(dateTimeStr.split(" ")[0] + " 10/30/00"); // 起始时间(10:30) Date endTime1 = sdf.parse(dateTimeStr.split(" ")[0] + " 14/30/00"); // 结束时间(14:30) Date startTime2 = sdf.parse(dateTimeStr.split(" ")[0] + " 17/00/00"); // 起始时间(17:00) Date endTime2 = sdf.parse(dateTimeStr.split(" ")[0] + " 20/30/00"); // 结束时间(20:30) if ((dateTime.after(startTime1) || dateTime.equals(startTime1)) && (dateTime.before(endTime1) || dateTime.equals(endTime1))) { // 在10:30~14:30之间 return 0.6f; } else if ((dateTime.after(startTime2) || dateTime.equals(startTime2)) && (dateTime.before(endTime2) || dateTime.equals(endTime2))) { // 在17:00~20:30之间 return 0.8f; } } else if (weekDay == 6 || weekDay == 0) { // 周六和周日 Date startTime = sdf.parse(dateTimeStr.split(" ")[0] + " 09/30/00"); // 起始时间(17:00) Date endTime = sdf.parse(dateTimeStr.split(" ")[0] + " 21/30/00"); // 结束时间(20:30) if ((dateTime.after(startTime) || dateTime.equals(startTime)) && (dateTime.before(endTime) || dateTime.equals(endTime))) { return 1.0f; } } } catch (ParseException e) { e.printStackTrace(); } return 0.0f; // 其它时间返回0.0 } public static void main(String[] args) { sdf.setLenient(false); Boolean ifMenu = true; Boolean ifTable = false; Scanner sc = new Scanner(System.in); Menu menu = new Menu(); Calendar cal = Calendar.getInstance(); int oFlag = 1; List<Table> tableList = new ArrayList<>(); int tcd = 0; Table table = null; while (true) { String ipt = sc.nextLine(); if (ipt.equals("end")) break; int type = 0; if (type == 0) { if (ipt.contains("a") && !ifTable) { System.out.println("wrong format"); break; } System.out.println("wrong format"); if (ifTable || ifMenu) { continue; } } String[] s = ipt.split(" "); if (type == 4 && ifTable) { //7 delete int i = Integer.parseInt(s[0]); if (!table.checkIfDelete(i)) { System.out.println("deduplication " + i); continue; } else { if (table.del(i) || table.otherMap.remove(i) != null) { continue; } System.out.println("delete error;"); } } else if (type == 2) { ifMenu = false; //table 2 2022/12/3 15/03/02 tcd = Integer.parseInt(s[1]); try { String s1 = s[2] + " " + s[3]; sdf.parse(s1); } catch (Exception e) { System.out.println(tcd + " date error"); continue; } LocalDate date = LocalDate.parse(s[2], formatter); if (!((date.isAfter(startDate) || date.isEqual(startDate)) && (date.isBefore(endDate) || date.isEqual(endDate)))) { System.out.println("not a valid time period"); continue; } if (tcd > 55 || tcd < 1) { System.out.println(tcd + " table num out of range"); continue; } if (s[1].charAt(0) == '0') { System.out.println("wrong format"); continue; } boolean ifExist = false; if (checkTime(s[2] + " " + s[3]) == 0) { System.out.println("table " + tcd + " out of opening hours"); continue; } table = new Table(tcd, s[2] + " " + s[3]); tableList.add(table); ifTable = true; oFlag = 1; System.out.println("table " + tcd + ": "); } else if (type == 3 && ifTable) { //1 麻婆豆腐 2 2 if (s[0].charAt(0) == '0') { System.out.println("wrong format"); oFlag++; continue; } Dish dish = menu.find(s[1]); if (dish == null) { oFlag++; System.out.println(s[1] + " does not exist"); } else { int no = Integer.parseInt(s[0]); int n = Integer.parseInt(s[3]); int p = Integer.parseInt(s[2]); if (n < 0 || n > 15) { oFlag++; System.out.println(no + " num out of range " + n); continue; } if (p > 3 || p < 1) { oFlag++; System.out.println(no + " portion out of range " + p); continue; } if (no != oFlag) { // oFlag++; System.out.println("record serial number sequence error"); continue; } oFlag++; Record record = new Record(dish, n, Integer.parseInt(s[2])); table.add(record, no); System.out.println(no + " " + dish.name + " " + record.price); } } else if (type == 1) { if (ifMenu) { //麻婆豆腐 12 menu.add(s[0], Integer.parseInt(s[1]), false); } else { System.out.println("invalid dish"); } } else if (type == 6) { if (ifMenu) { //麻婆豆腐 12 T if (s[1].charAt(0) == '0') { System.out.println("wrong format"); continue; } int price = Integer.parseInt(s[1]); if (price < 0 || price > 300) { System.out.println(s[0] + " price out of range " + price); continue; } menu.add(s[0], price, true); } else { System.out.println("invalid dish"); } } else if (s.length == 5 && ifTable) { //1 4 麻婆豆腐 1 1 Dish dish = menu.find(s[2]); if (dish == null) { System.out.println(s[2] + " does not exist"); } else { int tableNo = Integer.parseInt(s[0]); int ordern = Integer.parseInt(s[1]); int gg = Integer.parseInt(s[3]); int fs = Integer.parseInt(s[4]); Record rec = new Record(dish, fs, gg); table.otherMap.put(ordern, rec.price); System.out.println(ordern + " table " + tcd + " pay for table " + tableNo + " " + rec.price); } } } Map<Integer, List<Table>> map = tableList.stream().collect(Collectors.groupingBy(Table::gettNo)); for (List<Table> value : map.values()) { if (value.size() == 1) { Table t = value.get(0); getOneInfo(t); } else if (value.size() == 2) { Table t1 = value.get(0); Table t2 = value.get(1); if (t1.date.split(" ")[0].equals(t2.date.split(" ")[0])) { float v = checkTime(t1.date); if (v == 0.0f) { System.out.println("table " + t1.tNo + " out of opening hours"); } else if (v == 1.0f) { System.out.println("table " + t1.tNo + ": " + t1.all() + t2.all() + " " + t1.all() + t2.all()); } else { System.out.println("table " + t1.tNo + ": " + t1.all() + t2.all() + " " + t1.all(true, v) + t2.all(true, v)); } } else { getOneInfo(t1); getOneInfo(t2); } } } for (Table t : tableList) { if (t == null) continue; } } private static void getOneInfo(Table t) { float v = checkTime(t.date); if (v == 0.0f) { System.out.println("table " + t.tNo + " out of opening hours"); } else if (v == 1.0f) { System.out.println("table " + t.tNo + ": " + t.all() + " " + t.all()); } else { System.out.println("table " + t.tNo + ": " + t.all() + " " + t.all(true, v)); } } } class Dish { String name; int price; boolean ifT; public Dish(String name, int price, boolean ifT) { this.name = name; this.price = price; this.ifT = ifT; } } class Menu { int count = 0; Dish[] dishes = new Dish[60]; public void add(String name, int price, boolean ifT) { dishes[count] = new Dish(name, price, ifT); count++; } public Dish find(String name) { Dish r = null; for (int i = 0; i < dishes.length; i++) { if (dishes[i] != null && dishes[i].name.equals(name)) { r = dishes[i]; } } return r; } } class Record { Dish dish; int price; boolean ifT; int gg; int fs; public void setPrice() { if (gg == 1) { price = dish.price * fs; } else if (gg == 2) { price = Math.round((float) dish.price * 1.5f) * fs; } else if (gg == 3) { price = dish.price * 2 * fs; } } public Record(Dish dish, int fs, int gg) { this.dish = dish; this.gg = gg; this.fs = fs; this.ifT = dish.ifT; setPrice(); } public void addFs(int fs) { this.fs += fs; setPrice(); } } class Table { int tNo; Record[] records = new Record[100]; HashMap<Integer, Integer> deleteNo = new HashMap<>(); String date; Map<Integer, Integer> otherMap = new HashMap<>(); public Table(int tNo, String date) { this.tNo = tNo; this.date = date; } public void add(Record record, int no) { records[no] = record; } public int gettNo() { return tNo; } public boolean del(int no) { if (records[no] == null) { return false; } else { records[no] = null; return true; } } public int all(boolean dazhe, float xishu) { int sum = 0; for (Record record : records) { if (record == null) continue; if (dazhe) { if (record.ifT) { sum += Math.round((float) record.price * 0.7); } else { sum += Math.round((float) record.price * xishu); } } else { sum += record.price; } } for (Integer value : otherMap.values()) { sum += value; } return sum; } public int all() { int sum = 0; for (Record record : records) { if (record == null) continue; sum += record.price; } for (Integer value : otherMap.values()) { sum += value; } return sum; } boolean checkIfDelete(Integer no) { if (deleteNo.get(no) == null) { deleteNo.put(no, no); return true; } return false; } }
这一个菜单主考考察对时间的判断,尤其是工作日的营业时间的折扣计算,假期的营业时间折扣计算。同样菜单里包含adddish的操作将dish存贮起来,dish里面新加了属性T是来判断是否是特色菜的功用,而table类则设计了all方法用来总和价格。add用来加入点菜,del用来删除点菜订单记录,checkifdelete判断是否已经删除成功来是否输出已经重复删除,这一个菜单主要围绕table类进行展开,根据table进行分类判断。更多的考察是正则表达式的应用来判断是否正确输入。
这里是类图的构架。
第5次菜单也就是最近的一次
import java.time.DayOfWeek; import java.time.LocalDate; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String a; int[] c = new int[10000]; Menu p = new Menu(); Order l = new Order(); Order rt = new Order(); table [] t=new table[1000]; Dish q; int count = 0; int flag = 0; int con = 0; int flag1 = 0; int count1 = 0; int rw = 0; int[] a1 = new int[10000]; while (true) { a = in.nextLine(); if (a.equals("end")) break; String[] b = a.split(" "); if (b.length == 2 && !b[1].equals("delete")) { p.m[rw] = p.addDish(b[0], Integer.parseInt(b[1])); rw++; } // Dish q; // for (int i = 0; i < count; i++) { // q = p.searthDish(l.records[i].d.name); // if (q == null) // System.out.println(l.records[i].d.name + " does not exist"); if(b[0].equals("table")) { flag1=Integer.parseInt(b[1]); count++; con=0; } if(b.length==4&&a.length()<11) { t[flag1]=new table(); t[flag1].x1.records[con]= rt.addARecord(Integer.parseInt(b[0]), b[1], Integer.parseInt(b[2]),Integer.parseInt(b[3])); t[flag1].x1.ci++; t[flag1].x1.records[con].d.unit_price=p.m[con].unit_price; t[flag1].jiage+=t[flag1].x1.records[con].getPrice(); con++; } if(b[1].equals("delete")) { if(t[flag1].x1.delARecordByOrderNum(Integer.parseInt(b[0]))==0) t[flag1].x1.check++; t[flag1].jiage-=t[flag1].x1.delARecordByOrderNum(Integer.parseInt(b[0])); } } for(int i=1;i<=count;i++) { System.out.println("table "+i+":"); for(int j=1;j<t[i].x1.ci;j++) { System.out.println(j+1+" "+t[i].x1.records[j].d.name+" "+t[i].x1.records[i].getPrice()); } for (int sss = 0; sss < rw; sss++) { q = p.searthDish(t[i].x1.records[i].d.name); if (q == null) System.out.println(t[i].x1.records[i].d.name + " does not exist");} for(int iq=0;iq<t[i].x1.check;i++) { System.out.println("delete error;"); } } for(int i=1;i<=count;i++) { System.out.println("table "+i+": "+t[i].jiage); } } } class Dish { String name; int unit_price; int getPrice(int portion) { if (portion == 1) { return ((int) (Math.round(unit_price * 1.0))); } if (portion == 2) { return ((int) (Math.round(unit_price * 1.5))); } if (portion == 3) { return ((int) (Math.round(unit_price * 2.0))); } return 0; } } class Menu { Dish[] m=new Dish[10000]; int count=0; Dish searthDish(String dishName) { for(int i=0;i<count;i++) { if(dishName.equals(m[i].name)) { return m[i]; } } return null; } Dish addDish(String dishName,int unit_price) { Dish s = new Dish(); s.name=dishName; s.unit_price=unit_price; count++; return s; } } class Record { Dish d=new Dish(); int portion; int fen; int xu; int getPrice() { return fen*d.getPrice(portion); } } class Order { Record[] records=new Record[50000]; int sum=0; int i1; int check=0; int ci=0; int getTotalPrice() { for(int i=0;i<i1;i++) { sum=sum+records[i].getPrice(); } return sum; } Record addARecord(int xu,String dishName,int portion,int fen) { Record o=new Record(); o.d.name=dishName; o.portion = portion; o.fen=fen; o.xu=xu; i1++; return o; } int delARecordByOrderNum(int orderNum) { if(findRecordByNum(orderNum)==true) { sum=0-records[orderNum].getPrice(); return sum; } else return 0; } boolean findRecordByNum(int orderNum) { for(int i=0;i<i1;i++) { if(orderNum==i) return true; } return false; } } class table { int tablenumber; int nian; int yue; int ri; int shi; int fen; int miao; int jiage; Order x1 = new Order(); }
这个菜单我因为我个人原因导致没有写完,做的很有限,只是简单的分出了大概的功用但是具体的判断还没搞出来,这个菜单是从菜单三开始应该改进的,因为我没写完所以这个菜单少聊一点,仅仅聊一下我的观点看法思路。这一次菜单相对于之前添加了辣度,增加了川菜浙菜晋菜三个菜系分别对应辣度酸度甜度,当时特色菜时才会进行判断,这里可以设计一个类是特色菜,而特色菜是川菜浙菜晋菜的父类。这里就设计到了继承也就是最近学习的知识点。这就是对最近知识的考察。同时再在特色类里加一个判断甜度酸度辣度的方法,更多的是考察内部类,类与类之间的关系。内部类在这几次菜单里可谓是必须要掌握的,就比如桌子table类和什么有关,和订单record有关和点菜order,而order和什么有关和dish有关。dish又与特色菜类有关所以特色菜又是dish的内部类。这里就蕴含了类嵌套类的模式。
期中考试:
代码先都放在这里
import java.util.Scanner; import java.lang.Math; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in); double a; Circle b = new Circle(); a=in.nextDouble(); if(a>0) { b.setr(a); b.shuchu(); } else { System.out.println("Wrong Format"); } } } class Circle{ private double r; void setr(double r) { this.r=r; } void shuchu() { double mianji; mianji=r*r*Math.PI; System.out.printf("%.2f",mianji); } }
import java.util.Scanner; import java.lang.Math; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in); String sa; double a,b,c,d; a=in.nextDouble(); b=in.nextDouble(); c=in.nextDouble(); d=in.nextDouble(); Rectangle q = new Rectangle(); q.topLeftPoint.setX(a); q.topLeftPoint.setY(b); q.lowerRightPoint.setX(c); q.lowerRightPoint.setY(d); double le=q.getLength(); double he=q.getHeight(); double ss=he*le; System.out.printf("%.2f",ss); } } class Rectangle{ Point topLeftPoint = new Point(); Point lowerRightPoint = new Point(); Point getTopLeftPoint() { return topLeftPoint; } void setTopLeftPoint(Point topLeftPoint) { this.topLeftPoint=topLeftPoint; } Point getLowerRightPoint() { return lowerRightPoint; } void setLowerRightPoint(Point lowerRightPoint) { this.lowerRightPoint=lowerRightPoint; } double getLength() { double Length; Length=Math.abs(topLeftPoint.x-lowerRightPoint.x); return Length; } double getHeight(){ double Height; Height=Math.abs(topLeftPoint.y-lowerRightPoint.y); return Height; } // double getArea() // { // double s; // return s; // } } class Point{ double x; double y; double getX() { return x; } double getY() { return y; } void setX(double x) { this.x=x; } void setY(double y) { this.y=y; } }
import java.util.Scanner; import java.lang.Math; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in); int choice = in.nextInt(); switch(choice) { case 1://Circle double radiums = in.nextDouble(); Circle as= new Circle(); if(radiums>0){ as.setRadiums(radiums); double s=as.getArea(radiums); System.out.printf("%.2f",s);} else System.out.println("Wrong Format"); break; case 2://Rectangle double a,b,c,d; a=in.nextDouble(); b=in.nextDouble(); c=in.nextDouble(); d=in.nextDouble(); Rectangle q = new Rectangle(); q.topLeftPoint.setX(a); q.topLeftPoint.setY(b); q.lowerRightPoint.setX(c); q.lowerRightPoint.setY(d); double le=q.getLength(); double he=q.getHeight(); double ss=he*le; System.out.printf("%.2f",ss); break; } } } class Shape{ public double getArea() { return 0; } } class Rectangle extends Shape{ Point topLeftPoint = new Point(); Point lowerRightPoint = new Point(); Point getTopLeftPoint() { return topLeftPoint; } void setTopLeftPoint(Point topLeftPoint) { this.topLeftPoint=topLeftPoint; } Point getLowerRightPoint() { return lowerRightPoint; } void setLowerRightPoint(Point lowerRightPoint) { this.lowerRightPoint=lowerRightPoint; } double getLength() { double Length; Length=Math.abs(topLeftPoint.x-lowerRightPoint.x); return Length; } double getHeight(){ double Height; Height=Math.abs(topLeftPoint.y-lowerRightPoint.y); return Height; } double getArea(double Length,double Height) { double s=Length*Height; return s; } } class Circle extends Shape{ double radiums = 0; double getRadiums() { return radiums; } void setRadiums(double radiums){ this.radiums=radiums; } double getArea(double radiums) { double s=radiums*radiums*Math.PI; return s; } } class Point{ double x; double y; double getX() { return x; } double getY() { return y; } void setX(double x) { this.x=x; } void setY(double y) { this.y=y; } }
import java.util.Scanner; import java.lang.Math; import java.util.ArrayList;; import java.util.Comparator; import java.lang.Comparable; import java.util.List; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in); int choice = in.nextInt(); ArrayList<Shape> list = new ArrayList<>(); while(choice != 0){ switch(choice){ case 1://Circle double radiums = in.nextDouble(); Circle as= new Circle(); list.add(circle); if(radiums>0) { as.setRadiums(radiums); double s=as.getArea(radiums); } else {System.out.println("Wrong Format");} break; case 2://Rectangle double a,b,c,d; a=in.nextDouble(); b=in.nextDouble(); c=in.nextDouble(); d=in.nextDouble(); Rectangle q = new Rectangle(); q.topLeftPoint.setX(a); q.topLeftPoint.setY(b); q.lowerRightPoint.setX(c); q.lowerRightPoint.setY(d); list.add(rectangle); double le=q.getLength(); double he=q.getHeight(); double ss=he*le; break; } choice = in.nextInt(); } list.sort(Comparator.naturalOrder());//正向排序 for(int i = 0; i < list.size(); i++) { System.out.print(String.format("%.2f", list.get(i).getArea()) + " "); } } } class Shape implements comparator{ public double getArea() { return 0; } } class Rectangle extends Shape{ Point topLeftPoint = new Point(); Point lowerRightPoint = new Point(); Point getTopLeftPoint() { return topLeftPoint; } void setTopLeftPoint(Point topLeftPoint) { this.topLeftPoint=topLeftPoint; } Point getLowerRightPoint() { return lowerRightPoint; } void setLowerRightPoint(Point lowerRightPoint) { this.lowerRightPoint=lowerRightPoint; } double getLength() { double Length; Length=Math.abs(topLeftPoint.x-lowerRightPoint.x); return Length; } double getHeight(){ double Height; Height=Math.abs(topLeftPoint.y-lowerRightPoint.y); return Height; } double getArea(double Length,double Height) { double s=Length*Height; return s; } } class Circle extends Shape{ double radiums = 0; double getRadiums() { return radiums; } void setRadiums(double radiums){ this.radiums=radiums; } double getArea(double radiums) { double s=radiums*radiums*Math.PI; return s; } } class Point{ double x; double y; double getX() { return x; } double getY() { return y; } void setX(double x) { this.x=x; } void setY(double y) { this.y=y; } } interface comparator{ int compareTo(Shape); }
以上是这四道题的代码,第一题和第二题相对与第三题和第四题来说是基础,第三题和第四题都是在第一二题的基础上进行的。第一题要求求圆形的面积,第二题要求求矩形的面积,第三题要求类的设计也就是设计个父类shape类circle类和rectangle类的父类
这是要求设计的类的关系,point类试试rectagantle的内部类即矩形又点构成,x和y是从输入台里面获取得到的,getx和gety就是从输入台得到后传入到类内部来进行求值,同类getlength和getheight也是同理的关系
同时circle类也同rectangle是并列的关系同属于shape的子类,这里考到了继承。这里的方法在以上代码都有体现就不说了
而最后一题考到了接口,这里由于我对接口的不熟悉以及对Comparable接口的不熟悉没有拿到满分,这里我找到了答案看到后发现就是一个简单的接口设计,Comparable接口到shape类对shape类的area进行排序其中用到了list包里的arraylist排序用list.sort来进行面积的排序然后输出。
采坑心得:由于我第二次菜单没写,我就简单的说说其中提交的问题包括期中考试一开始求距离时忘记了加绝对值。
提交时忘记了半径为0的情况。
输入的时候我以为6 5.8 -7 8.9这样子的输入可以看成为字符串其实是不可以的,因为又有double又有int,只能用in.nextdouble();来写。
菜单里面正则表达式用不准确会经常报错甚至起不到作用。
改进建议:
尽量多去总结类类之间的关系,创建父类子类等继承关系方便后续改进代码,而不是以面向过程的思路去写代码,这样子和c语言无异,违背了java面对对象的原则。
尽量改代码使得代码时间复杂度下降,而不是一直ctrl+c和ctrl+v尽量用类里的方法去解决。
多用调试的方法找到问题所在。
总结:
这几次学习让我认识到了我的不足,尤其是对正则表达式的应用生疏,对继承子类父类的知识不熟悉。
上课多注意老师的纠出来的易错点,都可能是我们会出现的。
下次上课一定要带电脑而不是纸上谈兵多写代码总胜过多空口夸谈。
应该尽量减少面对过程的代码量尽量面向对象。
标签:return,int,double,System,BLOG,new,out From: https://www.cnblogs.com/ALEXLJH/p/17407024.html