首页 > 其他分享 >OOP面向对象第二个月学习总结

OOP面向对象第二个月学习总结

时间:2023-04-30 20:14:06浏览次数:44  
标签:return String 第二个 int void 面向对象 OOP date public

OOP面向对象第二个月学习总结

目录

 · 前言

 · 设计与分析

 · 踩坑心得

 · 改进建议

 · 总结

 

一、前言

这个月的学习相比上个月的学习,难度就提升了极多,为了让我们更好的理解面向对象的几个特点和原则,题目以及作业的难度,复杂度,综合性增加了许多。主要有几次实验,还有几道PTA的难题。这样每个月写一个博客。主要做一种纪念性的工作。纪念自己不同阶段的不同状态。待以后蓦然回首时,些许感慨,些许回忆。

南昌航空大学-222019-刘嘉轩

2023-04-28

二、设计与分析 

OOP训练集04 7-1

题目为:

7-1 菜单计价程序-3 分数 30 作者 蔡轲 单位 南昌航空大学

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

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

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

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

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

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

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

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

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

删除记录格式:序号 delete

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

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

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

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

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

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

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

周一至周五营业时间与折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余时间不营业。

周末全价,营业时间: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

 

 

 主要问题:

1.都第一次这样较复杂的问题,并无一个清晰的思路。导致越写越乱,越写越浮躁。也像我玩笑常说的“依托答辩”

2.对于输入输出处理的并不是很好。

3.对数据的处理思路不清晰,应以更加清晰的思路去编写此道题

4.最终也没拿到满分

启示:

写代码前多做分析

增加对java的熟练程度

 

感慨:

可恶的测试点,真的很难不令人讨厌,题目又不说清楚,样例也不给的全面,非要自己一个个点去试,这不是纯纯,浪费时间么。

也存在可能,是因为自己写的很不清晰,改起来的时候无从下手,导致这样的结果。

 

最终代码

复制代码

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Scanner;

class Dish {
private String name;//菜品名称
private int unit_price; //单价
Dish() {

}
Dish(String name, int unit_price) {

this.unit_price = unit_price;
this.name = name;

}

public int getPrice(int portion) {

return (int) (0.5 + unit_price * 0.5 * (portion + 1));//小份菜的价格=菜品的基础价格。中份菜的价格=菜品的基础价格1.5。小份菜的价格=菜品的基础价格2。如果计算出现小数,按四舍五入的规则进行处理。

}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public void setUnit_price(int unit_price) {
this.unit_price = unit_price;
}
}//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)

class Menu {
public static ArrayList<Dish> dishes = new ArrayList<>();
public static boolean isSearchRight(String dishName){
boolean flag = false;
for (int i = 0; i < dishes.size(); i++) {
if(dishName.equals(dishes.get(i).getName())) {
flag = true;
}
}
if(flag)return true;
else return false;
}
public static Dish searchDish(String dishName) {
int index = 0;
boolean flag = false;
for (int i = 0; i < dishes.size(); i++) {
if(dishName.equals(dishes.get(i).getName())) {
index = i;
}
}
return dishes.get(index);
}

public static void addDish(String dishName, int unit_price) {
if(!isSearchRight(dishName)){
Dish dish = new Dish();
dish.setName(dishName);
dish.setUnit_price(unit_price);
dishes.add(dish);
}

}
}

