我正在尝试制作一个 Java 应用程序,该应用程序允许用户为超市制作目录,然后显示用户输入到目录中的所有产品。现在我在填充应该由用户输入填充的对象数组时遇到问题。输出应如下所示(用户输入粗体):
输入产品描述(或 # 停止):Condensed Powdered water 输入产品代码:P3487 输入产品单价:$2.50 输入产品单位短语:每包
输入产品描述(或 # 停止):Distilled Moonbeams 输入产品代码:K3876 输入产品单价:$3.00 输入产品单位短语:一打
输入产品描述(或 # 停止):Anti-Gravity Pills 输入产品代码:Z9983 输入产品单价:$12.75 输入产品单价:60
输入产品描述(或 # 停止):#
您的目录:P3487,浓缩粉状水,每包 2.50 美元。K3876,蒸馏月光,3.00 美元一打。Z9983,反重力药丸,60 美元,12.75 美元。
我写的代码:2类1类:
public class Catalog {
private String description ;
private String code ;
private double price ;
private String phrase ;
int counter = 0;
private Catalog [] list = new Catalog [100];
public Catalog (String productDescription , String productCode , double productPrice , String productPhrase)
{
description = productDescription;
code = productCode;
price = productPrice;
phrase = productPhrase;
}
public void setDescription (String productDescription)
{
description = productDescription;
}
public String getDescription ()
{
return description;
}
public void setCode (String productCode)
{
code = productCode;
}
public String getCode ()
{
return code;
}
public void setPrice (double productPrice)
{
price = productPrice;
}
public double getPrice ()
{
return price;
}
public void setPhrase (String productPhrase)
{
phrase = productPhrase;
}
public String getPhrase ()
{
return phrase;
}
第 2 类:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CatalogTest {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String name = null;
String code = null;
double price = 0.0;
String phrase = null;
BufferedReader input = new BufferedReader (new InputStreamReader (System.in));
Catalog product = new Catalog(name,code,price,phrase);
Catalog [] productsArray = new Catalog [100];
for (int i = 0 ; i < productsArray.length ; i ++)
{
System.out.println("Enter product description (or # to stop): ");
name = input.readLine();
if (!("#".equals(name)))
{
productsArray [i] = product;
product.setDescription(name);
System.out.println("Enter product code: ");
code = input.readLine();
productsArray [i] = product;
product.setCode(code);
System.out.println("Enter product unit price: ");
price = Double.parseDouble(input.readLine());
productsArray [i] = product;
product.setPrice(price);
System.out.println("Enter product unit phrase: ");
phrase = input.readLine();
productsArray [i] = product;
product.setPhrase(phrase);
productsArray [i] = new Catalog (name,code,price,phrase);
}
else
{
System.out.println("Your Catalog:");
System.out.printf("%s, %s,$%.2f %s",product.getCode(),product.getDescription(),product.getPrice(),product
.getPhrase());
break;
}
}
}
}
我得到的输出(用户输入粗体):输入产品描述(或#停止): 冷凝水 输入产品代码: p3487 输入产品单价: 2.50 输入产品单位短语: 每包 输入产品描述(或#停止): 蒸馏月光 输入产品代码: k3876 输入产品单价: 3 输入产品单位短语: 一打 输入产品描述(或 # 停止): # 您的目录:k3876,蒸馏月光,3.00 美元一打
所以有什么帮助吗?