第二次博客作业
第二次博客作业
一、前言:总结之前所涉及到的知识点、题量、难度等情况
1、知识点:
(1)、菜单计价程序-4:
1时间格式合法: String[] parts = date.split("/");
int year = Integer.parseInt(parts[0]);
int month = Integer.parseInt(parts[1]);
int day = Integer.parseInt(parts[2]);
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, day);
int weekdayIndex = calendar.get(Calendar.DAY_OF_WEEK) - 1;
虽然我们输入的时候是以00:00:00的格式进行输入的,但是我们在Calender类中调用getInstance()方法需要将其改成00/00/00的格式。而且在西方是按照星期天为一星期第一天来进行计算的,所以在计算星期几的时候要在原有基础上-1。
2重复删除:重复的删除记录输出"deduplication : "+序号。
可以建立一个数组,假如要删除账单中第一条,则可以在a[1]中+1,如果此时a[n]大于等于1,则说明删除重复。
3序号按从小到大的顺序排列:
两种方法(冒泡输出,整体互换输出)
(2)、菜单计价程序-5:
1时间格式合法:同菜单计价程序-4。
2计算特色菜:特色菜和非特色菜的打折情况是分开来计算的。特色菜周一至周五 7折,周末全价。而普通菜周一至周五晚上8折,中午6折,其余时间不营业。
由于价格需要和时间挂钩,也与该菜本身的性质挂钩,所以在这里我将计算总价格的方法从order类搬到了table类。
3不考虑各桌时间段的问题,同一个客户的所有table金额都要累加。(循环计算总量)
(3)、期中考试:
继承的应用:
继承是一种面向对象编程的特性,它允许一个类(子类)继承另一个类(父类)的属性和方法。子类可以使用父类的属性和方法,但也可以添加自己的属性和方法,从而实现代码的重用和扩展。类A会继承类B的所有公共属性和方法,而且可以通过调用super关键字来访问父类的属性和方法。此外,子类A还可以覆盖(重写)父类的方法,使其具有不同的行为。同时也可以提高代码的可维护性和可重用性。(摘自百科)
在期中考试中,将测验1与测验2的类设计进行合并设计,抽象出Shape父类(抽象类),Circle及Rectangle作为子类。
抽象类与接口:
接口的调用(class Shape implements Comparable<Shape>),在定义的类中实现Comparable接口,并重写compareTo这个方法。可以将this当做之前compare中的第一个参数,入参后做为第二个参数使用。(参考资料:https://blog.csdn.net/Zlase/article/details/110852486)
list.sort(Comparator.naturalOrder());这是对数据正向排序的一个方法。
2、 题量:题目关于细节方面的点还是很多
二、设计与分析:重点对题目的提交源码进行分析,可参考SourceMonitor的生成报表内容以及PowerDesigner的相应类图,要有相应的解释和心得(做到有图有真相),本次Blog主要分析PTA中的菜单计价系列的题目以及期中考试的三道题目
菜单计价顺序-4:
(因和实验5类似,所以省略)
菜单计价程序-5:
import java.util.*;
class Dish {
String name;
String caixi;
int unit_price;
public Dish(String name, String caixi, int unit_price) {
this.name = name;
this.caixi = caixi;
this.unit_price = unit_price;
}
public int getPrice(int portion) {
double price = 0.0;
if (portion == 1) price = unit_price;
else if (portion == 2) price = unit_price * 1.5;
else if (portion == 3) price = unit_price * 2.0;
return (int) Math.round(price);
}
}
class Menu {
Map<String, Dish> dishMap = new HashMap<String, Dish>();
public Dish searchDish(String dishName) {
return dishMap.get(dishName);
}
public Dish addDish(String dishName, String caixi, int unit_price) {
Dish dish = new Dish(dishName, caixi, unit_price);
dishMap.put(dishName,dish);
dishMap.put(caixi,dish);
return dish;
}
}
class Record {
int orderNum;
Dish dish;
int taste=-2;
int portion;
int num;
//是否删除
boolean isDel=false;
//删除成功?
boolean isDelOrSue=false;
//代付?
boolean payOnBehalf=false;
int payTableId=0;
int payTableId2=0;
public Record(int orderNum, Dish dish, int portion, int num) {
this.orderNum = orderNum;
this.dish = dish;
this.portion = portion;
this.num = num;
}
public Record(int orderNum, Dish dish, int taste, int portion, int num) {
this.orderNum = orderNum;
this.dish = dish;
this.taste = taste;
this.portion = portion;
this.num = num;
}
public int getPrice() {
return !isDel?dish.getPrice(portion) * num:0;
}
public int getTaste() {
return !isDel?taste:-1;
}
}
class Order {
List<Record> records = new ArrayList<>();
public int getTotalPrice0() {
int totalPrice = 0;
for (Record record : records) {
if((record.dish.caixi.equals("川菜")&&record.taste>5)||(record.dish.caixi.equals("晋菜")&&record.taste>4)||(record.dish.caixi.equals("浙菜")&&record.taste>3))
continue;
else if(record.payTableId!=0)
continue;
else
totalPrice += record.getPrice();
}
return totalPrice;
}
public Record addARecord2(int orderNum, String dishName, int taste, int portion, int num, Menu menu) {
Dish dish = menu.searchDish(dishName);
if (dish == null || dish.unit_price == -1) {
dish = menu.addDish2(dishName,"0", 0);
}
Record record = new Record(orderNum, dish, taste, portion, num);
records.add(record);
return record;
}
public boolean delARecordByOrderNum(int orderNum) {
for (Record record1 : records) {
if (record1.orderNum==orderNum){
record1.isDel=true;
record1.isDelOrSue=true;
return true;
}
}
return false;
}
}
class Table {
int tableNum;
String name;
String phone;
String date;
String time;
Order order = new Order();
public Table(int tableNum,String name,String phone, String date, String time) {
this.tableNum = tableNum;
this.name = name;
this.phone = phone;
this.date = date;
this.time = time;
}
public double getTotalPrice2() {
String weekday = getWeekday(date);
if (weekday.equals("Monday") || weekday.equals("Tuesday") || weekday.equals("Wednesday")
|| weekday.equals("Thursday") || weekday.equals("Friday")) {
return 0.7;
}
else return 1;
}
public double day(){
String weekday = getWeekday(date);
time = time.replaceAll("/", ":"); // 将斜杠替换为冒号
String[] timeParts = time.split(":"); // 将时间拆分为小时、分钟和秒钟
if (timeParts[0].length() < 2) {
timeParts[0] = "0" + timeParts[0]; // 如果小时部分缺少前导零,则添加前导零
}
time= String.join(":", timeParts); // 将时间部分重新组合为一个字符串
if (weekday.equals("Saturday") || weekday.equals("Sunday")) {
if (time.compareTo("09:30:00") >= 0 && time.compareTo("21:30:00") <= 0) {
return 1.0;
}
else return 0;
}
else if (weekday.equals("Monday") || weekday.equals("Tuesday") || weekday.equals("Wednesday")
|| weekday.equals("Thursday") || weekday.equals("Friday")) {
if (time.compareTo("10:30:00") >= 0 && time.compareTo("14:30:00") <= 0) {
return 0.6;
} else if (time.compareTo("17:00:00") >= 0 && time.compareTo("20:30:00") <= 0) {
return 0.8;
} else {
return 0;
}
}
else return 0;
}
public int getTotalPrice0(){
return order.getTotalPrice0();
}
public int getTotalPrice() {
int totalPrice = 0;
for (Record record : order.records) {
if((record.dish.caixi.equals("川菜")&&record.taste>5)||(record.dish.caixi.equals("晋菜")&&record.taste>4)||(record.dish.caixi.equals("浙菜")&&record.taste>3))
continue;
else if(record.payTableId!=0)
continue;
else
{
if(record.taste!=-2)
totalPrice = totalPrice + (int) Math.round (record.getPrice() * getTotalPrice2());
else totalPrice = totalPrice + (int) Math.round(record.getPrice() * day());
}
}
return totalPrice;
}
public int getchuan(){
int numchuan = 0;
for (Record record : order.records) {
if(record.dish.caixi.equals("川菜")&&record.taste<=5&&record.getTaste()>=0)
numchuan += record.num;
}
return numchuan;
}
public int getjin(){
int numjin = 0;
for (Record record : order.records) {
if(record.dish.caixi.equals("晋菜")&&record.taste<=4&&record.getTaste()>=0)
numjin += record.num;
}
return numjin;
}
public int getzhe(){
int numzhe = 0;
for (Record record : order.records) {
if(record.dish.caixi.equals("浙菜")&&record.taste<=3&&record.getTaste()>=0)
numzhe += record.num;
}
return numzhe;
}
public double getchuanwei(){
double numchuanwei = 0;
for (Record record : order.records) {
if(record.dish.caixi.equals("川菜")&&record.taste<=5&&record.getTaste()>=0)
numchuanwei += (record.getTaste() * record.num);
}
return numchuanwei;
}
public double getjinwei(){
double numjinwei = 0;
for (Record record : order.records) {
if(record.dish.caixi.equals("晋菜")&&record.taste<=4&&record.getTaste()>=0)
numjinwei += (record.getTaste() * record.num);
}
return numjinwei;
}
public double getzhewei(){
double numzhewei = 0;
for (Record record : order.records) {
if(record.dish.caixi.equals("浙菜")&&record.taste<=3&&record.getTaste()>=0)
numzhewei += (record.getTaste() * record.num);
}
return numzhewei;
}
private String getWeekday(String date) { //星期
String[] parts = date.split("/");
int year = Integer.parseInt(parts[0]);
int month = Integer.parseInt(parts[1]);
int day = Integer.parseInt(parts[2]);
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, day);
int weekdayIndex = calendar.get(Calendar.DAY_OF_WEEK) - 1;
String[] weekdays = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
return weekdays[weekdayIndex];
}
}
class People {
String name=new String();
String phone=new String();
int zong=0;
public People(String name,String phone) {
this.name = name;
this.phone = phone;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Menu menu = new Menu();
List<Table> tables = new ArrayList<>();
Table currentTable = null;
boolean isTable=false;
while (scanner.hasNext()) {
String line = scanner.nextLine();
if (line.equals("end")) {
break;
}
String[] parts = line.split("\\s+");
if (parts[0].equals("table")) {
if(parts.length!=7){
System.out.println("wrong format");
System.exit(0);
}
else{
isTable=true;
int tableNum = Integer.parseInt(parts[1]);
String name = parts[3];
String phone = parts[4];
// int phoneNum = Integer.parseInt(parts[4].substring(0, 3));
String date = parts[5];
String time = parts[6];
currentTable = new Table(tableNum ,name ,phone ,date ,time);
tables.add(currentTable);
}
} else if (parts[1].equals("delete")) {
int orderNum = Integer.parseInt(parts[0]);
int totalPrice = currentTable.getTotalPrice0();
boolean b = currentTable.order.delARecordByOrderNum(orderNum);
Record record = currentTable.order.addARecord2(orderNum,"",0, -1, -1, menu);
record.dish.unit_price=-1;
record.isDelOrSue=b;
} else if (parts[0].equals("end")) {
break;
} else if (currentTable == null) {
if(parts.length==2){
String dishName = parts[0];
int unit_price = Integer.parseInt(parts[1]);
menu.addDish2(dishName,"0",unit_price);
}
else if(parts.length==4){
String dishName = parts[0];
String caixi = parts[1];
int unit_price = Integer.parseInt(parts[2]);
menu.addDish2(dishName, caixi , unit_price);
}
else System.out.println("wrong format");
}
else if (parts.length==4){
int tableNum = currentTable.tableNum;
int orderNum = Integer.parseInt(parts[0]);
String dishName = parts[1];
int taste = -2;
int portion = Integer.parseInt(parts[2]);
int num = Integer.parseInt(parts[3]);
for (Table table : tables) {
if (table.tableNum == tableNum) {
Record record = table.order.addARecord2(orderNum, dishName, taste, portion, num, menu);
break;
}
}
}
else if (parts.length==5){
int tableNum = currentTable.tableNum;
int orderNum = Integer.parseInt(parts[0]);
String dishName = parts[1];
int taste = Integer.parseInt(parts[2]);
int portion = Integer.parseInt(parts[3]);
int num = Integer.parseInt(parts[4]);
for (Table table : tables) {
if (table.tableNum == tableNum) {
Record record = table.order.addARecord2(orderNum, dishName, taste, portion, num, menu);
break;
}
}
}
else if (parts.length==6){
int tableNum = currentTable.tableNum;
int tableNum0 = Integer.parseInt(parts[0]);
int orderNum = Integer.parseInt(parts[1]);
String dishName = parts[2];
int taste = Integer.parseInt(parts[3]);
int portion = Integer.parseInt(parts[4]);
int num = Integer.parseInt(parts[5]);
for (Table table : tables) {
if (table.tableNum == tableNum) {
Record record = table.order.addARecord2(orderNum, dishName, -1, portion, num, menu);
record.payTableId2=Integer.parseInt(parts[0]);
record.payOnBehalf=true;
break;
}
}
for (Table table : tables) {
if (table.tableNum == tableNum0) {
Record record = table.order.addARecord2(orderNum, dishName, taste, portion, num, menu);
record.payTableId=Integer.parseInt(parts[0]);
record.payOnBehalf=true;
break;
}
}
}
}
ArrayList<Table> objects = new ArrayList<>();
for (Table table : tables) {
int x=Integer.parseInt(table.phone.substring(0, 3));
if(table.phone.length()!=11||(x!=180&&x!=181&&x!=189&&x!=133&&x!=135&&x!=136)){
System.out.println("wrong format");
System.exit(0);
}
}
for (Table table : tables) {
double panduan = table.day();
if (panduan==0){
System.out.println("table " + table.tableNum + " out of opening hours");
System.exit(0);
}
System.out.println("table " + table.tableNum + ": ");
for (Record record : table.order.records) {
if (!record.payOnBehalf){
if(record.dish.caixi.equals("川菜")&&record.taste>5)
System.out.println("spicy num out of range :" + record.taste);
else if(record.dish.caixi.equals("晋菜")&&record.taste>4)
System.out.println("acidity num out of range :" + record.taste);
else if(record.dish.caixi.equals("浙菜")&&record.taste>3)
System.out.println("sweetness num out of range :" + record.taste);
else if (record.dish.unit_price != 0&&record.dish.unit_price!=-1){
System.out.print(record.orderNum + " " + record.dish.name + " ");
System.out.println(record.dish.getPrice(record.portion) * record.num);
}
if (record.dish.unit_price == 0) {
System.out.println(record.dish.name+" does not exist");
}
if (record.dish.unit_price==-1){
if (!record.isDelOrSue|| record.isDel){
System.out.println("delete error;");
}
}
}else if(record.payTableId2>0){
System.out.println(record.orderNum + " table " + table.tableNum + " pay for table "+record.payTableId2+" "+ record.dish.getPrice(record.portion) * record.num);
}
objects.add(table);
}
}
for (Table table : tables) {
int totalPrice0 = table.getTotalPrice0();
int totalPrice = table.getTotalPrice();
int numchuan = table.getchuan();
int numjin = table.getjin();
int numzhe = table.getzhe();
double a=table.getchuanwei();
double b=table.getjinwei();
double c=table.getzhewei();
int numchuanwei=0;
int numjinwei=0;
int numzhewei=0;
if(numchuan!=0)
numchuanwei =(int) Math.round (a/numchuan);
if(numjin!=0)
numjinwei = (int) Math.round (b/numjin);
if(numzhe!=0)
numzhewei = (int) Math.round (c/numzhe);
String chuanchuan= new String();
String jinjin= new String();
String zhezhe=new String();
if(numchuanwei==0)
chuanchuan="不辣";
else if(numchuanwei==1)
chuanchuan="微辣";
else if(numchuanwei==2)
chuanchuan="稍辣";
else if(numchuanwei==3)
chuanchuan="辣";
else if(numchuanwei==4)
chuanchuan="很辣";
else if(numchuanwei==5)
chuanchuan="爆辣";
else System.exit(0);
if(numjinwei==0)
jinjin="不酸";
else if(numjinwei==1)
jinjin="微酸";
else if(numjinwei==2)
jinjin="稍酸";
else if(numjinwei==3)
jinjin="酸";
else if(numjinwei==4)
jinjin="很酸";
else System.exit(0);
if(numzhewei==0)
zhezhe="不甜";
else if(numzhewei==1)
zhezhe="微甜";
else if(numzhewei==2)
zhezhe="稍甜";
else if(numzhewei==3)
zhezhe="甜";
else System.exit(0);
int n=0;
System.out.print("table " + table.tableNum + ": "+totalPrice0+" "+totalPrice+" ");
if(numchuan != 0)
{
System.out.printf("川菜 %d %s",numchuan,chuanchuan);
n++;
}
if(numjin != 0)
{
if(n>0)
System.out.printf(" ");
System.out.printf("晋菜 %d %s",numjin,jinjin);
n++;
}
if(numzhe != 0)
{
if(n>0)
System.out.printf(" ");
System.out.printf("浙菜 %d %s",numzhe,zhezhe);
}
System.out.println("");
}
List<People> peoples = new ArrayList<>();
for ( Table table : tables){
People currentPeople=null;
int flag=0;
for(People people: peoples){
if(people.name.equals(table.name))
flag=1;
}
if(flag==0){
currentPeople = new People(table.name,table.phone);
peoples.add(currentPeople);
}
}
// for(int i=0;i<table.size();i++) {
// for(int j=0;i<table.size();i++)
// }
for (People people: peoples) {
for(Table table : tables)
{
if(people.name.equals(table.name))
people.zong +=table.getTotalPrice();
}
}
for(int i=8;i>=0;i--)
{
for (People people: peoples) {
if(people.name.length()==i)
System.out.println(people.name +" " + people.phone +" "+people.zong);
}
}
}
}
分析:在第五次作业中,菜单输入时增加特色菜,特色菜的输入格式:菜品名+英文空格+口味类型+英文空格+基础价格+"T",普通菜和特色菜的区别就在于多了一个口味类型。
为了简便,我将普通菜的taste全部设为-1,(特色菜的区间为0-5之间)这样可以保证互相不冲突。
在后面计算口味度平均值时,可以设一个条件,即必须taste在0-5之间的才可以相加。
在星期几的计算中,我调用了Calender类中的getInstance()方法(需要将其改成00/00/00的格式)并确定当天是星期几。
相关类图:

