2013-02-04 36 views
0

我试图制作一个允许用户为超市制作目录的Java应用程序,然后显示用户输入到目录中的所有产品。现在我遇到了填充用户输入时应填入的对象数组的问题。 输出应该像下面(用户输入在粗体):在java中填充用户输入的对象数组

输入产品描述(或#停止):冷凝粉状水 输入产品代码:P3487 输入产品单价: $ 2.50 输入产品单元短语:每个包

输入产品描述(或#停止):蒸馏水月光 输入产品代码:K3876 输入产品单价:$ 3.00 输入产品单位短语:十几

输入产品说明(或#停止):反重力丸 进入产品代码:Z9983 输入产品单价:$ 12.75 输入产品单位短语:

输入产品说明(或#停止):

你的目录: P3487,冷凝水粉每包$ 2,50。 K3876,蒸馏月光,一打3.00美元。 Z9983,反重力丸,12.75 $为60

,我写的代码: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 输入产品单价: 输入产品单位短语: 一打 输入产品说明(或#停止): 您的产品: k3876,蒸馏式光束,一打3.00美元

所以任何帮助请??

+0

我还没有完全阅读你的问题,但你有没有考虑过使用'ArrayList'而不是数组? – adarshr

+2

也许我很笨......但你的问题是什么? –

+0

你期待什么输出? –

回答

0

这里是更新的代码,工程...

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:"); 
       for (int j = 0; j < productsArray.length; j++) 
       { 
        if(productsArray[j]!=null) 
        { 
         System.out.printf("%s, %s,$%.2f %s,", 
          productsArray[j].getCode(), 
          productsArray[j].getDescription(), 
          productsArray[j].getPrice(), 
          productsArray[j].getPhrase()); 
        } 
       } 
       break; 
      } 

     } 

    } 
} 
+0

非常感谢你的回答非常有用它的工作 – Lula

+0

我还有一个问题,我可以使用sort()方法根据用户输入的代码对数组进行排序吗? – Lula

+1

您可以使目录实现为Comparable。在compareTo方法中,您使用逻辑来比较产品代码。然后,您可以使用Collections.sort根据产品代码进行排序.. – Amit

0

我不是已经给出,但在未来它可能是有益的调查ArrayListsVectors正确答案阐述。当您不确定需要存储的项目数量时,两者都是更好的数据结构,因为它们是可增长的数据存储区,即您在初始化时不需要定义它们的大小。

例如,在您最初的尝试中,您将productsArray定义为大小100.如果用户想要输入101个项目,该怎么办?使用Vector可以解决这个问题,因为它可以随时随地增长或缩小。

我是一个3年的软件开发的学生,当我开始使用向量在阵列编程问题变得轻松了很多工作,因为灵活性载体的周围报价:)

+0

感谢您提供这些信息我将尽快使用它们 – Lula

+0

我还有一个问题,我可以使用sort()方法根据代码由用户输入? – Lula