首页 > 其他分享 >BLOG-2

BLOG-2

时间:2023-11-19 20:37:22浏览次数:24  
标签:tables return int System BLOG records num

作业总结

1.1 前言

  • 这几次作业主要是对菜单计价程序的完善,第四次作业中的菜单计价程序2是在菜单计价程序1的基础上进行完善,添加了输入菜单和份额的要求,难度还在可以接受的范围。第四次作业中的菜单计价程序3则是在菜单计价程序2的基础上添加了一系列的要求,包括添加桌号、代点菜等要求等,本次作业相较于在菜单计价程序2难度则是有了很大的增加。

1.2 程序设计


1.2.1 第四次作业

**7-4 菜单计价-2

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

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

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

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

订单分:点菜记录和删除信息。每一类信息都可包含一条或多条记录,每条记录一行。 点菜记录包含:序号、菜名、份额、份数。 份额可选项包括:1、2、3,分别代表小、中、大份。

删除记录格式:序号 delete

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

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

参考以下类的模板进行设计: 菜品类:对应菜谱上一道菜的信息。

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)//根据序号查找一条记录
}
输入格式:

菜品记录格式:

菜名+英文空格+基础价格

如果有多条相同的菜名的记录,菜品的基础价格以最后一条记录为准。

点菜记录格式: 序号+英文空格+菜名+英文空格+份额+英文空格+份数 注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。

删除记录格式:序号 +英文空格+delete

最后一条记录以“end”结束。

输出格式:

按顺序输出每条订单记录的处理信息,

每条点菜记录输出:序号+英文空格+菜名+英文空格+价格。其中的价格等于对应记录的菜品*份数,序号是之前输入的订单记录的序号。 如果订单中包含不能识别的菜名,则输出“** does not exist”,**是不能识别的菜名

如果删除记录的序号不存在,则输出“delete error”

最后输出订单上所有菜品的总价(整数数值),

本次题目不考虑其他错误情况,如:菜单订单顺序颠倒、不符合格式的输入、序号重复等。

代码:
import java.awt.*;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner cs = new Scanner(System.in);
String a = "";
a = cs.nextLine();
String []b= new String[10];
Order order = new Order();
Menu menu = new Menu();
int s = 0;
int i = 0;
int j = 0;
int k = 0;
int l = 0;
int g = 0;
int m = 0;

while(!a.equals("end"))
{
b = a.split(" ");
if(b.length == 2&&!b[1].equals("delete"))
{
s = Integer.parseInt(b[1]);
menu.dishs[i]= new Dish();
menu.dishs[i] = menu.addDish(b[0],s);
i++;
}

if(b.length == 4)
{
m = Integer.parseInt(b[0]);
j = Integer.parseInt(b[2]);
k = Integer.parseInt(b[3]);
order.records[g] = new Record();
order.records[g] = order.addARecord(m,b[1],j,k);
order.records[g].d = menu.searthDish(b[1]);
if(order.records[g].d == null)
{
System.out.println(b[1]+" "+"does not exist");
}
else
System.out.println(order.records[g].orderNum+" "+order.records[g].d.name+" "+order.records[g].getPrice());
g++;
}

if(b[1].equals("delete"))
{
m = Integer.parseInt(b[0]);
order.delARecordByOrderNum(m);
}
a = cs.nextLine();
}
System.out.println(order.getTotalPrice());
}
}

class Dish {
String name;//菜品名称
int unit_price; //单价
int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
{
switch (portion) {
case 1:
return unit_price;
case 2:
return (int) Math.round(unit_price * 1.5);
case 3:
return unit_price * 2;
}
return 0;
}

}


class Menu {
Dish[] dishs = new Dish[10];//菜品数组,保存所有菜品信息
int all = 0;
int i;

Dish searthDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。
{
int flag = 0;
for (i = all - 1; i >= 0; i--) {
if (dishName.equals(dishs[i].name) == true) {
flag = 1;
break;
}
}
if (flag == 1)
return dishs[i];
else
return null;
}


Dish addDish(String dishName,int unit_price)//添加一道菜品信息
{
Dish dish1 = new Dish();
dish1.name = dishName;
dish1.unit_price = unit_price;
dishs[all++] = dish1;
return dish1;
}
}

class Record {
int num;
int orderNum;//序号\
Dish d = new Dish();//菜品\
int portion;//份额(1/2/3代表小/中/大份)\

int getPrice()//计价,计算本条记录的价格\
{
return (int) Math.round(num*d.getPrice(portion));
}
}

class Order {

Record[] records = new Record[20];//保存订单上每一道的记录
int count = 0;
int all =0;
int getTotalPrice()//计算订单的总价
{
for (int i = 0; i < count ; i++) {
if(records[i].d != null)
{
all+= records[i].getPrice();
}
}
all = (int) Math.round(all);
return all;
}
Record addARecord(int orderNum,String dishName,int portion,int num)//添加一条菜品信息到订单中。
{
Record a = new Record();
a.orderNum = orderNum;
a.d.name = dishName;
a.portion = portion;
a.num = num;
count++;
return a;
}
void delARecordByOrderNum(int orderNum)//根据序号删除一条记录
{
if(orderNum<= count)
{
records[orderNum-1].num =0;
}
else
System.out.println("delete error;");

}

}

 

**7-1 菜单计价程序-3
输入格式:

桌号标识格式: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+英文空格+桌号+“:”+英文空格+当前桌的总价

本次题目不考虑其他错误情况,如:桌号、菜单订单顺序颠倒、不符合格式的输入、序号重复等,在本系列的后续作业中会做要求。