实验结果:

期中考试第三题(一二题省略):
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
double radiums = input.nextDouble();
Shape circle = new Circle(radiums);
if(radiums>0)
printArea(circle);
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.printf("%.2f",shape.getArea());
}
}
class Shape{
Shape(){
}
public double getArea()
{
return getArea();
}
}
class Circle extends Shape{
private double radiums=0;
Circle(){
}
Circle(double radiums)
{
this.radiums=radiums;
}
public double getRadiums()
{
return radiums;
}
public void setRadiums(double radiums)
{
this.radiums=radiums;
}
public double getArea()
{
return radiums*radiums*3.1415926535;
}
}
class Rectangle extends Shape{
Point topLeftPoint;
Point lowerRightPoint;
Rectangle(){
}
Rectangle(Point topLeftPoint,Point lowerRightPoint)
{
this.topLeftPoint=topLeftPoint;
this.lowerRightPoint=lowerRightPoint;
}
public Point getTopLeftPoint(){
return topLeftPoint;
}
public void setTopLeftPoint(Point topLeftPoint){
this.topLeftPoint=topLeftPoint;
}
public Point getLowerRightPoint(){
return lowerRightPoint;
}
public void setLowerRightPoint(Point lowerRightPoint){
this.lowerRightPoint=lowerRightPoint;
}
public double getLength(){
return Math.abs(topLeftPoint.x-lowerRightPoint.x);
}
public double getHeight(){
return Math.abs(topLeftPoint.y-lowerRightPoint.y);
}
public double getArea(){
return getHeight()*getLength();
}
}
class Point{
double x;
double y;
Point(){
}
Point(double x,double y)
{
this.x=x;
this.y=y;
}
public double getX(){
return x;
}
public void setX(double x){
this.x=x;
}
public double getY(){
return y;
}
public void setY(double y){
this.y=y;
}
}
分析:
将测验1与测验2的类设计进行合并设计,抽象出Shape父类(抽象类),Circle及Rectangle作为子类。
其中,printArea(Shape shape)方法为定义在Main类中的静态方法,体现程序设计的多态性。
相关类图:

