BLOG-2

(1)前言:菜单计价程序4帮助掌握了正则表达式,并且特色菜的单独计价帮助我加深了对类设计的理解。菜单计价程序5总体迭代与4类似,但是许多测试点找不出来,导致无法得分。期中考试较为简单的考察了类的设计、继承、接口以及将类图实现为代码的能力。

(2)设计与分析:菜单计价程序4代码

class Dish {
String name;
boolean isSpecial = false;
int unit_price; //单价
boolean exist = true;
//int num;

int getPrice(int portion) {
int peic = 0;
if (portion == 1) {
peic = unit_price ;
} else if (portion == 2) {
peic = Math.round((float) (unit_price * 1.5)) ;
} else if (portion == 3) {
peic = (unit_price * 2) ;
}
return peic;
}
}

class Menu {
Dish[] dishs = new Dish[10];
int count = 0;
Dish searthDish(String dishName){
Dish temd = null;
for(int i=count-1;i>=0;i--){
if(dishName.equals(dishs[i].name)&&dishs[i].exist){
temd = dishs[i];
break;
}
}
if(temd==null){
System.out.println(dishName+" does not exist");
}
return temd;
}
Dish addDish(String dishName,int unit_price){

Dish dh = new Dish();
dh.name = dishName;
dh.unit_price = unit_price;
count++;
if(unit_price<1||unit_price>300) {
dh.exist = false;
System.out.println(dishName+" price out of range "+unit_price);
}
return dh;
}
}

class Record {
int orderNum;
Dish d = new Dish();
int num = 0;
int portion;
boolean noNeed = true;
int exist = 1;
float realdiscnt = 1;
//int asum = 0;
boolean isTaked = false;
int getPrice(){
return Math.round(d.getPrice(portion)*num);
}
float Checkportion(float discnt){
if(portion>3||portion<1) {
System.out.println(orderNum + " portion out of range " + portion);
exist = 0;
noNeed = false;
}else if(num<1||num>15) {
System.out.println(orderNum + " num out of range" +" "+num);
exist = 0;
noNeed = false;
}else{
if(d.isSpecial)
if(discnt==0.8F||discnt==0.6F)
realdiscnt = 0.7F;
if(exist==1&&!isTaked) {
System.out.println(orderNum + " " + d.name + " " + getPrice());
}
}
return realdiscnt;
}
boolean Checkahead(){
if(num<1||num>15) {
return false;
}else if(portion>3||portion<1){
return false;
}
return true;
}
}

class Record {
int orderNum;
Dish d = new Dish();
int num = 0;
int portion;
boolean noNeed = true;
int exist = 1;
float realdiscnt = 1;
//int asum = 0;
boolean isTaked = false;
int getPrice(){
return Math.round(d.getPrice(portion)*num);
}
float Checkportion(float discnt){
if(portion>3||portion<1) {
System.out.println(orderNum + " portion out of range " + portion);
exist = 0;
noNeed = false;
}else if(num<1||num>15) {
System.out.println(orderNum + " num out of range" +" "+num);
exist = 0;
noNeed = false;
}else{
if(d.isSpecial)
if(discnt==0.8F||discnt==0.6F)
realdiscnt = 0.7F;
if(exist==1&&!isTaked) {
System.out.println(orderNum + " " + d.name + " " + getPrice());
}
}
return realdiscnt;
}
boolean Checkahead(){
if(num<1||num>15) {
return false;
}else if(portion>3||portion<1){
return false;
}
return true;
}
}

class Order {
Record[] records = new Record[100];
int count = 0;
int Ordercnt = 0;
int getTotalPrice(){
int sum=0;
for(int i=0;i<count;i++){
if(records[i].exist==0)
continue;
sum=sum+records[i].getPrice();
}
return sum;
}
Record addARecord(boolean istosum,int orderNum,String dishName,int portion,int num){
boolean flag = true;
for(int i=0;i<count;i++){
if(records[i].d.name.equals(dishName)&&records[i].portion==portion)
flag = false;
}
count++;
records[count] = new Record();
records[count].d.name = dishName;
records[count].orderNum = orderNum;
records[count].portion = portion;
records[count].num = num;
records[count].noNeed = flag;
return records[count];
}
Record TakeOdfor(int mynum,Table[] table, int AnotherNum, int orderNum, String dishName, int portion, int num){
boolean flag = false;
for(Table t : table) {
if(t==null)
continue;
if (t.tableNum != mynum&&t.tableNum==AnotherNum) {
flag = true;
break;
}
}
if(flag == true) {
count++;
records[count] = new Record();
records[count].d.name = dishName;
records[count].orderNum = orderNum;
records[count].portion = portion;
records[count].num = num;
return records[count];
}else{
System.out.println("Table number :" + AnotherNum + " does not exist");
return null;
}

}

void delARecordByOrderNum(int orderNum){
if(orderNum>count||orderNum<=0){
System.out.println("delete error;");
return;
}else {
if(records[orderNum-1].exist==0) {
System.out.println("deduplication "+orderNum);
return;
}
records[orderNum-1].exist = 0;
}
}
}