代码:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class Main {
public static void main(String[] args) throws ParseException {
Scanner input = new Scanner(System.in);
int i, t = 0, year, month, day, shi, fen, miao, f = 0, y = -1;
String a = null;
Menu menu = new Menu();
Table[] tables = new Table[10];
for (i = 0; ; i++) {
if(f==1)
break;
if (y == -1)
a = input.nextLine();
if (a.equals("end"))
break;
String[] s = a.split(" ");
int x = s.length;
if (x <= 3 && t == 0) {//菜品
int l = 0;
int n = 0;
if (!s[1].matches("[1-9]||[1-9][0-9]||[1-2][0-9][0-9]")) {
System.out.println("wrong format");
menu.dishs[menu.t] = new Dish();
} else {
n = Integer.parseInt(s[1]);
boolean special = false;
if (x == 3) {
if(s[2].matches("T"))
special = true;
else{
System.out.println("wrong format");
System.exit(0);
}
}
menu.addDish(s[0], n, special);
if (n <= 0 || n >= 300) {//菜价超出范围
System.out.println(s[0] + " price out of range " + n);
}
}
} else {

t = 1;
if(x>4){
System.out.println("wrong format");
System.exit(0);
}
while (true) {
if(f==1)
break;
y++;
if (x == 4 && !(s[0].matches("table")) && y == 0) {//第一个
System.out.println("wrong format");
System.exit(0);
}
while ((x == 4 && s[0].matches("table")) || y > 0) {
if(f==1)
break;
if (s.length == 4) {////后面的桌子直接进入点菜,后面所有的信息并入上一桌一起计算
tables[y] = new Table();
tables[y].order = new Order();
String[] s1 = s[2].split("/");//年月日的分割
String[] s2 = s[3].split("/");//时分秒的分割
SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat d2 = new SimpleDateFormat("u");
Date date = d.parse(s1[0] + "-" + s1[1] + "-" + s1[2]);//日期格式化
int week = Integer.parseInt(d2.format(date));//提取星期几
tables[y].week = week;
if (!s[1].matches("\\d*")) {//检查"table"部分
System.out.println("wrong format");
if (y == 0)//第一个桌子桌号错误直接退出
System.exit(0);
else//后面所有的信息并入上一桌一起计算
y--;
} else {
tables[y].num = Integer.parseInt(s[1]);
tables[y].year = Integer.parseInt(s1[0]);
tables[y].month = Integer.parseInt(s1[1]);
tables[y].day = Integer.parseInt(s1[2]);
tables[y].shi = Integer.parseInt(s2[0]);
tables[y].fen = Integer.parseInt(s2[1]);
tables[y].miao = Integer.parseInt(s2[2]);
if(s[1].matches("[0].+")){
System.out.println("wrong format");
System.exit(0);
}

if (!(tables[y].num <= 55 && tables[y].num >= 1)) {
System.out.println("table num out of range");
System.exit(0);
}
else if(!tables[y].check()){
System.out.println(tables[y].num+" date error");
System.exit(0);
}
else if(tables[y].year>2023||tables[y].year<2022){
System.out.println("not a valid time period");
System.exit(0);
}
else
System.out.println("table " + Integer.parseInt(s[1]) + ": ");
}
} else {
System.out.println("wrong format");
f = 0;
y--;//数据并入上一卓
}
for (; ; i++) {//点菜
if (f == 1 || f == 2)
break;
String aa = input.nextLine();
String[] ss = aa.split(" ");
//System.out.println(y + "---------" + aa);
if (ss.length == 4 && ss[0].charAt(0) == 't') {//新桌子
s = ss;
y++;
break;
}
if (ss.length == 4) {//点菜
//System.out.println(y+"%"+ss[0]);
//tables[y].order.addARecord(Integer.parseInt(ss[0]), ss[1], Integer.parseInt(ss[2]), Integer.parseInt(ss[3]), menu, tables[y].week);
if (!tables[y].checkorder(Integer.parseInt(ss[0])))//检查订单序号顺序
System.out.println("record serial number sequence error");
else if (menu.searthDish(ss[1])==null)//订单的菜不存在
System.out.println(ss[1] + " does not exist");
else if (Integer.parseInt(ss[2]) > 3) {//份额超过3
System.out.println(ss[0] + " " + "portion out of range" + " " + ss[2]);
} else if (Integer.parseInt(ss[3]) > 15) {//订单大于15
System.out.println(ss[0] + " " + "num out of range" + " " + ss[3]);
} else {
tables[y].order.addARecord(Integer.parseInt(ss[0]), ss[1], Integer.parseInt(ss[2]), Integer.parseInt(ss[3]), menu, tables[y].week);
System.out.println(ss[0] + " " + ss[1] + " " + tables[y].order.records[tables[y].order.s - 1].ygetPrice());
tables[y].order.records[tables[y].order.s - 1].shi=tables[y].shi;
tables[y].order.records[tables[y].order.s - 1].fen=tables[y].fen;

}
}
if (ss.length == 2 && ss[1].matches("delete")) {//删除
if(tables[y].order.findRecordByNum(Integer.parseInt(ss[0]))==-1) {
System.out.println("delete error");
}
else {

tables[y].order.delARecordByOrderNum(Integer.parseInt(ss[0]));
if (tables[y].order.records[tables[y].order.findRecordByNum(Integer.parseInt(ss[0]))].life == 2) //重复删除的情况
System.out.println("deduplication " + ss[0]);
}
}
if ((ss.length == 2 || ss.length == 3) && !ss[1].matches("delete")) {
System.out.println("invalid dish");
}
if (ss.length > 4) {
a = aa;
s = a.split(" ");

f = 2;

}
// System.out.println("////");
/*if(ss.length==5){//代点菜
for(int t=0;t<=y;t++){
if(ss[0].equals(tables[i].num))
}
}*/
if (aa.equals("end")) {
f = 1;
break;
}

}
if (f == 1 || f == 2)
break;
}
if (f == 1)
break;
}
}
}
for (i = 0; i <= y; i++) {
System.out.println("table " + tables[i].num + ": " + tables[i].order.ygetTotalPrice() + " " + tables[i].order.getTotalPrice());
}
}
}