class Record {
private int type = 1;//1 正常点菜 2 帮别人点菜 3 删除菜
private int orderNum;//序号
//点菜属性 帮点和自己点
//帮自己点
private Dish dish;//菜品
private int portion;//份额
private int num;//份数
//帮点桌
private int helpTable = 0;//0表示不帮别人点菜
//是否点菜成功
private boolean isOrderRight = true;
private String wrongDish;
//是否删除成功
private boolean isDeleteSuccessful = true;


Record(){

}

Record(int orderNum, Dish d, int portion,int num) {
this.dish = d;
this.orderNum = orderNum;
this.portion = portion;
this.num = num;
}

public Dish getDish(){
return dish;
}


public int getPrice() {
if (type != 3 && isOrderRight){

return dish.getPrice(portion) * num;
}
return 0;
}

public int getOrderNum() {
return orderNum;
}

public void setOrderNum(int orderNum) {
this.orderNum = orderNum;
}

public void setD(Dish dish) {
this.dish = dish;
}

public void setPortion(int portion) {
this.portion = portion;
}

public int getNum() {
return num;
}

public void setNum(int num) {
this.num = num;
}

public boolean isOrderRight() {
return isOrderRight;
}

public void setOrderRight(boolean orderRight) {
isOrderRight = orderRight;
}

public int getHelpTable() {
return helpTable;
}

public void setHelpTable(int helpTable) {
this.helpTable = helpTable;
}

public boolean isDeleteSuccessful() {
return isDeleteSuccessful;
}

public void setDeleteSuccessful(boolean deleteSuccessful) {
isDeleteSuccessful = deleteSuccessful;
}

public int getType() {
return type;
}

public void setType(int type) {
this.type = type;
}

public String getWrongDish() {
return wrongDish;
}

public void setWrongDish(String wrongDish) {
this.wrongDish = wrongDish;
}
}
//答辩
class Order {
private ArrayList<Record> records = new ArrayList<>();
private int refund = 0;//删菜退款
private Menu menu;

Order(){
menu = new Menu();
}
public int getTotalPrice() {
int total = 0;
for (int i = 0; i < records.size(); i++) {
if(records.get(i).getType() != 3 ) {
total += records.get(i).getPrice();
}
}

total -= refund;
return total;
}
public ArrayList<Record> getRecords(){
return records;
}
public Record addARecord(int orderNum, String dishName, int portion, int num) {
Record record = new Record(orderNum, Menu.searchDish(dishName), portion,num);
records.add(record);
return record;
}
public void deleteARecord(int orderNum){
refund += records.get(orderNum - 1).getPrice();

}
public Menu getMenu(){
return menu;
}
public void setMenu(Menu menu) {
this.menu = menu;
}

}
class Table{
public static int tables_index = -1;//main中table数组的下标,为保证每次处理的是最新的table
private int number;//序号
private LocalDateTime date_time;//日期
private int total_price;
private Order order;
//由于取的为闭区间,如早于14.30,下面日期即14.31
final private LocalTime week_mid_bgn_time = LocalTime.of(10,29,59);
final private LocalTime week_mid_end_time = LocalTime.of(14,31,0);
final private LocalTime week_night_bgn_time = LocalTime.of(16,59,59);
final private LocalTime week_night_end_time = LocalTime.of(20,31,0);
final private LocalTime weekend_bgn_time = LocalTime.of(9,29,59);
final private LocalTime weekend_end_time = LocalTime.of(21,31,0);
//返回折数
public int count() {
if (isWeekday()) {
if (date_time.toLocalTime().isBefore(week_mid_end_time) &&
date_time.toLocalTime().isAfter(week_mid_bgn_time)) {
return 6;
} else if (date_time.toLocalTime().isBefore(week_night_end_time) &&
date_time.toLocalTime().isAfter(week_night_bgn_time)) {
return 8;
} else return 0;
} else return 10;
}

public void printRecord(){
for (int i = 0; i < order.getRecords().size(); i++) {
if (order.getRecords().get(i).getType() != 3) {
if (order.getRecords().get(i).isOrderRight()) {//输出自己成功点菜
if (order.getRecords().get(i).getHelpTable() == 0) {
System.out.println(order.getRecords().get(i).getOrderNum() + " " + order.getRecords().get(i).getDish().getName() + " " + order.getRecords().get(i).getPrice());
} else {//有缺失自身桌号
System.out.println(order.getRecords().get(i).getOrderNum() + " table " + number + " pay for table " + order.getRecords().get(i).getHelpTable() + " " + order.getRecords().get(i).getPrice());
}
} else {
System.out.println(order.getRecords().get(i).getWrongDish() + " does not exist");
}
} else {
if (!order.getRecords().get(i).isDeleteSuccessful()) {

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

public boolean is_On_Work(){
if (isWeekday()){
if(date_time.toLocalTime().isBefore(week_mid_end_time) &&
date_time.toLocalTime().isAfter(week_mid_bgn_time)){
return true;
}else if(date_time.toLocalTime().isBefore(week_night_end_time) &&
date_time.toLocalTime().isAfter(week_night_bgn_time)){
return true;
}else return false;
}else if(date_time.toLocalTime().isAfter(weekend_bgn_time) &&
date_time.toLocalTime().isBefore(weekend_end_time)){
return true;
}else return false;
}
public boolean isWeekday(){
int day = date_time.getDayOfWeek().getValue();
if(day < 6)return true;
else return false;
}//工作日返回true节假日返回false

Table(){
order = new Order();
tables_index++;
}

public Table(int number, int year, int month, int day, int hour, int min, int sec){
tables_index++;
setNumber(number);
setDate_time(year, month, day,hour,min,sec);
order = new Order();
}

public int getNumber() {
return number;
}

public void setNumber(int number) {
this.number = number;
}

public LocalDateTime getDate_time() {
return date_time;
}

public void setDate_time(int year,int month, int day,int hour,int min,int sec) {
date_time = LocalDateTime.of(year, month, day,hour,min,sec);
}

public int getTotal_price() {
total_price = (int) (order.getTotalPrice() * count() * 1.0 / 10 + 0.5);
return total_price;
}

public void setTotal_price(int total_price) {
this.total_price = total_price;
}

public Order getOrder() {
return order;
}

}

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<Table> tables = new ArrayList<>();
Table table1 = new Table();//为什么第一桌先输菜单再输table,第二桌反过来
tables.add(table1);
while (true) {
String[] line = input.nextLine().split(" ");
if ("end".equals(line[0])) {//结束
break;
} else if ("table".equals(line[0])) {//新桌
String[] date = line[2].split("/");
String[] time = line[3].split("/");
int number = Integer.parseInt(line[1]);
int year = Integer.parseInt(date[0]);
int month = Integer.parseInt(date[1]);
int day = Integer.parseInt(date[2]);
int hour = Integer.parseInt(time[0]);
int min = Integer.parseInt(time[1]);
int sec = Integer.parseInt(time[2]);
if(number == 1){
table1.setNumber(number);
table1.setDate_time(year,month,day,hour,min,sec);
}else{
Table table = new Table(number,year,month,day,hour,min,sec);//输入订桌日期时间
tables.add(table);
}
//当前桌的record
}else{
try {
try{//帮点菜//
int oderNum = Integer.parseInt(line[1]);
int helpTable = Integer.parseInt(line[0]);
String dish = line[2];
int portion = Integer.parseInt(line[3]);
int num = Integer.parseInt(line[4]);
Record record = new Record();
if(Menu.isSearchRight(dish)){
record.setOrderNum(oderNum);
record.setType(2);
record.setHelpTable(helpTable);
record.setPortion(portion);
record.setNum(num);
record.setD(Menu.searchDish(dish));
}else{//点菜错误
record.setOrderRight(false);
record.setWrongDish(dish);
}
tables.get(Table.tables_index).getOrder().getRecords().add(record);

}
catch (NumberFormatException e){
if("delete".equals(line[1])){//delete菜
int deleteTable = Integer.parseInt(line[0]);

if(deleteTable <= tables.get(Table.tables_index).getOrder().getRecords().size() && tables.get(Table.tables_index).getOrder().getRecords().get(deleteTable).getType() != 3){
tables.get(Table.tables_index).getOrder().deleteARecord(deleteTable);
}else{//delete失败
Record record = new Record();
record.setDeleteSuccessful(false);
record.setType(3);
tables.get(Table.tables_index).getOrder().getRecords().add(record);
}
}else{
int order_num = Integer.parseInt(line[0]);
String dish = line[1];
int portion = Integer.parseInt(line[2]);
int num = Integer.parseInt(line[3]);
if(Menu.isSearchRight(dish)){
tables.get(Table.tables_index).getOrder().addARecord(order_num,dish,portion,num);//加菜

}else{
Record record = new Record();
record.setOrderRight(false);
record.setWrongDish(dish);
tables.get(Table.tables_index).getOrder().getRecords().add(record);
}
}
}
} catch (NumberFormatException e) {
//麻婆豆腐 12 加入菜单
int i = Integer.parseInt(line[1]);
Menu.addDish(line[0],i);
}
}
}
//输出桌号及其所点菜
for (int i = 0; i < tables.size(); i++) {
if(tables.get(i).getNumber() != 0){//预防仅有菜谱的情况
System.out.println("table " + tables.get(i).getNumber() + ": ");
tables.get(i).printRecord();

}
}
//输出每桌总和
for (int i = 0; i < tables.size(); i++) {
if(tables.get(i).getNumber() != 0) {//预防仅有菜谱的情况
if(tables.get(i).is_On_Work()){
System.out.println("table " + tables.get(i).getNumber() + ": " + tables.get(i).getTotal_price());
}else{
System.out.println("table " + tables.get(i).getNumber() + " out of opening hours");
}
}

}
}
}

复制代码

OOP训练集05 7-6

题目为:

7-6 日期问题面向对象设计(聚合二) 分数 34 作者 段喜龙 单位 南昌航空大学

参考题目7-3的要求,设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1820,2020] ,month∈[1,12] ,day∈[1,31] , 设计类图如下:

类图.jpg

应用程序共测试三个功能:

  1. 求下n天
  2. 求前n天
  3. 求两个日期相差的天数

注意:严禁使用Java中提供的任何与日期相关的类与方法,并提交完整源码,包括主类及方法(已提供,不需修改)

输入格式:

有三种输入方式(以输入的第一个数字划分[1,3]):

  • 1 year month day n //测试输入日期的下n天
  • 2 year month day n //测试输入日期的前n天
  • 3 year1 month1 day1 year2 month2 day2 //测试两个日期之间相差的天数

输出格式:

  • 当输入有误时,输出格式如下:
    Wrong Format
  • 当第一个数字为1且输入均有效,输出格式如下:
    year1-month1-day1 next n days is:year2-month2-day2
     
  • 当第一个数字为2且输入均有效,输出格式如下:
    year1-month1-day1 previous n days is:year2-month2-day2
     