class Table {
int tableNum;
boolean inputIsvalid = false;
boolean istoSum = false;
String tableDtime;
int year,month,day,week,hh,mm,ss;
int sum = 0;
int primesum = 0;
Order odt = new Order();
float discnt = -1;
float rediscnt = 1;
void Gettottalprice(){
if(discnt>0){
primesum = odt.getTotalPrice();
if(primesum==0)
sum=0;
System.out.println("table " + tableNum + ": " +primesum+" "+sum);
}
}
void AheadProcess(String tableDtime){
this.tableDtime = tableDtime;
processTime();
discount();
}


void processTime(){
String[] temp = tableDtime.split(" ");
tableNum = Integer.parseInt(temp[1]);
String[] temp1 = temp[2].split("/");
String[] temp2 = temp[3].split("/");

year = Integer.parseInt(temp1[0]);
month = Integer.parseInt(temp1[1]);
day = Integer.parseInt(temp1[2]);

Calendar c = Calendar.getInstance();
c.set(year, (month-1), day);
week = c.get(Calendar.DAY_OF_WEEK);
if(week==1)
week = 7;
else
week--;
hh = Integer.parseInt(temp2[0]);
mm = Integer.parseInt(temp2[1]);
ss = Integer.parseInt(temp2[2]);

}
boolean DateisValid(){
if(year<1000)
return false;
if(month<1||month>12)
return false;
YearMonth mytime = YearMonth.of(year,month);
if(!mytime.isValidDay(day))
return false;
return true;
}
boolean Daterande(){
boolean flag = true;
if(year<2022||year>2023) {
flag = false;
}
if(flag==false){
System.out.println("not a valid time period");
inputIsvalid = false;
}
return flag;

}
void discount(){

if(week>=1&&week<=5)
{
if(hh>=17&&hh<20)
discnt=0.8F;
else if(hh==20&&mm<30)
discnt=0.8F;
else if(hh==20&&mm==30&&ss==0)
discnt=0.8F;
else if(hh>=11&&hh<=13||hh==10&&mm>=30)
discnt=0.6F;
else if(hh==14&&mm<30)
discnt=0.6F;
else if(hh==14&&mm==30&&ss==0)
discnt=0.6F;
}
else
{
if(hh>=10&&hh<=20)
discnt= 1.0F;
else if(hh==9&&mm>=30)
discnt= 1.0F;
else if(hh==21&&mm<30||hh==21&&mm==30&&ss==0)
discnt= 1.0F;
}
}
boolean CheckSameTime(Table table){
if(tableNum!=table.tableNum)
return false;
if(week==table.week&&week>=1&&week<=5){
if(discnt==table.discnt)
return true;
else
return false;
}else if(week==table.week&&week==6||week==7){
int subtime = hh*3600+mm*60+ss-table.hh*3600-table.mm*60-ss;
if(Math.abs(subtime)<3600)
return true;
else
return false;
}
return false;
}
}

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Menu mu = new Menu();
Table[] tablemes = new Table[57];
int j = 0;
int l = 0;
int k = 0;
int z = 1;
int cntTab = 0;
String[] temp;
int tempordernum = 0;
String input;
String regex = "[\\u4e00-\\u9fa5]{0,}[ ]\\d{0,}";
String regex1 = "[\\u4e00-\\u9fa5]{0,}[ ]\\d{0,}[ ][T]";
String regex2 = "\\d[ ](delete)";
String regex3 = "(table)[ ]([1-4][0-9]*|[5][0-5]*)[ ]\\d{4}[/]\\d{1,}[/]\\d{1,2}[ ]([0-1]?[0-9]|2[0-3])/([0-5][0-9])/([0-5][0-9])";
String regex4 = "([1-9]|[1-9][0-9]*)[ ][\\u4e00-\\u9fa5]{1,}[ ]\\d{1,}[ ]\\d{1,}";
String regex5 = "([1-4][0-9]*|[5][0-5]*|[1-9])[ ]\\d[ ][\\u4e00-\\u9fa5]{1,}[ ][123][ ]([1][0-5]|[1-9])";
while (true) {
int count = 0;
input = sc.nextLine();
if(input.equals("end"))
break;
if(input.equals("")) {
System.out.println("wrong format");
continue;
}
temp = input.split(" ");

count = temp.length;
/* if(input.contains("/")||temp[0].equals("table")) {
cntTab++;
tablemes[cntTab] = new Table();
}*/
if(count==2){
if(input.matches(regex2)){
if(tablemes[cntTab].inputIsvalid){
tablemes[cntTab].odt.delARecordByOrderNum(Integer.parseInt(temp[0]));

}
}else if(input.matches(regex)){
if (cntTab == 0) {
mu.dishs[j] = new Dish();
mu.dishs[j] = mu.addDish(temp[0], Integer.parseInt(temp[1]));
j++;
continue;
} else if(tablemes[cntTab].inputIsvalid){
System.out.println("invalid dish");
}

}else {
System.out.println("wrong format");
continue;
}
}else if(count==3){/
if(input.matches(regex1)) {
if(cntTab!=0) {
if(tablemes[cntTab].inputIsvalid)
System.out.println("invalid dish");
else
continue;
}

mu.dishs[j] = new Dish();
mu.dishs[j] = mu.addDish(temp[0], Integer.parseInt(temp[1]));
mu.dishs[j].isSpecial = true;
j++;
continue;
}else
System.out.println("wrong format");
}

else if(temp[0].equals("table")||input.contains("/")) {
cntTab++;
tablemes[cntTab] = new Table();
if(temp[0].equals("table")){
if(input.matches(regex3))
tablemes[cntTab].AheadProcess(input);
tablemes[cntTab].inputIsvalid=true;
if (input.matches(regex3)) {
if(tablemes[cntTab].inputIsvalid&&!tablemes[cntTab].DateisValid()){
System.out.println(tablemes[cntTab].tableNum+" date error");
tablemes[cntTab].inputIsvalid=false;
continue;
}
if(tablemes[cntTab].inputIsvalid&&!tablemes[cntTab].Daterande()){
tablemes[cntTab].inputIsvalid=false;
continue;
}
for(int i =1;i<cntTab;i++){
if(z!=cntTab){
if(tablemes[i].inputIsvalid&&tablemes[cntTab].CheckSameTime(tablemes[i])) {
tablemes[cntTab].istoSum = true;
tempordernum = 0;
}else{
l=0;
tempordernum = 0;
}
}
}
if(tablemes[cntTab].discnt>0)
System.out.println("table " + tablemes[cntTab].tableNum + ": ");
else {
System.out.println("table " + tablemes[cntTab].tableNum + " out of opening hours");
tablemes[cntTab].inputIsvalid = false;
}
continue;
} else {
tablemes[cntTab].inputIsvalid = false;
if(!temp[1].equals("")&&temp[1].charAt(0)>='1'&&temp[1].charAt(0)<='9'&&!temp[1].contains("/")){
if(Integer.parseInt(temp[1])<1||Integer.parseInt(temp[1])>55){
System.out.println(Integer.parseInt(temp[1])+" table num out of range");
continue;
}
}
}
}
System.out.println("wrong format");
tablemes[cntTab].inputIsvalid = false;
if(!temp[0].equals("table"))
tablemes[cntTab].istoSum = true;
continue;

}else if(count==4){
if(input.matches(regex4)&&(tablemes[cntTab].inputIsvalid||tablemes[cntTab].istoSum)){
Dish tem;
int aheadcntTab = cntTab-1;
boolean tem1;
if(!tablemes[cntTab].istoSum) {
tablemes[cntTab].odt.records[l] = new Record();
tablemes[cntTab].odt.records[l] = tablemes[cntTab].odt.addARecord(tablemes[cntTab].istoSum,Integer.parseInt(temp[0]), temp[1], Integer.parseInt(temp[2]), Integer.parseInt(temp[3]));
if(l-1>=0){
if(Integer.parseInt(temp[0])<=tempordernum) {
System.out.println("record serial number sequence error");
tablemes[cntTab].odt.records[l].exist=0;
}else
if(tablemes[cntTab].odt.records[l]!=null&&tablemes[cntTab].odt.records[l].noNeed)
tempordernum = tablemes[cntTab].odt.records[l].orderNum;
}else if(l==0){
if(tablemes[cntTab].odt.records[l].Checkahead())
tempordernum = tablemes[cntTab].odt.records[l].orderNum;
}
if((tem = mu.searthDish(temp[1]))!=null){
float temdiscnt;
tablemes[cntTab].odt.records[l].d = tem;
temdiscnt = tablemes[cntTab].odt.records[l].Checkportion(tablemes[cntTab].discnt);
if(temdiscnt!=1F)
tablemes[cntTab].rediscnt = temdiscnt;
if(tablemes[cntTab].odt.records[l].exist==1&&tablemes[cntTab].odt.records[l].noNeed) {
if(tablemes[cntTab].rediscnt!=1) {
tablemes[cntTab].sum += Math.round(tablemes[cntTab].odt.records[l].getPrice() * tablemes[cntTab].rediscnt);
tablemes[cntTab].rediscnt=1.0F;
}
else
tablemes[cntTab].sum += Math.round(tablemes[cntTab].odt.records[l].getPrice() * tablemes[cntTab].discnt);
}
}

if(!tablemes[cntTab].istoSum){
l++;
}
} else{
tablemes[aheadcntTab].odt.records[l] = new Record();
tablemes[aheadcntTab].odt.records[l] = tablemes[aheadcntTab].odt.addARecord(tablemes[cntTab].istoSum,Integer.parseInt(temp[0]), temp[1], Integer.parseInt(temp[2]), Integer.parseInt(temp[3]));
if(l-1>=0){
if(Integer.parseInt(temp[0])<=tempordernum) {
System.out.println("record serial number sequence error");
tablemes[aheadcntTab].odt.records[l].exist=0;
}else
if(tablemes[aheadcntTab].odt.records[l]!=null&&tablemes[aheadcntTab].odt.records[l].noNeed)
tempordernum = tablemes[aheadcntTab].odt.records[l].orderNum;
}
if((tem = mu.searthDish(temp[1]))!=null){
float temdiscnt;
tablemes[aheadcntTab].odt.records[l].d = tem;
temdiscnt = tablemes[aheadcntTab].odt.records[l].Checkportion(tablemes[aheadcntTab].discnt);//计算折扣
if(temdiscnt==0.7F)
tablemes[aheadcntTab].rediscnt = temdiscnt;
if(tablemes[aheadcntTab].odt.records[l].exist==1) {
if(tablemes[aheadcntTab].rediscnt!=1) {
tablemes[aheadcntTab].sum += Math.round(tablemes[aheadcntTab].odt.records[l].getPrice() * tablemes[aheadcntTab].rediscnt);
tablemes[aheadcntTab].rediscnt=1.0F;
}
else
tablemes[aheadcntTab].sum += Math.round(tablemes[aheadcntTab].odt.records[l].getPrice() * tablemes[cntTab].discnt)-1;
}
}

l++;
}

}else {
if(tablemes[cntTab]==null) {
System.out.println("wrong format");
continue;
}
if(tablemes[cntTab]!=null&&tablemes[cntTab].inputIsvalid)
System.out.println("wrong format");
continue;
}
}
else if(count==5&&tablemes[cntTab].inputIsvalid){
int temSum = 0;
if(input.matches(regex5)){
int AnothNum = Integer.parseInt(temp[0]);
tablemes[cntTab].odt.records[l] = tablemes[cntTab].odt.TakeOdfor(tablemes[cntTab].tableNum,tablemes,AnothNum,Integer.parseInt(temp[1]),temp[2],Integer.parseInt(temp[3]),Integer.parseInt(temp[4]));
Dish tem;
if((tem = mu.searthDish(temp[2]))!=null&&tablemes[cntTab].odt.records[l]!=null){
tablemes[cntTab].odt.records[l].isTaked = true;
float temdiscnt;
tablemes[cntTab].odt.records[l].d = tem;
temdiscnt = tablemes[cntTab].odt.records[l].Checkportion(tablemes[cntTab].discnt);
if(temdiscnt!=1F)
tablemes[cntTab].rediscnt = temdiscnt;
if(tablemes[cntTab].odt.records[l].exist==1&&tablemes[cntTab].odt.records[l].noNeed) {
if(tablemes[cntTab].rediscnt!=1) {
tablemes[cntTab].sum += Math.round((temSum=tablemes[cntTab].odt.records[l].getPrice()) * tablemes[cntTab].rediscnt);
tablemes[cntTab].rediscnt=1.0F;
}
else
tablemes[cntTab].sum += Math.round((temSum =tablemes[cntTab].odt.records[l].getPrice()) * tablemes[cntTab].discnt);
}
System.out.println(temp[1] + " table " + tablemes[cntTab].tableNum + " pay for table " + temp[0] + " " + temSum);
}
if(tablemes[cntTab].odt.records[l]!=null)
l++;
}else
System.out.println("wrong format");

}else{
if(tablemes[cntTab].inputIsvalid)
System.out.println("wrong format");
}
}
for(int i=1;i<=cntTab;i++){
if(tablemes[i].inputIsvalid&&!tablemes[i].istoSum)
tablemes[i].Gettottalprice();
}

}
}

 

 

 

 

 

 

 

 

 

