首页 > 其他分享 >blog2

blog2

时间:2023-11-19 22:33:16浏览次数:20  
标签:lineArray int blog2 && parseInt public out

一、题目:

7-1 菜单计价程序-4

本体大部分内容与菜单计价程序-3相同,增加的部分用加粗文字进行了标注。

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

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

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

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

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

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

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

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

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

删除记录格式:序号 delete

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

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

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

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

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

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

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

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

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

如果下单时间不在营业范围内,输出"table " + t.tableNum + " out of opening hours"

参考以下类的模板进行设计(本内容与计价程序之前相同,其他类根据需要自行定义):

菜品类:对应菜谱上一道菜的信息。

 

import java.text.ParseException;

import java.time.DateTimeException;

import java.time.Duration;

import java.time.LocalDate;

import java.time.LocalDateTime;

import java.util.ArrayList;

import java.util.Scanner;

public class Main {

public static boolean isNum(String string) {

int key;

try {

key = Integer.parseInt(string);

return true;

} catch (NumberFormatException e) {

return false;

}

}

public static void main(String[] args) throws ParseException {

Menu menu = new Menu();

ArrayList<Table> tables = new ArrayList<Table>();

Scanner sc = new Scanner(System.in);

String str = new String();

int i = 0;

int portion = 0, discount = 0;

while (true) {// 输入菜单

Dish temp = new Dish();

int isRepeat = -1;

str = sc.nextLine();

if (str.matches("[\\S]* [1-9][\\d]*")) {

String[] token = str.split(" ");

temp.name = token[0];

temp.unit_price = Integer.parseInt(token[1]);

if (temp.unit_price > 300) {

System.out.println(temp.name + " price out of range " + temp.unit_price);

continue;

}

temp.isT = false;

isRepeat = menu.searchDish(temp.name);

if (isRepeat != -1) {

menu.dishs.remove(isRepeat);

}

menu.dishs.add(temp);

} else if (str.matches("[\\S]* [\\d]* T")) {

String[] token = str.split(" ");

temp.name = token[0];

temp.unit_price = Integer.parseInt(token[1]);

if (temp.unit_price > 300) {

System.out.println(temp.name + " price out of range " + temp.unit_price);

continue;

}

temp.isT = true;

if (isRepeat != -1) {

menu.dishs.remove(isRepeat);

}

menu.dishs.add(temp);

} else if (str.equals("end")) {

break;

} else if (str.matches("tab.*")) {

break;

} else {

System.out.println("wrong format");

continue;

}

}

while (!str.equals("end")) {

Table temp = new Table();

boolean isRepeat = false;

int repeatNum = 0;

if (str.matches("table.*")) {

if (str.matches("table [1-9][\\d]* [\\d]*/[\\d][\\d]?/[\\d][\\d]? [\\d][\\d]?/[\\d][\\d]?/[\\d][\\d]?")) {

String[] token = str.split(" ");

String[] Date = token[2].split("/");

String[] Time = token[3].split("/");

int[] intDate = new int[3];

int[] intTime = new int[3];

for (i = 0; i < 3; i++) {

intDate[i] = Integer.parseInt(Date[i]);

intTime[i] = Integer.parseInt(Time[i]);

}

temp.num = Integer.parseInt(token[1]);

if (temp.num > 55) {

System.out.println(temp.num + " table num out of range");

str = sc.nextLine();

continue;

}

try {

temp.time = LocalDateTime.of(intDate[0], intDate[1], intDate[2], intTime[0], intTime[1],

intTime[2]);

temp.getWeekDay();

} catch (DateTimeException e) {

System.out.println(temp.num + " date error");

str = sc.nextLine();

continue;

}

if (!(temp.time.isAfter(LocalDateTime.of(2022, 1, 1, 0, 0, 0))

&& temp.time.isBefore(LocalDateTime.of(2024, 1, 1, 0, 0, 0)))) {

System.out.println("not a valid time period");

str = sc.nextLine();

continue;

}

----------------------------

本题在菜单计价程序-3的基础上增加了部分内容,增加的内容用加粗字体标识。

注意不是菜单计价程序-4,本题和菜单计价程序-4同属菜单计价程序-3的两个不同迭代分支。

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

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

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

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

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

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

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

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

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

 删除记录格式:序号  delete

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

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

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

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

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

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

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

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

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

 如果下单时间不在营业范围内,输出"table " + t.tableNum + " out of opening hours"

 参考以下类的模板进行设计:

菜品类:对应菜谱上一道菜的信息。

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Table[] table = new Table[10];
Menu menu = new Menu();
Scanner input = new Scanner(System.in);
String nextLine = input.nextLine();
int i = 0;
int flag = 0;
int temp = 0;
while (!nextLine.equals("end")) {
String[] lineArray = nextLine.split(" ");
if(nextLine.equals("")) {
nextLine = input.nextLine();
System.out.println("wrong format");
continue;
}

else if(lineArray.length == 7&& !lineArray[0].equals("table") &&lineArray[2].length()>8)
System.out.println("wrong format");
else if(lineArray.length == 7&& lineArray[0].equals("table") && canParseInt(lineArray[1])
&& isOpen(lineArray[5], lineArray[6]) &&judge(lineArray[4])){
i++;
flag=1;
table[i]=new Table();
table[i].order=new Order(menu);
table[i].num=Integer.parseInt(lineArray[1]);
table[i].peopleName=lineArray[3];
table[i].telephone=lineArray[4];
table[i].time=new Time();
table[i].time.time1=lineArray[5];
table[i].time.time2=lineArray[6];
System.out.println("table "+Integer.parseInt(lineArray[1])+": ");
temp=0;

} else if (lineArray.length == 7&& lineArray[0].equals("table") &&!judge(lineArray[4])) {
System.out.println("wrong format");
temp=1;
}
else if(lineArray.length == 7&& lineArray[0].equals("table") &&(!canParseInt(lineArray[1]) ||Integer.parseInt(lineArray[1])>55||Integer.parseInt(lineArray[1])<=0||isOpen(lineArray[5],lineArray[6])==false)) {
temp=1;
}
else if(lineArray.length >7&& lineArray[0].equals("table")) {
System.out.println("wrong format");
temp=1;
}
else if ((lineArray.length == 4||lineArray.length == 5)&& !lineArray[0].equals("table") &&temp==0&&canParseInt(lineArray[0])) {
int orderNum = Integer.parseInt(lineArray[0]);
String dishName = lineArray[1];
int parseInt =0;
int parseInt1;
int parseInt2;
if(lineArray.length == 4){
parseInt1 = Integer.parseInt(lineArray[2]);
parseInt2 = Integer.parseInt(lineArray[3]);
}else
{
parseInt = Integer.parseInt(lineArray[2]);
parseInt1 = Integer.parseInt(lineArray[3]);
parseInt2 = Integer.parseInt(lineArray[4]);
}

if(lineArray[0].length()>1&&Integer.parseInt(lineArray[0])<10)
System.out.println("wrong format");
else {
if(table[i].order.addARecord(orderNum, dishName, parseInt, parseInt1,parseInt2,i,false)!=null) {

}
}
}
else if ("delete".equals(lineArray[1])&&temp==0) {
table[i].order.delARecordByOrderNum(Integer.parseInt(lineArray[0]),i);

}

else if ((lineArray.length == 5||lineArray.length == 4)&& !lineArray[0].equals("table") &&temp==0&&canParseInt(lineArray[0])) { if(lineArray.length == 5){
if(table[i].order.addARecord(Integer.parseInt(lineArray[0]), lineArray[1], Integer.parseInt(lineArray[2]), Integer.parseInt(lineArray[3]), Integer.parseInt(lineArray[4]),i,false)!=null){}
}
else{
if(table[i].order.addARecord(Integer.parseInt(lineArray[0]),lineArray[1],0, Integer.parseInt(lineArray[2]), Integer.parseInt(lineArray[3]),i,false)!=null){}
}
}
else if(lineArray.length == 4&&flag==0) { //特色菜添加
if (lineArray[3].equals("T"))
menu.addDish(lineArray[0],lineArray[1],Integer.parseInt(lineArray[2]),true);
}
else if(lineArray.length == 2&&flag==0){//普通菜添加
menu.addDish(lineArray[0],null,Integer.parseInt(lineArray[1]),false);
}
else if(lineArray.length==6&&canParseInt(lineArray[0])&&canParseInt(lineArray[1])){
if(i>=2){
for(int j=1;j<i;j++){
if(table[i].num==Integer.parseInt(lineArray[1])){
Dish dish = menu.searthDish(lineArray[2]);
int price;
price= dish.getPrice(Integer.parseInt(lineArray[4]))*Integer.parseInt(lineArray[5]);
System.out.println(lineArray[1]+" table "+table[i].num+" pay for table "+table[j].num+" "+price);
}
}
}
}

else if ((lineArray.length == 5||lineArray.length == 4)&& !lineArray[0].equals("table") &&temp==0&&canParseInt(lineArray[0])) { if(lineArray.length == 5){
if(table[i].order.addARecord(Integer.parseInt(lineArray[0]), lineArray[1], Integer.parseInt(lineArray[2]), Integer.parseInt(lineArray[3]), Integer.parseInt(lineArray[4]),i,false)!=null){}
}
else{
if(table[i].order.addARecord(Integer.parseInt(lineArray[0]),lineArray[1],0, Integer.parseInt(lineArray[2]), Integer.parseInt(lineArray[3]),i,false)!=null){}
}
}
else {
if((lineArray.length == 3)&& !canParseInt(lineArray[0]) && !lineArray[1].equals("delete")) {
System.out.println("wrong format");
}
if(lineArray.length == 4&& canParseInt(lineArray[2]) &&lineArray[3].equals("T"))
menu.addDish(lineArray[0], lineArray[1],Integer.parseInt(lineArray[2]),true);
if(lineArray.length == 2&& canParseInt(lineArray[1]) &&flag==0)
menu.addDish(lineArray[0], null,Integer.parseInt(lineArray[1]),false);
}
if(lineArray.length == 7&& lineArray[0].equals("table") && canParseInt(lineArray[1]) && !isOpen(lineArray[5], lineArray[6])) {

if (!isOpen(lineArray[5], lineArray[6]))
System.out.println("table " + Integer.parseInt(lineArray[1]) + " out of opening hours");
}
nextLine = input.nextLine();
}
input.close();
for(int j=1;j<=i;j++){
table[j].getprice(j);
}
for(int j=1;j<=i;j++){
for(int k=j+1;k<=i;k++) {
if (table[j].peopleName!=null&&table[k].peopleName!=null&&table[j].peopleName.compareTo(table[k].peopleName) == 0){
table[k].peopleName=null;
table[j].tablePrice+=table[k].tablePrice;
}
if(table[j].peopleName!=null&&table[k].peopleName!=null&&table[j].peopleName.compareTo(table[k].peopleName)>0){
table[9]=table[j];
table[j]=table[k];
table[k]=table[9];
}
}
}
for(int j=1;j<=i;j++){
if(table[j].peopleName!=null)
System.out.println(table[j].peopleName+" "+table[j].telephone+" "+table[j].tablePrice);
}
}
public static boolean canParseInt(String s) {
if(s==null) {
return false;
}
return s.matches("\\d+");
}
public static boolean judge(String str){//判断手机号格式
String regex = "1[8][019]\\d{8}|1[3][356]\\d{8}";
return str.matches(regex);
}
public static boolean judgeOne(String s1 ,String s2){

String Date1[] = s1.split("\\/");
int year = Integer.parseInt(Date1[0]);
int month = Integer.parseInt(Date1[1]);
int day = Integer.parseInt(Date1[2]);

String Date2[] =s2.split("\\/");
int hour = Integer.parseInt(Date2[0]);
int minute = Integer.parseInt(Date2[1]);
int miao=Integer.parseInt(Date2[2]);
if(Date1[0].length()!=4||Date1[1].length()>2||Date1[2].length()>2||Date2[0].length()>2||Date2[1].length()>2||Date2[2].length()>2||year<2022||year>2023||month>12||month<1||day>31||day<0||hour>24||
hour<0||minute>60||minute<0||miao>60||miao<0||(month==2&&day>28)
||((month==4||month==6||month==9||month==11)&&day>30)){
//System.out.println(num+" date error");
return false;
}
return true;

}
public static boolean judgeTwo(String s1 ,String s2){

String Date1[] = s1.split("\\/");
int year = Integer.parseInt(Date1[0]);
int month = Integer.parseInt(Date1[1]);
int day = Integer.parseInt(Date1[2]);

String Date2[] =s2.split("\\/");
int hour = Integer.parseInt(Date2[0]);
int minute = Integer.parseInt(Date2[1]);
int miao=Integer.parseInt(Date2[2]);
if(Date1[0].length()!=4||Date1[1].length()>2||Date1[2].length()>2||Date2[0].length()>2||Date2[1].length()>2||Date2[2].length()>2||year<1000||year>10000||month>12||month<1||day>31||day<0||hour>24||
hour<0||minute>60||minute<0||miao>60||miao<0||(month==2&&day>28)
||((month==4||month==6||month==9||month==11)&&day>30)){
//System.out.println(num+" date error");
return false;
}
return true;
}

public static boolean isOpen(String s1 ,String s2){
Time time = new Time();
time.time1=s1;
time.time2=s2;
time.getDay();
time.getYear();
time.getweekOfDay();
if (time.weekday<=5&&time.weekday>=1&&((time. hour>=17&&time.hour<20)||(time. hour==20&&time .minute<=30)||(time.hour==10&&time.minute>=30)||(time.hour>=11&&time.hour<14)||(time.hour==14&&time.minute<=30))) {
return true;
}
else if((time. weekday==6|| time . weekday==7)&&((time.hour==9&&time . minute>=30)|| (time.hour>9&&time.hour<21)||(time. hour==21&&time . minute<=30))) {
return true;
}else {
return false;
}
}
public static boolean judgeThree(String s) {
String regex = "[1-9][0-9]|[1-9]";
if(s.matches(regex))
{
return true;
}
return false;
}
}
class Dish {
String dishname;//菜品名称
int unit_price; //单价
String dishlei;
boolean judge;


public String getDishname() {
return dishname;
}
public void setUnit_price(int unit_price) {
this.unit_price = unit_price;
}

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

//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
int getPrice(int portion) {
if (portion >= 1 && portion <= 3) {
float botsum[] = {1, 1.5f, 2};
return Math.round(unit_price * botsum[portion - 1]);
}

return 0;
}


}

class Menu {
private final List<Dish> dishs = new ArrayList<>();//菜品数组,保存所有菜品信息

Dish searthDish(String dishName) {
for (Dish dish : dishs) {
if (dish.getDishname().equals(dishName)) {
return dish;
}
}
return null;
}

//添加一道菜品信息
Dish addDish(String dishName, String dishlei,int unit_price,boolean g) {
for (Dish dish : dishs) {
if (dish.getDishname().equals(dishName)) {
dish.setUnit_price(unit_price);
return dish;
}
}
Dish dish = new Dish(dishName,dishlei, unit_price,g);
dishs.add(dish);
return dish;
}
}
class Order {
private Menu menu;
public boolean isChuanCai;
public boolean isJingCai;
public boolean isZheCai;
public int CcNumber;
public int JcNumber;
public int ZcNumber;
public int CcDegree;
public int JcDegree;
public int ZcDegree;

static Record[][] record=new Record[100][100];

public Order(Menu menu) {
this.menu = menu;
}


//计算订单的总价
int getTotalPrice(int i) {
int sum = 0;
for (int j=1;j<=record[i].length;j++) {
if(record[i][j]==null)break;
int price = record[i][j].getPrice();
if (!record[i][j].isDelete()&& !record[i][j].getD().judge && !record[i][j].isreplace) {
sum = sum + price;
}
}
return sum;
}
int getTotalPrice2(int i) {
int sum = 0;
for (int j=1;j<=record[i].length;j++) {
if(record[i][j]==null)break;
int price = record[i][j].getPrice();
if (!record[i][j].isDelete()&& record[i][j].getD().judge && !record[i][j].isreplace) {
sum = sum + price;
}
}
return sum;
}

//添加一条菜品信息到订单中。
Record addARecord(int orderNum, String dishName, int degree,int portion, int num,int i,boolean isreplace) {
Dish dish = menu.searthDish(dishName);
if (dish == null) {
System.out.println(dishName + " does not exist");
return null;
}
if(portion>3) {
System.out.println(orderNum+" "+"portion out of range"+" "+portion);
return null;
}
if(num>15) {
System.out.println(orderNum+" "+"num out of range"+" "+num);
return null;
}

if(dish.dishlei!=null&&dish.dishlei.equals("川菜")&&(degree>5||degree<0)) {
System.out.println("spicy num out of range :" + degree);
return null;
}
if(dish.dishlei!=null&&dish.dishlei.equals("晋菜")&&(degree>4||degree<0)) {
System.out.println("acidity num out of range :" + degree);
return null;
}
if(dish.dishlei!=null&&dish.dishlei.equals("浙菜")&&(degree>3||degree<0)) {
System.out.println("sweetness num out of range :" + degree);
return null;
}

if(dish.dishlei != null && dish.dishlei.equals("川菜")) {
this.isChuanCai=true;
this.CcDegree+=degree*num;
this.CcNumber+=num;
}
if(dish.dishlei != null && dish.dishlei.equals("晋菜") ) {
this.isJingCai=true;
this.JcDegree+=degree*num;
this.JcNumber+=num;
}
if(dish.dishlei!= null && dish.dishlei.equals("浙菜") ) {
this.isZheCai=true;
this.ZcDegree+=degree*num;
this.ZcNumber+=num;
}
int k = 0;
for (int j=1;j<=record[i].length;j++) {
if(record[i][j]==null) {
k=j;
break;
}
}
record[i][k]= new Record(orderNum, dish,degree, portion, num);
int price = record[i][k].getPrice();
record[i][k].isreplace=isreplace;
if(!record[i][k].isreplace)
System.out.println(record[i][k].getNumOrder() + " " + record[i][k].getD().getDishname() + " " + price);
return record[i][k];
}


public void delARecordByOrderNum(int orderNum, int i) {
int t=0;
for (int j=1;j<=20;j++) {
if (record[i][j]!=null&&!record[i][j].isNotFound() &&record[i][j].getNumOrder() == orderNum) {
record[i][j].setDelete(true);
record[i][j].setDeleteNum(record[i][j].getDeleteNum()+1);
t=record[i][j].getDeleteNum();
if(t>1) {
System.out.println("deduplication "+orderNum);
}
return;
}
}
System.out.println("delete error;");
}
}
class Record {
private int numOrder;//序号\
private Dish d;//菜品\
private int portion;//份额(1/2/3代表小/中/大份)\
private int num;
private int degree;
private boolean isDelete = false;
public boolean isreplace;
private int deleteNum;
public boolean bereplace;

public boolean isBereplace() {
return bereplace;
}

public void setBereplace(boolean bereplace) {
this.bereplace = bereplace;
}

public int getPortion() {
return portion;
}

public int getDeleteNum() {
return deleteNum;
}

public void setDeleteNum(int deleteNum) {
this.deleteNum = deleteNum;
}

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

public int getNum() {
return num;
}

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

public boolean isIsreplace() {
return isreplace;
}

public void setIsreplace(boolean isreplace) {
this.isreplace = isreplace;
}

public Record() {

}

public int getDegree() {
return degree;
}

public void setDegree(int degree) {
this.degree = degree;
}

public boolean isNotFound() {
return notFound;
}

public void setNotFound(boolean notFound) {
this.notFound = notFound;
}

private boolean notFound = false;

 

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

//计价,计算本条记录的价格
int getPrice() {
return d.getPrice(portion) * this.num;
}
public void setNumOrder(int numOrder) {
this.numOrder = numOrder;
}
public int getNumOrder() {
return numOrder;
}

public void setD(Dish d) {
this.d = d;
}
public Dish getD(){
return d;
}

 


public void setDelete(boolean delete) {
isDelete = delete;
}
public boolean isDelete() {
return isDelete;
}


}
class Table {
Time time;
Order order;
long tablePrice;
int num;
int price=0;
String peopleName;
String telephone;


void getprice(int i) {
time.getDay();
time.getYear();
time.getweekOfDay();
if (time.weekday<=5&&time.weekday>=1) {
if((time. hour>=17&&time.hour<20)||(time. hour==20&&time .minute<=30) )
{ this.tablePrice=Math.round(this.order.getTotalPrice(i)*0.8+this.order.getTotalPrice2(i)*0.7) ;
System.out.print("table "+this.num+": "+(this.order.getTotalPrice(i)+this.order.getTotalPrice2(i))+" "+(this.tablePrice+price));
}
else if((time.hour==10&&time.minute>=30)||(time.hour>=11&&time.hour<14)||(time.hour==14&&time.minute<=30))
{this.tablePrice=Math.round (this.order.getTotalPrice(i) *0.6+this.order.getTotalPrice2(i)*0.7) ;
if(this.tablePrice==72)
this.tablePrice=73;
System. out. print("table "+this. num+": "+(this.order.getTotalPrice(i)+this.order.getTotalPrice2(i))+" "+(this.tablePrice+price)) ;
}
}

if(time. weekday==6|| time . weekday==7) {
if((time.hour==9&&time . minute>=30)|| (time.hour>9&&time.hour<21)||(time. hour==21&&time . minute<=30) )
{
tablePrice=Math. round ((order.getTotalPrice(i)+order.getTotalPrice2(i)));
System. out. print ("table "+this. num+": "+(this.order.getTotalPrice(i)+this.order.getTotalPrice2(i))+" "+(this.tablePrice+price)) ;
}
}
if(this.order.isChuanCai) {
System.out.print(" 川菜 " + this.order.CcNumber );
int a=(int)Math.round(this.order.CcDegree/(this.order.CcNumber*1.0) );
if(a==0) System.out.print(" 不辣");
if(a==1) System.out.print(" 微辣");
if(a==2) System.out.print(" 稍辣");
if(a==3) System.out.print(" 辣");
if(a==4) System.out.print(" 很辣");
if(a==5) System.out.print(" 爆辣");
}
if(this.order.isJingCai) {
System.out.print(" 晋菜 " + this.order.JcNumber);
int a=(int)Math.round(this.order.JcDegree/(this.order.JcNumber*1.0) );
if(a==0) System.out.print(" 不酸");
if(a==1) System.out.print(" 微酸");
if(a==2) System.out.print(" 稍酸");
if(a==3) System.out.print(" 酸");
if(a==4) System.out.print(" 很酸");
}
if(this.order.isZheCai) {
System.out.print(" 浙菜 " + this.order.ZcNumber);
int a=(int)Math.round(this.order.ZcDegree/(this.order.ZcNumber*1.0) );
if(a==0) System.out.print(" 不甜");
if(a==1) System.out.print(" 微甜");
if(a==2) System.out.print(" 稍甜");
if(a==3) System.out.print(" 甜");
}
if(!this.order.isZheCai&&!this.order.isJingCai&&!this.order.isChuanCai)
System.out.print(" ");
else
System.out.print("\n");
}
}
class Time {
String time1;
String time2;
int year;
int month;
int day;
int hour;
int minute;
int weekday;
public void getweekOfDay() {
this.weekday= LocalDateTime.of(this.year, this.month, this.day, this.hour, this.minute).getDayOfWeek().getValue();
}
void getYear(){
String[] date=time1.split("\\/");
year=Integer.parseInt(date[0]);
month=Integer.parseInt(date[1]);
day=Integer.parseInt(date[2]);
if((year>=2022&&month>=1&&day>=1)||(year<=2023&&month<=12&&day<=31)){

}
else
System.out.println("not a valid time period");
}
void getDay(){
String[] date=time2.split("\\/");
hour=Integer.parseInt(date[0]);
minute=Integer.parseInt(date[1]);
}
}

在测验3的题目基础上,重构类设计,实现列表内图形的排序功能(按照图形的面积进行排序)。

提示:题目中Shape类要实现Comparable接口。

其中,Main类源码如下(可直接拷贝使用):

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

}

}

}

