搜索方法无法按预期工作

时间:2014-10-15 11:43:01

标签: java

我正在构建书店系统,我所拥有的方法之一是search,它将根据书籍的ID代码搜索该书。
问题是如果用户添加了两本以上的书,代码就无法运行。如果用户添加了2本书,并在数组中搜索了第二本书,该方法将打印第二本书的ID代码,但是第一本书的信息。

以下是search方法的代码

public void searchBook(){
    boolean invalidInput;
    int q = -1;

    do {        
        try {        
            invalidInput = false;
    System.out.println("Enter the book's code you want to search for : ");
        q = s.nextInt();
        for (int i = 0; i<=items.length; i++){
            if(q == items[i].getCode(z)){
                System.out.println(items[i]);
                break;
            }else{
                System.out.println("The book is not found");
            }
        }

        } catch (InputMismatchException e) {
    System.out.println("Please enter a valid code [Numbers Only]");
            s.next();
    invalidInput = true;  // This is what will get the program to loop back
        }
    } while (invalidInput);

}

这是Items类

public class Items {
private int code, quantity;
private String description;
private double costPrice, sellingPrice;
String status, discount;

public Items(){
    this.code = 1111;
    this.quantity = 1;
    this.description = "Action";
    this.costPrice = 12.00;
    this.sellingPrice = 16.00;
    this.discount = "5%";
    this.status = "Unvailable";

}

public Items(int code, String description, int quantity,
        double costPrice, double sellingPrice, String status, String discount){
    this.code = code;
    this.description = description;
    this.quantity = quantity;
    this.costPrice = costPrice;
    this.sellingPrice = sellingPrice;
    this.status = status;
    this.discount = discount;
}

public void setCode(int code){
    this.code = code;
}

public void setQuantity(int quantity){
    this.quantity = quantity;
}

public void setDescription(String description){
    this.description = description;
}

public void setcostPrice(double costPrice){
    this.costPrice = costPrice;
}

public void setsellingPrice(double sellingPrice){
    this.sellingPrice = sellingPrice;
}

public void setStatus(String status){
    this.status = status;
}

public void setDiscount(String discount){
    this.discount = discount;
}

public int getCode(int code){
    this.code = code;
    return this.code;
}

public int getQuantity(int quantity){
    this.quantity = quantity;
    return this.quantity;
}

public String getDescription(String description){
    this.description = description;
    return this.description;
}

public double getcostPrice(double costPrice){
    this.costPrice = costPrice;
    return this.costPrice;
}

public double getsellingPrice(double sellingPrice){
    this.sellingPrice = sellingPrice;
    return this.sellingPrice;
}

public String getStatus(String status){
    this.status = status;
    return this.status;
}

public String getDiscount(String discount){
    this.discount = discount;
    return this.discount;
}

public String toString(){
    return ("code : " + this.code + "\nQuantity : " + this.quantity +
            "\nDescription : " + this.description + "\nCost price : " + this.costPrice
            + "\nSelling price : " + this.sellingPrice + "\nstatus : " + this.status
            + "\ndiscount : " + this.discount);
}

}

Initialaized项目              public class userChoices {

   Items[] items = new Items[200];
AddItem add = new AddItem();
Scanner s = new Scanner(System.in);
int z,x;
double c,v;
String n,m,b;

2 个答案:

答案 0 :(得分:0)

我解决了这个问题,感谢Philippe作为评论重播 删除我的getters中的参数:)

答案 1 :(得分:0)

为什么不创建一个整数和项目的地图?然后,您可以轻松地从代码中获取该项目。

示例:

Map<Integer, Item> map = new HashMap<>();

map.put(1, new Item(1));
map.put(2, new Item(2));

Item item1 = map.get(1);
Item item2 = map.get(2);

简化Item类以演示实现。

  public static class Item {
    final int code;

    public int getCode() {
      return code;
    }

    public Item(int code) {
      this.code = code;
    }
  }