菜单计价程序5代码如下:

import java.util.Calendar;
import java.util.Objects;
import java.util.Scanner;

import static java.lang.System.exit;

//特色菜
class dish {
//特色菜的属性:菜名、口味、价格
String name;
String taste;
int price;

public dish(String name, String taste, int price) {
this.name = name;
this.taste = taste;
this.price = price;
}

public dish(String name, int price) {
this.name = name;
this.price = price;
}

public String getName() {
return name;
}

public String getTaste() {
return taste;
}

public int getPrice() {
return price;
}

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

public void setTaste(String taste) {
this.taste = taste;
}

public void setPrice(int price) {
this.price = price;
}
}

//普通菜

//桌号
//table + 序号 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)
class Table {
int num;
String name;
String phone;
String date;
//时间
String time;

public Table(int num, String name, String phone, String date, String time) {
this.num = num;
this.name = name;
this.phone = phone;
this.date = date;
this.time = time;
}

public int getNum() {
return num;
}

public String getName() {
return name;
}

public String getPhone() {
return phone;
}

public String getDate() {
return date;
}

public String getTime() {
return time;
}

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

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

public void setPhone(String phone) {
this.phone = phone;
}

public void setDate(String date) {
this.date = date;
}

public void setTime(String time) {
this.time = time;
}


}