  • 当第一个数字为3且输入均有效,输出格式如下:
    The days between year1-month1-day1 and year2-month2-day2 are:值
     

输入样例1:

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

3 2014 2 14 2020 6 14
 

输出样例1:

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

The days between 2014-2-14 and 2020-6-14 are:2312
 

输入样例2:

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

2 1834 2 17 7821
 

输出样例2:

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

1834-2-17 previous 7821 days is:1812-9-19
 

输入样例3:

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

1 1999 3 28 6543
 

输出样例3:

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

1999-3-28 next 6543 days is:2017-2-24
 

输入样例4:

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

0 2000 5 12 30
 

输出样例4:

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

Wrong Format
  代码长度限制 16 KB 时间限制 10000 ms 内存限制 64 MB

 

感慨:

最近太烦了,写一题让自己有点成就感的一题。嘻嘻!

主要之前没给像这样计算日期的类图,我写的就跟这个差不多,说明自己还是不错的,嘿嘿

 

最终代码:

复制代码

//规范
import java.util.Scanner;

class DateUtil{
private Year year;
private Month month;
private Day day;
private int[] mon_maxnum = {0,31,28,31,30,31,30,31,31,30,31,30,31};
DateUtil(){

}
DateUtil(int y,int m,int d){
Year year = new Year(y);
Month month = new Month(m);
Day day = new Day(d);
this.year = year;
this.month = month;
this.day = day;
}

public boolean compareDates(DateUtil date){
//比形参大或相等,返回true
if(year.getValue() > date.getYear().getValue())return true;
else if(year.getValue() < date.getYear().getValue()) return false;
else if(month.getValue() > date.getMonth().getValue())return true;
else if(month.getValue() < date.getMonth().getValue())return false;
else if(day.getValue() > date.getDay().getValue())return true;
else if(day.getValue() < date.getDay().getValue())return false;
else return true;
}//比较当前日期与date的大小(先后)
public boolean equalTwoDates(DateUtil date){
if(year.getValue() == date.getYear().getValue() && month.getValue() == date.getMonth().getValue() && day.getValue() == date.getDay().getValue()){
return true;
}else return false;
}//判断两个日期是否相等

public int getDaysofDates(DateUtil date) {
int n = 0;
if (equalTwoDates(date)) {
return n;
} else if (compareDates(date)) {
for (;;) {
if (equalTwoDates(date)) break;
n++;
if (date.getYear().isLeapYear()) {
date.setMon_maxnum(2,29);
} else {
date.setMon_maxnum(2,28);
}
if (date.getDay().getValue() < date.getMon_maxnum(date.getMonth().getValue())){
date.getDay().dayIncrement();
}
else if (date.getMonth().getValue() == 12 && date.getDay().getValue() == date.getMon_maxnum(12)) {
date.getYear().yearIncrement();
//换年更改2月份最大天数
if(date.getYear().isLeapYear()){
date.setMon_maxnum(2,29);
}else{
date.setMon_maxnum(2,28);
}
date.getMonth().setValue(1);
date.getDay().setValue(1);
} else if (date.getDay().getValue() == date.getMon_maxnum(date.getMonth().getValue())) {
date.getMonth().monthIncrement();
date.getDay().setValue(1);
}
}
} else if (!compareDates(date)) {
if (date.getYear().isLeapYear()) {
date.setMon_maxnum(2,29);
}
for (; ; ) {
if (equalTwoDates(date)) break;
n++;
if (date.getYear().isLeapYear()) {
date.setMon_maxnum(2,29);
} else {
date.setMon_maxnum(2,28);
}
if (date.getDay().getValue() > 1){
date.getDay().dayReduction();
}
else if (date.getMonth().getValue() == 1 && date.getDay().getValue() == 1) {
date.getYear().yearReduction();
if (date.getYear().isLeapYear()) {
date.setMon_maxnum(2,29);
} else {
date.setMon_maxnum(2,28);
}
date.getMonth().setValue(12);
date.getDay().setValue(31);
} else if (date.getDay().getValue() == 1) {
date.getMonth().monthReduction();

date.getDay().setValue(date.getMon_maxnum(date.getMonth().getValue()));
}
}
}
return n;
}//求当前日期与date之间相差的天数
public int getMon_maxnum(int i) {
return mon_maxnum[i];
}

public void setMon_maxnum(int i , int n) {
mon_maxnum[i] = n;
}

public DateUtil getPreviousNDays(int n){
DateUtil newDate = new DateUtil(year.getValue(),month.getValue(),day.getValue());

//判断是不是闰年,以初始化
if(newDate.getYear().isLeapYear()){
newDate.setMon_maxnum(2,29);
}
//循环计算天数
for (int i = 0; i < n; i++) {
//正常减少天数day
if (newDate.getDay().getValue() > 1){
newDate.getDay().dayReduction();
}
//如果是一年的第一天
else if(newDate.getMonth().getValue() == 1 && newDate.getDay().getValue() == 1){
newDate.getYear().yearReduction();
newDate.getMonth().resetMax();
newDate.getDay().setValue(newDate.getMon_maxnum(newDate.getMonth().getValue()));
//换年更改2月份最大天数
if(newDate.getYear().isLeapYear()){
newDate.setMon_maxnum(2,29);
}else{
newDate.setMon_maxnum(2,28);
}
}
//跨月
else if(newDate.getDay().getValue() == 1){
newDate.getMonth().monthReduction();
newDate.getDay().setValue(newDate.getMon_maxnum(newDate.getMonth().getValue()));
}
}
return newDate;
}
public DateUtil getNextNDays(int n){
DateUtil newDate = new DateUtil(year.getValue(),month.getValue(),day.getValue());
//判断是不是闰年,以初始化
if(year.isLeapYear()){
newDate.setMon_maxnum(2,29);
}
//循环计算天数
for (int i = 0; i < n; i++) {
//正常增加天数day
if (newDate.getDay().getValue() < newDate.getMon_maxnum(newDate.getMonth().getValue())){
newDate.getDay().dayIncrement();
}
//如果是一年的最后一天
else if(newDate.getMonth().getValue() == 12 && newDate.getDay().getValue() == newDate.getMon_maxnum(12)){
newDate.getYear().yearIncrement();
newDate.getMonth().resetMin();
newDate.getDay().setValue(1);
//换年更改2月份最大天数
if(newDate.getYear().isLeapYear()){
newDate.setMon_maxnum(2,29);
}else{
newDate.setMon_maxnum(2,28);
}
}
//跨月
else if(newDate.getDay().getValue() == newDate.getMon_maxnum(newDate.getMonth().getValue())){
newDate.getMonth().monthIncrement();
newDate.getDay().setValue(1);
}
}
return newDate;
}
public String showDate(){
return (year.getValue() + "-" + month.getValue() + "-" + day.getValue());
}

public boolean checkInputValidity(){
if(year.getValue() < 2021 && year.getValue() > 1819 && month.validate() && day.getValue() > 0 && day.getValue() <= mon_maxnum[month.getValue()]){
return true;
}else{
return false;
}
}
public void setDayMin(){
day.setValue(1);
}

public void setDayMax(){
day.setValue(mon_maxnum[month.getValue()]);

}
public void setYear(Year year) {
this.year = year;
}

public Year getYear() {
return year;
}

public void setMonth(Month month) {
this.month = month;
}

public Month getMonth() {
return month;
}

public void setDay(Day day) {
this.day = day;
}

public Day getDay() {
return day;
}
}

class Year{
private int value;

Year(){

}
Year(int value){
this.value = value;
}

public int getValue() {
return value;
}

public void setValue(int year) {
this.value = year;
}
public boolean isLeapYear(){
if((value % 4 == 0 && value % 100 != 0) || (value % 400 == 0)){
return true;
}else{
return false;
}
}
public void yearIncrement(){
value++;
}
public void yearReduction(){
value--;
}
}

class Month {
private int value;

Month(){

}
Month(int m){
this.value = m;
}
public int getValue() {
return value;
}

public void setValue(int value) {
this.value = value;
}

public void monthIncrement() {
value++;
}

public void monthReduction() {
value--;
}

public void resetMax() {
value = 12;
}

public void resetMin() {
value = 1;
}

public boolean validate() {
if (value > 12 || value < 1) {
return false;
} else {
return true;
}
}
}
class Day{

private int value;

Day(){

}

Day(int d){
value = d;
}

public int getValue() {
return value;
}

public void setValue(int value) {
this.value = value;
}

public void dayIncrement(){
value++;
}

public void dayReduction(){
value--;
}
}


public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int year = 0;
int month = 0;
int day = 0;
int choice = input.nextInt();//判断怎么找
if (choice == 1) { // test getNextNDays method找接下来的几天
int m = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

DateUtil date = new DateUtil(year, month, day);

if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}
m = input.nextInt();
if (m < 0) {
System.out.println("Wrong Format");
System.exit(0);
}
System.out.print(date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " next " + m + " days is:");
System.out.println(date.getNextNDays(m).showDate());
}else if (choice == 2) { // test getPreviousNDays method
int n = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

DateUtil date = new DateUtil(year, month, day);

if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}

