南昌航空大学BLOG-2Java总结

发布时间 2023-11-19 17:50:34作者: 蟹老板798

题目列表

 

 

 前言

、Java基础作业总结
在Java基础作业中,我们学习了Java的基本语法、数据类型、运算符、流程控制等内容。通过作业的练习,我对Java的基础知识有了更深入的理解。在这次作业中,我发现了自己在一些基础知识上的不足,比如对于数据类型的理解不够深刻,对于流程控制的应用不够灵活。因此,我在后续的学习中会更加注重这些基础知识的理解和掌握。

二、Java面向对象作业总结
在Java面向对象作业中,我们学习了类与对象、封装、继承、多态等内容。通过作业的练习,我对Java面向对象的思想有了更深入的理解。在这次作业中,我发现了自己在面向对象编程方面的不足,比如对于封装和继承的应用不够熟练,对于多态的理解不够深刻。因此,我在后续的学习中会更加注重面向对象编程的理解和应用。

三、Java集合框架作业总结
在Java集合框架作业中,我们学习了集合框架的基本概念、ArrayList、LinkedList、HashMap等集合类的使用。通过作业的练习,我对Java集合框架有了更深入的理解。在这次作业中,我发现了自己在集合框架的应用方面的不足,比如对于集合类的选择不够合理,对于集合类的操作不够熟练。因此,我在后续的学习中会更加注重集合框架的应用和实践。

总结:
通过这几次作业的学习和总结,我发现了自己在Java编程方面的不足之处,也对自己未来的学习方向有了更清晰的认识。在接下来的学习中,我会更加努力地学习和实践,不断提升自己的编程能力,为将来的发展打下坚实的基础。

菜单计价程序2

题目要求:
设计点菜计价程序,根据输入的信息,计算并输出总价格。输入内容按先后顺序包括两部分:菜单、订单,最后以"end"结束。菜单由一条或多条菜品记录组成,每条记录一行。每条菜品记录包含:菜名、基础价格两个信息。订单分:点菜记录和删除信息。每一类信息都可包含一条或多条记录,每条记录一行。点菜记录包含:序号、菜名、份额、份数。份额可选项包括:1、2、3,分别代表小、中、大份。删除记录格式:序号 delete
 

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

class MenuItem {
String name;
double price;

public MenuItem(String name, double price) {
this.name = name;
this.price = price;
}
}

class OrderItem {
int number;
String name;
String size;
int quantity;

public OrderItem(int number, String name, String size, int quantity) {
this.number = number;
this.name = name;
this.size = size;
this.quantity = quantity;
}
}