class User {
int num;
String name;
String phone;
int price;

public User(int num, String name, String phone, int price) {
this.num = num;
this.name = name;
this.phone = phone;
this.price = price;
}


public String getName() {
return name;
}

public String getPhone() {
return phone;
}

public int getPrice() {
return price;
}

public int getNum() {
return num;
}

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

public void setPhone(String phone) {
this.phone = phone;
}

public void setPrice(int price) {
this.price = price;
}

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

}

//点菜记录格式:序号+英文空格+菜名+英文空格+辣/酸/甜度值+英文空格+份额+英文空格+份数 特色菜
class recorde {
int tableNum1;
int num;
String name;
String taste;
int portion;
int count;


public recorde(int num, String name, String taste, int portion, int count) {
this.num = num;
this.name = name;
this.taste = taste;
this.portion = portion;
this.count = count;
}

//普通点菜
//序号、菜名、份额、份数
public recorde(int num, String name, int portion, int count) {
this.num = num;
this.name = name;
this.portion = portion;
this.count = count;
}

public recorde(int tableNum1, int num, String name, String taste, int portion, int count) {
this.tableNum1 = tableNum1;
this.num = num;
this.name = name;
this.taste = taste;
this.portion = portion;
this.count = count;
}


public int getTableNum1() {
return tableNum1;
}

public int getNum() {
return num;
}


public String getName() {
return name;
}

public String getTaste() {
return taste;
}

public int getPortion() {
return portion;
}

public int getCount() {
return count;
}

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

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

public void setTaste(String taste) {
this.taste = taste;
}

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

public void setCount(int count) {
this.count = count;
}

public void setTableNum1(int tableNum1) {
this.tableNum1 = tableNum1;
}

}