n = input.nextInt();

if (n < 0) {
System.out.println("Wrong Format");
System.exit(0);
}
System.out.print(date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " previous " + n + " days is:");
System.out.println(date.getPreviousNDays(n).showDate());
}else if (choice == 3) { //test getDaysofDates method
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

int anotherYear = Integer.parseInt(input.next());
int anotherMonth = Integer.parseInt(input.next());
int anotherDay = Integer.parseInt(input.next());

DateUtil fromDate = new DateUtil(year, month, day);
DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);

if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
System.out.println("The days between " + fromDate.showDate() +
" and " + toDate.showDate() + " are:"
+ fromDate.getDaysofDates(toDate));
} else {
System.out.println("Wrong Format");
System.exit(0);
}
}
else{
System.out.println("Wrong Format");
System.exit(0);
}
}
}

复制代码

 

OOP06

 

我讨厌这个题目集,自己写的稀里糊涂,这段时间也很忙,分也没得多少。可恶可恶可恶可恶。

主要有水文数据处理,ATM机两题,ok了,复制个代码上去算了,不想分析了。烦死了

题目为:

7-3 水文数据校验及处理 分数 25 作者 段喜龙 单位 南昌航空大学

使用Java中的字符串处理类以及正则表达式对输入字符串数据进行合法性校验及计算。(具体需求参见附件
2021-OO第04次作业-1指导书V1.0.pdf
)

输入格式:

假定分水口门的数据上报时是采用人工输入的方式,每一行代表一个整点时刻的分水数据,各数据之间采用“|”符号进行分隔,每次可以输入多条数据,直到遇到用户输入“exit”为止,每一行输入数据共包含五部分:测量时间、目标水位、实际水位、开度(包含目标开度和实际开度,以“/”分隔)、流量。
各数据格式要求如下:

  1. 测量时间:格式为“年/月/日 时:分”,其中年份取值范围为[1,9999],“月”与“日”为一位数时之前不加“0”,日期与时间之间有一个空格,“时”与“分”之间采用冒号分隔(英文半角),“时”为一位数时之前不加“0”,“分”始终保持两位,且始终为“00”。注意:“时”数必须是24小时进制中的偶数值。
  2. 目标水位、实际水位、流量:均为实型数,取值范围为[1,1000), 小数点后保留1-3位小数或无小数(也无小数点)
  3. 目标开度、实际开度:实型数,取值范围为[1,10),必须保留2位小数,两个开度之间用“/”分隔

输出格式:

  1. 对输入的数据进行有效性校验,其规则如前所述,如遇到不符合规则的数据,系统应能够给出错误提示,提示规则如下:
  • 如果每一行输入数据不是由“|”分隔的五部分,则输出:
    Wrong Format
    Data:输入的数据
     
  • 如果某一部分数据有误,则按如下方式显示:
    Row:行号,Column:列号Wrong Format
    Data:输入的数据
      其中,行号为输入数的行数(从1开始),列号为6个数据的序号(从1开始,最大为6,顺序参见输入数据结构说明)
  1. 由于人工输入数据可能存在疏忽,在每一个输入数据两端均可能存在多余的空格,程序应该能够自动过滤这些空格(不报错)。
  2. 如果用户未输入数据,则直接输出Max Actual Water Level和Total Water Flow的值即可(均为0)
  3. 若输入无误,则对数据进行如下处理:
  • 当实际开度的值大于目标开度时,程序给出如下警告:
    Row:1 GateOpening Warning
     
  • 求出所输入数据中的最大实际水位值(保留2位小数),输出格式如下:
    Max Actual Water Level:实际水位值
  • 根据每个整点时刻的瞬时流量求出所输入的所有时段的总流量(保留2位小数),其计算公式为(参见作业指导书):
    $$p = \sum_{n=1}^N2*60*60*Flow$$
     

输出格式如下:

Total Water Flow:总流量值
 

输入样例1:

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

2015/8/2 4:00|133.8400|133.070|1.11/1.21|75.780
2015/8/2 6:00|133.840|133.080|11.11/1.11|72.8a0
2015/8/2 8:00|133.830|133.070|1.11/1.11|73.890
2015/8/2 10:00|133.820|133.080|1.11/1.11|74.380
exit
 

输出样例1:

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

Row:1,Column:2Wrong Format
Data:2015/8/2 4:00|133.8400|133.070|1.11/1.21|75.780
Row:2,Column:4Wrong Format
Row:2,Column:6Wrong Format
Data:2015/8/2 6:00|133.840|133.080|11.11/1.11|72.8a0
 

输入样例2:

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

2015/8/5 2:00|133.800|133.080|1.11/1.11|73.870
2015/8/5 4:00|133.800|133.070|1.11/1.11|73.330
2015/8/5 6:00|133.830|133.110|1.11/1.21|70.610
2015/8/5 8:00|133.860|133.140|1.11/1.11|73.430
2015/8/5 10:00|133.91|133.15|1.11/1.11|73.06
2015/8/5 12:00|133.900|133.110|1.16/1.11|75.460
2015/8/5 14:00|133.920|133.140|1.16/1.11|77.030
2015/8/5 16:00|133.92|133.16|1.16/1.91|79.4
2015/8/5 18:00|133.940|133.170|1.16/1.11|76.810
2015/8/5 20:00|133.94|133.19|1.16/1.11|74.53
2015/8/5 22:00|133.930|133.200|1.16/1.11|74.400
2015/8/6 0:00|133.930|133.200|1.16/1.11|73.150
2015/8/6 2:00|133.930|133.180|1.16/1.11|74.830
2015/8/6 4:00|133.910|133.180|1.16/1.11|    73.270
exit
 

输出样例2:

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