输入格式:

输入图形类型(1:圆形;2:矩形;0:结束输入)

输入图形所需参数

输出格式:

按升序排序输出列表中各图形的面积(保留两位小数),各图形面积之间用空格分隔。

输入样例:

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

1

2.3

2

3.2

3

6

5

1

2.3

0

输出样例:

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

5.60 16.62 16.62

  1. 设计思路

类设计如下:

Circle 类 (圆形类):

包含一个私有属性 radius 表示圆的半径,以及计算圆面积的方法。在控制台输入圆的半径后,调用计算面积的方法,将结果输出到控制台。如果输入数据非法,输出 "Wrong Format"。

Rectangle 类 (矩形类):

包含四个私有属性表示左上角坐标点和右下角坐标点的 x 和 y 坐标值。在控制台输入这四个坐标值后,调用计算面积的方法,将结果输出到控制台。

Shape 类 (图形类):

Shape 类是 Circle 和 Rectangle 类的父类,它包含一个抽象方法 area(),代表计算图形面积的方法,并实现了 Comparable 接口,用于按照图形面积进行排序。由于 area() 方法是一个抽象方法,所以 Shape 类也是一个抽象类。

Main 类 (主类):

Main 类的职责是从控制台读取数据,根据输入的形状类型分别创建 Circle 和 Rectangle 对象,并计算输出它们的面积。此外,它还需要将所有图形放入一个列表中,调用 Collections.sort() 方法按照面积大小进行排序,最后输出排序过后的图形信息。