public class Main {
//特色菜记录数
public static int dishNum = 0;
public static dish dish[] = new dish[100];
//普通菜记录数
//桌号
public static int tableNum = 0;
public static Table table[] = new Table[100];
public static recorde recorde[][] = new recorde[100][100];
public static int Sum[] = new int[100];

public static int userNum = 0;
public static User user[] = new User[100];

public static boolean flag1=false;
public static boolean flag2=false;
public static boolean flag3=false;

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


while (true) {
String str = scanner.nextLine();
if (str.equals("end"))
break;
//菜单输入时增加特色菜,特色菜的输入格式:菜品名+英文空格+口味类型+英文空格+基础价格+"T"
//
//例如:麻婆豆腐 川菜 9 T
//普通菜的输入格式:菜品名+英文空格+基础价格
//如果格式不对,直接输出wrong format

String[] str1 = str.split(" ");
//多条记录
//重复记录
if (str1.length == 4 && str1[3].equals("T")) {
dishNum++;
String name = str1[0];
String taste = str1[1];
int price = Integer.parseInt(str1[2]);
dish[dishNum] = new dish(name, taste, price);
int num=find4(name);
if(num!=-1) dish[num] = new dish(name, taste, price);
} else if (str1.length == 2) {
String name=str1[0];
int price=Integer.parseInt(str1[1]);
dish[++dishNum] = new dish(str1[0], Integer.parseInt(str1[1]));
int num=find4(name);
if(num!=-1) dish[num] = new dish(name, price);
}
else if(str1[2].equals("T")||str1[1].equals("T")||str1[0].equals("T"))
{
flag1=true;
}
//输入格式:
//table 1 : tom 13605054400 2023/5/1 20/30/00
else if (str1[0].equals("table"))//如果是table
{
tableNum++;
if(str1.length!=7)
{
flag2=true;
continue;
}
int num = Integer.parseInt(str1[1]);
String name = str1[3];
String phone = str1[4];
String date = str1[5];
String time = str1[6];
if(!check2(name,phone)) flag2=true;
table[tableNum] = new Table(num, name, phone, date, time);
}
//点菜
//点菜记录格式:序号+英文空格+菜名+英文空格+辣/酸/甜度值+英文空格+份额+英文空格+份数
else if (str1[0].length() == 1 && str1.length == 5) {
Sum[tableNum]++;
int num = Integer.parseInt(str1[0]);
String name = str1[1];
String taste = str1[2];
int portion = Integer.parseInt(str1[3]);
int count = Integer.parseInt(str1[4]);
recorde[tableNum][Sum[tableNum]] = new recorde(num, name, taste, portion, count);
//1 醋浇羊肉 30
}
//普通点菜
//序号、菜名、份额、份数
else if (str1[0].length() == 1 && str1.length == 4) {
Sum[tableNum]++;
int num = Integer.parseInt(str1[0]);
String name = str1[1];
int portion = Integer.parseInt(str1[2]);
int count = Integer.parseInt(str1[3]);
recorde[tableNum][Sum[tableNum]] = new recorde(num, name, portion, count);
}
//代点菜1 1 醋浇羊肉 0 1 2
else if (str1[0].length() == 1&& str1.length == 6) {
Sum[tableNum]++;
int tableNum1 = Integer.parseInt(str1[0]);
int num = Integer.parseInt(str1[1]);
String name = str1[2];
String taste = str1[3];
int portion = Integer.parseInt(str1[4]);
int count = Integer.parseInt(str1[5]);
recorde[tableNum][Sum[tableNum]] = new recorde(num, tableNum1, name, taste, portion, count);
}
//序号 delete
//删除记录格式:序号 delete
//
//标识删除对应序号的那条点菜记录。
//
//如果序号不对,输出"delete error"
else if (str1[1].equals("delete")) {
int num = Integer.parseInt(str1[0]);
if (num > Sum[tableNum] || num < 1) {
System.out.println("delete error");
exit(0);
continue;
}
for (int i = num; i < Sum[tableNum]; i++) {
recorde[tableNum][i] = recorde[tableNum][i + 1];
}
Sum[tableNum]--;
}
else if(tableNum>=1)
{
if(str1[0].length()!=1) flag3=true;

if(str1[1].length()<=2) flag3=true;

String name=str1[1];
name=check1(name);
if(name==null)
{
if(str1.length!=4) flag3=true;
}
else
{
if(str1.length!=5) flag3=true;
}
}
}

//计算总价


//输出
//table 1:
//1 麻婆豆腐 24
//2 油淋生菜 14
//3 麻婆豆腐 48
//table 1: 86 62 川菜 4 稍辣
//tom 13605054400 62
//2023/5/1 20/30/00
//首先输出桌号
//辣度


int spicy[] = new int[100];
/*
川菜增加辣度值:辣度0-5级;对应辣度水平为:不辣、微辣、稍辣、辣、很辣、爆辣;

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

浙菜增加甜度值,甜度0-3级;对应酸度水平为:不甜、微甜、稍甜、甜;
*/
int[] acidity = new int[100];
int[] sweetness = new int[100];

String[] spicy1 = {"不辣", "微辣", "稍辣", "辣", "很辣", "爆辣"};
int[] spicyNum = new int[100];
int[] spicyNum1 = new int[100];

String[] acidity1 = {"不酸", "微酸", "稍酸", "酸", "很酸"};
int[] acidityNum = new int[100];
int[] acidityNum1 = new int[100];

String[] sweetness1 = {"不甜", "微甜", "稍甜", "甜"};
int[] sweetnessNum = new int[100];
int[] sweetnessNum1 = new int[100];


/* if (check(table[1])) {
System.out.println("table 1 out of opening hours");
exit(0);
}*/

//System.out.println("table 1:"+table[1].getDate()+" "+table[1].getTime());

boolean flag=false;

if(flag1)
{
flag=true;
System.out.print("wrong format");
}
if(flag2)
{
flag=true;
if(flag1) System.out.println();
System.out.print("wrong format");
}
if(flag3)
{
flag=true;
if(flag1||flag2) System.out.println();
System.out.print("wrong format");
}

if(flag) exit(0);


for(int i=1;i<=tableNum;i++)
{
if(!check(table[i]))
{
System.out.println("table "+i+" out of opening hours");
exit(0);
}
}

//输出点菜记录
int[] price = new int[100];
int[] price1 = new int[100];

for (int i = 1; i <= tableNum; i++) {
System.out.println("table " + i + ":"+" ");
for (int j = 1; j <= Sum[i]; j++) {
//每条点菜记录输出:序号+英文空格+菜名+英文空格+价格。其中的价格等于对应记录的菜品\*份数,序号是之前输入的订单记录的序号。如果订单中包含不能识别的菜名,则输出“\*\* does not exist”,\*\*是不能识别的菜名
if(recorde[i][j].getName()!=null &&!find3(recorde[i][j].getName())){
System.out.println(recorde[i][j].getName()+" does not exist");
continue;
}

if (recorde[i][j] == null)
continue;

if (recorde[i][j].getTableNum1() != 0) {
int k = recorde[i][j].getTableNum1();
System.out.println(recorde[i][j].getNum() + " table " + i + " pay for table " + recorde[i][j].getTableNum1() + " " + (int) (find(recorde[i][j].getName()) * recorde[i][j].getCount() * prices(recorde[i][j].getPortion()) + 0.5));
if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "1")) {
spicyNum[k] += recorde[i][j].getCount();
spicyNum1[k] += recorde[i][j].getCount();
spicy[k] += (int) (Integer.parseInt(recorde[i][j].getTaste()) * recorde[i][j].getCount() + 0.5);
} else if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "2")) {
acidityNum[k] += recorde[i][j].getCount();
acidityNum1[k] += recorde[i][j].getCount();
acidity[k] += (int) (Integer.parseInt(recorde[i][j].getTaste()) * recorde[i][j].getCount() + 0.5);
} else if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "3")) {
sweetnessNum[k] += recorde[i][j].getCount();
sweetnessNum1[k] += recorde[i][j].getCount();
sweetness[k] += (int) (Integer.parseInt(recorde[i][j].getTaste()) * recorde[i][j].getCount() + 0.5);
}
int n = (int) (1.0*find(recorde[i][j].getName()) * recorde[i][j].getCount() * prices(recorde[i][j].getPortion()) + 0.5);
int n1= (int) ((int)(1.0*find(recorde[i][j].getName()) * recorde[i][j].getCount() * prices(recorde[i][j].getPortion()) + 0.5) * discount(table[i], recorde[i][j].getName()) + 0.5);;
price[i] += n;
price1[i] += n1;
continue;
}