public class MenuPricingProgram {
public static void main(String[] args) {
ArrayList<MenuItem> menu = new ArrayList<>();
ArrayList<OrderItem> order = new ArrayList<>();
Scanner scanner = new Scanner(System.in);

// 读取菜单信息
System.out.println("请输入菜单信息,格式为:菜名 价格,输入end结束");
String input;
while (!(input = scanner.nextLine()).equals("end")) {
String[] menuInfo = input.split(" ");
menu.add(new MenuItem(menuInfo[0], Double.parseDouble(menuInfo[1])));
}

// 读取订单信息
System.out.println("请输入订单信息,格式为:序号 菜名 份额 份数 或 序号 delete,输入end结束");
while (!(input = scanner.nextLine()).equals("end")) {
if (input.contains("delete")) {
int deleteNumber = Integer.parseInt(input.split(" ")[0]);
order.removeIf(item -> item.number == deleteNumber);
} else {
String[] orderInfo = input.split(" ");
order.add(new OrderItem(Integer.parseInt(orderInfo[0]), orderInfo[1], orderInfo[2], Integer.parseInt(orderInfo[3]));
}
}

// 计算总价格
double totalPrice = 0;
for (OrderItem item : order) {
for (MenuItem menuItem : menu) {
if (menuItem.name.equals(item.name)) {
double itemPrice = menuItem.price;
if (item.size.equals("中")) {
itemPrice *= 1.5;
} else if (item.size.equals("大")) {
itemPrice *= 2.0;
}
totalPrice += itemPrice * item.quantity;
}
}
}

System.out.println("总价格为:" + totalPrice);

scanner.close();
}
}

2、菜单计价程序3

题目要求:在菜单计价程序2的基础上,添加以下要求:

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

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

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

点菜记录包含:序号、菜名、份额、份数。份额可选项包括: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"

import java.util.*;

class MenuItem {
String name;
double price;

public MenuItem(String name, double price) {
this.name = name;
this.price = price;
}
}

class OrderItem {
int number;
String name;
String size;
int quantity;

public OrderItem(int number, String name, String size, int quantity) {
this.number = number;
this.name = name;
this.size = size;
this.quantity = quantity;
}
}

class TableOrder {
int tableNum;
String time;
ArrayList<OrderItem> orders;

public TableOrder(int tableNum, String time) {
this.tableNum = tableNum;
this.time = time;
this.orders = new ArrayList<>();
}

public void addOrder(OrderItem order) {
orders.add(order);
}

public void removeOrder(int orderNumber) {
orders.removeIf(item -> item.number == orderNumber);
}

public double calculateTotalPrice(ArrayList<MenuItem> menu) {
// 计算总价格
double totalPrice = 0;
for (OrderItem item : orders) {
for (MenuItem menuItem : menu) {
if (menuItem.name.equals(item.name)) {
double itemPrice = menuItem.price;
if (item.size.equals("2")) {
itemPrice *= 1.5;
} else if (item.size.equals("3")) {
itemPrice *= 2.0;
}
totalPrice += itemPrice * item.quantity;
}
}
}

// 根据时间计算折扣
// ...

return totalPrice;
}
}

public class MenuPricingProgram {
public static void main(String[] args) {
ArrayList<MenuItem> menu = new ArrayList<>();
ArrayList<TableOrder> tableOrders = new ArrayList<>();
Scanner scanner = new Scanner(System.in);

// 读取菜单信息
// ...

// 读取订单信息
// ...

// 计算每桌总价
for (TableOrder order : tableOrders) {
double totalPrice = order.calculateTotalPrice(menu);
System.out.println("桌号 " + order.tableNum + " 的总价格为:" + Math.round(totalPrice));
}

scanner.close();
}
}

 

3、菜单计价程序4

题目要求:在菜单计价程序3的基础上,添加以下要求:

本次课题比菜单计价系列-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折, 周末全价

 

import java.time.DateTimeException;
import java.time.LocalDate;
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Order order = new Order();
Menu menu = new Menu();
boolean tnum = false;
boolean tablefomat = true;
String time = "";
boolean mixmenu = true;
String s = sc.nextLine();
while (!s.equals("end")){
int flag = 0;
boolean flag1 = s.matches("[\\S]*\\s[0-9]*");
boolean flag2 = s.matches("table\\s[^0\\s]\\S*\\s\\d{4}/\\d{1,2}/\\d{1,2}\\s\\d{1,2}/\\d{1,2}/\\d{1,2}");
boolean flag3 = s.matches("[1-9]\\d*\\s[\\S]*\\s\\d\\s\\d*");
boolean flag4 = s.matches("[1-9]\\d*\\s\\d*\\s[\\S]*\\s\\d\\s\\d*");
boolean flag5 = s.matches("\\d*\\sdelete");
boolean flag6 = s.matches("[\\S]*\\s[0-9]*\\sT");
String[] str = s.split(" ");
if((flag1||flag6)){
if((!mixmenu)&& tnum){
System.out.println("invalid dish");
}
else {
String name = str[0];
int price = Integer.parseInt(str[1]);
if(price<=0||price>=300){
System.out.println(name+ " " + "price out of range " + price);
}
else {
if (!flag6) {
menu.addDish(name, price, "F");
} else {
menu.addDish(name, price, "T");
}
}
}
}
else if(flag2){
mixmenu = false;
time = s;
try {
String[] strings = s.split(" ");
int num = Integer.parseInt(strings[1]);
tablefomat = true;
if(!(num>0&&num<=55)){
System.out.println(num + " table num out of range");
tnum = false;
tablefomat = false;
}
else {
try {
String[] s1 = strings[2].split("/");
LocalDate ld = LocalDate.of(Integer.parseInt(s1[0]), Integer.parseInt(s1[1]), Integer.parseInt(s1[2]));
if (!isTimeInRange(strings[2])) {
System.out.println("not a valid time period");
tnum = false;
tablefomat = false;
} else if (!order.whetherTimeRight(time)) {
System.out.println(strings[0] + " " + strings[1] + " out of opening hours");
tnum = false;
tablefomat = false;
} else {
tnum = true;
Table table = new Table(s, -1, -1);
order.tables.add(table);
System.out.println(strings[0] + " " + num + ": ");
}
} catch (DateTimeException e) {
tnum = false;
System.out.println(strings[1] + " date error");
}
}
}catch (NumberFormatException e){
tnum = false;
tablefomat = false;
System.out.println("wrong format");
}
}
else if(flag3){
if(tnum) {
int orderNum = Integer.parseInt(str[0]);
String name = str[1];
int portion = Integer.parseInt(str[2]);
int num = Integer.parseInt(str[3]);
if (menu.searthDish(name) != null) {
order.addARecord(orderNum, name, portion, num, menu, time, flag);
} else {
System.out.println(name + " does not exist");
}
}
}
else if(flag4){
if(tnum) {
flag = Integer.parseInt(str[0]);
if(!order.isTableNumisExist(flag)){
System.out.println("Table number :" + flag +" does not exist");
}
else {
int orderNum = Integer.parseInt(str[1]);
String name = str[2];
int portion = Integer.parseInt(str[3]);
int num = Integer.parseInt(str[4]);
if (menu.searthDish(name) != null) {
order.addARecord(orderNum, name, portion, num, menu, time, flag);
} else {
System.out.println(name + " does not exist");
}
}
}
}
else if(flag5){
if(tnum) {
int ordernum = Integer.parseInt(str[0]);
order.delARecordByOrderNum(ordernum, time);
}
}
else {
if(str[0].equals("table")){
tnum = false;
tablefomat = false;
System.out.println("wrong format");
}
if (tablefomat) {
System.out.println("wrong format");
}
}
s = sc.nextLine();
}
order.tables = order.getTotalPrice();
for (Table table : order.tables) {
String[] strings = table.tablenum.split(" ");
if(Integer.parseInt(strings[1])>=1&&Integer.parseInt(strings[1])<=55) {
if (table.primprice != -1) {
System.out.println(strings[0] +" " +strings[1]+ ": " + table.primprice + " " + table.lastprice);
}
}
}
}
private static boolean isTimeInRange(String time){
String[] strings = time.split("/");
int year = Integer.parseInt(strings[0]);
int month = Integer.parseInt(strings[1]);
int day = Integer.parseInt(strings[2]);
LocalDate localDate1 = LocalDate.of(2022,1,1);
LocalDate localDate2 = LocalDate.of(2023,12,31);
LocalDate localDate3 = LocalDate.of(year,month,day);
if(localDate3.isBefore(localDate1)||localDate3.isAfter(localDate2)) {
return false;
}
return true;
}


}
class Dish {
String name;//菜品名称
int unit_price; //单价
String type;
int getPrice(int portion){
double[] arr = {0,1,1.5,2};
return (int) Math.round(this.unit_price*arr[portion]);
}
}

