调用包含变量int和String的数组

时间:2013-12-03 16:08:40

标签: java

我正在开发一个简单的产品库存程序,允许用户跟踪商店库存。我正在使用OOP,所以我的程序由2个类组成,一个是包含变量和初始打印方法的类产品{}。第二个类称为class productdatafinder {},它包含一个长表,其中包含以下所示形式的库存信息:

class  productsDataFinder
{
   public static void main( String[] not_in_use )
   {
      products[]  products_table  =
      {
//Milk products:
         new products( 3333, "Cheese",   "milk product", 1, "A" ),
         new products( 6778, "Butter",    "milk product", 1, "A" ),
         new products( 5642, "Yougurt", "milk product", 1, "A" ),
         new products( 4453, "High fat milk",   "milk product", 1, "A"  ), 
         new products( 3423, "Low fat milk",   "milk product", 1, "A" ),
         new products( 2296, "Coco milk","milk product", 1, "A" ),
         new products( 1796, "Fruit yougurt",  "milk product", 1, "A"   ),

//Fruits:    
         new products( 4379, "Apple",    "Fruit", 3, "B"    ),

以下是按顺序定义它们的方法:int product_id; String product_name; String product_group; int product_aisle; String product_row;

现在我的问题:

我已经知道如何打印出每个产品信息,给出了int类型的product_id,但现在我想通过给出类型为String的product_name来做同样的事情。我怎样才能做到这一点?我正在寻找一些提示和点击或任何有用的网络链接。这是节目输出:

 This program prints out your inventory data. 
 The serial number is in the form of (1-9)(1-9)(1-9)(1-9) 
 Insert your serial number: 3333

    Your product id is: 3333
    Your product name is: Cheese
    Your product group is: milk product
    Your product aisle number is: 1
    Your product row number is: A

Process completed

谢谢你,抱歉这篇长篇文章

这是我的变量product_id的print方法。这是第一堂课

public void print_products_data()
   {
      System.out.print( "\n    Your product id is: "  +  product_id  +
                        "\n    Your product name is: " +  product_name  +
                        "\n    Your product group is: " + product_group +
                        "\n    Your product aisle number is: " + product_aisle +
                        "\n    Your product row number is: " + product_row) ;

这是第二个

System.out.print("\n This program prints out your inventory data. "
                     + "\n The serial number is in the form of (1-9)(1-9)(1-9)(1-9) "
                     + "\n Insert your serial number: "  ) ;

      Scanner  keyboard  =  new Scanner( System.in ) ;
      int  given_id  =  keyboard.nextInt() ;

      int  products_index  =  0 ;

      boolean table_search_ready  =  false ;

         while ( table_search_ready  ==  false )
         {

         if ( products_index >= products_table.length )
         {

            table_search_ready  =  true ;

                      System.out.print( "\n    Sorry, no such product id "
                           +  given_id  + ".\n" ) ;
         }

         else if ( products_table[ products_index ].get_id()  ==  given_id )
         {
            products_table[ products_index ].print_products_data() ;

            table_search_ready  =  true ;
         }



         else
         {
            products_index  ++  ;
         }

3 个答案:

答案 0 :(得分:0)

这将以您当前查找系统运行的相同方式运行。但是,请检查product_name(或任何其他属性),而不是检查product_id。

//returns the Product which matches the name given by the parameter
//only works for unique names
public Product nameLookup(String name){
    for(Product p: products_table){
        if(name.equals(p.getProductName()))
            return p;
    }
}

//If names are not unique
public ArrayList<Product> nameLookup(String name){
    List<Product> templist = new ArrayList<Product>();
    for(Product p: products_table){
        if(name.equals(p.getProductName()))
            templist.add(p);
    }
    return templist;
}

答案 1 :(得分:0)

.equals()方法可能就是你要找的。你可以用这样的东西来测试它是否适合你

Scanner input = new Scanner(System.in);
System.out.print("Enter product: ");
if(input.nextLine().equals(products_table[0].productName){//or getProductName() if you have that
    //print product data
}

编辑:这是指向javadoc页面的链接,其中描述了.equals()方法。也可以看看.equalsIgnoreCase(),以便用户可以输入任何想要的东西!

String javadoc

答案 2 :(得分:0)

在遍历product_table时,通过equals test调用print方法。

String name="XYZ";
for(int i=0;i<product_table.length;i++) {
if(name.equals(products_table[i].getProduct_name())) {
  //Call print on products_table[i]
}
}