//判断辣度是否超出范围
if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "1") && Integer.parseInt(recorde[i][j].getTaste()) > 5) {
System.out.println("spicy num out of range :" + recorde[i][j].getTaste());
continue;
}

//判断酸度是否超出范围
if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "2") && Integer.parseInt(recorde[i][j].getTaste()) > 4) {
System.out.println("acidity num out of range :" + recorde[i][j].getTaste());
continue;
}

//判断甜度是否超出范围
if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "3") && Integer.parseInt(recorde[i][j].getTaste()) > 3) {
System.out.println("sweetness num out of range :" + recorde[i][j].getTaste());
continue;
}


price[i] += (int) (1.0*find(recorde[i][j].getName()) * recorde[i][j].getCount() * prices(recorde[i][j].getPortion()) + 0.5);
price1[i] += (int) ((int)(1.0*find(recorde[i][j].getName()) * recorde[i][j].getCount() * prices(recorde[i][j].getPortion()) + 0.5) * discount(table[i],recorde[i][j].getName()) + 0.5);

System.out.println(recorde[i][j].getNum() + " " + recorde[i][j].getName() + " " + (int) (find(recorde[i][j].getName()) * recorde[i][j].getCount() * prices(recorde[i][j].getPortion()) + 0.5));

if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "1")) {
spicyNum[i] += recorde[i][j].getCount();
spicyNum1[i] += recorde[i][j].getCount();
spicy[i] += (int) (Integer.parseInt(recorde[i][j].getTaste()) * recorde[i][j].getCount() + 0.5);
} else if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "2")) {
acidityNum[i] += recorde[i][j].getCount();
acidityNum1[i] += recorde[i][j].getCount();
acidity[i] += (int) (Integer.parseInt(recorde[i][j].getTaste()) * recorde[i][j].getCount() + 0.5);
} else if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "3")) {
sweetnessNum[i] += recorde[i][j].getCount();
sweetnessNum1[i] += recorde[i][j].getCount();
sweetness[i] += (int) (Integer.parseInt(recorde[i][j].getTaste()) * recorde[i][j].getCount() + 0.5);
}
}