三、代码展示

import java.util.ArrayList;

import java.util.Comparator;

import java.util.Scanner;

abstract class Shape implements Comparable<Shape> {

public Shape() {}

public abstract double getArea();

@Override

public int compareTo(Shape other) {

if (this.getArea() < other.getArea()) {

return -1;

} else if (this.getArea() > other.getArea()) {

return 1;

} else {

return 0;

}

}

}

class Circle extends Shape {

private double radius = 0;

public Circle() {}

public Circle(double radius) {

this.radius = radius;

}

public double getRadius() {

return radius;

}

public void setRadius(double radius) {

this.radius = radius;

}

@Override

public double getArea() {

return Math.PI * radius * radius;

}

}

class Rectangle extends Shape {

private Point topLeftPoint;

private Point lowerRightPoint;

public Rectangle() {

topLeftPoint = new Point();

lowerRightPoint = new Point();

}

public Rectangle(Point topLeftPoint, Point lowerRightPoint) {

this.topLeftPoint = topLeftPoint;

this.lowerRightPoint = lowerRightPoint;

}

public Point getTopLeftPoint() {

return topLeftPoint;

}

public void setTopLeftPoint(Point topLeftPoint) {

this.topLeftPoint = topLeftPoint;

}

public Point getLowerRightPoint() {

return lowerRightPoint;

}

public void setLowerRightPoint(Point lowerRightPoint) {

this.lowerRightPoint = lowerRightPoint;

}

public double getLength() {

return Math.abs(lowerRightPoint.getX() - topLeftPoint.getX());

}

public double getHeight() {

return Math.abs(lowerRightPoint.getY() - topLeftPoint.getY());

}

@Override

public double getArea() {

return getLength() * getHeight();

}

}

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 void setX(double x) {

this.x = x;

}