Row:3 GateOpening Warning
Row:8 GateOpening Warning
Max Actual Water Level:133.20
Total Water Flow:7510896.00
  代码长度限制 16 KB 时间限制 1500 ms 内存限制 64 MB

 

最终代码:

 

复制代码

import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Scanner;

class DealData{
StringBuilder stringBuilder = new StringBuilder();

private int row;

private static boolean isDataRight = true;

public static void setIsDataRight(boolean isDataRight) {
DealData.isDataRight = isDataRight;
}

public static ArrayList<HydrologicalInfo> getHydrologicalInfoArrayList() {
return hydrologicalInfoArrayList;
}

public void setHydrologicalInfoArrayList(ArrayList<HydrologicalInfo> hydrologicalInfoArrayList) {
this.hydrologicalInfoArrayList = hydrologicalInfoArrayList;
}

private static ArrayList<HydrologicalInfo> hydrologicalInfoArrayList = new ArrayList<>();

public static boolean isIsDataRight() {
return isDataRight;
}

public DealData() {
}

public DealData(StringBuilder stringBuilder,int row) {
this.stringBuilder = stringBuilder;
this.row = row;
}

public int getRow() {
return row;
}

public void setRow(int row) {
this.row = row;
}

public StringBuilder getStringBuilder() {
return stringBuilder;
}

public void setStringBuilder(StringBuilder stringBuilder) {
this.stringBuilder = stringBuilder;
}

public void printWrongDataResult(){
CheckData checkData = new CheckData(stringBuilder.toString(),row);
boolean isRowDataRight = true;
if(!checkData.isCharacterRight()){
System.out.println("Wrong Format");
isRowDataRight = false;
isDataRight = false;
System.out.println("Data:" + stringBuilder.toString());
}else{
if(!checkData.validateData() || !checkData.validateMeasureDateTime()){
System.out.println("Row:" + row + ",Column:1Wrong Format");
isRowDataRight = false;
isDataRight = false;
}
if(!checkData.validateWaterLevel(checkData.getSingleData()[1])){
System.out.println("Row:" + row + ",Column:2Wrong Format");
isRowDataRight = false;
isDataRight = false;
}
if(!checkData.validateWaterLevel(checkData.getSingleData()[2])){
System.out.println("Row:" + row + ",Column:3Wrong Format");
isRowDataRight = false;
isDataRight = false;
}
if(!checkData.validateGateOpening(checkData.getSingleData()[3].split("/")[0])){
System.out.println("Row:" + row + ",Column:4Wrong Format");
isRowDataRight = false;
isDataRight = false;
}
if(!checkData.validateGateOpening(checkData.getSingleData()[3].split("/")[1])) {
System.out.println("Row:" + row + ",Column:5Wrong Format");
isRowDataRight = false;
isDataRight = false;
}if(!checkData.validateWaterFlow()){
System.out.println("Row:" + row + ",Column:6Wrong Format");
isRowDataRight = false;
isDataRight = false;
}
if(isRowDataRight){
hydrologicalInfoArrayList.add(checkData.toHydrologicalInfo());
}else{
System.out.println("Data:" + stringBuilder.toString());
}
}

}

public void printRightResult(){
CheckData checkData = new CheckData(stringBuilder.toString(),row);
if(checkData.isGateOpeningWarning()){
System.out.println("Row:"+ row + " GateOpening Warning");
}
}

public double ActualWaterLevel(){
CheckData checkData = new CheckData(stringBuilder.toString(),row);
return Double.parseDouble(checkData.getSingleData()[2]);
}

public static void computeData(HydrologicalInfo hydrologicalInfo[]){
double totalFlow = 0;
for (int i = 0; i < hydrologicalInfo.length; i++) {
totalFlow += hydrologicalInfo[i].getWaterFlow() * 3600 * 2;
}

System.out.println("Total Water Flow:" + String.format("%.2f",totalFlow));

}
}

class HydrologicalInfo{
private LocalDateTime measureDateTime;
private double objectWaterLevel;
private double actualWaterLevel;
private double objectGateOpening;
private double actualGateOpening;
private double waterFlow;

public HydrologicalInfo() {
}

public HydrologicalInfo(LocalDateTime measureDateTime, double objectWaterLevel, double actualWaterLevel, double objectGateOpening, double actualGateOpening, double waterFlow) {
this.measureDateTime = measureDateTime;
this.objectWaterLevel = objectWaterLevel;
this.actualWaterLevel = actualWaterLevel;
this.objectGateOpening = objectGateOpening;
this.actualGateOpening = actualGateOpening;
this.waterFlow = waterFlow;
}

public LocalDateTime getMeasureDateTime() {
return measureDateTime;
}

public void setMeasureDateTime(LocalDateTime measureDateTime) {
this.measureDateTime = measureDateTime;
}

public double getObjectWaterLevel() {
return objectWaterLevel;
}

public void setObjectWaterLevel(double objectWaterLevel) {
this.objectWaterLevel = objectWaterLevel;
}

public double getActualWaterLevel() {
return actualWaterLevel;
}

public void setActualWaterLevel(double actualWaterLevel) {
this.actualWaterLevel = actualWaterLevel;
}

public double getObjectGateOpening() {
return objectGateOpening;
}

public void setObjectGateOpening(double objectGateOpening) {
this.objectGateOpening = objectGateOpening;
}

public double getActualGateOpening() {
return actualGateOpening;
}

public void setActualGateOpening(double actualGateOpening) {
this.actualGateOpening = actualGateOpening;
}

public double getWaterFlow() {
return waterFlow;
}

public void setWaterFlow(double waterFlow) {
this.waterFlow = waterFlow;
}
}