//菜谱类:对应菜谱,包含饭店提供的所有菜的信息。
class Menu {
ArrayList<Dish> dishs = new ArrayList<>();//菜品数组,保存所有菜品信息
Dish searthDish(String dishName){
for (int i = 0; i < this.dishs.size(); i++) {
if (this.dishs.get(i).name.equals(dishName)){
return this.dishs.get(i);
}
}
return null;
}//根据菜名在菜谱中查找菜品信息,返回Dish对象。
int searthDishIndix(String dishName){
for (int i = 0; i < this.dishs.size(); i++) {
if (this.dishs.get(i).name.equals(dishName)){
return i;
}
}
return -1;
}//根据菜名在菜谱中查找菜品信息,返回Dish对象。
Dish addDish(String dishName,int unit_price,String type){
Dish dish = new Dish();
dish.name = dishName;
dish.unit_price = unit_price;
dish.type = type;
if(this.searthDish(dishName)==null) {
dishs.add(dish);
}
else {
int flag = this.searthDishIndix(dishName);
dishs.set(flag,dish);
}
return dish;
}
}
// 点菜记录类:保存订单上的一道菜品记录
class Record {
String time;
int orderNum;
Dish d;//菜品
int portion;//份额(1/2/3代表小/中/大份)
int num;
int getPrice(){
return d.getPrice(this.portion)*this.num;
}//计价,计算本条记录的价格
}