//判断是否有重复的用户
//如果没有重复的用户,就创建新的用户
if(!find2(table[i].name)){
user[userNum++] = new User(userNum, table[i].name, table[i].phone, price1[i]);
}else{
for(int k = 0; k < userNum; k++){
if(Objects.equals(user[k].name, table[i].name)){
user[k].price += price1[i];
break;
}
}
}
}
/*
table 1: 113 113 川菜 1 稍辣 晋菜 6 微酸 浙菜 1 甜
table 2: 168 118 川菜 4 稍辣
table 3: 103 73 川菜 1 爆辣 晋菜 1 稍酸 浙菜 3 微甜
* */

for (int i = 1; i <= tableNum; i++) {
if (spicyNum[i] != 0) spicy[i] = (int) (1.0*spicy[i] / spicyNum[i] + 0.5);
if (acidityNum[i] != 0) acidity[i] = (int) (1.0*acidity[i] / acidityNum[i] + 0.5);
if (sweetnessNum[i] != 0) sweetness[i] = (int) (1.0*sweetness[i] / sweetnessNum[i] + 0.5);
}
for (int i = 1; i <= tableNum; i++) {
System.out.print("table " + table[i].getNum() + ": ");
System.out.print(price[i] + " " + (price1[i]));
if (spicy[i] != 0) {
System.out.print(" " + "川菜" + " " + spicyNum1[i] + " " + spicy1[spicy[i]]);
}
if (acidity[i] != 0) {
System.out.print(" " + "晋菜" + " " + acidityNum1[i] + " " + acidity1[acidity[i]]);
}
if (sweetness[i] != 0) {
System.out.print(" " + "浙菜" + " " + sweetnessNum1[i] + " " + sweetness1[sweetness[i]]);
}
System.out.println();
}


//最后按name顺序输出每位客户(不考虑客户同名或拼音相同的情况)的支付金额,格式: 用户姓名+英文空格+手机号+英文空格+支付总金额,按输入顺序排列。
//排序
for (int i = 0; i < userNum; i++) {
for (int j = i + 1; j < userNum; j++) {
if (user[i].getName().compareTo(user[j].getName()) > 0) {
User temp = user[i];
user[i] = user[j];
user[j] = temp;
}
}
}

//输出
for (int i = 0; i < userNum-1; i++) {
System.out.println(user[i].getName() + " " + user[i].getPhone() + " " + user[i].getPrice());
}
System.out.print(user[userNum-1].getName() + " " + user[userNum-1].getPhone() + " " + user[userNum-1].getPrice());

}

public static String check1(String name) {
name = find1(name);

if(name==null)
return null;

if (name.equals("川菜"))
return "1";
if (name.equals("晋菜"))
return "2";
if (name.equals("浙菜"))
return "3";
return null;
}

public static boolean find3(String name)
{
for(int i =1; i <= dishNum; i++)
{
if(Objects.equals(dish[i].getName(), name))
return true;
}
return false;
}

//约束条件:客户姓名不超过10个字符,手机号11位,前三位必须是180、181、189、133、135、136其中之一。
public static boolean check2(String name, String phone) {
if (name.length() > 10)
return false;
if (phone.length() != 11)
return false;
if (phone.charAt(0) != '1')
return false;
if (phone.charAt(1) != '8' && phone.charAt(1) != '3')
return false;
if (phone.charAt(1) == '8' && phone.charAt(2) != '0' && phone.charAt(2) != '1' && phone.charAt(2) != '9')
return false;
if (phone.charAt(1) == '3' && phone.charAt(2) != '3' && phone.charAt(2) != '5' && phone.charAt(2) != '6')
return false;
return true;
}

public static boolean find2(String name) {
for (int i = 0; i < userNum; i++) {
if (user[i].getName().equals(name))
return true;
}
return false;
}

public static String find1(String name) {
for (int i = 1; i <= dishNum; i++) {
if (dish[i].getName().equals(name))
return dish[i].getTaste();
}
return null;
}

public static int find4(String name)
{
for(int i=1;i<=dishNum;i++)
{
if(dish[i].getName()==null)
return -1;
if(Objects.equals(dish[i].getName(),name))
return i;
}
return -1;
}

//桌号时间超出营业范围。例如:
public static boolean check(Table table) {
int year = Integer.parseInt(table.date.split("/")[0]);
int month = Integer.parseInt(table.date.split("/")[1]);
int day = Integer.parseInt(table.date.split("/")[2]);
int hour = Integer.parseInt(table.time.split("/")[0]);
int minute = Integer.parseInt(table.time.split("/")[1]);
int second = Integer.parseInt(table.time.split("/")[2]);
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day, hour, minute, second);
int week = calendar.get(Calendar.DAY_OF_WEEK);
if (week == 3 || week == 4) {
if(hour >=10 && hour <=20) return true;
if(hour==9&&minute>=30) return true;
return hour == 21 && minute <= 30;
} else {
if(hour >=10 && hour <=14) return true;
if(hour >=17 && hour <=19) return true;
if(hour==9&&minute>=30) return true;
return hour == 20 && minute <= 30;
}

}


//计算折扣
//折扣的计算方法(注:以下时间段均按闭区间计算):
//周一至周五营业时间与折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余时间不营业。

//周末全价,营业时间:9:30-21:30