class Dish {

String name;//菜品名称

int unit_price; //单价
int life=0;//
boolean special = false;
public Dish(){

}
public Dish(String name,int unit_price,boolean special){
this.name = name;
this.unit_price = unit_price;
this.special = special;
}

int getPrice(int portion){
int s = 0;
if(portion==1)
s = unit_price*1;
if (portion==2)
s = (int) Math.round(unit_price*1.5);
if(portion==3)
s = unit_price*2;
return s;
}//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
}
class Menu {

Dish[] dishs = new Dish[10];//菜品数组,保存所有菜品信息
int t=0;
public Menu(){

}
Dish searthDish(String dishName) {
for (int i = 0;i < t; i++) {
if (dishName.equals(dishs[i].name)) {
return dishs[i];
}
}
return null;
}
//根据菜名在菜谱中查找菜品信息,返回Dish对象。

Dish addDish(String dishName,int unit_price,boolean special){
if(t>0){
int k=t-1;
for (;k>=0;k--){
if (dishName.matches(dishs[k].name)){
dishs[k].unit_price = unit_price;
return null;
}
}
}
dishs[t] = new Dish(dishName,unit_price,special);
t++;
return dishs[t-1];
}//添加一道菜品信息

}
class Record {

int orderNum;//序号

Dish d;//菜品\\
int portion;//份额(1/2/3代表小/中/大份)
int num;//数量
int week;//星期几
int shi;
int fen;
int miao;

int life = 0;//初始为0,删除为1,重复删除为2,无视为3
public Record(){

}
int getPrice(){
int s=1;
if(d.special==true) {//特价菜订单价格
if (week <= 5 && week >= 1)
s = (int)Math.round(d.getPrice(portion) * num * 0.7);
else
s = (int) Math.round(d.getPrice(portion) * num);
}
else{//普通菜订单价格
if(week <= 5 && week >= 1) {
if ((shi >= 17 && shi < 20) || (shi == 20 && fen <= 30))
s = (int) Math.round(d.getPrice(portion) * num * 0.8);
else
s = (int) Math.round(d.getPrice(portion) * num * 0.6);
}
else
s = (int) Math.round(d.getPrice(portion) * num);
}
return s;
}//计价,计算本条记录的价格
int ygetPrice(){
int s=1;
s = (int) Math.round(d.getPrice(portion) * num);
return s;
}

}
class Order {

Record[] records = new Record[100];//保存订单上每一道的记录
int i=0,s=0;//s为点菜数
int year;
int month;
int day;


public Order(){

}


int getTotalPrice(){
int num = 0;
for(i=0;i<s;i++){
if(records[i].life==0)
num = num + records[i].getPrice();
}
return num;
}//计算订单的总价
int ygetTotalPrice(){
int num = 0;
for(i=0;i<s;i++){
if(records[i].life==0)
num = num + records[i].ygetPrice();
}
return num;
}//计算订单的原总价

public Record addARecord(int orderNum,String dishName,int portion,int num,Menu menu,int week){

for (i=0;i<s;i++){
if(dishName==records[i].d.name&&portion==records[i].portion) {
records[i].num = records[i].num + num;
s++;
return records[i];
}
}
if(i==s) {

records[i] = new Record();
if(menu.searthDish(dishName)!=null) {
records[i].d = menu.searthDish(dishName);
records[i].orderNum = orderNum;
records[i].d.name = dishName;
records[i].portion = portion;
records[i].num = num;
records[i].week = week;

s++;
}
else {
records[i].d = new Dish();
records[i].d.name=dishName;
records[i].life=6;//订单的菜不存在
s++;
}
return records[i];
}
return null;
}//添加一条菜品信息到订单中。

public void delARecordByOrderNum(int orderNum){
for (i=0;i<s;i++){
if(records[i].orderNum==orderNum){
if(records[i].life==1)
records[i].life = 2;
else
records[i].life = 1;
}

}

}//根据序号删除一条记录

public int findRecordByNum(int orderNum){
for (i=0;i<s;i++){
if (records[i].orderNum==orderNum)
return i;
}
return -1;
}//根据序号查找一条记录

}
class Table {
Order order;
int num;
int year;
int month;
int day;
int shi;
int fen;
int miao;
int week;
public Table(){

}
public boolean check(){
int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (month >= 1 && month <= 12) {
//判断是否为闰年
if ((year % 100 == 0 && year % 400 == 0) || year % 4 == 0) {
//判断当前月份是否为2月,因为闰年的2月份为29天
if (month == 2 && day <= 29) return true;
else {
if (day <= days[month - 1]) return true;
}
} else {
if (day <= days[month - 1])
return true;
}
}
return false;
}
boolean checkorder(int x){//检查点菜序号
for(int j=0;j<=order.s-1;j++){
if(x<=order.records[j].orderNum)
return false;
}
return true;
}

}
类图:类图-4

1.2.2 第五次作业


**7-1 菜单计价-4

这道题在菜单计价程序-3的基础上增加了特色菜的处理以及大量异常输入的处理,对于该题我发现原有的代码主体部分不足以满足要求,不好进行增加特色菜和异常处理的代码更改,于是我进行了一定的调整。首先是特色菜的处理。由于特色菜与普通菜都是属于菜品,属于菜品的数据处理工作,所以我在处理菜品的类:Dish类中增加了Boolean T来达到合并的效果,一旦处理数据为特色菜,则T为true,反之为false,这样既避免了多加一个类来处理,又方便了数据的使用。

代码:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

class Dish{
String name; //菜品名
int unit_price; //单价
boolean T; //是否为特色菜
/* 输入:份额
* 处理:根据份额的不同计算菜品的价格
*输出:价格
*/
public int getPrice(int portion){
int price=0;
double pRice;
if(portion==1)
price=unit_price;
if(portion==2){
pRice=unit_price*1.5;
price=(int)Math.round(pRice);
}
if(portion==3)
price=unit_price*2;
return price;
}
}

class Record{
Dish d; //菜品
int portion; //份额
String orderNum; //序号
int copies; //份数
boolean ifDeleted; //是否被删除
public int getprice(){
return Math.round(d.getPrice(portion)*copies);
}
}

class Menu{
Dish[] dishs=new Dish[10]; //菜品数组
/* 输入:菜品名
* 处理:判断输入的菜品名是否存在
* 输出:菜品是否存在
*/
public Dish searchDish(String dishName) {for(int i=0;dishs[i]!=null;i++){
if(dishName.equalsIgnoreCase(dishs[i].name)){
return dishs[i];
}
}
return null;
}
public void addDish(String[] str,int num){
dishs[num]= new Dish();
for(int i=0;i<num;i++){
if(str[0].equals(dishs[i].name)){
dishs[i].unit_price=Integer.parseInt(str[1]);
return;
}
}
dishs[num].name=str[0];
dishs[num].unit_price=Integer.parseInt(str[1]);
if(str.length==3)
dishs[num].T=true;
}
}

class Order{
Record[] records=new Record[10]; //点菜记录数组
/*
*输入:菜品,序号,份额,份数
*处理:添加至records数组中
*输出:
*/
public void addRecord(Dish dish,String portion,String orderNum,String copies,int numble){
records[numble]=new Record();
records[numble].d=dish; //输入菜品
records[numble].orderNum=orderNum; //输入序号
records[numble].portion=Integer.parseInt(portion); //输入份额
records[numble].copies=Integer.parseInt(copies); //输入份数
}
/*
*输入:菜品信息
*处理:将菜品的总价进行计算
*输出:输出用花费
*/
public int getTotalPrice(){
int totalPrice=0;
for(int i=0;records[i]!=null;i++)
if(!records[i].ifDeleted)
totalPrice+=records[i].getprice();
return totalPrice;
}
/*
* 输入:需要删除的菜品的序号
* 处理:在recors数组中检测是否存在此序号
* 输出:若存在则无输出,存在输出"delete error"
*/
public void delARecordByOrderNum(String ordernum,int amount){
int flag=0;
for(int i=0;i<amount;i++){
if(records[i].orderNum.equalsIgnoreCase(ordernum)){
if(records[i].ifDeleted){
System.out.println("deduplication "+ordernum);
}
records[i].ifDeleted=true;
flag=1;
}
}
if(flag==0){
System.out.println("delete error;");
}
}
}

class Table{
int number;
Order order1; //自己点的
Record[] records=new Record[10]; //代点的
Time time; //时间
int flag; //时间段标识
boolean isCount; //是否已经被计算
/*
* 输入:菜品,序号,份额,份数
* 处理:添加至records数组中
*输出:
*/
public void replaceOrder(Dish dish,String portion,String orderNum,String copies,int numble,Time time){
records[numble]=new Record();
records[numble].d=dish; //输入菜品
records[numble].orderNum=orderNum; //输入序号
records[numble].portion=Integer.parseInt(portion); //输入份额
records[numble].copies=Integer.parseInt(copies); //输入份数
// records[numble].time=time; //输入时间
}
/* 输入:
* 处理:计算打折后的金额
*输出:最终的金额
*/
public int getFinalPrice(){
double replacePrice=0;
if(records[0]!=null){
for(int i=0;records[i]!=null;i++)
replacePrice+=records[i].getprice();
}
return (int)Math.round(order1.getTotalPrice()+replacePrice);
}
public int getDiscountPrice(){
int price=0;
if(records[0]!=null){
for(int i=0;records[i]!=null;i++){
price+=Math.round(records[i].getprice()*time.getDiscount(records[i].d));
}
}
for(int i=0;order1.records[i]!=null;i++){
if(!order1.records[i].ifDeleted)
price+=Math.round(order1.records[i].getprice()* time.getDiscount(order1.records[i].d));
}
return price;
}
}

