菜单计价程序1-3
菜单计价程序-1
一丶代码:
1.菜品类
点击查看代码
class Dish{
String name;
int unit_prince;
int getPrice(int portion){
int peic = 0;
switch(portion){
case 1:
peic=(int)unit_prince;break;
case 2:
peic=Math.round((float)(unit_prince*1.5));break;
case 3:
peic=(int)(unit_prince*2);break;
}
return peic;
}
}
点击查看代码
class Menu{
Dish[] dishs = new Dish[4];
Dish searthDish(String dishName){
dishs[0] = new Dish();
dishs[1] = new Dish();
dishs[2] = new Dish();
dishs[3] = new Dish();
dishs[0].name ="西红柿炒蛋";
dishs[1].name = "清炒土豆丝";
dishs[2].name = "麻婆豆腐";
dishs[3].name = "油淋生菜";
dishs[0].unit_prince = 15;
dishs[1].unit_prince = 12;
dishs[2].unit_prince = 12;
dishs[3].unit_prince = 9;
for(int i=0;i<4;i++){
if(dishName.equals(dishs[i].name)){
return dishs[i];
}
}
return null;
}
}
点击查看代码
class Record{
Dish d = new Dish();
int portion;
int getPrice(){
return d.getPrice(portion);
}
}
点击查看代码
class Order {
Record[] records =new Record[1000000];
int count = 0;
int getTotalPrice(){
int sum=0;
for(int i=0;i<count;i++){
sum=sum+records[i].getPrice();
}
return sum;
}
Record addARecord(String dishName,int portion){
Record rd1 = new Record();
rd1.d.name = dishName;
rd1.portion = portion;
count++;
return rd1;
}
}
点击查看代码
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Menu mu = new Menu();
Order od = new Order();
String tem = null;
int i=0;
int a=0;
while(!"end".equals(tem)){
tem = sc.nextLine();
if(tem.equals("end")){
break;
}
String[] tem1 = tem.split(" ");
od.records[i] = new Record();
od.records[i] = od.addARecord(tem1[0],Integer.parseInt(tem1[1]));
i++;
a++;
}
/*
判断是否存在;
*/
Dish te;
int countt=0;
for(i=0;i<a;i++){
te = mu.searthDish(od.records[i].d.name);
if(te==null){
System.out.println(od.records[i].d.name+" "+"does not exist");
}
else{
od.records[i].d.unit_prince = te.unit_prince;
}
}
System.out.println(od.getTotalPrice());
}
}
错误分析
1.题面给了价格计算要四舍五入,简单的强制类型转换不能满足要求。改用Math. round。
2.第一次接触java类的概念。在增加菜品和订单的时候,返回值应是被new 实例化的对象。否则非零返回。
菜单计价程序-2
一丶代码
1.菜品类
点击查看代码
class Dish {
String name;//菜品名称
int unit_price; //单价
int num;
int getPrice(int portion) {
int peic = 0;
switch (portion) {
case 1:
peic = (int)unit_price * num;
break;
case 2:
peic = Math.round((float) (unit_price * 1.5)) * num;
break;
case 3:
peic = (int) (unit_price * 2) * num;
break;
}
return peic;//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
}
}
点击查看代码
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)){
temd = dishs[i];
break;
}
}
if(temd==null){
System.out.println(dishName+" does not exist");
}
return temd;
}//根据菜名在菜谱中查找菜品信息,返回Dish对象。
Dish addDish(String dishName,int unit_price){
Dish dh = new Dish();
dh.name = dishName;
dh.unit_price = unit_price;
count++;
return dh;
}//添加一道菜品信息
}
点击查看代码
class Record {
int orderNum;//序号\
Dish d = new Dish();//菜品\
int portion;//份额(1/2/3代表小/中/大份)\
int exist = 1;
int getPrice(){
return d.getPrice(portion);
}//计价,计算本条记录的价格\
}
点击查看代码
class Order {
Record[] records = new Record[1000];//保存订单上每一道的记录
int count = 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(int orderNum,String dishName,int portion,int num){
Record rd1 = new Record();
rd1.d.name = dishName;
rd1.orderNum = orderNum;
rd1.portion = portion;
rd1.d.num = num;
count++;
return rd1;
}//添加一条菜品信息到订单中。
boolean delARecordByOrderNum(int orderNum){
if(orderNum>count||orderNum<=0){
//System.out.println("delete error");
return false;
}else
records[orderNum-1].exist=0;
return true;
}//根据序号删除一条记录
void findRecordByNum(int orderNum){
}//根据序号查找一条记录
}
点击查看代码
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Menu mu = new Menu();
Order od = new Order();
int j,l;
j=l=0;
String[] inputt = new String[1000];
int incount = 0;
Dish tt = null;
while(true){
int count = 0;
String st = sc.nextLine();
for(int i=0;i<st.length();i++){
String te = st.substring(i,i+1);
if(te.equals(" ")){
count++;
}
}
if(st.equals("end"))
break;
if(count==1){
String[] temp = st.split(" ");
if(temp[1].equals("delete")){
if(!od.delARecordByOrderNum(Integer.parseInt(temp[0]))){
System.out.println("delete error;");
incount++;
}
}else{
mu.dishs[l] = mu.addDish(temp[0],Integer.parseInt(temp[1]));
l++;
}
}
if(count==3){
String[] temp1 = st.split(" ");
od.records[j]=od.addARecord(Integer.parseInt(temp1[0]),temp1[1],Integer.parseInt(temp1[2]),Integer.parseInt(temp1[3]));
tt = mu.searthDish(od.records[j].d.name);
if(tt==null){
System.out.println(od.records[j].d.name+" does not exist");
}else{
od.records[j].d.unit_price = tt.unit_price;
System.out.println(od.records[j].orderNum+" "+od.records[j].d.name+" "+od.records[j].d.getPrice(od.records[j].portion));
}
j++;
}
}
System.out.print(od.getTotalPrice());
}
}
错误分析
1.最后一个测试点,将信息集中处理改成了输入即处理即可通过。但不明白测试点具体内容。
2.删除掉的记录并没真正意义上的删除,只是做了一个flag作为标识变量,在计算价格的时候排除掉而已。
3.多条删除记录时,考虑了是否有删除错误的情况,但是只打印了一次delete error,导致许多测试点不能正常通过。
4.查找的菜单没有以最后一个菜品作为价格参考。
菜单计价程序-3
1.菜品类
点击查看代码
class Dish {
String name;//菜品名称
int unit_price; //单价
//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;//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
}
}
点击查看代码
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)){
temd = dishs[i];
break;
}
}
if(temd==null){
System.out.println(dishName+" does not exist");
}
return temd;
}//根据菜名在菜谱中查找菜品信息,返回Dish对象。
Dish addDish(String dishName,int unit_price){
Dish dh = new Dish();
dh.name = dishName;
dh.unit_price = unit_price;
count++;
return dh;
}//添加一道菜品信息
}
点击查看代码
class Record {
int orderNum;//序号\
//int AntherOrderNum;
Dish d = new Dish();//菜品\
int num = 0;
int portion;//份额(1/2/3代表小/中/大份)\
//int exist = 1;
int getPrice(){
return d.getPrice(portion)*num;
}//计价,计算本条记录的价格\
}
点击查看代码
class Order {
Record[] records = new Record[10];//保存订单上每一道的记录
int count = 0;//订单数量
//int forcount = 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;
}//计算订单的总价*/
void addARecord(int orderNum,String dishName,int portion,int num){
records[count] = new Record();
records[count].d.name = dishName;
records[count].orderNum = orderNum;
records[count].portion = portion;
records[count].num = num;
count++;
}//添加一条菜品信息到订单中。
/*Record TakeOdfor(int AnotherNUm,int orderNum,String dishName,int portion,int num){
Record rd2 = new Record();
rd2.d.name = dishName;
rd2.orderNum = orderNum;
rd2.portion = portion;
rd2.d.num = num;
rd2.AntherOrderNum = AnotherNUm;
//forcount++;
return rd2;
}*/
int delARecordByOrderNum(int orderNum){
if(orderNum>count||orderNum<=0){
System.out.println("delete error;");
return 0;
}else {
return records[orderNum - 1].getPrice();
}
}//根据序号删除一条记录
}
点击查看代码
class Table {
int tableNum;
String tableDtime;
int year,month,day,week,hh,mm,ss;
int sum = 0;//一桌价格 ;
// boolean f = true;
Order odt = new Order();
//Order odre = new Order();
float discnt = -1;
void Gettottalprice(){
if(discnt>0){
sum = Math.round(sum*discnt);
System.out.println("table " + tableNum + ": " + sum);
}else {
System.out.println("table " + tableNum + " out of opening hours");
}
}
void AheadProcess(String tableDtime){
this.tableDtime = tableDtime;
processTime();
discount();
//CheckAtime();
}
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]);
}
//void CheckAtime(){
// f= !(discnt < 0);
// }
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;
}
}
}
点击查看代码
import java.util.Calendar;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Menu mu = new Menu();
Table[] tablemes = new Table[10];
int j = 0;//菜单数
int l = 0;//订单数
int k = 0;//代点菜数
Dish tt;
//int sum = 0;
int cntTable = 0;//桌号
int count;
String[] temp;
int a1,a2,a3,a4,a5;
while (true) {
String st = sc.nextLine();
temp = st.split(" ");
if(st.equals("end"))
break;
count = temp.length;
if (count == 2) {//一个空格
//String[] temp1 = st.split(" ");
if (temp[1].equals("delete")) {//第二个为delete
a1 = Integer.parseInt(temp[0]);
int c = tablemes[cntTable].odt.delARecordByOrderNum(a1);
tablemes[cntTable].sum-=c;
} else {//菜单添加
a2 = Integer.parseInt(temp[1]);
mu.dishs[j] = mu.addDish(temp[0], a2);
j++;
}
//continue;
}
else if (count == 4) {//三个空格
//String[] temp2 = st.split(" ");
if (temp[0].equals("table")) {//桌号
cntTable++;//跳过0;
l = 0;
tablemes[cntTable] = new Table();
//tablemes[cntTable].tableDtime = st;
tablemes[cntTable].AheadProcess(st);
System.out.println("table " + cntTable + ": ");
} else {//增加订单的情况;
a3 =Integer.parseInt(temp[0]);
a4 = Integer.parseInt(temp[2]);
a5=Integer.parseInt(temp[3]);
tablemes[cntTable].odt.addARecord(a3, temp[1],a4 , a5);
tt = mu.searthDish(temp[1]);
if (tt != null) {
tablemes[cntTable].odt.records[l].d = tt;
int a = tablemes[cntTable].odt.records[l].getPrice();
System.out.println(tablemes[cntTable].odt.records[l].orderNum + " " + tt.name + " " +a );
tablemes[cntTable].sum +=a;
}
l++;
}
//continue;
}
else if (count == 5) {//代点菜
//String[] temp3 = st.split(" ");
a1 = Integer.parseInt(temp[1]);
a2 = Integer.parseInt(temp[3]);
a3 = Integer.parseInt(temp[4]);
tablemes[cntTable].odt.addARecord( a1, temp[2], a2, a3);
tt = mu.searthDish(temp[2]);
if (tt != null) {
tablemes[cntTable].odt.records[l].d.unit_price = tt.unit_price;
int b = tablemes[cntTable].odt.records[l].getPrice();
System.out.println(temp[1] + " table " + tablemes[cntTable].tableNum + " pay for table " + temp[0] + " " + b);
tablemes[cntTable].sum += b;
}
l++;
}
//st = sc.nextLine();
}
for (int i = 1; i < cntTable + 1; i++) {
tablemes[i].Gettottalprice();
}
}
}
二丶代码分析
1.主类过于冗余,判断在输出的信息,应该提取在多个方法中。
2.定义的gettotalprice();注释掉了,使用了一个单独变量作为价格总和。面向过程的代码,复用率低。
3.日期的判断采用的提取字符串。可用Java自带的日期类减少代码量和复杂度。
4.程序时间复杂度高。过测试点过的很极限。
5.桌类的定义没有很好的与前几个类组合一起,方法的定义很长。
错误分析
1.大量的运行超时。在类中实例化过多的自定义类的数组,应结合实际来开合适的数量。
2.使用Java自带的日期类,致使程序运行超时,代码如下:
`float discount(){
float discnt = 1;
String[] tt = tableDtime.split(" ");
String[] datett = tt[0].split("/");
String[] timett = tt[1].split("/");
if(datett[1].length()1){
datett[1] = "0"+datett[1];
}
if(datett[2].length()1){
datett[2] = "0"+datett[2];
}
if(timett[0].length()1){
timett[0] = "0"+timett[0];
}
if(timett[1].length()1){
timett[1] = "0"+timett[1];
}
if(timett[2].length()==1){
timett[2] = "0"+timett[2];
}
tableDtime = datett[0]+"/"+datett[1]+"/"+datett[2]+" "+timett[0]+"/"+timett[1]+"/"+timett[2];
LocalDateTime ldt3 = LocalDateTime.parse(tableDtime, df1);
if(ldt3.getDayOfWeek().getValue()>=1&&ldt3.getDayOfWeek().getValue()<=5){//星期1-5
if(ldt3.toLocalTime().isAfter(ldt1)&&ldt3.toLocalTime().isBefore(ldt2)){
discnt = 0.8F;
}else if(ldt3.toLocalTime().isAfter(ldt4)&&ldt3.toLocalTime().isBefore(ldt5)){
discnt = 0.6F;
}else{
discnt = -1;
}
}else{//星期6-7
if(ldt3.toLocalTime().isAfter(ldt6)&&ldt3.toLocalTime().isBefore(ldt7)){
discnt = 1;
}else{
discnt = -1;
}
}
return discnt;`
这部分占时间复杂度很高,暂时不知道是什么原因。
3.非题面测试用例,提交会偶尔过不了。测试点过的很极限。
最终总结
在近几次的pta作业中,我感受到了较大的阻力。对类的理解,面向对象的思考,属性,方法等概念在被近几次大作业中得到了很好的消化,对于idea的使用,比如调试,插件的安装与查找等技能,得到了很好的锻炼。现在可以做到以idea调试独自解决代码出现的问题。我的毅力更是更上一层楼,多次有过放弃的想法,但是由于看着别人100,我在思考我也一定100,最终我都能坚持下去。java的学习相比C语言的学习,节奏更加的快,内容很充实,一环扣一环,很多问题通过百度可以解决。自身解决问题能力大大提升。

浙公网安备 33010602011771号