- 前言:
关于类:
1、类似C++中的struct,构造函数、内置方法(函数 )都比较相似
2、但是无法重载运算符,这是比较难受的一点。
3、尽量避免代码的重复,把private和public的方法搞清晰。
4、把我们需要解决的问题进行拆分,拆得好就写得快
关于java语法:
1、小心使用运算符,第一次作业用==去比较两个BigInteger调了很久。
2、慎用clone,(我选择手写copy
3、注意可变类的赋值是引用
其实我学得不太多,理解也不够深,像继承等等东西,也没怎么用过,等以后再来谈这方面吧。
- 设计与分析:
类图:
源码:
import java.text.ParseException;
import java.time.DateTimeException;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws ParseException {
Menu menu = new Menu();
ArrayList<Table> tables = new ArrayList<>();
Scanner input = new Scanner(System.in);
String str1;
int i = 0;
int portion = 0, quota = 0;
while (true) {
// 输入菜单
Dish temp = new Dish();
int isRepeat = -1;
str1 = input.nextLine();
if (str1.matches("[\\S]* [1-9][\\d]*")) {
String[] token = str1.split(" ");
temp.name = token[0];
temp.unit_price = Integer.parseInt(token[1]);
if (temp.unit_price > 300) {
System.out.println(temp.name + " price out of range " + temp.unit_price);
continue;
}
temp.isT = false;
isRepeat = menu.searchDish(temp.name);
if (isRepeat != -1) {
menu.dishs.remove(isRepeat);
}
menu.dishs.add(temp);
} else if (str1.matches("[\\S]* [\\d]* T")) {
String[] token = str1.split(" ");
temp.name = token[0];
temp.unit_price = Integer.parseInt(token[1]);
if (temp.unit_price > 300) {
System.out.println(temp.name + " price out of range " + temp.unit_price);
continue;
}
temp.isT = true;
if (isRepeat != -1) {
menu.dishs.remove(isRepeat);
}
menu.dishs.add(temp);
} else if (str1.equals("end")) {
break;
} else if (str1.matches("tab.*")) {
break;
} else {
System.out.println("wrong format");
continue;
}
}
while (!str1.equals("end")) {
Table temp = new Table();
boolean isRepeat = false;
int repeatNum = 0;
if (str1.matches("table.*")) {
if (str1.matches("table [1-9][\\d]* [\\d]*/[\\d][\\d]?/[\\d][\\d]? [\\d][\\d]?/[\\d][\\d]?/[\\d][\\d]?")) {
String[] token = str1.split(" ");
String[] Date = token[2].split("/");
String[] Time = token[3].split("/");
int[] intDate = new int[3];
int[] intTime = new int[3];
for (i = 0; i < 3; i++) {
intDate[i] = Integer.parseInt(Date[i]);
intTime[i] = Integer.parseInt(Time[i]);
}
temp.num = Integer.parseInt(token[1]);
if (temp.num > 55) {
System.out.println(temp.num + " table num out of range");
str1 = input.nextLine();
continue;
}
try {
temp.time = LocalDateTime.of(intDate[0], intDate[1], intDate[2], intTime[0], intTime[1],
intTime[2]);
temp.getWeekDay();
} catch (DateTimeException e) {
System.out.println(temp.num + " date error");
str1 = input.nextLine();
continue;
}
if (!(temp.time.isAfter(LocalDateTime.of(2022, 1, 1, 0, 0, 0))
&& temp.time.isBefore(LocalDateTime.of(2024, 1, 1, 0, 0, 0)))) {
System.out.println("not a valid time period");
str1 = input.nextLine();
continue;
}
// 判断桌号是否重复
if (temp.isOpen()) {
for (i = 0; i < tables.size(); i++) {
// 有重复的桌号
if (temp.num == tables.get(i).num && tables.get(i).isOpen()) {
Duration duration = Duration.between(temp.time, tables.get(i).time);
// 同一天
if (duration.toDays() == 0) {
// 在周一到周五
if (temp.weekday > 0 && temp.weekday < 6) {
// 在同一时间段
if (temp.time.getHour() < 15 && tables.get(i).time.getHour() < 15) {
temp = tables.get(i);
isRepeat = true;
repeatNum = i;
break;
}
}
// 在周末
else {
// 时间相差小于一小时
if (duration.toHours() < 3600) {
temp = tables.get(i);
repeatNum = i;
isRepeat = true;
break;
}
}
}
}
}
}
if (!isRepeat) {
System.out.println("table " + temp.num + ": ");
}
} else {
System.out.println("wrong format");
str1 = input.nextLine();
continue;
}
// 本桌开始点菜
while (true) {
str1 = input.nextLine();
if (str1.matches("[1-9][\\d]* [\\S]* [\\d] [1-9][\\d]*")) {
String[] inform = str1.split(" ");
portion = Integer.parseInt(inform[2]);
quota = Integer.parseInt(inform[3]);
if (temp.order.records.size() > 0) {
if (Integer.parseInt(inform[0]) <= temp.order.records.get(temp.order.records.size() - 1).orderNum) {
System.out.println("record serial number sequence error");
continue;
}
}
if (menu.searchDish(inform[1]) == -1) {
System.out.println(inform[1] + " does not exist");
continue;
}
if (portion > 3 || portion < 1) {
System.out.println(Integer.parseInt(inform[0]) + " portion out of range " + portion);
continue;
}
if (quota > 15) {
System.out.println(Integer.parseInt(inform[0]) + " num out of range " + quota);
continue;
}
temp.od(menu, inform[0], inform[1], portion, quota);
}
// 判断是否为删除订单
else if (str1.matches("[1-9][\\d]* delete")) {
String[] delete = str1.split(" ");
temp.order.delARecordByOrderNum(Integer.parseInt(delete[0]));
}
// 判断是否为夹杂菜单
else if (str1.matches("[\\S]* [\\d]*")) {
System.out.println("invalid dish");
continue;
} else if (str1.matches("[\\S]* [\\d]* T")) {
System.out.println("invalid dish");
continue;
}
else if (str1.equals("end")) {
break;
} else if (str1.matches("ta.*")) {
break;
} else {
System.out.println("wrong format");
continue;
}
}
// 本桌点菜结束,进入下一桌
if (isRepeat) {
tables.remove(repeatNum);
}
temp.getSum();
tables.add(temp);
} else if (str1.matches("t.*")) {
System.out.println("wrong format");
isRepeat = true;
if (tables.size() != 0) {
temp = tables.get(tables.size() - 1);
}
while (true) {
str1 = input.nextLine();
if (str1.matches("[1-9][\\d]* [\\S]* [\\d] [1-9][\\d]*")) {
String[] inform = str1.split(" ");
portion = Integer.parseInt(inform[2]);
quota = Integer.parseInt(inform[3]);
// 判断订单号是否由小到大排列
if (temp.order.records.size() > 0) {
if (Integer.parseInt(inform[0]) <= temp.order.records.get(temp.order.records.size() - 1).orderNum) {
System.out.println("record serial number sequence error");
continue;
}
}
if (menu.searchDish(inform[1]) == -1) {
System.out.println(inform[1] + " does not exist");
continue;
}
if (portion > 3 || portion < 1) {
System.out.println(Integer.parseInt(inform[0]) + " portion out of range " + portion);
continue;
}
if (quota > 15) {
System.out.println(Integer.parseInt(inform[0]) + " num out of range " + quota);
continue;
}
temp.od(menu, inform[0], inform[1], portion, quota);
}
// 判断是否为删除订单
else if (str1.matches("[1-9][\\d]* delete")) {
String[] delete = str1.split(" ");
temp.order.delARecordByOrderNum(Integer.parseInt(delete[0]));
}
// 判断是否为夹杂菜单
else if (str1.matches("[\\S]* [\\d]*")) {
System.out.println("invalid dish");
continue;
} else if (str1.matches("[\\S]* [\\d]* T")) {
System.out.println("invalid dish");
continue;
}
// 判断是否为代点
else if (str1.matches("[\\d]* [\\d]* [\\S]* [\\d] [1-9][\\d]*")) {
String[] token = str1.split(" ");
// 判断代点桌号是否存在
boolean exist = false;
for (int j = 0; j < tables.size(); j++) {
if (tables.get(j).num == Integer.parseInt(token[0])) {
exist = true;
break;
}
}
if (exist) {
System.out.print(Integer.parseInt(token[1]) + " table " + temp.num + " pay for table " + Integer.parseInt(token[0]) + " ");
Record treat = new Record();
treat.d = menu.dishs.get(menu.searchDish(token[2]));
portion = Integer.parseInt(token[3]);
quota = Integer.parseInt(token[4]);
treat.portion = portion;
treat.quota = quota;
System.out.print(treat.getPrice() + "\n");
temp.sum += treat.getPrice();
}
// 若不存在则输出内容
else {
System.out.println("Table number :" + Integer.parseInt(token[0]) + " does not exist");
}
} else if (str1.equals("end")) {
break;
} else {
System.out.println("wrong format");
continue;
}
}
if (tables.size() != 0) {
tables.remove(tables.size() - 1);
temp.sum = 0;
temp.origSum = 0;
temp.getSum();
tables.add(temp);
}
} else {
str1 = input.nextLine();
}
}
// 最终输出桌号订单信息
for (i = 0; i < tables.size(); i++) {
if (tables.get(i).isOpen()) {
System.out.println("table " + tables.get(i).num + ": " + tables.get(i).origSum + " " + tables.get(i).sum);
} else
System.out.println("table " + tables.get(i).num + " out of opening hours");
}
}
static class Dish {
String name;
int unit_price;
boolean isT = false;
}
static class Record {
int orderNum;
Dish d;
int portion;
int quota;
boolean isDeleted = false;
int getPrice() {
if (portion == 2)
return (int) Math.round(1.5 * d.unit_price) * quota;
else if (portion == 3)
return 2 * d.unit_price * quota;
else
return d.unit_price * quota;
}
}
static class Menu {
ArrayList<Dish> dishs = new ArrayList<>();
int searchDish(String dishName) {
for (int i = 0; i < dishs.size(); i++) {
if (dishName.equals(dishs.get(i).name)) {
return i;
}
}
return -1;
}
}
static class Order {
ArrayList<Record> records = new ArrayList<>();
Record addARecord(int orderNum, String dishName, int portion, int quota, Menu menu) {
Record newRecord = new Record();
newRecord.orderNum = orderNum;
newRecord.d = menu.dishs.get(menu.searchDish(dishName));
newRecord.portion = portion;
newRecord.quota = quota;
System.out.println(newRecord.orderNum + " " + newRecord.d.name + " " + newRecord.getPrice());
return newRecord;
}
boolean delARecordByOrderNum(int orderNum) {
int i = 0, flag = 0;
for (i = 0; i < records.size(); i++) {
if (records.get(i).orderNum == orderNum) {
if (records.get(i).isDeleted == false) {
records.get(i).isDeleted = true;
} else {
System.out.println("deduplication " + orderNum);
}
flag++;
}
}
if (flag == 0) {
System.out.println("delete error;");
return false;
}
return true;
}
}
static class Table {
Order order = new Order();
int num;
LocalDateTime time;
int weekday;
long sum = 0;
long origSum = 0;
void od(Menu menu, String str1, String str2, int portion, int quota) {
{
order.records.add(order.addARecord(Integer.parseInt(str1), str2, portion, quota, menu));
}
}
void getWeekDay() {
weekday = time.getDayOfWeek().getValue();
}
void getSum() {
for (int i = 0; i < order.records.size(); i++) {
if (!order.records.get(i).isDeleted) {
origSum += order.records.get(i).getPrice();
if (order.records.get(i).d.isT) {
if (weekday > 0 && weekday < 6) {
sum += Math.round(order.records.get(i).getPrice() * 0.7);
} else {
sum += order.records.get(i).getPrice();
}
} else {
if (weekday > 0 && weekday < 6) {
if (time.getHour() >= 17 && time.getHour() < 20)
sum += Math.round(order.records.get(i).getPrice() * 0.8);
if (time.getHour() == 20) {
if (time.getMinute() <= 30)
sum += Math.round(order.records.get(i).getPrice() * 0.8);
}
if (time.getHour() >= 10 && time.getHour() < 14)
sum += Math.round(order.records.get(i).getPrice() * 0.6);
if (time.getHour() == 14) {
if (time.getMinute() <= 30)
sum += Math.round(order.records.get(i).getPrice() * 0.6);
}
} else sum += order.records.get(i).getPrice();
}
}
}
}
boolean isOpen() {
if (weekday > 0 && weekday < 6) {
if (time.getHour() >= 17 && time.getHour() < 20)
return true;
if (time.getHour() == 20) {
if (time.getMinute() <= 30)
return true;
}
if (time.getHour() > 10 && time.getHour() < 14)
return true;
if (time.getHour() == 10) {
if (time.getMinute() >= 30)
return true;
}
if (time.getHour() == 14) {
if (time.getMinute() <= 30)
return true;
}
} else {
if (time.getHour() > 9 && time.getHour() < 21)
return true;
if (time.getHour() == 9) {
if (time.getMinute() >= 30)
return true;
}
if (time.getHour() == 21) {
if (time.getMinute() <= 30)
return true;
}
}
return false;
}
}
}
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);
printArea(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);
printArea(rectangle);
break;
}
}
第四次:
本次作业中对时间范围进行了界定,运用Date类的format将字符串转化成整数后进行判断即可;
以及table格式的判断,最好的方法就是使用正则表达式,将正确格式正则化,其余格式均不做输入操作,并输出“Wrong Format”;
第五次:
本次作业中往Dish中添加了口味度,最后根据口味度判断该table的酸/辣/甜度,
同样考察了正则表达式;
期中:
期中考察难度比较基础,题目循序渐进,基本上完成了第二题就算完成了大部分题目,考察了抽象接口和抽象类的使用,同时还有内部类的考察;
- 采坑心得:
我认为写程序和人体的免疫一样,有三道防线:
优秀的架构与细节设计。优秀的含义很广泛,清晰、可读、易维护、易扩展的设计往往是鲁棒的,这样的设计往往很难出严重的纰漏,因为很多错误已经被规避掉了,即使有错误,发现、纠正的难度也不会太大
内陷的异常、错误与断言。这些人为指定的运行行为是最容易帮助我们发现并定位潜在问题的
充分的测试,黑箱测、白箱测、压力测、让老妈帮你测,总之测就完事儿了。
出现bug往往是因为这三道防线被全部突破,无险可守了。
首先就是正则表达式,包括正则的效率问题。然后就是程序的鲁棒性,实际工程中程序对
种不同状况做出的反应都应该符合用户需求。最最重要的就是面向对象的思维。
- 改进建议:
难度建议不要太大,可以设计一些课外的知识,通过自学掌握,但类于类之间的关系不要太复杂,例如第五次作业第一题难度较大,第七次作业难度较合适,题量也合适。希望老师可以讲解PTA或者我们自己写过的作业,因为自己写的作业自己思考过,每个人知道自己的问题在哪里,带着问题去学习相比于课堂上的书本上的代码样例可能更能带来触动,并留下更深的印象!或者讲解题目涉及的课外知识等!
- 总结:
类的设计尽量做到,简单,复用性强,每个类的作用都明确切少。类的设计符合JAVA类设计的五大原则,做到独立可复用,类与类之间的关系也做到,简单明了,符合JAVA的三大特性。
标签:temp,str1,System,期中,time,println,out From: https://www.cnblogs.com/catfishhome/p/17516691.html