// 订单类:保存用户点的所有菜的信息。
class Order {
double discount1;
double discount2;
int countNum = -1;
ArrayList<Record> records=new ArrayList<>();//保存订单上每一道的记录
ArrayList<Record> repeaatrecords=new ArrayList<>();//保存订单上每一道的记录
ArrayList<Table> tables = new ArrayList<>();
ArrayList<Table> getTotalPrice(){
tables = caculatelastprice(tables);
for (int i = 0; i < tables.size(); i++) {
int price = 0;
int price1 = 0;
int price2 = 0;
int price3 = 0;
boolean flagg = whetherTimeRight(tables.get(i).tablenum);
for (Record record : records){
if(isTheSameTime(record.time,tables.get(i).tablenum)){
price += record.getPrice();
if(record.d.type.equals("T")){
price2+=record.getPrice();
}
else {
price3+=record.getPrice();
}
}
}
price1 = (int) Math.round(price2 * this.discount2) + (int) Math.round(price3 * this.discount1);
Table table = new Table(tables.get(i).tablenum,price,price1);
tables.set(i,table);
}
return this.tables;
}
//添加一条菜品信息到订单中。
void addARecord(int orderNum,String dishName,int portion,int num,Menu menu,String time,int flag){
String[] strings = time.split(" ");
boolean ordernumisright = true;
if(flag==0) {
if (this.countNum != -1) {
if (records.get(this.countNum).time.equals(time)) {
if (records.get(this.countNum).orderNum >= orderNum) {
System.out.println("record serial number sequence error");
ordernumisright = false;
}
}
}
}
if(ordernumisright) {
Record record = new Record();
Dish dish = menu.searthDish(dishName);
record.orderNum = orderNum;
record.d = dish;
record.portion = portion;
record.num = num;
record.time = time;
if (portion != 1 && portion != 2 && portion != 3) {
System.out.println(orderNum + " portion out of range " + portion);
}
else if (num > 15) {
System.out.println(orderNum + " num out of range " + num);
}
else {
records.add(record);
this.countNum++;
if (flag != 0) {
System.out.println(record.orderNum + " " + strings[0] + " " + strings[1] + " pay for table " + flag + " " + record.getPrice());
} else {
System.out.println(record.orderNum + " " + record.d.name + " " + record.getPrice());
}
}
}
}
//根据序号删除一条记录
void delARecordByOrderNum(int orderNum,String time){
for (int i = 0; i < records.size(); i++) {
if(records.get(i).time.equals(time)) {
if (records.get(i).orderNum == orderNum) {
Record record = records.remove(i);
this.countNum--;
repeaatrecords.add(record);
return;
}
}
}
if(ifRepeatDelete(orderNum,time)){
System.out.println("deduplication " + orderNum);
}
else {
System.out.println("delete error;");
}
}
boolean ifRepeatDelete(int orderNum,String time){
for (Record repeaatrecord : repeaatrecords) {
if(repeaatrecord.orderNum==orderNum&&repeaatrecord.time.equals(time)){
return true;
}
}
return false;
}

boolean isTableNumisExist(int num){
for (Table table : tables) {
String[] strings = table.tablenum.split(" ");
if(Integer.parseInt(strings[1])==num){
return true;
}
}
return false;
}
boolean whetherTimeRight(String time){
String[] str = time.split(" ");
Date time1 = new Date(str[2]);
Calendar cal = Calendar.getInstance();
cal.setTime(time1);
int[] arr = {7,1,2,3,4,5,6};
int week = arr[cal.get(Calendar.DAY_OF_WEEK)-1];

String time2 = str[3];
String[] str1 = time2.split("/");
int hour = Integer.parseInt(str1[0]);
int min = Integer.parseInt(str1[1]);
if(week<6){
if(hour<10||(hour>14&&hour<17)||hour>20){
return false;
}
else if((hour==10&&min<30) ||(hour==14&&min>30)||(hour==20&&min>30)){
return false;
}
else {
this.discount2 = 0.7;
if(hour<=14){
this.discount1 = 0.6;
}
else {
this.discount1 = 0.8;
}
}
}
else{
if(hour<9||hour>21){
return false;
}
else if((hour==9&&min<30) ||(hour==21&&min>30)){
return false;
}
this.discount1 =1;
this.discount2 =1;
}
return true;
}

ArrayList<Table> caculatelastprice(ArrayList<Table> tables){
if(tables.size()>=2) {
for (int i = 0; i < tables.size() - 1; i++) {
for (int j = i + 1; j < tables.size(); j++) {
if (isTheSameTime(tables.get(i).tablenum, tables.get(j).tablenum)) {
tables.remove(j);
}
}
}
}
return tables;
}
boolean isTheSameTime(String time1,String time2){
String[] strings1 = time1.split(" ");
String[] strings2 = time2.split(" ");
if(strings1[1].equals(strings2[1]) &&strings1[2].equals(strings2[2]))
{
String[] str1 = strings1[3].split("/");
String[] str2 = strings2[3].split("/");
int hour1 = Integer.parseInt(str1[0]);
int min1 = Integer.parseInt(str1[1]);
int second1 = Integer.parseInt(str1[2]);
int hour2 = Integer.parseInt(str2[0]);
int min2 = Integer.parseInt(str2[1]);
int second2 = Integer.parseInt(str2[2]);
if(weekday(time1)<6){
if((hour1<=14 && hour2<=14) || (hour1>=17 && hour2>=17)){
return true;
}
}
else {
long sencond = 0;
if(hour1 > hour2){
sencond = (hour1 - hour2) * 3600 + min1 * 60 + second1 - min2 * 60 -second2;
}
else if(hour1 == hour2){
return true;
}

else if(hour1 < hour2){
sencond = (hour2 - hour1) * 3600 + min2 * 60 + second2 - min1 * 60 -second1;
}
if(sencond < 3600){
return true;
}
}
}
return false;
}

int weekday(String time){
String[] str = time.split(" ");
Date time1 = new Date(str[2]);
Calendar cal = Calendar.getInstance();
cal.setTime(time1);
int[] arr = {7,1,2,3,4,5,6};
int week = arr[cal.get(Calendar.DAY_OF_WEEK)-1];
return week;
}
}

class Table{
String tablenum;
int primprice;
int lastprice;

public Table() {
}

public Table(String tablenum, int primprice, int lastprice) {
this.tablenum = tablenum;
this.primprice = primprice;
this.lastprice = lastprice;
}
}

4、菜单计价程序5

 题目要求:在菜单计价程序3的基础上,添加以下要求:
 

1、菜单输入时增加特色菜,特色菜的输入格式:菜品名+英文空格+口味类型+英文空格+基础价格+"T"

例如:麻婆豆腐 川菜 9 T

菜价的计算方法:

周一至周五 7折, 周末全价。

特色菜的口味类型:川菜、晋菜、浙菜

川菜增加辣度值:辣度0-5级;对应辣度水平为:不辣、微辣、稍辣、辣、很辣、爆辣;

