题目:
桌布的计算
设计要求:必须使用面向对象的程序设计思想完成设计。
(1)某商家提供桌布定制服务,因为是定制的,所以桌布的价格按照桌布的面积计算。不同型号的桌布单价不同,单价由商家提供。顾客定制时需提供桌布的宽与长,桌布的型号,依据桌布的面积计算桌布价格。如果桌布的型号输入错误,就认为是无效对象,显示“type 值 not exists”。
(2)某顾客依据个人的需要定制了多块桌布,请列出该顾客的订单所有内容,并显示总件数与总价格。
提示:使用Scanner对象reader调用方法reader.hasnextLine(),判断是否以EOF结束,键盘输入以Ctrl+Z表示结束。
输入:商家桌布型号与面积单价,若干块桌布的型号、长度、宽度。
输出:若干块桌布的信息(型号、长度、宽度、桌布价格),总件数,总价格,其中,长度、宽度、价格保留一位小数。
样例输入:
J001:25 T001:100 L003:72
J001 1.2 0.8
J002 0.6 0.6
L003 1 0.6
样例输出:
type J002 not exists
J001 1.2 0.8 24.0
L003 1.0 0.6 43.2
2 67.2
我的代码
package test4;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
Tablecloth tc[]=new Tablecloth[100];
Customer cu[]=new Customer[100];
String str=reader.nextLine();
String[] a=str.split(" ");
for(int i=0;i<a.length;i++) {
String info[]=a[i].split(":");
tc[i]=new Tablecloth();
tc[i].style=a[0];
double p=Double.parseDouble(info[1]);
tc[i].price=p;
}
int k=0;
while(reader.hasNextLine()) {
str=reader.nextLine();
String[] b=str.split(" ");
cu[k]=new Customer();
cu[k].style=b[0];
cu[k].height=Double.parseDouble(b[1]);
cu[k].width=Double.parseDouble(b[2]);
k++;
}
double w=0;//总价格
double v = 0;//单价
int c=0;//满足条件的个数
for(int i=0;i<k;i++) {
int f=0;//判断是否存在
for(int j=0;j<a.length;j++) {
if(cu[i].style.equals(tc[j].style)) {
f=1;
v=tc[j].price*(cu[i].height*cu[i].width);
c++;
System.out.printf("%s %.1f %.1f %.1f",cu[i].style+cu[i].height+cu[i].width+v);
}
}
if(f==0) System.out.println("type"+" "+cu[i].style+" not exists");
else w=w+v;
}
System.out.printf("%d %.1f",c,w);
}
}
class Customer{
String style;
double height;
double width;
}
class Tablecloth{
String style;
double price;
}
我真心感觉我写的完全没有错,但是就是输不出来,到底里错了,请指出我的错误,让我的代码能够运行(要在我的代码基础上改!),谢谢!