实验结果:

三、采坑心得:对源码的提交过程中出现的问题及心得进行总结,务必做到详实,拿数据、源码及测试结果说话,切忌假大空
坑1:
在实验5中,本来想把table类中的时间被order类调用的,后面发现不行,只能table调用order方法,所以最后将价格的计算放到order里了。
坑2:
在实验5中,发现如果用传统方法来区分普通菜和特色菜,那将非常麻烦。如果将taste设为0,就很好地巧妙地避开了属性数量的不同这个问题。
坑3:
在期末考试4中,虽然引用了Comparable接口,重写compareTo这个方法,但是实际一直未排序,不知道怎么解决。
四、改进建议:对相应题目的编码改进给出自己的见解,做到可持续改进
改进1:
最后按拼音顺序输出每位客户(不考虑客户同名或拼音相同的情况)的支付金额,这里当时我投机取巧成功了,但是后面学习了Comparable接口之后可以用此方法来改进代码。
改进2:
可以使用继承方法来改进实验5代码,把菜系作为一个父类,把川菜,晋菜,浙菜作为子类,子类可以使用父类的关于特色菜的一些方法,但也可以添加自己的属性和方法(例如口维度区间),从而实现代码的重用和扩展,并提高代码的可维护性和可重用性。
五、总结:对本阶段(6-9周)综合性总结,学到了什么,哪些地方需要进一步学习及研究,对教师、课程、作业、实验、课上及课下组织方式等方面的改进建议及意见。
在这段学习期间我进一步强化了类的设计与架构,和有关继承和多态的方法。
例如:继承的权限:子类可以访问父类的 public 和 protected ,但无法访问 private 中的成员。
继承的方法覆盖:子类可以重写(override)父类中的方法,子类的方法必须和父类中的方法具有相同的名称、参数列表及返回值类型。
继承的方法调用:子类可以使用 super() 方法调用父类的方法。
希望老师上课的时候多教点语法,少叫我们画类图,语法为代码之基,很多语法光看书和视频根本没用,将类和语法合起来讲,让时间利用更有效率。
课下希望能留长一点时间给我们做大作业,其他班都是至少一个多星期完成,为何我们两个班只有三天时间完成?!博客的完成也是如此,时间太赶了,我们也有其他的作业和工作要做,希望进度能和其他班一样,不要太赶。

浙公网安备 33010602011771号