class Time{
String time;
String time1;
String time2;
int dayWeek;
double t;
public boolean information() throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH/mm/ss");
Date date = formatter.parse(time);
Date firstDay=formatter.parse("2022/01/01 00/00/00");
Date finalDay=formatter.parse("2023/12/31 23/59/59");
if(date.compareTo(firstDay)<0||date.compareTo(finalDay)>0){
System.out.println("not a valid time period");
return false;
}
Calendar c1 = Calendar.getInstance();
c1.setTime(date);
int[] week= new int[]{7, 1, 2, 3, 4, 5, 6};
dayWeek= week[c1.get(Calendar.DAY_OF_WEEK)-1];
t=c1.get(Calendar.HOUR_OF_DAY)+ c1.get(Calendar.MINUTE)*1.0/60+c1.get(Calendar.SECOND)*1.0/3600;
return true;
}
public boolean dateError(String s){
boolean result=true;
String[] str=time1.split("/");
String[] str1=time2.split("/");
int year=Integer.parseInt(str[0]);
int month=Integer.parseInt(str[1]);
int day=Integer.parseInt(str[2]);
boolean leapYear= (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
if (year <= 0 || month <= 0 || month > 12 || day <= 0 || day > 31) {
result=false;
}
if ((month == 4 || month == 6 || month == 9 || month == 11)&&day>30) {
result=false;
}
if (month == 2) {
if (leapYear && day > 29) {
result=false;
} else if (day>28) {
result=false;
}
}
if(Integer.parseInt(str1[0])>24||Integer.parseInt(str1[0])<0)
result=false;
if(Integer.parseInt(str1[1])>60||Integer.parseInt(str1[1])<0)
result=false;
if(Integer.parseInt(str1[2])>60||Integer.parseInt(str1[2])<0)
result=false;
if(!result)
System.out.println(s+" date error");
return result;
}

public double getDiscount(Dish dish){
if(1<=dayWeek&&dayWeek<=5){
if(dish.T)
return 0.7;
if(t>=10.5&&t<=14.5)
return 0.6;
else if(t>=17&&t<=20.5)
return 0.8;
}
else{
if(t>=9.5&&t<=21.5)
return 1;
}
return 0;
}
public boolean isOpening(String s){
boolean open=false;
if((1<=dayWeek&&dayWeek<=5)){
if(t>=10.5&&t<=14.5||t>=17&&t<=20.5)
open=true;
}
else{
if(t>=9.5&&t<=21.5)
open=true;
}
if(!open){
System.out.println("table "+s+" out of opening hours");
}
return open;
}
public int isTableFlag(){
if(1<=dayWeek&&dayWeek<=5){
if(t>=10.5&&t<=14.5)
return 1;
else if(t>=17&&t<=20.5)
return 2;
}
else {
if(t>=9.5&&t<=21.5)
return 3;
}
return 0;
}
}

class checkInput {
Menu menu;
String in;
String[] str;
public boolean menuInput(){ //判断菜品输入正确
if(!in.matches("\\S+ -?[0-9]+(\\\\.[0-9]+)?")&&!in.matches("\\S+ -?[0-9]+(\\\\.[0-9.]+)? T")){
System.out.println("wrong format");
return false;
}if(Integer.parseInt(str[1])>=300||Integer.parseInt(str[1])<=0){
System.out.println(str[0]+" price out of range "+str[1]);
return false;
}
return true;
}
public boolean recordInput(int num){ //判断点菜记录输入
if(menu.searchDish(str[1])==null){
System.out.println(str[1]+" does not exist"); //输出错误菜品名
return false;
}
int portion=Integer.parseInt(str[2]);
int copies=Integer.parseInt(str[3]);
if(portion>=10||str[3].charAt(0)=='0'||str[0].charAt(0)=='0'){
System.out.println("wrong format");
return false;
}
if (portion<1||portion>3) {
System.out.println(str[0]+" portion out of range "+str[2]);
return false;
}
if(copies>15||copies<=0){
System.out.println(str[0]+" num out of range "+copies);
return false;
}
return true;
}
public boolean tableInput(){ //判断桌号输入
if(!in.matches("table\\s\\d+\\s\\d+/\\d+/\\d+\\s([0-9]|[0-9][0-9]|)/([0-9]|[0-9][0-9])/([0-9]|[0-9][0-9])")||str[1].charAt(0)=='0'){
System.out.println("wrong format");
return false;
}

if(Integer.parseInt(str[1])>55||Integer.parseInt(str[1])<1){
System.out.println(str[1] +" table num out of range");
return false;
}
return true;
}
public boolean isExitTableNum(Table[] tables,int num){ //判断代点桌号是否存在
boolean exit=false;
for(int i=0;tables[i+1]!=null;i++){
if(tables[i].number==num){
return true;
}
}
System.out.println("Table number :"+num+" does not exist");
return exit;
}
public boolean isOrder(Record[] records){ //判断点菜记录的大小顺序
int i=0;
while (records[i]!=null){
i++;
}
if(i!=0&&Integer.parseInt(str[0])<=Integer.parseInt(records[i-1].orderNum)){
System.out.println("record serial number sequence error");
return false;
}
return true;
}
public int repeatTablePrice(Table[] tables,Table table,int x,int a){ //重复桌号
int price=0;
for(int i=x+1;tables[i]!=null;i++){
if(tables[i].flag==table.flag&&tables[i].flag*table.flag!=0&&tables[i].time.time1.equals(table.time.time1)){
tables[i].isCount=true;
if(a==1){
price+=tables[i].getFinalPrice();
}
if (a==2){
checkInput.isRecordExit(tables[i].order1.records,table.order1.records);
if((table.flag==1||table.flag==2)){
price+=tables[i].getDiscountPrice();
}
if(table.flag==3&&Math.abs(tables[i].time.t-table.time.t)<=1){
price+=tables[i].getDiscountPrice();
}
}

}
}
if(a==1)
return price+table.getFinalPrice();
return price+table.getDiscountPrice();
}
public static void isRecordExit(Record[] records,Record[] records1){ //records1表示原桌,record表示重复桌
for(int i=0;records1[i]!=null;i++){
for(int j=0;records[j]!=null;j++){
if(records[i].d==records1[j].d&&records[i].portion==records1[j].portion){
records1[i].copies+=records[j].copies;
records[j].copies=0;
}
}
}
}
}