晋菜增加酸度值,酸度0-4级;对应酸度水平为:不酸、微酸、稍酸、酸、很酸;

浙菜增加甜度值,甜度0-3级;对应酸度水平为:不甜、微甜、稍甜、甜;    

例如:麻婆豆腐 川菜 9 T

输入订单记录时如果是特色菜,添加口味度(辣/酸/甜度)值,格式为:序号+英文空格+菜名+英文空格+口味度值+英文空格+份额+英文空格+份数

例如:1 麻婆豆腐 4 1 9

单条信息在处理时,如果口味度超过正常范围,输出"spicy/acidity/sweetness num out of range : "+口味度值,spicy/acidity/sweetness(辣度/酸度/甜度)根据菜品类型择一输出,例如:

acidity num out of range : 5

输出一桌的信息时,按辣、酸、甜度的顺序依次输出本桌菜各种口味的口味度水平,如果没有某个类型的菜,对应的口味(辣/酸/甜)度不输出,只输出已点的菜的口味度。口味度水平由口味度平均值确定,口味度平均值只综合对应口味菜系的菜计算,不做所有菜的平均。比如,某桌菜点了3份川菜,辣度分别是1、3、5;还有4份晋菜,酸度分别是,1、1、2、2,辣度平均值为3、酸度平均值四舍五入为2,甜度没有,不输出。

一桌信息的输出格式:table+英文空格+桌号+:+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价+英文空格+"川菜"+数量+辣度+英文空格+"晋菜"+数量+酸度+英文空格+"浙菜"+数量+甜度。

如果整桌菜没有特色菜,则只输出table的基本信息,格式如下,注意最后加一个英文空格:

table+英文空格+桌号+:+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价+英文空格

2、考虑客户订多桌菜的情况,输入时桌号时,增加用户的信息:

格式:table+英文空格+桌号+英文空格+":"+英文空格+客户姓名+英文空格+手机号+日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)

例如:table 1 : tom 13670008181 2023/5/1 21/30/00

约束条件:客户姓名不超过10个字符,手机号11位,前三位必须是180、181、189、133、135、136其中之一。

输出结果时,先按要求输出每一桌的信息,最后按字母顺序依次输出每位客户需要支付的金额。不考虑各桌时间段的问题,同一个客户的所有table金额都要累加。

import java.util.*;

class MenuItem {
String name;
double price;

public MenuItem(String name, double price) {
this.name = name;
this.price = price;
}
}

class OrderItem {
int number;
String name;
String size;
int quantity;

public OrderItem(int number, String name, String size, int quantity) {
this.number = number;
this.name = name;
this.size = size;
this.quantity = quantity;
}
}

class TableOrder {
int tableNum;
String time;
ArrayList<OrderItem> orders;

public TableOrder(int tableNum, String time) {
this.tableNum = tableNum;
this.time = time;
this.orders = new ArrayList<>();
}

public void addOrder(OrderItem order) {
orders.add(order);
}

public void removeOrder(int orderNumber) {
orders.removeIf(item -> item.number == orderNumber);
}

public double calculateTotalPrice(ArrayList<MenuItem> menu) {
// 计算总价格
double totalPrice = 0;
for (OrderItem item : orders) {
for (MenuItem menuItem : menu) {
if (menuItem.name.equals(item.name)) {
double itemPrice = menuItem.price;
if (item.size.equals("2")) {
itemPrice *= 1.5;
} else if (item.size.equals("3")) {
itemPrice *= 2.0;
}
totalPrice += itemPrice * item.quantity;
}
}
}

// 根据时间计算折扣
// ...

return totalPrice;
}
}

public class MenuPricingProgram {
public static void main(String[] args) {
ArrayList<MenuItem> menu = new ArrayList<>();
ArrayList<TableOrder> tableOrders = new ArrayList<>();
Scanner scanner = new Scanner(System.in);

// 读取菜单信息
// ...

// 读取订单信息
// ...

// 计算每桌总价
for (TableOrder order : tableOrders) {
double totalPrice = order.calculateTotalPrice(menu);
System.out.println("桌号 " + order.tableNum + " 的总价格为:" + Math.round(totalPrice));
}

scanner.close();
}
}

总结;

处理多桌点菜的情况。它包括了菜品和订单的类,以及一些基本的属性和构造函数。但是,这只是一个起点,还需要添加更多的功能和逻辑来完善整个程序。

在实际编码中,您可能需要考虑以下几点:

  1. 用户输入和验证:需要添加用户输入和验证逻辑,确保输入的桌号、菜品信息、口味度等都是合法的。
  2. 计算价格和折扣:需要添加计算价格和折扣的逻辑,根据菜品的不同份额价格、特色菜折扣等进行计算。
  3. 口味度处理:需要根据菜品类型和口味度值进行口味度的处理和输出。
  4. 输出结果:需要按照要求输出每一桌的信息,最后按字母顺序依次输出每位客户需要支付的金额。