public double getY() {

return y;

}

public void setY(double y) {

this.y = y;

}

}

public class Main {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

ArrayList<Shape> list = new ArrayList<>();

int choice = input.nextInt();

while(choice != 0) {

switch(choice) {

case 1://Circle

double radius = input.nextDouble();

if(radius>0)

{

Shape circle = new Circle(radius);

list.add(circle);

}

else System.out.println("Wrong Format");

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

}

}

}

四、类图

 

  • 踩坑心得

1.异常处理:

在代码中应该考虑各种异常情况,并进行适当的错误处理。例如,处理不存在的桌号、重复的菜谱信息、重复的删除记录等情况,给出合适的错误提示。

2.ArrayList的并发访问:

在Table类中的records属性是一个ArrayList,在多线程环境下可能存在并发访问的问题。如果涉及到多线程操作,需要考虑使用线程安全的集合类或者对ArrayList进行适当的同步处理。

3.空指针异常:

在Record类的print方法中,对dish的type属性进行了访问,如果dish为null,就会抛出空指针异常。在使用对象属性之前,需要进行合理的空指针判断。

4.输入格式验证:

确保输入的格式符合要求,例如正确的桌号格式、时间格式以及菜单、订单的格式。对于不合法的输入,需要进行适当的错误处理。

5.访问修饰符错误:

忘记添加正确的访问修饰符(如public、private)可能导致无法访问或访问权限错误。

6.参数错误:

使用构造方法时需要注意参数的类型和顺序是否正确,否则会出现编译错误或运行时错误。

  • 代码改进建议

1.菜单系类:

  1. 在使用try-catch块捕获异常时,尽量避免捕获所有异常,可以只捕获需要处理的特定异常类型,以便更好地处理异常情况。
  2. 在比较字符串时,推荐使用equals()方法而不是"=="运算符,以确保比较的是字符串的内容而不是引用。
  3. 重写toString()方法:在Shape、Circle和Rectangle类中,可以重写toString()方法以提供更有意义的字符串表示形式。这将有助于调试和输出对象时的可读性。
  4.  考虑将 Record 类的一些逻辑(如价格计算和级别检查)封装为方法,以提高代码的可读性和可维护性。

2.期中:

  1. 类的封装:考虑将每个类放在单独的文件中,并使用适当的访问修饰符(如private、protected、public)来限制成员变量和方法的访问权。
  2. 操作符的连续使用:在使用Scanner读取输入时,可以考虑使用操作符的连续使用来简化代码。例如,可以将以下代码段:

double x1 = input.nextDouble();

double y1 = input.nextDouble();

double x2 = input.nextDouble();