public class Main {
public static void main(String[] args)
throws ParseException {
Scanner input = new Scanner(System.in);
Order[] O1 = new Order[10]; //创造自点订单
Menu menu = new Menu(); //创造菜单对象
Table[] table = new Table[10]; //创造饭桌对象
Time[] date = new Time[10]; //创造时间对象
checkInput check = new checkInput(); //创造检查输入是否合法对象
check.menu = menu;
boolean overMenu = false;
int i = 0, j = 0, l = 0, k = 0, flag = 1; //i控制基础菜单数组,j控制点菜数组,l控制桌号,k控制代点菜单
while (true) {
String in = input.nextLine(); //输入一行的信息
String[] str = in.split(" ");
check.in = in;
check.str = str;
if (in.equals(""))
System.out.println("wrong format");
else if (str[0].equals("end"))
break;
//基础菜品
else if (in.matches("[\u4e00-\u9fa5]" + ".*") && flag == 1) {
if (check.menuInput()) {
if (overMenu)
System.out.println("invalid dish");
menu.addDish(str, i);
i++;
}
}
//删除记录
if (in.matches("[1-9] delete") && flag == 1) {
O1[l - 1].delARecordByOrderNum(str[0], j);
}
//点菜
else if (in.matches("\\d+.*" + "[\u4e00-\u9fa5]" + ".*") && flag == 1) {
if (str.length == 4) {
if (check.recordInput(l) && check.isOrder(O1[l - 1].records)) {
O1[l - 1].addRecord(menu.searchDish(str[1]), str[2], str[0], str[3], j);
System.out.println(str[0] + " " + str[1] + " " + O1[l - 1].records[j].getprice());
j++;
}
}
if (str.length == 5 && check.isExitTableNum(table, Integer.parseInt(str[0]))) {
if (menu.searchDish(str[2]) != null)
System.out.println(str[1] + " table " + table[l - 1].number + " pay for table " + str[0] + " " + menu.searchDish(str[2]).getPrice(Integer.parseInt(str[3])));
for (int n = 0; n < str.length - 1; n++) {
str[n] = str[n + 1];
}
if (check.recordInput(l)) {
table[l - 1].replaceOrder(menu.searchDish(str[1]), str[2], str[0], str[3], k, date[l - 1]);
k++;
}
}
}
//饭桌
else if (in.matches("\\w{3}\\D+\\d+.*")) {
if (!str[0].equals("table")) {
System.out.println("wrong format");
continue;
}
overMenu = true;
if (!check.tableInput()) {
flag = 0;
continue;
}
date[l] = new Time();
date[l].time = str[2] + " " + str[3];
date[l].time1 = str[2];
date[l].time2 = str[3];
if (!date[l].dateError(str[1]) || !date[l].information() || !date[l].isOpening(str[1])) {
flag = 0;
continue;
} else
flag = 1;
table[l] = new Table();
O1[l] = new Order();
date[l].time1 = str[2];
table[l].number = Integer.parseInt(str[1]);
table[l].order1 = O1[l];
table[l].time = date[l];
System.out.println("table " + table[l].number + ": ");
table[l].flag = date[l].isTableFlag();
j = 0;
k = 0;
l++; //桌号加一
}
}
int a; //真实金额
int b; //折扣金额
for (int x = 0; table[x] != null; x++) {
if (!table[x].isCount) {
a = check.repeatTablePrice(table, table[x], x, 1);
b = check.repeatTablePrice(table, table[x], x, 2);
System.out.println("table " + table[x].number + ": " + a + " " + b);
}
}
}
}
类图:

 

 

1.2.3 第六次作业

7-1 菜单计价-5
增加:

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+英文空格+桌号+:+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价+英文空格

例如:table 1: 60 36 川菜 2 爆辣 浙菜 1 微甜

计算口味度时要累计本桌各类菜系所有记录的口味度总和(每条记录的口味度乘以菜的份数),再除以对应菜系菜的总份数,最后四舍五入。