另外,还需要考虑异常处理、边界条件、测试等方面。由于这是一个相当复杂的程序,我建议您在编码时逐步实现和测试各个功能,确保程序的正确性和健壮性。

7-1 点与线(类设计)

    设计一个类表示平面直角坐标系上的点Point,私有属性分别为横坐标x与纵坐标y,数据类型均为实型数,除构造方法以及属性的getter与setter方法外,定义一个用于显示信息的方法display(),用来输出该坐标点的坐标信息,格式如下:(x,y),数值保留两位小数。为简化题目,其中,坐标点的取值范围设定为(0,200]。若输入有误,系统则直接输出Wrong Format

    设计一个类表示平面直角坐标系上的线Line,私有属性除了标识线段两端的点point1、point2外,还有一个字符串类型的color,用于表示该线段的颜色,同样,除构造方法以及属性的getter与setter方法外,定义一个用于计算该线段长度的方法getDistance(),还有一个用于显示信息的方法display(),用来输出线段的相关信息,输出格式如下:
       The line's color is:颜色值
       The line's begin point's Coordinate is:
       (x1,y1)
       The line's end point's Coordinate is:
       (x2,y2)
       The line's length is:长度值
  其中,所有数值均保留两位小数,建议可用String.format("%.2f", data)方法。


** 题目要求:在主方法中定义一条线段对象,从键盘输入该线段的起点坐标与终点坐标以及颜色,然后调用该线段的display()方法进行输出。**

以下情况为无效作业
    无法运行
    设计不符合所给类图要求
    未通过任何测试点测试
    判定为抄袭

输入格式:

分别输入线段的起点横坐标、纵坐标、终点的横坐标、纵坐标以及颜色,中间可用一个或多个空格、tab或者回车分隔。

输出格式:

The line's color is:颜色值
The line's begin point's Coordinate is:
(x1,y1)
The line's end point's Coordinate is:
(x2,y2)
The line's length is:长度值

代码如下


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double []num =new double[5];
        for(int i=1;i<=4;i++){
            num[i] = input.nextDouble();
            if(num[i]>200||num[i]<=0){
                System.out.printf("Wrong Format\n");
                return;
            }
        }
        String  color = input.next();
        Point point1 = new Point(num[1],num[2]),point2 = new Point(num[3],num[4]);
        Line line = new Line(point1,point2,color);
        line.display();

    }


}
class Point {
    private double x,y;
    public Point(){

    }
    public Point(double x, double y){
        setX(x);setY(y);
    }
    void setX(double x){
        this.x =x;
    }
    void setY(double y){
        this.y = y;
    }
    double getX(){
        return this.x;
    }
    double getY(){
        return this.y;
    }
    void display(){
        double numx,numy;
        String str1 = String.format("%.3f",getX()),str2 = String.format("%.3f",getY());
        numx = Double.parseDouble(str1);numy = Double.parseDouble(str2);

        System.out.printf("(%.2f,%.2f)",numx,numy);
    }
}
class Line {
    Point point1,point2;
    String color;
    public Line(){}
    public Line(Point point1, Point point2,String color){
        setColor(color);setPoint1(point1) ;setPoint2(point2);
    }
    Point getPoint1(){return point1;}
    Point getPoint2(){return point2;}
    String getColor(){return color;}
    void setPoint1(Point point1){
        this.point1 = new Point(point1.getX(),point1.getY());
    }
    void setPoint2(Point point2){
        this.point2 = new Point(point2.getX(),point2.getY());
    }
    void  setColor(String color){
        this.color = color;
    }
    double getDistance(){
        return Math.sqrt((point1.getX()- point2.getX())*(point1.getX()- point2.getX())+(point1.getY()- point2.getY())*(point1.getY()- point2.getY()));
    }
    void display(){
        System.out.printf("The line's color is:" +getColor()+
                "\nThe line's begin point's Coordinate is:\n" );
        point1.display();
        System.out.printf("\n"+"The line's end point's Coordinate is:\n" );
        point2.display() ;
        System.out.printf("\n"+"The line's length is:" );
        double num;
        String str1 = String.format("%.3f",getDistance());
        num= Double.parseDouble(str1);
        System.out.printf("%.2f",num);
    }



}

7-2 点线面问题重构(继承与多态)

在“点与线(类设计)”题目基础上,对题目的类设计进行重构,以实现继承与多态的技术性需求。

对题目中的点Point类和线Line类进行进一步抽象,定义一个两个类的共同父类Element(抽象类),将display()方法在该方法中进行声明(抽象方法),将Point类和Line类作为该类的子类。
再定义一个Element类的子类面Plane,该类只有一个私有属性颜色color,除了构造方法和属性的getter、setter方法外,display()方法用于输出面的颜色,输出格式如下:The Plane's color is:颜色
在主方法内,定义两个Point(线段的起点和终点)对象、一个Line对象和一个Plane对象,依次从键盘输入两个Point对象的起点、终点坐标和颜色值(Line对象和Plane对象颜色相同),然后定义一个Element类的引用,分别使用该引用调用以上四个对象的display()方法,从而实现多态特性。示例代码如下:
          element = p1;//起点Point
          element.display();
          
          element = p2;//终点Point
          element.display();
          
          element = line;//线段
          element.display();
          
          element = plane;//面
          element.display();