class CheckData{
private String data;
private int row;
private String[] singleData;


public boolean isGateOpeningWarning(){
double objectGateOpening = Double.parseDouble(singleData[3].split("/")[0]);
double actualGateOpening = Double.parseDouble(singleData[3].split("/")[1]);
if(objectGateOpening < actualGateOpening){
return true;
}else{
return false;
}
}

public boolean isCharacterRight(){
return singleData.length == 5;
}

public HydrologicalInfo toHydrologicalInfo(){
String[] datetime = singleData[0].split(" ");
String[] dates = datetime[0].split("/");
String[] times = datetime[1].split(":");
int year = Integer.parseInt(dates[0]);
int month = Integer.parseInt(dates[1]);
int day = Integer.parseInt(dates[2]);
int hour = Integer.parseInt(times[0]);
int min = Integer.parseInt(times[1]);

LocalDateTime localDateTime = LocalDateTime.of(year,month,day,hour,min);
double objectWaterLevel = Double.parseDouble(singleData[1]);
double actualWaterLevel = Double.parseDouble(singleData[2]);
double objectGateOpening = Double.parseDouble(singleData[3].split("/")[0]);
double actualGateOpening = Double.parseDouble(singleData[3].split("/")[1]);
double waterFlow = Double.parseDouble(singleData[4]);
HydrologicalInfo hydrologicalInfo = new HydrologicalInfo(localDateTime,objectWaterLevel,actualWaterLevel,objectGateOpening,actualGateOpening,waterFlow);
return hydrologicalInfo;
}

public boolean validateWaterFlow(){
String regex = "([1-9]\\d{0,2})((\\.\\d{0,3}))?";
return singleData[4].matches(regex);
}

public boolean validateGateOpening(String gateOpening){
String regex = "[1-9]\\.\\d{2}";
return gateOpening.matches(regex);
}

public boolean validateWaterLevel(String waterLevel){
String regex = "([1-9]\\d{0,2})((\\.\\d{0,3}))?";
return waterLevel.matches(regex);
}

public boolean validateData(){
String regex = "([1-9]\\d\\d\\d|[1-9]\\d\\d|[1-9]\\d|[1-9])/([1[0-2]]|[1-9])/(3[01]|[12[0-9]]|[1-9]) ([12][0-9]|[0-9]):\\d\\d";
String[] datetime = singleData[0].split(" ");
String[] dates = datetime[0].split("/");
try{
int year = Integer.parseInt(dates[0]);
int month = Integer.parseInt(dates[1]);
int day = Integer.parseInt(dates[2]);
try{
LocalDate localDate = LocalDate.of(year,month,day);
return singleData[0].matches(regex);
}catch(DateTimeException e){
return false;
}
}catch(NumberFormatException e){
return false;
}


}

public boolean validateMeasureDateTime(){
String[] time = singleData[0].replaceAll("/"," ").replaceAll(":"," ").split(" ");
int hour = Integer.parseInt(time[3]);

if(hour % 2 != 0){
return false;
}
else if(!time[4].equals("00")){
return false;
}
else{
return true;
}
}

public String[] getSingleData() {
return singleData;
}

public void setSingleData(String[] singleData) {
this.singleData = singleData;
}

public CheckData() {
}

public CheckData(String data, int row) {
this.data = data;
this.row = row;
singleData = data.trim().split("\\|");
for (int i = 0; i < singleData.length; i++) {
singleData[i] = singleData[i].trim();
}
}

public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}

public int getRow() {
return row;
}

public void setRow(int row) {
this.row = row;
}
}

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

boolean isDataRight = true;
ArrayList<DealData> dealDataArrayList = new ArrayList<>();
ArrayList<HydrologicalInfo> hydrologicalInfoArrayList = new ArrayList<>();
//输入
int row = 1;
while(true){
String line = input.nextLine();
//退出
if("exit".equals(line) || "".equals(line) || line.matches(" *")){
break;
}
else{
DealData data = new DealData(new StringBuilder(line),row);
dealDataArrayList.add(data);
}
row++;
}

//输出
for (DealData data : dealDataArrayList) {
data.printWrongDataResult();
}
if(DealData.isIsDataRight()){
HydrologicalInfo[] hydrologicalInfos = (HydrologicalInfo[]) DealData.getHydrologicalInfoArrayList().toArray(new HydrologicalInfo[hydrologicalInfoArrayList.size()]);

double maxActualWaterLevel = 0;
for (DealData dealData : dealDataArrayList) {
if (dealData.ActualWaterLevel() > maxActualWaterLevel) {
maxActualWaterLevel = dealData.ActualWaterLevel();
}
dealData.printRightResult();
}
System.out.println("Max Actual Water Level:" + String.format("%.2f",maxActualWaterLevel));
DealData.computeData(hydrologicalInfos);
}

}

}

复制代码

 

OOP06 7-4

题目为:

 

 

7-4 ATM机类结构设计(一) 分数 20 作者 段喜龙 单位 南昌航空大学

设计ATM仿真系统,具体要求参见作业说明。
OO作业8-1题目说明.pdf

输入格式:

每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业务操作输入格式如下:

  • 存款、取款功能输入数据格式:
    卡号 密码 ATM机编号 金额(由一个或多个空格分隔),
    其中,当金额大于0时,代表取款,否则代表存款。
  • 查询余额功能输入数据格式:
    卡号

输出格式:

①输入错误处理

  • 如果输入卡号不存在,则输出Sorry,this card does not exist.
  • 如果输入ATM机编号不存在,则输出Sorry,the ATM's id is wrong.
  • 如果输入银行卡密码错误,则输出Sorry,your password is wrong.
  • 如果输入取款金额大于账户余额,则输出Sorry,your account balance is insufficient.
  • 如果检测为跨行存取款,则输出Sorry,cross-bank withdrawal is not supported.

②取款业务输出

输出共两行,格式分别为:

[用户姓名]在[银行名称]的[ATM编号]上取款¥[金额]

当前余额为¥[金额]

其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。

③存款业务输出

输出共两行,格式分别为:

[用户姓名]在[银行名称]的[ATM编号]上存款¥[金额]

当前余额为¥[金额]

其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。

④查询余额业务输出

¥[金额]

金额保留两位小数。

输入样例1:

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

6222081502001312390 88888888 06 -500.00
#
 

输出样例1:

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

张无忌在中国工商银行的06号ATM机上存款¥500.00
当前余额为¥10500.00
 

输入样例2:

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

6217000010041315709  88888888 02 3500.00
#
 

输出样例2:

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

杨过在中国建设银行的02号ATM机上取款¥3500.00
当前余额为¥6500.00
 

输入样例3:

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

6217000010041315715
#
 

输出样例3:

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

¥10000.00
 

输入样例4:

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

6222081502001312390 88888888 06 -500.00
6222081502051320786 88888888 06 1200.00
6217000010041315715 88888888 02 1500.00
6217000010041315709  88888888 02 3500.00
6217000010041315715
#
 

输出样例4:

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

张无忌在中国工商银行的06号ATM机上存款¥500.00
当前余额为¥10500.00
韦小宝在中国工商银行的06号ATM机上取款¥1200.00
当前余额为¥8800.00
杨过在中国建设银行的02号ATM机上取款¥1500.00
当前余额为¥8500.00
杨过在中国建设银行的02号ATM机上取款¥3500.00
当前余额为¥5000.00
¥5000.00
  代码长度限制 20 KB 时间限制 400 ms 内存限制 64 MB

 

 

 最终代码:

复制代码

import java.util.ArrayList;
import java.util.Scanner;

class UnionPay{
public UnionPay() {
cards = new ArrayList<Card>();
atms = new ArrayList<Atm>();
}

public static int findCard(String inputNumber){
//找到输入银行卡账号的下标,若没找到,则返回-1
for (int i = 0; i < cards.size(); i++) {
if(cards.get(i).getNumber().equals(inputNumber)){
return i;
}
}
return -1;
}

public static int findAtm(String inputNumber){
//找到输入银行卡账号的下标,若没找到,则返回-1
for (int i = 0; i < atms.size(); i++) {
if(atms.get(i).getNumber().equals(inputNumber)){
return i;
}
}
return -1;
}

private static ArrayList<Bank> banks;

public static ArrayList<Bank> getBanks() {
return banks;
}

public static void setBanks(ArrayList<Bank> banks) {
UnionPay.banks = banks;
}

public static ArrayList<User> getUsers() {
return users;
}

public static void setUsers(ArrayList<User> users) {
UnionPay.users = users;
}

public static ArrayList<Account> getAccounts() {
return accounts;
}

public static void setAccounts(ArrayList<Account> accounts) {
UnionPay.accounts = accounts;
}

public static ArrayList<Card> getCards() {
return cards;
}

public static void setCards(ArrayList<Card> cards) {
UnionPay.cards = cards;
}

public static ArrayList<Atm> getAtms() {
return atms;
}

public static void setAtms(ArrayList<Atm> atms) {
UnionPay.atms = atms;
}

private static ArrayList<User> users;
private static ArrayList<Account> accounts;
private static ArrayList<Card> cards;
private static ArrayList<Atm> atms;

}