注:本题要考虑代点菜的情况,当前桌点的菜要加上被其他桌代点的菜综合计算口味度平均值。

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 javax.management.ListenerNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class Main {
public static void main(String[] args)throws ParseException {
Scanner input = new Scanner(System.in);
int i, t = 0,x = -1,T = -1, year, month, day, shi, fen, miao, f = 0,y=-1;
String a = null;
Menu menu = new Menu();
Table[] tables = new Table[10];
People[] people = new People[10];
for (; ; ) {
a = input.nextLine();
if (a.equals("end"))
break;
String[] s = a.split(" ");
int l = s.length;
if (!s[0].equals("table")&&t==0) {//菜品

if(menu.t!=-1){
y = -1;
for (int j=0;j<=menu.t;j++){
if(s[0].equals(menu.dishs[j].name)) {
y = j;
break;
}
}
}
if (y!=-1){//菜谱重复
if(l==2)
menu.dishs[y].unit_price = Integer.parseInt(s[1]);
else
menu.dishs[y].unit_price = Integer.parseInt(s[2]);
}
else {
menu.t++;
menu.dishs[menu.t] = new Dish();

if (l == 2) {//普通菜
menu.dishs[menu.t].name = s[0];
menu.dishs[menu.t].unit_price = Integer.parseInt(s[1]);
} else if(l==4){//特价菜
menu.dishs[menu.t].name = s[0];
menu.dishs[menu.t].breed = s[1];
menu.dishs[menu.t].unit_price = Integer.parseInt(s[2]);
menu.dishs[menu.t].special = true;
}
else{
System.out.println("wrong format");
System.out.println("wrong format");
System.exit(0);
}
}
} else {
t = 1;//不再进入菜单
if(s[0].equals("table")) {//桌子
x++;
tables[x] = new Table();
tables[x].order = new Order();
tables[x].num = Integer.parseInt(s[1]);
tables[x].name = s[3];
if (T == -1) {
T++;
people[T] = new People();
people[T].name = s[3];
people[T].phone = s[4];
} else {
int b = 0;
for (int I = 0; I <=T; I++) {
if (s[3].equals(people[I].name))
b = 1;
}
if (b == 0) {
T++;
people[T] = new People();
people[T].name = s[3];
people[T].phone = s[4];
}
}
String[] s1 = s[5].split("/");//年月日的分割
String[] s2 = s[6].split("/");//时分秒的分割
tables[x].num = Integer.parseInt(s[1]);
tables[x].year = Integer.parseInt(s1[0]);
tables[x].month = Integer.parseInt(s1[1]);
tables[x].day = Integer.parseInt(s1[2]);
tables[x].shi = Integer.parseInt(s2[0]);
tables[x].fen = Integer.parseInt(s2[1]);
tables[x].miao = Integer.parseInt(s2[2]);
SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat d2 = new SimpleDateFormat("u");
Date date = d.parse(s1[0] + "-" + s1[1] + "-" + s1[2]);//日期格式化
int week = Integer.parseInt(d2.format(date));//提取星期几
tables[x].week = week;
}
else {
if (s[1].equals("delete")) {//删除记录
if (tables[x].order.findRecordByNum(Integer.parseInt(s[0])) != -1)
tables[x].order.records[tables[x].order.findRecordByNum(Integer.parseInt(s[0]))].life = 1;//点菜成功删除
else {
tables[x].order.i++;
i = tables[x].order.i;
tables[x].order.records[i] = new Record();
tables[x].order.records[i].d = menu.dishs[0];
tables[x].order.records[i].life = 2;//删除的点菜不存在
}
} else {

//点菜

tables[x].order.i++;
i = tables[x].order.i;
tables[x].order.records[i] = new Record();
tables[x].order.records[i].orderNum = Integer.parseInt(s[0]);
if (menu.searthDish(s[1]) != null) {
tables[x].order.records[i].d = menu.searthDish(s[1]);
if (l == 4) {//普通菜
tables[x].order.records[i].portion = Integer.parseInt(s[2]);
tables[x].order.records[i].num = Integer.parseInt(s[3]);
} else if (l == 5) {//特色菜
tables[x].order.records[i].taste = Integer.parseInt(s[2]);
tables[x].order.records[i].portion = Integer.parseInt(s[3]);
tables[x].order.records[i].num = Integer.parseInt(s[4]);
int c = tables[x].order.records[i].taste;
String b = tables[x].order.records[i].d.breed;
switch (b) {
case "川菜":
if (c > 5 || c < 0)
tables[x].order.records[i].life = 4;
break;
case "晋菜":
if (c > 4 || c < 0)
tables[x].order.records[i].life = 4;
break;
case "浙菜":
if (c > 3 || c < 0)
tables[x].order.records[i].life = 4;
break;
}
}
tables[x].order.records[i].week = tables[x].week;
tables[x].order.records[i].shi = tables[x].shi;
tables[x].order.records[i].fen = tables[x].fen;
} else {//菜品不存在
tables[x].order.records[i].life = 3;
tables[x].order.records[i].name = s[1];
}



}
}
}
}
for (int j=0;j<=x;j++){
if (!tables[j].check()) {
System.out.println("table " + tables[j].num + " out of opening hours");
System.exit(0);
}
else {
System.out.println("table " + tables[j].num + ": ");
for (int p = 0; p <= tables[j].order.i; p++) {
int choice = tables[j].order.records[p].life;
switch (choice) {
case 0:
/*String breed = tables[j].order.records[tables[j].order.i].d.breed;
switch (breed){
case
}*/
System.out.println(tables[j].order.records[p].orderNum + " " + tables[j].order.records[p].d.name + " " + tables[j].order.records[p].ygetPrice());
break;
case 1:
break;
case 2:
System.out.println("delete error;");
break;
case 3:
System.out.println(tables[j].order.records[p].name + " does not exist");
break;
case 4:
switch (tables[j].order.records[p].d.breed) {
case "川菜":
System.out.println("spicy num out of range :" + tables[j].order.records[p].taste);
break;
case "晋菜":
System.out.println("acidity num out of range :" + tables[j].order.records[p].taste);
break;
case "浙菜":
System.out.println("sweetness num out of range :" + tables[j].order.records[p].taste);
break;
}
}
}
}
}
for (int j=0;j<=x;j++){

System.out.print("table " + tables[j].num + ": " + tables[j].order.ygetTotalPrice() + " " + tables[j].order.getTotalPrice());
if (tables[j].order.gettaste1() < 0&&tables[j].order.gettaste2() < 0&&tables[j].order.gettaste3() < 0)
System.out.print(" ");
else {
if (tables[j].order.gettaste1() >= 0) {
System.out.print(" 川菜 " + tables[j].order.tastenum1());
if (tables[j].order.gettaste1() == 0)
System.out.print(" 不辣");
if (tables[j].order.gettaste1() == 1)
System.out.print(" 微辣");
if (tables[j].order.gettaste1() == 2)
System.out.print(" 稍辣");
if (tables[j].order.gettaste1() == 3)
System.out.print(" 辣");
if (tables[j].order.gettaste1() == 4)
System.out.print(" 很辣");
if (tables[j].order.gettaste1() == 5)
System.out.print(" 爆辣");
}
if (tables[j].order.gettaste2() >= 0) {
System.out.print(" 晋菜 " + tables[j].order.tastenum2());
if (tables[j].order.gettaste2() == 0)
System.out.print(" 不酸");
if (tables[j].order.gettaste2() == 1)
System.out.print(" 微酸");
if (tables[j].order.gettaste2() == 2)
System.out.print(" 稍酸");
if (tables[j].order.gettaste2() == 3)
System.out.print(" 酸");
if (tables[j].order.gettaste2() == 4)
System.out.print(" 很酸");
}
if (tables[j].order.gettaste3() >= 0) {
System.out.print(" 浙菜 " + tables[j].order.tastenum3());
if (tables[j].order.gettaste3() == 0)
System.out.print(" 不甜");
if (tables[j].order.gettaste3() == 1)
System.out.print(" 微甜");
if (tables[j].order.gettaste3() == 2)
System.out.print(" 稍甜");
if (tables[j].order.gettaste3() == 3)
System.out.print(" 甜");
}
}
System.out.println("");
}

people[T+1] = new People();
for (int j=0;j<=T;j++){//将顾客排序
for (int q=j+1;q<=T;q++){
if(people[j].name.compareTo(people[q].name)>0) {
people[T+1] = people[j];
people[j] = people[q];
people[q] = people[T+1];
}
}
}
for (int j=0;j<=T;j++){
for (int q=0;q<=x;q++){
if (tables[q].name.equals(people[j].name))
people[j].cost = people[j].cost+tables[q].order.getTotalPrice();
}
if(people[j].check()) {
System.out.println("wrong format");
System.exit(0);
}

System.out.println(people[j].name+" "+people[j].phone+" "+people[j].cost);
}
}
}