其中,所有数值均保留两位小数,建议可用String.format("%.2f", data)方法。

以下情况为无效作业
    无法运行
    设计不符合所给类图要求
    未通过任何测试点测试
    判定为抄袭

输入格式:

分别输入线段的起点横坐标、纵坐标、终点的横坐标、纵坐标以及颜色,中间可用一个或多个空格、tab或者回车分隔。

输出格式:

(x1,y1)
(x2,y2)
The line's color is:颜色值
The line's begin point's Coordinate is:
(x1,y1)
The line's end point's Coordinate is:
(x2,y2)
The line's length is:长度值
The Plane's color is:颜色值

代码如下



import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double []num =new double[5];
        for(int i=1;i<=4;i++){
            num[i] = input.nextDouble();
            if(num[i]>200||num[i]<=0){
                System.out.printf("Wrong Format\n");
                return;
            }
        }
        String  color = input.next();
        Element point1 = new  Point(num[1],num[2]),point2 = new Point(num[3],num[4]);
        Element line = new Line(new Point(num[1],num[2]),new Point(num[3],num[4]),color);
        Element plane = new Plane(color);
        point1.display();
        point2.display();
        line.display();
        plane.display();

    }


}
class Point extends Element{
    private double x,y;

    public Point(){

    }

    public Point(double x, double y){
        setX(x);setY(y);
    }
    void setX(double x){
        this.x =x;
    }
    void setY(double y){
        this.y = y;
    }
    double getX(){
        return this.x;
    }
    double getY(){
        return this.y;
    }
    void display(){
        double numx,numy;
        String str1 = String.format("%.3f",getX()),str2 = String.format("%.3f",getY());
        numx = Double.parseDouble(str1);numy = Double.parseDouble(str2);

        System.out.printf("(%.2f,%.2f)\n",numx,numy);
    }
}
class Line extends Element{
    Point point1,point2;
    String color;
    public Line(){}


    public Line(Point point1, Point point2,String color){
        setColor(color);setPoint1(point1) ;setPoint2(point2);
    }
    Point getPoint1(){return point1;}
    Point getPoint2(){return point2;}
    String getColor(){return color;}
    void setPoint1(Point point1){
        this.point1 = new Point(point1.getX(),point1.getY());
    }
    void setPoint2(Point point2){
        this.point2 = new Point(point2.getX(),point2.getY());
    }
    void  setColor(String color){
        this.color = color;
    }
    double getDistance(){
        return Math.sqrt((point1.getX()- point2.getX())*(point1.getX()- point2.getX())+(point1.getY()- point2.getY())*(point1.getY()- point2.getY()));
    }
    void display(){
        System.out.printf("The line's color is:" +getColor()+
                "\nThe line's begin point's Coordinate is:\n" );
        point1.display();
        System.out.printf("The line's end point's Coordinate is:\n" );
        point2.display() ;
        System.out.printf("The line's length is:" );
        double num;
        String str1 = String.format("%.3f",getDistance());
        num= Double.parseDouble(str1);
        System.out.printf("%.2f\n",num);
    }



}
abstract class  Element{


    abstract void display();
}
class Plane extends Element{
    private String color;
    public Plane(){}
    public Plane(String color){
        setColor(color);
    }
    void display(){
        System.out.printf("The Plane's color is:"+getColor()+"\n");
    };
    void setColor(String color){this.color = color;}
    String getColor(){return color;}

}

7-3 点线面问题再重构(容器类)

在“点与线(继承与多态)”题目基础上,对题目的类设计进行重构,增加容器类保存点、线、面对象,并对该容器进行相应增、删、遍历操作。

在原有类设计的基础上,增加一个GeometryObject容器类,其属性为ArrayList<Element>类型的对象(若不了解泛型,可以不使用<Element>)
增加该类的add()方法及remove(int index)方法,其功能分别为向容器中增加对象及删除第index - 1(ArrayList中index>=0)个对象
在主方法中,用户循环输入要进行的操作(choice∈[0,4]),其含义如下:
    1:向容器中增加Point对象
    2:向容器中增加Line对象
    3:向容器中增加Plane对象
    4:删除容器中第index - 1个数据,若index数据非法,则无视此操作
    0:输入结束
示例代码如下:
       choice = input.nextInt();
        while(choice != 0) {
            switch(choice) {
            case 1://insert Point object into list 
              ...
                break;
            case 2://insert Line object into list
                ...
                break;
            case 3://insert Plane object into list
                ...
                break;
            case 4://delete index - 1 object from list
                int index = input.nextInt();
                ...
            }
            choice = input.nextInt();
        }
输入结束后,按容器中的对象顺序分别调用每个对象的display()方法进行输出。