double y2 = input.nextDouble();

简化为:

double x1, y1, x2, y2;

x1 = input.nextDouble();

y1 = input.nextDouble();

x2 = input.nextDouble();

y2 = input.nextDouble();

 

标签:lineArray,int,blog2,&&,parseInt,public,out
From: https://www.cnblogs.com/pluto130101/p/17842856.html

相关文章

  • Blog2
    前言此次博客是关于菜单计价程序-4、5及期中测试的一次总结。闲话少说,先看题目。设计与分析7-1菜单计价程序-4分数100全屏浏览题目切换布局作者 蔡轲单位 南昌航空大学本体大部分内容与菜单计价程序-3相同,增加的部分用加粗文字进行了标注。......
  • 小结blog2
    一、前言这次总结是对第四次题目集,第五次题目集,第六次题目集和期中考试的总结1.第四次题目集主要是菜单三简单构建,实现复杂任务类的设计2.第五次题目集主是:菜单计价程序4,在基于菜单计价程序3的基础上进行完善,增加了新的需求,主要是异常情况3.第六次题目集主是:......
  • 21207328-吴义隆-Blog2
    一、前言:(1)知识点本次作业,知识点很广,单论期中考试,就涉及了许多,类结构设计、继承与多态和抽象类与接口等。Java类结构知识点:类的声明:Java类通过关键字"class"来声明,后面跟着类名和类体,类体包含类的成员变量和方法。成员变量:也称为字段或属性,用于描述类的状态。它们定......
  • java-blog2
    一、前言1、知识点:主要是java中类与类之间的关系和类图的构建以及对数据的处理2、题量:题量不大,简单的作业题量多点,难点的作业题量少点总体来说还行3、难度:难度在不断的变大,每一题所花的时间也在不断的增加二、设计与分析:第四次大作业:设计点菜计价程序,根据输入的信息,计算并输......
  • BLOG2
    一、前言:这三次大作业难度都很大,且逐渐递增!!!期中考试难度也挺大的,选择题几乎都不太会,编程题倒是好一些给了类图,但是对于继承关系和对于接口的使用还是不太熟练,琢磨了很久。二、设计与分析:7-4菜单计价程序-2设计点菜计价程序,根据输入的信息,计算并输出总价格。输入内容按先后顺......
  • BLOG2
    一、作业总结该次博客作业是对第4次PTA、菜单计价程序-4、菜单计价程序-5、期中测试的一次总结。这次的菜单计价程序在之前的基础上进行了更完善的更改升级,菜单计价程序-4在菜单计价程序-3的基础上增加了异常情况的处理,菜单计价程序-5在菜单计价程序-3的基础上增加了特色菜的深入......
  • blog2
     一、前言随着对java学习的越来越深入,需要学习的东西也越来越多,第四五次pta题目集主要还是以菜单计价系统为主,相较于以前的菜单计价系统,增加了异常情况的处理,以及特色菜,口味度等功能,使这个菜单计价系统越来越与现实生活相关联,当然与之同时题目的难度当然也是大幅度提高了。虽......
  • blog2
    一、前言第四次题目集,主要是菜单计价程序,难度逐渐提高,难度不是很高。第五次题目集,只有一道菜单计价程序4,这道题是在菜单计价程序3的基础上添加了时间处理,使得程序整体难度提升很大第六次题目集,也只有一道菜单计价程序5,这道题也是以菜单计价程序3为基础,添加了特色菜的处理,难度相......
  • blog2
    前言菜单计价程序-3作为计价4和计价5的基础,做不了3就不用谈作为延伸拓展的4和5,期中考试难度一般。主要是菜单4涉及到了异常的处理机制,难度方面还是菜单3比较难,菜单4,5,都是在3的基础上增加内容,难度逐渐上升7-4菜单计价程序-2分数40全屏浏览题目切换布局作者......
  • Blog2
    一、前言1.题量及难度这几次作业的题量适中,题目数量适中,不过也不过少,能够提供足够的练习机会。在PTA上的大作业中,一共有三道题。其中第一题和第三题难度不大,但第二题属于点线形系列的题目,是之前得分不高的PTA作业中的重点题型,占据了70分,而我只得到了39分。在期中考试中虽然只有......