class Dish {

String name;//菜品名称

int unit_price; //单价
int life=0;//
boolean special = false;//是否为特价菜
String breed;//品种
public Dish(){

}
int getPrice(int portion){
int s = 0;
if(portion==1)
s = unit_price*1;
if (portion==2)
s = (int) Math.round(unit_price*1.5);
if(portion==3)
s = unit_price*2;
return s;
}//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
}
class Menu {
Dish[] dishs = new Dish[10];//菜品数组,保存所有菜品信息
int t=-1;
public Menu(){

}
Dish searthDish(String dishName) {
for (int i = 0;i <= t; i++) {
if (dishName.equals(dishs[i].name)) {
return dishs[i];
}
}
return null;
}
//根据菜名在菜谱中查找菜品信息,返回Dish对象。

}
class Record {

int orderNum;//序号

Dish d;//菜品
int taste = -1;//口味度
int portion;//份额(1/2/3代表小/中/大份)
int num;//数量
int week;//星期几
int shi;
int fen;
int miao;

int life = 0;//初始为0,删除为1,删除不存在为2,菜品不存在为3,特色菜味道超标为4
String name;//不存在的菜品名字
public Record(){

}
int getPrice(){
int s=1;
if(d.special==true) {//特价菜订单价格
if (week <= 5 && week >= 1)
s = (int)Math.round(d.getPrice(portion) * num * 0.7);
else
s = (int) Math.round(d.getPrice(portion) * num);
}
else{//普通菜订单价格
if(week <= 5 && week >= 1) {
if ((shi >= 17 && shi < 20) || (shi == 20 && fen <= 30))
s = (int) Math.round(d.getPrice(portion) * num * 0.8);
else
s = (int) Math.round(d.getPrice(portion) * num * 0.6);
}
else
s = (int) Math.round(d.getPrice(portion) * num);
}
return s;
}//计价,计算本条记录的价格
int ygetPrice(){
int s=1;
s = (int) Math.round(d.getPrice(portion) * num);
return s;
}

}
class Order {

Record[] records = new Record[100];//保存订单上每一道的记录
int i=-1,s=0;//s为点菜数
int year;
int month;
int day;


public Order(){

}


int getTotalPrice(){
int num = 0;
for(s=0;s<=i;s++){
if(records[s].life==0)
num = num + records[s].getPrice();
}
return num;
}//计算订单的总价
int ygetTotalPrice(){
int num = 0;
for(s=0;s<=i;s++){
if(records[s].life==0)
num = num + records[s].ygetPrice();
}
return num;
}//计算订单的原总价
int gettaste1() {
double num = 0;
int x = 0;
for (s = 0; s <= i; s++) {
if (records[s].d.special) {
if (records[s].life == 0 && records[s].d.breed.equals("川菜")) {
num = num + records[s].taste*records[s].num;
x=x+records[s].num;
}
}
}
if(x!=0)
num = (int) Math.round(num/x);
else
num = -1;
return (int)num;
}
int tastenum1(){
int x = 0;
for (s = 0; s <= i; s++) {
if (records[s].d.special) {
if (records[s].life == 0 && records[s].d.breed.equals("川菜")) {
x=x+records[s].num;
}
}
}
return x;
}
int gettaste2() {
double num = 0;
int x=0;
for (s = 0; s <= i; s++) {
if (records[s].d.special) {
if (records[s].life == 0 && records[s].d.breed.equals("晋菜")) {
num = num + records[s].taste*records[s].num;
x = x+records[s].num;
}
}
}

if(x!=0)
num = (int) Math.round(num/x);
else
num = -1;
return (int)num;
}
int tastenum2(){
int x = 0;
for (s = 0; s <= i; s++) {
if (records[s].d.special) {
if (records[s].life == 0 && records[s].d.breed.equals("晋菜")) {
x=x+records[s].num;
}
}
}
return x;
}
int gettaste3() {
double num = 0;
int x=0;
for (s = 0; s <= i; s++) {
if (records[s].d.special) {
if (records[s].life == 0 && records[s].d.breed.equals("浙菜")) {
num = num + records[s].taste*records[s].num;
x = x+records[s].num;
}
}
}
if(x!=0)
num = (int) Math.round(num/x);
else
num = -1;
return (int)num;
}
int tastenum3(){
int x = 0;
for (s = 0; s <= i; s++) {
if (records[s].d.special) {
if (records[s].life == 0 && records[s].d.breed.equals("浙菜")) {
x=x+records[s].num;
}
}
}
return x;
}



public void delARecordByOrderNum(int orderNum){
for (s=0;s<=i;s++){
if(records[s].orderNum==orderNum){
records[s].life = 1;
}
}
}//根据序号删除一条记录

public int findRecordByNum(int orderNum){
for (s=0;s<=i;s++){
if (records[s].orderNum==orderNum)
return s;
}
return -1;
}//根据序号查找一条记录

}
class Table {
Order order;
int num;
int year;
int month;
int day;
int shi;
int fen;
int miao;
int week;
int t=0;
String name;
public Table(){

}
boolean check(){
if(week<6){
if((shi>=17&&shi<=19)||(shi<14&&shi>10))
return true;
else if((shi==20||shi==14)&&fen<=30)
return true;
else if(shi==10&&fen>=30)
return true;
else
return false;
}
else {
if (shi>=10&&shi<=20)
return true;
else if (shi==21&&fen<=30)
return true;
else if(shi==9&&fen>=30)
return true;
else
return false;
}
}

}
class People{
String name;
String phone;
int cost;
public People(){

}
boolean check(){
if(!phone.matches("{11}"))
return false;
if(phone.matches("[181]||[180]||[189]||[133]||[135||[136]"))
return true;
else
return false;
}
}

部分测试点没有过:

 

1.2.4 期中考试

7-3 测验3-继承与多态
类图:

 

代码:


import java.util.Scanner;

public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner a = new Scanner(System.in);

int choice = a.nextInt();

switch(choice) {
case 1:
double r= a.nextDouble();
Circle circle = new Circle(r);
if (r <= 0) {
System.out.println("Wrong Format");
} else {
double area=circle.area(r);
System.out.println(String.format("%.2f",area));
}
break;
case 2:
double x1 = a.nextDouble();
double y1 = a.nextDouble();
double x2 = a.nextDouble();
double y2 = a.nextDouble();

Point leftTopPoint = new Point(x1,y1);
Point lowerRightPoint = new Point(x2,y2);

Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);

double area1=rectangle.getArea();
System.out.println(String.format("%.2f",area1));
break;
}

}
}

class Point {
double x;
double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}

public Point() {
}

public double getX() {
return x;
}

public void setX(double x) {
this.x = x;
}

public double getY() {
return y;
}

public void setY(double y) {
this.y = y;
}
}
class Rectangle {
Point topLeftPoint;
Point lowerRihgtPoint;
public Rectangle(Point topLeftPoint, Point lowerRihgtPoint) {
this.topLeftPoint = topLeftPoint;
this.lowerRihgtPoint = lowerRihgtPoint;
}

public Rectangle() {

}

public Point getTopLeftPoint() {
return topLeftPoint;
}

public void setTopLeftPoint(Point topLeftPoint) {
this.topLeftPoint = topLeftPoint;
}

public Point getLowerRihgtPoint() {
return lowerRihgtPoint;
}

public void setLowerRihgtPoint(Point lowerRihgtPoint) {
this.lowerRihgtPoint = lowerRihgtPoint;
}

public double getLenght() {
return Math.abs(topLeftPoint.getX() - lowerRihgtPoint.getX());
}

public double getHeight() {
return Math.abs(topLeftPoint.getY() - lowerRihgtPoint.getY());
}

public double getArea() {
return getHeight() * getLenght();
}

}
class Circle {
private double b;

public Circle() {
}

public Circle(double b) {
this.b = b;
}

public double area(double b) {
return Math.PI * Math.pow(b, 2);
}
}
7-4 测验4-抽象类与接口
代码:


import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
ArrayList<Shape> list = new ArrayList<>();

int choice = input.nextInt();

while(choice != 0) {
switch(choice) {
case 1://Circle
double radiums = input.nextDouble();
Shape circle = new Circle(radiums);
list.add(circle);
break;
case 2://Rectangle
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();
Point leftTopPoint = new Point(x1,y1);
Point lowerRightPoint = new Point(x2,y2);
Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);
list.add(rectangle);
break;
}
choice = input.nextInt();
}

list.sort(Comparator.naturalOrder()); // 使用默认的升序排序

for(int i = 0; i < list.size(); i++) {
System.out.print(String.format("%.2f", list.get(i).getArea()) + " ");
}
}
}

interface Shape extends Comparable<Shape> {
double getArea();
}

class Circle implements Shape {
private double a;

public Circle() {
}

public Circle(double a) {
this.a = a;
}

@Override
public double getArea() {
return Math.PI * Math.pow(a, 2);
}

@Override
public int compareTo(Shape other) {
return Double.compare(this.getArea(), other.getArea());
}
}


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

}


public Point(double x, double y) {
this.x = x;
this.y = y;
}

public double getX() {
return x;
}

public double getY() {
return y;
}
}
class Rectangle implements Shape{
Point leftTopPoint;
Point lowerRightPoint;
public Rectangle() {

}
public Rectangle(Point leftTopPoint,Point lowerRightPoint) {
this.leftTopPoint=leftTopPoint;
this.lowerRightPoint=lowerRightPoint;
}


@Override
public double getArea() {
double width = Math.abs(lowerRightPoint.getX() - leftTopPoint.getX());
double height = Math.abs(lowerRightPoint.getY() - leftTopPoint.getY());
double area = width * height;
return area;
}

@Override
public int compareTo(Shape other) {
return Double.compare(this.getArea(), other.getArea());
}

}

 

1.3 总结


这几次题目集最麻烦的就是菜单计价程序-4,题目本身并不需要用什么新方法,但是题目要求的东西非常多,错误类型多种多样,测试点有45个之多,开始写代码前要把各种错误情况和测试点的提示都看一遍。我就是没有弄明白就开写导致三次大规模重写。像把输出放最后;像写到后面桌子不知道怎么处理只能推倒前面的又创建新的类。复杂的程序一定要先把结构和功能想好再动手

类的设计可能并没有做到最合理,更加合理的类设计可能可以大大的简化代码量;对各种异常的处理比较分散,如何能够做到对所有的异常类进行统一处理可能不仅能简化代码,还能通过原来没通过的测试点。对方法的设计不够完美,每当添加了新的要求时,都不可避免的会对方法的变量、内容进行修改,如果能进行更好的方法设计,或许就能解决这些问题。

 

 

 

 

标签:tables,return,int,System,BLOG,records,num
From: https://www.cnblogs.com/Nova-Spark/p/17842561.html

相关文章

  • 南昌航空大学JAVA Blog-2题目4-6期中考试
    一.前言 在进行题目集4-6的练习时,老师课堂上讲的内容一般都会围绕在这次题目集需要用到的新内容上。对于题目集代码的完成有很大的帮助,如课堂讲的封装、继承、多态等内容,简化了代码修改的难度,正则表达式则在一定程度上减少了代码量。但是就我个人认为,这几次的题目集除了期中考试......
  • BLOG-2
    一.前言  这次是对之前发布的PTA题目集4、5、6以及期中考试的总结。题目集4有两个题目,是菜单计价程序-2和菜单计价程序-3,难度较后面的题目来说相对简单,但是我当时还是觉得不简单。题目集5、6都只有一个题目,分别是菜单计价程序-4和菜单计价程序-5,是菜单计价程序的最后两个题目......
  • BLOG-2
    前言:这是本学期第二次博客作业,是对pta题目集4,5,6以及期中考试的总结。第四次作业难度正常,熟练掌握知识点应该就能完成。之后的第五次和第六次难度都非常高,需要花费很多时间。第四次作业主要考察了知识点:类的应用,正则表达式,面向对象思想;第五次作业主要考察了知识点:类的应用,面向对象......
  • BLOG-2
    (1)前言:题目集4:知识点:类与对象、字符串方法调用、边界值判断题量:中难度:难题集5:知识点:类与对象、正则表达式题量:大难度:难题目集6:知识点:类与对象、、边界值判断、正则表达式题量:大难度:难期中考试:知识点:类设计、继承与多态题量:中难度:中(2)设计与分析:题集4:主要题目为菜......
  • BLOG-2
    7-1菜单计价程序-3分数40全屏浏览题目切换布局作者 蔡轲单位 南昌航空大学设计点菜计价程序,根据输入的信息,计算并输出总价格。输入内容按先后顺序包括两部分:菜单、订单,最后以"end"结束。菜单由一条或多条菜品记录组成,每条记录一行每条菜品记录......
  • 南昌航空大学BLOG-2Java总结
    题目列表   前言、Java基础作业总结在Java基础作业中,我们学习了Java的基本语法、数据类型、运算符、流程控制等内容。通过作业的练习,我对Java的基础知识有了更深入的理解。在这次作业中,我发现了自己在一些基础知识上的不足,比如对于数据类型的理解不够深刻,对于流程控制的......
  • 第二次blog
    作业总结1.1前言这几次作业主要是对菜单计价程序的完善,第四次作业中的菜单计价程序2是在菜单计价程序1的基础上进行完善,添加了输入菜单和份额的要求,难度还在可以接受的范围。第四次作业中的菜单计价程序3则是在菜单计价程序2的基础上添加了一系列的要求,包括添加桌号、代点菜......
  • PTA题目集4、5、6以及期中考试的总结性Blog
    一.前言    大三上学期开始,我们开始接触java这门语言,Java具有大部分编程语言所共有的一些特征,被特意设计用于互联网的分布式环境。Java具有类似于C++语言的形式和感觉,但它要比C++语言更易于使用,而且在编程时彻底采用了一种以对象为导向的方式。    pta已经写了六......
  • BLOG2
    一、前言:这三次大作业难度都很大,且逐渐递增!!!期中考试难度也挺大的,选择题几乎都不太会,编程题倒是好一些给了类图,但是对于继承关系和对于接口的使用还是不太熟练,琢磨了很久。二、设计与分析:7-4菜单计价程序-2设计点菜计价程序,根据输入的信息,计算并输出总价格。输入内容按先后顺......
  • 第二次blog-对菜单系统和期中考试的总结
     一、前言这三次菜单的更迭,基本每次都是在前一次的基础上增加部分功能,总体改动不是特别大,越到后期菜单系统越完善时功能修改的速度也更快。主要问题在于一开始的框架没有建好,输入信息后对信息的相关处理没有采取一个清晰地任务分割,而是堆砌在了主函数中,大量ifelse语句增......