以下情况为无效作业
    无法运行
    设计不符合所给类图要求
    未通过任何测试点测试
    判定为抄袭

输入格式:

switch(choice) {
            case 1://insert Point object into list 
              输入“点”对象的x,y值
                break;
            case 2://insert Line object into list
                输入“线”对象两个端点的x,y值
                break;
            case 3://insert Plane object into list
                输入“面”对象的颜色值
                break;
            case 4://delete index - 1 object from list
                输入要删除的对象位置(从1开始)
                ...
            }

输出格式:

Point、Line、Plane的输出参考题目2
删除对象时,若输入的index超出合法范围,程序自动忽略该操作

代码如下



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

public class Main {
    static double []num =new double[15];
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args) {
        int choice = input.nextInt();
        GeometryObject geometryObject = new GeometryObject();
        String color;
        while(choice != 0) {
            switch(choice) {
                case 1:
                    getNum(2);
                    Element point = new Point(num[1],num[2]);
                    geometryObject.add(point);
                    break;
                case 2://insert Line object into list
                    getNum(4);
                    color = input.next();
                    Element line = new Line(new Point(num[1],num[2]),new Point(num[3],num[4] ),color);
                    geometryObject.add(line);
                    break;
                case 3://insert Plane object into list

                    color = input.next();
                    Element plane = new Plane(color);
                    geometryObject.add(plane);
                    break;
                case 4://delete index - 1 object from list
                    int index = input.nextInt();
                    geometryObject.remove(index);
                    break;
            }

            //System.out.printf(choice+"\n");
            choice = input.nextInt();

        }
        geometryObject.display();
    }
    static void getNum(int x) {
        for (int i = 1; i <= x; i++) {
            num[i] = input.nextDouble();
        }
    }


}
class Point extends Element{
    private double x,y;

    public Point(){

    }

    public Point(double x, double y){
        setX(x);setY(y);
    }
    void setX(double x){
        this.x =x;
    }
    void setY(double y){
        this.y = y;
    }
    double getX(){
        return this.x;
    }
    double getY(){
        return this.y;
    }
    void display(){
        double numx,numy;
        String str1 = String.format("%.3f",getX()),str2 = String.format("%.3f",getY());
        numx = Double.parseDouble(str1);numy = Double.parseDouble(str2);

        System.out.printf("(%.2f,%.2f)\n",numx,numy);
    }
}
class Line extends Element{
    Point point1,point2;
    String color;
    public Line(){}


    public Line(Point point1, Point point2,String color){
        setColor(color);setPoint1(point1) ;setPoint2(point2);
    }
    Point getPoint1(){return point1;}
    Point getPoint2(){return point2;}
    String getColor(){return color;}
    void setPoint1(Point point1){
        this.point1 = new Point(point1.getX(),point1.getY());
    }
    void setPoint2(Point point2){
        this.point2 = new Point(point2.getX(),point2.getY());
    }
    void  setColor(String color){
        this.color = color;
    }
    double getDistance(){
        return Math.sqrt((point1.getX()- point2.getX())*(point1.getX()- point2.getX())+(point1.getY()- point2.getY())*(point1.getY()- point2.getY()));
    }
    void display(){
        System.out.printf("The line's color is:" +getColor()+
                "\nThe line's begin point's Coordinate is:\n" );
        point1.display();
        System.out.printf("The line's end point's Coordinate is:\n" );
        point2.display() ;
        System.out.printf("The line's length is:" );
        double num;
        String str1 = String.format("%.3f",getDistance());
        num= Double.parseDouble(str1);
        System.out.printf("%.2f\n",num);
    }



}
abstract class  Element{


    abstract void display();
}
class Plane extends Element{
    private String color;
    public Plane(){}
    public Plane(String color){
        setColor(color);
    }
    void display(){
        System.out.printf("The Plane's color is:"+getColor()+"\n");
    };
    void setColor(String color){this.color = color;}
    String getColor(){return color;}

}
class GeometryObject{
    ArrayList<Element> elements = new ArrayList<>();
    void add(Element element){
        elements.add(element);
    }
    void remove(int x){
        if(x>elements.size())
            return;
        elements.remove(x-1);
    }
    void display(){
        for (Element ele:elements){
            ele.display();
        }
    }
}
总结

通过这个框架,我们可以定义菜品信息和订单信息,并且可以进行一些简单的操作,比如创建菜品、创建订单等。

在这个框架的基础上,我们可以进一步完善功能,比如:

  1. 添加菜单管理功能:可以将菜单信息存储在数据库或者文件中,实现菜单的增删改查功能。
  2. 实现点菜功能:可以让用户根据菜单进行点菜,并生成订单。
  3. 实现计算价格功能:可以根据订单中的菜品信息计算总价格,并考虑折扣等因素。
  4. 实现订单管理功能:可以对订单进行管理,比如查询订单、修改订单、删除订单等。

总的来说,这个框架是一个很好的起点,可以根据实际需求进一步扩展和完善功能,实现一个完整的餐厅点菜系统。