class Atm{
private String number;
private Bank bank;

public Bank getBank() {
return bank;
}

public void setBank(Bank bank) {
this.bank = bank;
}

public Atm(String number, Bank bank) {
this.number = number;
this.bank = bank;
}

public Atm() {
}

public String getNumber() {
return number;
}

public void setNumber(String number) {
this.number = number;
}
}

class Bank{
private String name;

public Bank() {

}

public Bank(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

class Card{
private String number;
private String password;
private Account account;
private Bank bank;
private User user;

public boolean isCross_bank(Atm atm){
if(bank != atm.getBank()){
return true;
}else{
return false;
}
}

public boolean isPassWordRight(String inputPassWord){
if(password.equals(inputPassWord)){
return true;
}else{
return false;
}
}

public boolean canWithdraw(double money){
if(account.getBalance() > money){
return true;
}else{
return false;
}
}

public void deposit(double money){
account.setBalance(account.getBalance() + money);
}

public void withdraw(double money){
account.setBalance(account.getBalance() - money);
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

public Card(String number, String password, Account account, Bank bank, User user) {
this.number = number;
this.password = password;
this.account = account;
this.bank = bank;
this.user = user;
}

public Bank getBank() {
return bank;
}

public void setBank(Bank bank) {
this.bank = bank;
}

public Account getAccount() {
return account;
}

public void setAccount(Account account) {
this.account = account;
}

public Card() {
}

public String getNumber() {
return number;
}

public void setNumber(String number) {
this.number = number;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}

class Account{
private String number;
private double balance;

public Account() {
}

public Account(String number, double balance) {
this.number = number;
this.balance = balance;
}

public void setBalance(double balance) {
this.balance = balance;
}

public String getNumber() {
return number;
}

public void setNumber(String number) {
this.number = number;
}

public double getBalance() {
return balance;
}
}

class User{
private String name;

public User() {
}

public User(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

class Order{
private int type;
//type = 0查询,type = 1取款存款,
private int result;
//type = 0,result = 0 卡号不存在
// result = 1 查询成功
//type = 1 result = 0 卡号不存在
// result = 1 取款成功
// result = 6 存款成功
// result = 2 ATM机编号不存在
// result = 3 银行卡密码错误
// result = 4 取款金额大于账户余额
// result = 5 跨行存取款

private double money;
private Card card;
private Atm atm;
private double balance;

public void showOrder(){
switch (type){
case 0:
if(result == 0){
System.out.println("Sorry,this card does not exist.");
}else{
System.out.printf("¥%s",String.format("%.2f",balance));
}
break;
case 1:
switch(result){
//失败的
case 0:
System.out.println("Sorry,this card does not exist.");break;
case 2:
System.out.println("Sorry,the ATM's id is wrong.");break;
case 3:
System.out.println("Sorry,your password is wrong.");break;
case 4:
System.out.println("Sorry,your account balance is insufficient.");break;
case 5:
System.out.println("Sorry,cross-bank withdrawal is not supported.");break;
case 1:
System.out.printf("%s在%s的%s号ATM机上取款¥%s\n当前余额为¥%s%n", card.getUser().getName(), getCard().getBank().getName(), getAtm().getNumber() , String.format("%.2f", money),String.format("%.2f",balance));break;
case 6:
System.out.printf("%s在%s的%s号ATM机上存款¥%s\n当前余额为¥%s%n", card.getUser().getName(), getCard().getBank().getName(), getAtm().getNumber() , String.format("%.2f", -money),String.format("%.2f",balance));break;
}
}

}

public double getBalance() {
return balance;
}

public void setBalance(double balance) {
this.balance = balance;
}

public double getMoney() {
return money;
}

public void setMoney(double money) {
this.money = money;
}

public Card getCard() {
return card;
}

public void setCard(Card card) {
this.card = card;
}

public Atm getAtm() {
return atm;
}

public void setAtm(Atm atm) {
this.atm = atm;
}

public Order() {
}

public int getType() {
return type;
}

public void setType(int type) {
this.type = type;
}

public int getResult() {
return result;
}

public void setResult(int result) {
this.result = result;
}
}

public class Main {

public static void initialize(){

UnionPay unionPay = new UnionPay();
//ATM机及银行
Bank bank_construction = new Bank("中国建设银行");
Bank bank_ICBC = new Bank("中国工商银行");

Atm atm1 = new Atm("01",bank_construction);
Atm atm2 = new Atm("02",bank_construction);
Atm atm3 = new Atm("03",bank_construction);
Atm atm4 = new Atm("04",bank_construction);
Atm atm5 = new Atm("05",bank_ICBC);
Atm atm6 = new Atm("06",bank_ICBC);
UnionPay.getAtms().add(atm1);
UnionPay.getAtms().add(atm2);
UnionPay.getAtms().add(atm3);
UnionPay.getAtms().add(atm4);
UnionPay.getAtms().add(atm5);
UnionPay.getAtms().add(atm6);

//顾客初始化
//杨过
User YangGuo =new User("杨过");
Account account1_Yang = new Account("3217000010041315709",10000.00);
Card card1_Yang_Account1 = new Card("6217000010041315709","88888888",account1_Yang,bank_construction,YangGuo);
Card card2_Yang_Account1 = new Card("6217000010041315715","88888888",account1_Yang,bank_construction,YangGuo);
Account account2_Yang = new Account("3217000010041315715",10000.00);
Card card1_Yang_Account2 = new Card("6217000010041315718","88888888",account2_Yang,bank_construction,YangGuo);
UnionPay.getCards().add(card1_Yang_Account1);
UnionPay.getCards().add(card2_Yang_Account1);
UnionPay.getCards().add(card1_Yang_Account2);

//郭靖
User GuoJing =new User("郭靖");
Account account1_Guo = new Account("3217000010051320007",10000.00);
Card card1_Guo_Account1 = new Card("6217000010051320007","88888888",account1_Guo,bank_construction,GuoJing);
card1_Guo_Account1.setAccount(account1_Guo);
UnionPay.getCards().add(card1_Guo_Account1);

//张无忌
User ZhangWuJi =new User("张无忌");
Account account1_Zhang = new Account("3222081502001312389",10000.00);
Account account2_Zhang = new Account("3222081502001312390",10000.00);
Account account3_Zhang = new Account("3222081502001312399",10000.00);
Card card1_Zhang_Account1 = new Card("6222081502001312389","88888888",account1_Zhang,bank_ICBC,ZhangWuJi);
Card card1_Zhang_Account2 = new Card("6222081502001312390","88888888",account2_Zhang,bank_ICBC,ZhangWuJi);
Card card1_Zhang_Account3 = new Card("6222081502001312399","88888888",account3_Zhang,bank_ICBC,ZhangWuJi);
Card card2_Zhang_Account3 = new Card("6222081502001312400","88888888",account3_Zhang,bank_ICBC,ZhangWuJi);
UnionPay.getCards().add(card1_Zhang_Account1);
UnionPay.getCards().add(card1_Zhang_Account2);
UnionPay.getCards().add(card1_Zhang_Account3);
UnionPay.getCards().add(card2_Zhang_Account3);

//韦小宝
User WeiXiaoBao =new User("韦小宝");
Account account1_Wei = new Account("3222081502051320785",10000.00);
Account account2_Wei = new Account("3222081502051320786",10000.00);
Card card1_Wei_Account1 = new Card("6222081502051320785","88888888",account1_Wei,bank_ICBC,WeiXiaoBao);
Card card1_Wei_Account2 = new Card("6222081502051320786","88888888",account2_Wei,bank_ICBC,WeiXiaoBao);
UnionPay.getCards().add(card1_Wei_Account1);
UnionPay.getCards().add(card1_Wei_Account2);

}
public static void main(String[] args) {
//初始化
initialize();
ArrayList<Order> orders = new ArrayList<>();

//输入
Scanner input = new Scanner(System.in);
while(true){
String[] line = input.nextLine().replaceAll(" "," ").split("[ | ]");
//结束进程
if("#".equals(line[0])){
break;
}
//查询余额
else if(line.length == 1){
Order order = new Order();
order.setType(0);//type = 0 为查询余额
if(UnionPay.findCard(line[0]) != -1){
Card card = UnionPay.getCards().get(UnionPay.findCard(line[0]));
order.setCard(card);
order.setResult(1);
order.setBalance(card.getAccount().getBalance());
}else{
order.setResult(0);
}
orders.add(order);
}
//存取款
else if(line.length == 4){
Order order = new Order();
order.setType(1);
if(UnionPay.findCard(line[0]) != -1){
//对应卡号的银行卡
Card card = UnionPay.getCards().get(UnionPay.findCard(line[0]));
order.setCard(card);

double money = Double.parseDouble(line[3]);
order.setMoney(money);
if(UnionPay.findAtm(line[2]) != -1){
Atm atm = UnionPay.getAtms().get(UnionPay.findAtm(line[2]));
order.setAtm(atm);
if(card.isPassWordRight(line[1])){
if(card.isCross_bank(atm)){
order.setResult(5);//跨行存取款
}else{
if(money > 0){
if(card.canWithdraw(money)){
order.setResult(1);//取款成功
card.withdraw(money);
order.setBalance(card.getAccount().getBalance());
}else{
order.setResult(4);//取款金额大于余额
}
}else{//money < 0
order.setResult(6);//存款成功
card.deposit(-money);
order.setBalance(card.getAccount().getBalance());
}
}
}else{
order.setResult(3);//银行卡密码错误
}
}else{
order.setResult(2);//ATM机编号不存在
}
}else{
order.setResult(0);//卡号不存在
}
orders.add(order);
}
//输入有误立马退出
else{
break;
}
}

//输出
for (int i = 0; i < orders.size(); i++) {
orders.get(i).showOrder();
}
}
}

复制代码

 

 

三、踩坑心得

  踩到的坑都没解决哪来的踩坑心得。

四、改进建议

  最近学的很糟糕,需要更好的调整自己的状态,让自己保证一个好的,积极的学习状态,目前最失败的一次PTA和较为失败的一段时间。

五、总结

  说再多都没用,努力学学学。找到适合自己的方法,合适自己的方法,rush,rush,rush

 

标签:return,String,第二个,int,void,面向对象,OOP,date,public
From: https://www.cnblogs.com/2574-ljx/p/17365702.html

相关文章

  • 面向对象程序设计题目集总结blog2-22206110-胡瑞杰
    一、前言第二次在博客园上发布面向对象程序设计题目集的总结博客。经过几周的学习,面向对象的理念更加深入。虽然已经学了些面向对象程序设计,学好这部分内容还是有较大难度。关于知识点本次的题目集所体现的知识点已经不仅限于Java的语法知识,还需要考虑设计问题,不......
  • Hadoop全分布配置
    1.实验一:hadoop全分布配置1.1.实验目的完成本实验,您应该能够:掌握hadoop全分布的配置掌握hadoop全分布的安装掌握hadoop配置文件的参数意义1.2.实验要求熟悉hadoop全分布的安装了解hadoop配置文件的意义1.3.实验环境本实验所需之主要资源环境如表1-1......
  • Hadoop集群运行
    1.实验一:hadoop集群运行实验任务一:配置Hadoop格式化步骤一:NameNode格式化[root@master~]#su-hadoop[hadoop@master~]$cd/usr/local/src/hadoop/结果:[hadoop@master~]$./bin/hdfsnamenode–format-bash:./bin/hdfs:Nosuchfileordirectory[hadoop@......
  • hadoop平台安装
    Hadoop平台安装1.实验一:Linux操作系统环境设置1.1.实验目的完成本实验,您应该能够:掌握linux操作系统环境设置掌握hadoop安装的环境要求1.2.实验要求熟悉常用Linux操作系统命令熟悉hadoop安装的环境了解linux修改系统变量命令及各参数1.3.实验环境本实验所......
  • oop题目集04-06总结性blog
    oop题目集04*菜单计价程序-3*有重复的数据*去掉重复的数据*单词统计与排序*面向对象编程(封装性)*GPS测绘中度分秒转换*判断两个日期的先后,计算间隔天数、周数 oop题目集05*正则表达式训练-QQ号校验 * 字符串训练-字符排序*正则表达式训练-验证码校验* 正则表达式......
  • OOP4-6习题心得体会
    面向程序对象设计前言:1.相比于之前的题目,题目集4主要是在一些基本的题目类型,除去第一道题目,其他题目难度均比之前要小,包括字符串的排序,重复数据问题,以及java中Date类的使用,当然重中之重的是第一题的菜单题,不过由于个人的轻视以及懒导致并没有去做这一道题,直接导致了零......
  • OOP 4-6次题目集总结
    前言:   通过前一个月的基础学习,对Java有了一定的了解,在这一个月中学习的知识难度明显增加,题目集的难度也随之增加。第四次的题目集主要考察对LinkedHashSet、ArrayList、Java的封装性、LocalDate类、String类、Integer类、Scanner类等的了解和简单使用。第五次的题目集主要考......
  • OOP训练集04-06总结(22201237-周琪)
    一、前言  在本次Blog必须分析题目集4的7-1、题目集5的7-5、7-6,题目集6的7-1,而这四道题目都是比较难的题目,除这四道题之外的题目,还会再总结几道踩坑较多、难度相对较低的题目。关于此次Blog需要总结的题目集的难度,总体来说还是比较大的,相较于之前的题目集,难度提升了很多,在之......
  • OOP题目集4-6总结
    前言:题目集4-6的题目量不大,但难度比较大,第4次题目集考察的知识点主要是字符串的使用以及数组;第5次题目集考察的知识点主要是正则表达式、类、字符串语法和数组语法的使用和聚合的使用;第6次题目集只有一道题目,但这道题目是由菜单计价程序叠加四次的题目,主要考察类和逻辑,测试点十分......
  • OOP题目集4~6总结
    前言:这次的题目集难度较以往而言提升了很多,突然提升的难度难免会造成些许的不适应。但是题目虽然有难度,同样的收获也与难度成正比增长。而这次题目集总结主要以三道大题来进行分析。  第四次作业:题目7-1菜单计价程序-3:需求分析:设计菜单点单计价程序,根据输入......