菜单类题目
菜单一
题目:
某饭店提供4种菜,每种菜品的基础价格如下:
西红柿炒蛋 15
清炒土豆丝 12
麻婆豆腐 12
油淋生菜 9
设计点菜计价程序,根据输入的订单,计算并输出总价格。
订单由一条或多条点菜记录组成,每条记录一行,最后以"end"结束
每条点菜记录包含:菜名、份额两个信息。
份额可选项包括:1、2、3,分别代表小、中、大份)
不同份额菜价的计算方法:
小份菜的价格=菜品的基础价格。
中份菜的价格=菜品的基础价格1.5。
小份菜的价格=菜品的基础价格2。
如果计算出现小数,按四舍五入的规则进行处理。
参考以下类的模板进行设计:
菜品类:对应菜谱上一道菜的信息。
Dish {
String name;//菜品名称
int unit_price; //单价
int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
}
菜谱类:对应菜谱,包含饭店提供的所有菜的信息。
Menu {
Dish[] dishs ;//菜品数组,保存所有菜品信息
Dish searthDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。
}
点菜记录类:保存订单上的一道菜品记录
Record {
Dish d;//菜品
int portion;//份额(1/2/3代表小/中/大份)
int getPrice()//计价,计算本条记录的价格
}
订单类:保存用户点的所有菜的信息。
Order {
Record[] records;//保存订单上每一道的记录
int getTotalPrice()//计算订单的总价
Record addARecord(String dishName,int portion)
//添加一条菜品信息到订单中。
}
输入格式:
每条点菜记录的格式:
菜名+空格(英文)+份额
注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。
最后一条记录以“end”结束。
输出格式:
订单上所有菜品的总价(整数数值),每份菜
如果订单中包含不能识别的菜名,则在总价之前输出“** does not exist”,**是不能识别的菜名
输入样例:
在这里给出一组输入。例如:
麻婆豆腐 2
西红柿炒蛋 3
end
输出样例:
在这里给出相应的输出。例如:
48
输入样例1:
订单中包含不存在的菜品记录。例如:
麻婆豆腐 2
炒脆肚 2
西红柿炒蛋 3
end
输出样例1:
在这里给出相应的输出。例如:
炒脆肚 does not exist
48
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
代码及解析:
菜单1的功能较少,但是这里先用split给划开了,后面的菜单代码也是基于split,分割之后再进行对应的增删改查
基本每个题目要求给的那几个类都给改成类似pojo只储存数据的样子了(类似但不是)加完getter setter,差不多就结束了
踩的坑:当时一直有3个错我忘了是哪三个了,给的样本测试点又都没错,但是改了几次if else的顺序又对了,因为不知道测试点里的数据是啥,所以我是因为什么原因给它蒙对了我也不知道,但是可以知道这道题有3个测试点和我这道题的if else的顺序有关
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.*;
public class Main {
public static void main(String[] args) {
Menu menu = new Menu(new Dish[]{
new Dish("西红柿炒蛋", 15),
new Dish("清炒土豆丝", 12),
new Dish("麻婆豆腐", 12),
new Dish("油淋生菜", 9)
});
Order order = new Order();
List<String> notFounds = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if ("end".equals(line)) {
break;
}
String[] lineArray = line.split(" ");
String dishName = lineArray[0];
int portion = Integer.parseInt(lineArray[1]);
Dish dish = menu.searthDish(dishName);
if (dish == null) {
notFounds.add(dishName);
} else {
order.addARecord(dish, portion);
}
}
for (String dishName : notFounds) {
System.out.println(dishName + " does not exist");
}
System.out.println(order.getTotalPrice());
}
}
class Menu {
private Dish[] dishs;//菜品数组,保存所有菜品信息
Menu(Dish[] dishs) {
this.dishs = dishs;
}
Dish searthDish(String dishName) {
for (Dish dish : dishs) {
if (dish.getName().equals(dishName)) {
return dish;
}
}
return null;
}
}
class Dish {
String name;//菜品名称
int unit_price; //单价
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getUnit_price() {
return unit_price;
}
public void setUnit_price(int unit_price) {
this.unit_price = unit_price;
}
public Dish(String name, int unit_price) {
this.name = name;
this.unit_price = unit_price;
}
//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
int getPrice(int portion)
{
float b[] = {1, 1.5f, 2};
return Math.round((unit_price * b[portion - 1]));
}
}
class Record {
Dish d;//菜品
int portion;//份额(1/2/3代表小/中/大份)
public Dish getD() {
return d;
}
public void setD(Dish d) {
this.d = d;
}
public int getPortion() {
return portion;
}
public void setPortion(int portion) {
this.portion = portion;
}
public Record(Dish d, int portion) {
this.d = d;
this.portion = portion;
}
//计价,计算本条记录的价格
int getPrice()
{
return d.getPrice(portion);
}
}
class Order {
private List<Record> records = new ArrayList<>();//保存订单上每一道的记录
//计算订单的总价
int getTotalPrice()
{
int sum = 0;
for (Record record : records) {
sum += record.getPrice();
}
return sum;
}
//添加一条菜品信息到订单中。
Record addARecord(Dish dish, int portion) {
Record record = new Record(dish, portion);
records.add(record);
return record;
}
}
菜单二:
题目:
设计点菜计价程序,根据输入的信息,计算并输出总价格。
输入内容按先后顺序包括两部分:菜单、订单,最后以"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
代码及解析:
就如菜单一所说,需要用split进行分割后然后再进行操作,这次的菜单2也是再split的操作下进行改进的,用split之后的每一行的数组长度(或者该数组的第n个节点属于什么数据类型)来判断是属于哪一种指令
然后来改进
菜单2增加了判断时间来打折
我用的是日历类
它有现成的DAY_OF_MONTH,DAY_OF_WEEK,DAY_OF_YEAR这种来判断它是一年/一周/一月的第几天
不一样的是日历类判断第几周DAY_OF_WEEK的时候需要处理一下,要减1
然后还有一点是,这个代码有几个测试点是,有时候会超时有时候不会,我当时去搜了下ArrayList和HashMap哪个速度更快什么的,有的说ArrayList更快有的说HashMap,好像还要看数据量,然后还要看遍历的方法
我自己后面也拿HashMap重构过,发现HashMap无论我用什么方法它超时的项都更多,而且也不知道测试点的内容是啥,所以放弃了又改成了ArrayList
import java.text.ParseException;
import java.util.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
List<Table> tables = new ArrayList<>();
Menu menu = new Menu();
String line = in.nextLine();
Table curTable = null;
while (!line.equals("end")) {
String[] parts = line.split(" ");
if (parts.length > 2) {
if ("table".equals(parts[0])) {
Table table = new Table();
tables.add(table);
table.setTime(parts[2]);
table.setIndex(parts[1]);
table.setDayTime(parts[3]);
table.setOrder(new Order(menu, table));
curTable = table;
} else if (parts.length == 4) {
int orderNum = Integer.parseInt(parts[0]);
String dishName = parts[1];
int portion = Integer.parseInt(parts[2]);
int num = Integer.parseInt(parts[3]);
assert curTable != null;
curTable.getOrder().addARecord(orderNum, dishName, portion, num, curTable.getIndex(), curTable.getIndex());
} else if (parts.length == 5) {
for (Table table : tables) {
if (table.getIndex().equals(parts[0])) {
// 代點菜
int orderNum = Integer.parseInt(parts[1]);
String dishName = parts[2];
int portion = Integer.parseInt(parts[3]);
int num = Integer.parseInt(parts[4]);
table.getOrder().addARecord(orderNum, dishName, portion, num, curTable.getIndex(), parts[0]);
curTable.getOrder().addARecord(orderNum, dishName, portion, num, curTable.getIndex(), parts[0]);
break;
}
}
}
} else if ("delete".equals(parts[1])) {
assert curTable != null;
curTable.getOrder().delARecordByOrderNum(Integer.parseInt(parts[0]));
} else {
menu.addDish(parts[0], Integer.parseInt(parts[1]));
}
line = in.nextLine();
}
for (Table table : tables) {
System.out.println("table " + table.getIndex() + ": ");
Order order = table.getOrder();
for (Record record : order.getRecords()) {
String from = record.getFrom();
String to = record.getTo();
if (record.isAddError()) {
System.out.println(record.getD().getName() + " does not exist");
} else if (record.isDelError()) {
System.out.println("delete error;");
} else if (from.equals(table.getIndex())) {
if (to.equals(table.getIndex())) {
System.out.println(record.getOrderNum() + " " + record.getD().getName() + " " + record.getPrice());
} else {
System.out.println(record.getOrderNum() + " table " + from + " pay for table " + to + " " + record.getPrice());
}
}
}
}
for (Table table : tables) {
Order order = table.getOrder();
int sale = table.getSale();
if (sale > 0) {
System.out.println("table " + table.getIndex() + ": " + Math.round(order.getTotalPrice() * sale / 10f));
} else {
System.out.println("table " + table.getIndex() + " out of opening hours");
}
}
}
}
class Table {
private String index;
private String dayTime;
private String time;
private Order order;
public String getIndex() {
return index;
}
public void setIndex(String index) {
this.index = index;
}
public void setDayTime(String dayTime) {
this.dayTime = dayTime;
}
public void setTime(String time) {
this.time = time;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
private Calendar getTimeCalendar(String time) {
String[] arr = time.split("/");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(arr[0]));
cal.set(Calendar.MINUTE, Integer.parseInt(arr[1]));
cal.set(Calendar.SECOND, Integer.parseInt(arr[2]));
return cal;
}
public int getSale() {
String[] dayArray = time.split("/");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 32);
//
// calendar.set(Calendar.YEAR,Integer.parseInt(dayArray[0]));
// calendar.set(Calendar.MONTH, Integer.parseInt(dayArray[1]) - 1);
// calendar.set(Calendar.DATE,Integer.parseInt(dayArray[2]));
calendar.set(Integer.parseInt(dayArray[0]), Integer.parseInt(dayArray[1])-1, Integer.parseInt(dayArray[2]));
Calendar now = getTimeCalendar(dayTime);
int weekDay = calendar.get(Calendar.DAY_OF_WEEK) - 1;
if (weekDay == 0 || weekDay == 6) {
if (!now.before(getTimeCalendar("9/30/0")) && !now.after(getTimeCalendar("21/00/00"))) {
return 10;
}
} else {
if (!now.before(getTimeCalendar("17/00/00")) && !now.after(getTimeCalendar("20/30/00"))) {
return 8;
}
if (!now.before(getTimeCalendar("10/30/00")) && !now.after(getTimeCalendar("14/30/00"))) {
return 6;
}
}
// 表示不营业
return 0;
}
}
class Menu {
private final Map<String, Dish> dishs = new HashMap<>();//菜品数组,保存所有菜品信息
Menu() {
}
Dish searchDish(String dishName) {
return dishs.get(dishName);
}
//添加一道菜品信息
void addDish(String dishName, int unit_price) {
Dish dish = new Dish(dishName, unit_price);
dishs.put(dishName, dish);
}
}
class Dish {
String name;//菜品名称
int unit_price; //单价
public Dish(String dishName) {
this.name = dishName;
}
public String getName() {
return name;
}
public Dish(String name, int unit_price) {
this.name = name;
this.unit_price = unit_price;
}
//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
int getPrice(int portion) {
float b[] = {1, 1.5f, 2};
return Math.round((unit_price * b[portion - 1]));
}
}
class Record {
private int orderNum;//序号\
private Dish d;//菜品\
private int portion;//份额(1/2/3代表小/中/大份)\
private int num;
private String from;
private String to;
private boolean isDelete = false;
private boolean isAddError = false;
private boolean isDelError = false;
public Record() {
}
public Record(int orderNum, Dish d, int portion, int num, String from, String to) {
this.orderNum = orderNum;
this.d = d;
this.portion = portion;
this.num = num;
this.from = from;
this.to = to;
}
//计价,计算本条记录的价格
int getPrice() {
return d.getPrice(portion) * this.num;
}
public int getOrderNum() {
return orderNum;
}
public Dish getD() {
return d;
}
public void setD(Dish d) {
this.d = d;
}
public boolean isDelete() {
return isDelete;
}
public void setDelete(boolean delete) {
isDelete = delete;
}
public boolean isAddError() {
return isAddError;
}
public void setAddError(boolean addError) {
isAddError = addError;
}
public boolean isDelError() {
return isDelError;
}
public void setDelError(boolean delError) {
isDelError = delError;
}
public String getFrom() {
return from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
}
class Order {
private Menu menu;
private Table table;
public List<Record> getRecords() {
return records;
}
private final List<Record> records = new ArrayList<>();//保存订单上每一道的记录
public Order(Menu menu, Table table) {
this.menu = menu;
this.table = table;
}
//计算订单的总价
int getTotalPrice() {
int sum = 0;
for (Record record : records) {
if (!record.isDelete() && !record.isDelError() && !record.isAddError() && table.getIndex().equals(record.getFrom())) {
int price = record.getPrice();
sum = sum + price;
}
}
return sum;
}
//添加一条菜品信息到订单中。
void addARecord(int orderNum, String dishName, int portion, int num, String from, String to) {
Dish dish = menu.searchDish(dishName);
if (dish == null && table.getIndex().equals(from)) {
Record record = new Record();
record.setD(new Dish(dishName));
record.setAddError(true);
records.add(record);
return;
}
Record record = new Record(orderNum, dish, portion, num, from, to);
records.add(record);
}
public void delARecordByOrderNum(int orderNum) {
for (Record record : records) {
if (record.getOrderNum() == orderNum) {
record.setDelete(true);
return;
}
}
Record record = new Record();
record.setDelError(true);
records.add(record);
}
}
菜单三
题目
本体大部分内容与菜单计价程序-3相同,增加的部分用加粗文字进行了标注。
设计点菜计价程序,根据输入的信息,计算并输出总价格。
输入内容按先后顺序包括两部分:菜单、订单,最后以"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)//根据序号查找一条记录
}
本次课题比菜单计价系列-3增加的异常情况:
1、菜谱信息与订单信息混合,应忽略夹在订单信息中的菜谱信息。输出:“invalid dish”
2、桌号所带时间格式合法(格式见输入格式部分说明,其中年必须是4位数字,月、日、时、分、秒可以是1位或2位数),数据非法,比如:2023/15/16 ,输出桌号+" date error"
3、同一桌菜名、份额相同的点菜记录要合并成一条进行计算,否则可能会出现四舍五入的误差。
4、重复删除,重复的删除记录输出"deduplication :"+序号。
5、代点菜时,桌号不存在,输出"Table number :“+被点菜桌号+” does not exist";本次作业不考虑两桌记录时间不匹配的情况。
6、菜谱信息中出现重复的菜品名,以最后一条记录为准。
7、如果有重复的桌号信息,如果两条信息的时间不在同一时间段,(时段的认定:周一到周五的中午或晚上是同一时段,或者周末时间间隔1小时(不含一小时整,精确到秒)以内算统一时段),此时输出结果按不同的记录分别计价。
8、重复的桌号信息如果两条信息的时间在同一时间段,此时输出结果时合并点菜记录统一计价。前提:两个的桌号信息的时间都在有效时间段以内。计算每一桌总价要先合并符合本条件的饭桌的点菜记录,统一计价输出。
9、份额超出范围(1、2、3)输出:序号+" portion out of range "+份额,份额不能超过1位,否则为非法格式,参照第13条输出。
10、份数超出范围,每桌不超过15份,超出范围输出:序号+" num out of range "+份数。份数必须为数值,最高位不能为0,否则按非法格式参照第16条输出。
11、桌号超出范围[1,55]。输出:桌号 +" table num out of range",桌号必须为1位或多位数值,最高位不能为0,否则按非法格式参照第16条输出。
12、菜谱信息中菜价超出范围(区间(0,300)),输出:菜品名+" price out of range "+价格,菜价必须为数值,最高位不能为0,否则按非法格式参照第16条输出。
13、时间输入有效但超出范围[2022.1.1-2023.12.31],输出:“not a valid time period”
14、一条点菜记录中若格式正确,但数据出现问题,如:菜名不存在、份额超出范围、份数超出范围,按记录中从左到右的次序优先级由高到低,输出时只提示优先级最高的那个错误。
15、每桌的点菜记录的序号必须按从小到大的顺序排列(可以不连续,也可以不从1开始),未按序排列序号的输出:“record serial number sequence error”。当前记录忽略。(代点菜信息的序号除外)
16、所有记录其它非法格式输入,统一输出"wrong format"
17、如果记录以“table”开头,对应记录的格式或者数据不符合桌号的要求,那一桌下面定义的所有信息无论正确或错误均忽略,不做处理。如果记录不是以“table”开头,比如“tab le 55 2023/3/2 12/00/00”,该条记录认为是错误记录,后面所有的信息并入上一桌一起计算。
本次作业比菜单计价系列-3增加的功能:
菜单输入时增加特色菜,特色菜的输入格式:菜品名+英文空格+基础价格+“T”
例如:麻婆豆腐 9 T
菜价的计算方法:
周一至周五 7折, 周末全价。
注意:不同的四舍五入顺序可能会造成误差,请按以下步骤累计一桌菜的菜价:
计算每条记录的菜价:将每份菜的单价按份额进行四舍五入运算后,乘以份数计算多份的价格,然后乘以折扣,再进行四舍五入,得到本条记录的最终支付价格。
最后将所有记录的菜价累加得到整桌菜的价格。
输入格式:
桌号标识格式: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 T
table 31 2023/2/1 14/20/00
1 麻婆豆腐 1 16
2 油淋生菜 1 2
2 delete
2 delete
end
输出样例:
在这里给出相应的输出。例如:
table 31:
1 num out of range 16
2 油淋生菜 18
deduplication 2
table 31: 0 0
输入样例1:
份数超出范围+份额超出范围。例如:
麻婆豆腐 12
油淋生菜 9 T
table 31 2023/2/1 14/20/00
1 麻婆豆腐 1 16
2 油淋生菜 4 2
end
输出样例1:
份数超出范围+份额超出范围。例如:
table 31:
1 num out of range 16
2 portion out of range 4
table 31: 0 0
输入样例2:
桌号信息错误。例如:
麻婆豆腐 12
油淋生菜 9 T
table a 2023/3/15 12/00/00
1 麻婆豆腐 1 1
2 油淋生菜 2 1
end
输出样例2:
在这里给出相应的输出。例如:
wrong format
输入样例3:
混合错误:桌号信息格式错误+混合的菜谱信息(菜谱信息忽略)。例如:
麻婆豆腐 12
油淋生菜 9 T
table 55 2023/3/31 12/000/00
麻辣香锅 15
1 麻婆豆腐 1 1
2 油淋生菜 2 1
end
输出样例3:
在这里给出相应的输出。例如:
wrong format
输入样例4:
错误的菜谱记录。例如:
麻婆豆腐 12.0
油淋生菜 9 T
table 55 2023/3/31 12/00/00
麻辣香锅 15
1 麻婆豆腐 1 1
2 油淋生菜 2 1
end
输出样例4:
在这里给出相应的输出。例如:
wrong format
table 55:
invalid dish
麻婆豆腐 does not exist
2 油淋生菜 14
table 55: 14 10
输入样例5:
桌号格式错误(以“table”开头)+订单格式错误(忽略)。例如:
麻婆豆腐 12
油淋生菜 9 T
table a 2023/3/15 12/00/00
1 麻婆 豆腐 1 1
2 油淋生菜 2 1
end
输出样例5:
在这里给出相应的输出。例如:
wrong format
输入样例6:
桌号格式错误,不以“table”开头。例如:
麻婆豆腐 12
油淋生菜 9 T
table 1 2023/3/15 12/00/00
1 麻婆豆腐 1 1
2 油淋生菜 2 1
tab le 2 2023/3/15 12/00/00
1 麻婆豆腐 1 1
2 油淋生菜 2 1
end
输出样例6:
在这里给出相应的输出。例如:
table 1:
1 麻婆豆腐 12
2 油淋生菜 14
wrong format
record serial number sequence error
record serial number sequence error
table 1: 26 17
代码及解析
这次加的功能就相对比较多了用split来分之后用数组长度这种来判断已经不ok了,所以重构成了用正则表达式来判断
正则表达式语法课参考
添加链接描述
使用了try catch来捕获异常来解决题目需求,如果出现DateTimeException这个异常就按题目要求输出 桌号 + date error
这个是系统自带的抛出异常
其他的还是按if else
import java.text.ParseException;
import java.time.DateTimeException;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Scanner;
class Dish {
String name;
int unit_price;
boolean isT = false;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getUnit_price() {
return unit_price;
}
public void setUnit_price(int unit_price) {
this.unit_price = unit_price;
}
public boolean isT() {
return isT;
}
public void setT(boolean t) {
isT = t;
}
}
class Record {
int orderNum;
Dish d;
int portion;
int quota;
boolean isDeleted = false;
public int getOrderNum() {
return orderNum;
}
public void setOrderNum(int orderNum) {
this.orderNum = orderNum;
}
public Dish getD() {
return d;
}
public void setD(Dish d) {
this.d = d;
}
public int getPortion() {
return portion;
}
public void setPortion(int portion) {
this.portion = portion;
}
public int getQuota() {
return quota;
}
public void setQuota(int quota) {
this.quota = quota;
}
public boolean isDeleted() {
return isDeleted;
}
public void setDeleted(boolean deleted) {
isDeleted = deleted;
}
int getPrice() {
if (portion == 2)
return (int) Math.round(1.5 * d.getUnit_price()) * quota;
else if (portion == 3)
return 2 * d.getUnit_price() * quota;
else
return d.getUnit_price() * quota;
}
}
class Menu {
public ArrayList<Dish> getDishs() {
return dishs;
}
public void setDishs(ArrayList<Dish> dishs) {
this.dishs = dishs;
}
ArrayList<Dish> dishs = new ArrayList<Dish>();
Dish searchDish(String dishName) {
for (Dish d:dishs){
if (dishName.equals(d.name)) {
return d;
}
}
return null;
}
Dish addDish(String dishName, int unit_price) {
Dish newDish = new Dish();
newDish.setName(dishName);
newDish.setUnit_price(unit_price);
return newDish;
}
}
class Order {
public ArrayList<Record> getRecords() {
return records;
}
public void setRecords(ArrayList<Record> records) {
this.records = records;
}
// Record[] records = new Record[20];
ArrayList<Record> records = new ArrayList<Record>();
Record addARecord(int orderNum, String dishName, int portion, int quota, Menu menu) {
Record newRecord = new Record();
newRecord.orderNum = orderNum;
newRecord.d = menu.searchDish(dishName);
newRecord.portion = portion;
newRecord.quota = quota;
System.out.println(newRecord.orderNum + " " + newRecord.d.name + " " + newRecord.getPrice());
return newRecord;
}
int searchRecord(String name) {
int i=0;
for (Record r:records) {
if (r.getD().getName().equals(name) ) {
return i;
}
i++;
}
return -1;
}
void delARecordByOrderNum(int orderNum) {
int i = 0, flag = 0;
for (Record r:records){
if (r.getOrderNum() == orderNum) {
if (!r.isDeleted()) {
r.setDeleted(!r.isDeleted());
} else {
System.out.println("deduplication " + orderNum);
}
flag++;
}
}
if (flag == 0) {
System.out.println("delete error;");
}
}
}
class Table {
Order order = new Order();
int num;
LocalDateTime time;
int weekday;
long sum = 0;
long origSum = 0;
public long getSum() {
return sum;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public LocalDateTime getTime() {
return time;
}
public void setTime(LocalDateTime time) {
this.time = time;
}
public int getWeekDay() {
return weekday;
}
public void setWeekday(int weekday) {
this.weekday = weekday;
}
public void setSum(long sum) {
this.sum = sum;
}
public long getOrigSum() {
return origSum;
}
public void setOrigSum(long origSum) {
this.origSum = origSum;
}
void od(Menu menu, String str1, String str2, int portion, int quota) {
{
order.getRecords().add(order.addARecord(Integer.parseInt(str1), str2, portion, quota, menu));
}
}
void getWeekDay2() {
weekday = time.getDayOfWeek().getValue();
}
void getSum2() {
for (Record r:order.records) {
if (!r.isDeleted()) {
origSum += r.getPrice();
if (r.getD().isT()) {
if (weekday > 0 && weekday < 6) {
sum += Math.round(r.getPrice() * 0.7);
} else {
sum += r.getPrice();
}
} else {
if (weekday > 0 && weekday < 6) {
if (time.getHour() >= 17 && time.getHour() < 20)
sum += Math.round(r.getPrice() * 0.8);
if (time.getHour() == 20) {
if (time.getMinute() <= 30)
sum += Math.round(r.getPrice() * 0.8);
}
if (time.getHour() >= 10 && time.getHour() < 14)
sum += Math.round(r.getPrice() * 0.6);
if (time.getHour() == 14) {
if (time.getMinute() <= 30)
sum += Math.round(r.getPrice() * 0.6);
}
} else
sum += r.getPrice();
}
}
}
}
private Calendar getTimeCalendar(String time) {
String[] arr = time.split("/");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(arr[0]));
cal.set(Calendar.MINUTE, Integer.parseInt(arr[1]));
cal.set(Calendar.SECOND, Integer.parseInt(arr[2]));
return cal;
}
public int getSale() {
String dayTime = null;
String timeStr= String.valueOf(time);
String[] dayArray = timeStr.split("/");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 32);
calendar.set(Integer.parseInt(dayArray[0]), Integer.parseInt(dayArray[1])-1, Integer.parseInt(dayArray[2]));
Calendar now = getTimeCalendar(dayTime);
int weekDay = calendar.get(Calendar.DAY_OF_WEEK) - 1;
//周末全價且在營業時間内
if (weekDay == 0 || weekDay == 6) {
if (!now.before(getTimeCalendar("9/30/0")) && !now.after(getTimeCalendar("21/00/00"))) {
return 10;
}
} else {
if (!now.before(getTimeCalendar("17/00/00")) && !now.after(getTimeCalendar("20/30/00"))) {
return 8;
}
if (!now.before(getTimeCalendar("10/30/00")) && !now.after(getTimeCalendar("14/30/00"))) {
return 6;
}
}
// 表示不营业
return 0;
}
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 class Main {
public static void main(String[] args) throws ParseException {
Menu menu = new Menu();
ArrayList<Table> tables = new ArrayList<Table>();
Scanner scanner = new Scanner(System.in);
String str1;
int i = 0;
int portion = 0, quota = 0;
while (true) {// 输入菜单
Dish tempDish = new Dish();
Dish isRepeat = null;
str1 = scanner.nextLine();
if (str1.matches("[\\S]* [1-9][\\d]*")) {
String[] token = str1.split(" ");
tempDish.setName(token[0]);
tempDish.setUnit_price(Integer.parseInt(token[1]));
if (tempDish.getUnit_price() > 300) {
System.out.println(tempDish.getName() + " price out of range " + tempDish.getUnit_price());
continue;
}
tempDish.setT(false);
isRepeat = menu.searchDish(tempDish.getName());
if (isRepeat != null) {
menu.dishs.remove(isRepeat);
}
menu.getDishs().add(tempDish);
} else if (str1.matches("[\\S]* [\\d]* T")) {
String[] token = str1.split(" ");
tempDish.setName(token[0]);
tempDish.setUnit_price(Integer.parseInt(token[1]));
if (tempDish.getUnit_price() > 300) {
System.out.println(tempDish.getName() + " price out of range " + tempDish.getUnit_price());
continue;
}
tempDish.setT(true);
if (isRepeat != null) {
menu.dishs.remove(isRepeat);
}
menu.dishs.add(tempDish);
} else if (str1.equals("end")) {
break;
} else if (str1.matches("tab.*")) {
break;
} else {
System.out.println("wrong format");
continue;
}
}
while (!str1.equals("end")) {
Table tempTable = 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]);
}
tempTable.setNum(Integer.parseInt(token[1]));
if (tempTable.num > 55) {
System.out.println(tempTable.num + " table num out of range");
str1 = scanner.nextLine();
continue;
}
try {
tempTable.setTime(LocalDateTime.of(intDate[0], intDate[1], intDate[2], intTime[0], intTime[1], intTime[2]));
tempTable.getWeekDay2();
} catch (DateTimeException e) {
System.out.println(tempTable.num + " date error");
str1 = scanner.nextLine();
continue;
}
if (!tempTable.isOpen()) {
System.out.println("table " + tempTable.getNum() + " out of opening hours");
str1 = scanner.nextLine();
continue;
}
if (!(tempTable.time.isAfter(LocalDateTime.of(2022, 1, 1, 0, 0, 0))
&& tempTable.time.isBefore(LocalDateTime.of(2024, 1, 1, 0, 0, 0)))) {
System.out.println("not a valid time period");
str1 = scanner.nextLine();
continue;
}
// 判断桌号是否重复
if (tempTable.isOpen()) {
for (Table t:tables) {
// 有重复的桌号
if (tempTable.getNum() == t.getNum() && t.isOpen()) {
Duration duration = Duration.between(tempTable.getTime(), t.getTime());
// 同一天
if (duration.toDays() == 0) {
// 在周一到周五
if (tempTable.getWeekDay() > 0 && tempTable.getWeekDay() < 6) {
// 在同一时间段
if (tempTable.getTime().getHour() < 15 && t.getTime().getHour() < 15) {
isRepeat = true;
repeatNum = i;
break;
}
}
// 在周末
else {
// 时间相差小于一小时
if (duration.toHours() < 3600) {
repeatNum = i;
isRepeat = true;
break;
}
}
}
}
}
}
System.out.println("table " + tempTable.getNum() + ": ");
} else {
System.out.println("wrong format");
str1 = scanner.nextLine();
continue;
}
// 本桌开始点菜
while (true) {
str1 = scanner.nextLine();
if (str1.matches("[1-9][\\d]* [\\S]* [\\d] [1-9][\\d]*")) {
String[] token = str1.split(" ");
portion = Integer.parseInt(token[2]);
quota = Integer.parseInt(token[3]);
if (tempTable.getOrder().getRecords().size() > 0) {
if (Integer
.parseInt(token[0]) <= tempTable.getOrder().getRecords().get(tempTable.getOrder().getRecords().size() - 1).getOrderNum()) {
System.out.println("record serial number sequence error");
continue;
}
}
if (menu.searchDish(token[1]) == null) {
System.out.println(token[1] + " does not exist");
continue;
}
if (portion > 3 || portion < 1) {
System.out.println(Integer.parseInt(token[0]) + " portion out of range " + portion);
continue;
}
if (quota > 15) {
System.out.println(Integer.parseInt(token[0]) + " num out of range " + quota);
continue;
}
tempTable.od(menu, token[0], token[1], portion, quota);
}
// 判断是否为删除订单
else if (str1.matches("[1-9][\\d]* delete")) {
String[] token = str1.split(" ");
tempTable.order.delARecordByOrderNum(Integer.parseInt(token[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 (Table t:tables) {
if (t.getNum() == Integer.parseInt(token[0])) {
exist = true;
break;
}
}
if (exist) {
System.out.print(Integer.parseInt(token[1]) + " table " + tempTable.num + " pay for table "+ Integer.parseInt(token[0]) + " ");
Record treat = new Record();
treat.setD( menu.searchDish(token[2]));
portion = Integer.parseInt(token[3]);
quota = Integer.parseInt(token[4]);
treat.setPortion(portion);
treat.setQuota(quota);
System.out.print(treat.getPrice() + "\n");
tempTable.setOrigSum(tempTable.getOrigSum()+treat.getPrice());
}
// 若不存在则输出内容
else {
System.out.println("Table number :" + Integer.parseInt(token[0]) + " does not exist");
}
} else if (str1.equals("end")) {
break;
} else if (str1.matches("table.*")) {
break;
} else {
System.out.println("wrong format");
continue;
}
}
// 本桌点菜结束,进入下一桌
if (isRepeat) {
for (i = 0; i < tables.get(repeatNum).getOrder().getRecords().size(); i++) {
for (int j = 0; j < tempTable.getOrder().getRecords().size(); j++) {
if (tables.get(repeatNum).getOrder().getRecords().get(i).d.name == tempTable.getOrder().getRecords().get(j).d.getName()) {
if (tables.get(repeatNum).getOrder().getRecords().get(i).portion == tempTable.getOrder().getRecords()
.get(j).portion) {
tables.get(repeatNum).getOrder().getRecords().get(i).quota += tempTable.getOrder().getRecords().get(j).getQuota();
tempTable.getOrder().getRecords().remove(j);
}
}
}
}
tables.get(repeatNum).getOrder().getRecords().addAll(tempTable.getOrder().getRecords());
continue;
}
tables.add(tempTable);
}
else {
str1 = scanner.nextLine();
}
}
for (Table t:tables){
if (t.isOpen()){
t.getSum2();
System.out.println("table " + t.getNum() + ": " + t.getOrigSum() + " " + t.getSum());
}
}
// 最终输出桌号订单信息
}
}
一些其它题目
日期类
给定一个日期,判定是否为合法日期。如果合法,判断该年是否闰年,该日期是当年第几天、当月第几天、当周第几天、。
给定起始日期与结束日期,判定日期是否合法且结束日期是否早于起始日期。如果均合法,输出结束日期与起始日期之间的相差的天数、月数、念书。
输入格式:
第一行输入一个日期字符串,格式为"YYYY-MM-dd"
第二行输入两个日期字符串,中间使用空格隔开。分别代表开始日期与结束日期。
输出格式:
如果第一行日期字符串非法,输出自定义的错误信息。
如果第一行日期有效,输出相关信息,如果是闰年要输出是闰年。
如果第二行两个日期,只要有一个无效。就输出相关错误信息。
如果第二行两个日期有效且结束日期不早于开始日期,输出相关信息。
就和前面讲的菜单二的日期一样用的日期类
可以在这个页面ctrl f搜索日历类去前面看
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.Year;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s0 = sc.nextLine();//输入日期 格式为2020/3/21
String s1 = sc.nextLine();
DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
try {
Date star = dft.parse(s0);//开始时间
String[] st1=s0.split("-");
int t1=Integer.parseInt(st1[1]);
int t11=Integer.parseInt(st1[0]);
String[] st2=s1.split("-");
int t2=Integer.parseInt(st2[1]);
int t22=Integer.parseInt(st2[0]);
Date end = dft.parse(s1);
int q1=0;
int q2=0;
int q3=0;
if (star.before(end)){
System.out.println("第一个日期比第二个日期更早");
Year firstYear = Year.of(t11);
q3=t22-t11;
Calendar c1 = Calendar.getInstance();
//这里有bug必须减1
c1.set(t11, t1-1, Integer.parseInt(st1[2])); // 设置年月日,时分秒将默认采用当前值
int yearDay = c1.get(Calendar.DAY_OF_YEAR);
Calendar c2 = Calendar.getInstance();
//这里有bug必须减1
c2.set(t22, t2-1, Integer.parseInt(st2[2])); // 设置年月日,时分秒将默认采用当前值
int yearDa2y = c2.get(Calendar.DAY_OF_YEAR);
if (firstYear.isLeap(t11)){
int temp=yearDa2y-yearDay;
for (int i=t11+1;i<t22;i++){
if (firstYear.isLeap(i)){
q1=q1+366;
}else {
q1=q1+365;
}
}
q1=q1+temp;
}else {
int temp=yearDa2y+(365-yearDay);
for (int i=t11+1;i<t22;i++){
if (firstYear.isLeap(i)){
q1=q1+366;
}else {
q1=q1+365;
}
}
q1=q1+temp;
}
q2=Integer.parseInt(st2[1])-Integer.parseInt(st1[1]);
}
if (end.before(star)){
q3=t11-t22;
Calendar c1 = Calendar.getInstance();
//这里有bug必须减1
c1.set(t11, t1-1, Integer.parseInt(st1[2])); // 设置年月日,时分秒将默认采用当前值
int yearDay = c1.get(Calendar.DAY_OF_YEAR);
Calendar c2 = Calendar.getInstance();
//这里有bug必须减1
c2.set(t22, t2-1, Integer.parseInt(st2[2])); // 设置年月日,时分秒将默认采用当前值
int yearDa2y = c2.get(Calendar.DAY_OF_YEAR);
if (Year.isLeap(t22)){
int temp=yearDa2y+(366-yearDay);
for (int i=t22+1;i<t11;i++){
if (Year.isLeap(i)){
q1=q1+366;
}else {
q1=q1+365;
}
}
q1=q1+temp;
}else {
int temp=yearDay-yearDa2y;
for (int i=t22+1;i<t11;i++){
if (Year.isLeap(i)){
q1=q1+366;
}else {
q1=q1+365;
}
}
q1=q1+temp;
}
q2=Integer.parseInt(st2[1])-Integer.parseInt(st1[1]);
System.out.println("第一个日期比第二个日期更晚");
}
if ((judge(t11,t1,Integer.parseInt(st1[2]))==true)&&judge(t22,t2,Integer.parseInt(st2[2]))==true){
System.out.println("两个日期间隔"+q1+"天");
System.out.println("两个日期间隔"+q1/7+"周");
}else {
// System.out.println(s0+"或"+s1+"中有不合法的日期.");
}
} catch (ParseException e) {
e.printStackTrace();
}
}
public static boolean isValidDate(String dttm,String format) {
if (dttm == null || dttm.isEmpty() || format == null || format.isEmpty()) {
return false;
}
if (format.replaceAll("'.+?'", "").indexOf("y") < 0) {
format += "/yyyy";
DateFormat formatter = new SimpleDateFormat("/yyyy");
dttm += formatter.format(new Date());
}
String regex = "^\\d{4}\\-(0[1-9]|1[012])\\-(0[1-9]|[12][0-9]|3[01])$";
if (!dttm.matches(regex)){
return false;
}
DateFormat formatter = new SimpleDateFormat(format);
formatter.setLenient(false);
ParsePosition pos = new ParsePosition(0);
Date date = formatter.parse(dttm, pos);
if (date == null || pos.getErrorIndex() > 0) {
return false;
}
if (pos.getIndex() != dttm.length()) {
return false;
}
if (formatter.getCalendar().get(Calendar.YEAR) > 9999) {
return false;
}
return true;
}
public static boolean judge(int y,int m,int d) {
boolean p=false;
if(m<1||m>12) {
p=false;}
else if(m==1||m==3||m==5||m==7||m==8||m==10||m==12) {
if(d<=31) {
p=true;}
else {
p=false;
}
}
else if(m==2) {
if(y%400==0||(y%4==0&&y%100!=0)) {
if(d<=29) {
p=true;
}
else {
p=false;
}
}
else {
if(d<=28){
p=true;
}
else {
p=false;
}
}
}
else {
if(d<=30){
p=true;
}
else {
p=false;
}
}
return p;
}
}
小明走格子
从A点到B点有n个格子,小明现在要从A点到B点,小明吃了些东西,补充了一下体力,他可以一步迈一个格子,也可以一步迈两个格子,也可以一步迈3个格子,也可以一步迈4个格子。请编写程序计算小明从A点到B点一共有多少种走法。
grid2.jpg
输入格式:
输入包含多组数据,第一行为一个整数m,m不超过10000,表示输入数据组数。接下来m行,每行为一个整数n(保证对应的输出结果小于2
31
),表示从A点到B点的格子数。
输出格式:
输出为m个整数,表示对于每组数据小明从A点到B点的走法数。
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner cin=new Scanner(System.in);
int n= cin.nextInt();
long[] b=new long[1000];
int[] a=new int[200];
b[0]=1;
b[1]=1;
b[2]=2;
b[3]=4;
b[4]=8;
for(int i=1;i<=n;i++)
a[i]=cin.nextInt();
for(int i=1;i<=n;i++)
{
if(a[i]<5)
System.out.println(b[a[i]]);
else
{
for(int j=5;j<=a[i];j++)
b[j]=2*b[j-1]-b[j-5];
System.out.println(b[a[i]]);
}
}
}
}
单词判断
从键盘录入一段英文文本(句子之间的标点符号只包括“,”或“.”,单词之间、单词与标点之间都以" "分割。
要求:按照每个单词的长度由高到低输出各个单词(重复单词只输出一次),如果单词长度相同,则按照单词的首字母顺序(不区分大小写,首字母相同的比较第二个字母,以此类推)升序输出。
输入格式:
一段英文文本。
输出格式:
按照题目要求输出的各个单词(每个单词一行)。
核心是使用这个现成的方法
Collections.sort(list, new Comparator()
s1.compareToIgnoreCase(s2)
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String T = sc.nextLine();
String[] words = T.split("[,. ]+");
Set<String> set = new HashSet<>(Arrays.asList(words));
List<String> list = new ArrayList<>(set);
Collections.sort(list, new Comparator<String>() {
public int compare(String s1, String s2) {
if (s1.length() != s2.length()) {
return s2.length() - s1.length();
} else {
return s1.compareToIgnoreCase(s2);
}
}
});
for (String word : list) {
System.out.println(word);
}
}
}
日期类 计算隔天数
从键盘输入两个日期,格式如:2022-06-18。判断两个日期的先后,并输出它们之间间隔的天数、周数(不足一周按0计算)。
预备知识:通过查询Java API文档,了解Scanner类中nextLine()等方法、String类中split()等方法、Integer类中parseInt()等方法的用法,了解LocalDate类中of()、isAfter()、isBefore()、until()等方法的使用规则,了解ChronoUnit类中DAYS、WEEKS、MONTHS等单位的用法。
输入格式:
输入两行,每行输入一个日期,日期格式如:2022-06-18
输出格式:
第一行输出:第一个日期比第二个日期更早(晚)
第二行输出:两个日期间隔XX天
第三行输出:两个日期间隔XX周
直接日历类减就可以了,然后事先先判断个先后
直接用LocalDate现成的isBefore()
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
String s2 = sc.nextLine();
String [] dateStr1 = s1.split("-");
String [] dateStr2 = s2.split("-");
int year1 = Integer.parseInt( dateStr1[0] );
int month1 = Integer.parseInt( dateStr1[1] );
int day1 = Integer.parseInt( dateStr1[2] );
int year2 = Integer.parseInt( dateStr2[0] );
int month2 = Integer.parseInt( dateStr2[1] );
int day2 = Integer.parseInt( dateStr2[2] );
LocalDate date1 = LocalDate.of(year1, month1, day1);
LocalDate date2 = LocalDate.of(year2, month2, day2);
if(date1.isBefore(date2) )
System.out.println("第一个日期比第二个日期更早");
else
System.out.println("第一个日期比第二个日期更晚");
long dayCount = date1.until(date2, ChronoUnit.DAYS);
long weekCount = date1.until(date2, ChronoUnit.WEEKS);
System.out.println("两个日期间隔" + Math.abs(dayCount) + "天" );
System.out.println("两个日期间隔" + Math.abs(weekCount) + "周" );
sc.close();
}
}