//判断是周几,然后打几折
public static double discount(Table table,String name){

int year = Integer.parseInt(table.date.split("/")[0]);
int month = Integer.parseInt(table.date.split("/")[1]);
int day = Integer.parseInt(table.date.split("/")[2]);
int hour = Integer.parseInt(table.time.split("/")[0]);
int minute = Integer.parseInt(table.time.split("/")[1]);
int second = Integer.parseInt(table.time.split("/")[2]);

Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day, hour, minute, second);
int week = calendar.get(Calendar.DAY_OF_WEEK);

name=check1(name);
if((name!=null)&&(name.equals("1")||name.equals("2")||name.equals("3")))
{
if(week==3||week==4) return 1.0;
else return 0.7;
}
else
{
if(week==3||week==4) return 1.0;
else
{
if(hour>=17&&hour<=19) return 0.8;
if(hour==20&&minute<=30) return 0.8;
if(hour>=11&&hour<=13) return 0.6;
if(hour==10&&minute>=30) return 0.6;
if(hour==14&&minute<=30) return 0.6;
}
}

return 1.0;


}

public static double prices(int num) {
if (num == 1) return 1.0;
if (num == 2) return 1.5;
if (num == 3) return 2.0;
return 0;
}

public static int find(String name) {
for (int i = 1; i <= dishNum; i++) {
if (dish[i].getName().equals(name))
return dish[i].getPrice();
}
return 0;
}
}

期中考试第一题

import java.util.Scanner;

class Circle {
private double r;
public Circle(){

}
public double getbanjing() {
return r;
}

public void setbanjing(double r) {
this.r = r;
}
public double getmianji() {
return 3.141592654*(r*r);
}
}
public class Main{
public static void main(String[] args){
Scanner cao=new Scanner(System.in);
double r = cao.nextDouble();
Circle circle = new Circle();
circle.setbanjing(r);
// double rr=circle.getbanjing()*3.14159*circle.getbanjing();
if(r>0)
System.out.println(String.format("%.2f",circle.getmianji()));
else
System.out.println("Wrong Format");
}



}

 期中考试第二题

import java.util.Scanner;


c

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

double x1=cao.nextDouble();
double y1=cao.nextDouble();
double x2=cao.nextDouble();
double y2=cao.nextDouble();

Point b= new Point(x2,y2);
Point a= new Point(x1,y1);
Rectangle juxing = new Rectangle(a,b);

double m;
m=juxing.getmianji();
if(m>=0)
System.out.println(String.format("%.2f",juxing.getmianji()));
if(m<0)
System.out.println(String.format("%.2f",-1*juxing.getmianji()));
}
}

期中考试第三题

import java.util.Scanner;

class Shape{
double Area;
int i=1;
public Shape(){

}
public double getArea(){
return Area;
}

}

class Circle extends Shape {
private double r;
public Circle(){

}
public Circle(double r){

this.r = r;
}
public double getbanjing() {
return r;
}

public void setbanjing(double r) {
this.r = r;
}

public double getArea() {
if(r<=0)
return -1;
return 3.141592654*(r*r);
}
}

class Rectangle extends Shape{
private Point b;
private Point a;

public Rectangle(Point a,Point b)
{

this.a=a;
this.b=b;
}
public double getArea() {
return (b.getx()-a.getx())*(b.gety()-a.gety());
}

}
class Point{
private double x;
private double y;
public Point(double x,double y){
this.x=x;
this.y=y;
}
public double getx(){
return x;
}
public double gety(){
return y;
}


}

 

 

class Main{
public static void printArea(Shape a){

if(a.getArea()==-1)
System.out.println("Wrong Format");
else
System.out.println(String.format("%.2f",a.getArea()));

}


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

int choice = input.nextInt();

switch(choice) {
case 1://Circle
double radiums = input.nextDouble();
Shape circle = new Circle(radiums);
printArea(circle);
break;
case 2://Rectangle
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();

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

Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);

printArea(rectangle);
break;
}

}
}

 期中考试第四题

import java.util.Scanner;
import java.util.*;

 

class Shape implements Comparable{
double Area;
int i=1;
public Shape(){

}
public double getArea(){
return Area;
}

}

class Circle extends Shape {
private double r;
public Circle(){

}
public Circle(double r){

this.r = r;
}
public double getbanjing() {
return r;
}

public void setbanjing(double r) {
this.r = r;
}

public double getArea() {
if(r<=0)
return -1;
return 3.141592654*(r*r);
}
}

class Rectangle extends Shape{
private Point b;
private Point a;

public Rectangle(Point a,Point b)
{

this.a=a;
this.b=b;
}
public double getArea() {
return (b.getx()-a.getx())*(b.gety()-a.gety());
}

}
class Point{
private double x;
private double y;
public Point(double x,double y){
this.x=x;
this.y=y;
}
public double getx(){
return x;
}
public double gety(){
return y;
}


}

 

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()) + " ");
}
}
}

 

(3)采坑心得:菜单计价4的类设计还是略显冗杂,格式处理空格出现问题,正则表达式运用不熟练。

菜单计价5代码量太大,而且很多细节测试点没办法对应。

期中考试不难,但是我对于接口不熟练,导致没用按时完成第四题,未获得满分。

(4)改进建议:多写多练,上课不要摸鱼,不要一开电脑就打游戏,多掌一些java自带库的使用。

(5)总结:最近学习状态不好,两次作业都没有按时完成,期中考试也没有满分,望自己再接再厉。

posted @ 2023-05-17 20:42  江小皮不皮呀  阅读(73)  评论(2)    收藏  举报