繁体   English   中英

如何在构造函数类中创建数组?

[英]How to create an Array in the constructor class?

我正在创建一个图书清点程序。 我有两个类,一个是主类,另一个是构造函数类名称Item 在主类上,我创建了一个数组(Item[] book = new Item[100])来存储我的输入。 在我的Item类中,我想在下面创建一个函数

public boolean addItem(Item[] iArray, String itemCode){

    boolean c = false;


    for(int i=0; i<iArray.length; i++){
        if(iArray[i].getItemCode().equals(itemCode)){
            c = true;
        }
        else{
            c = false;

        }
    }

    return c;
}

我如何使Item[] iArray与主类中的书本数组同步?


public class Item {


private String itemCode;
private String description;
private int quantity;
private double costprice;
private double sellprice;
private String status = "Available";
private boolean check;
private double  discount;


public Item(){
    this("A000","default",0,0.00,0.00,0.25,"Available");
}

//construtor with parameter
public Item(String itemCode, String description, int quantity, double costprice, double sellprice, double discount, String status){
    this.setItemCode(itemCode);
    this.setDescription(description);
    this.setQuantity(quantity);
    this.setCostprice(costprice);
    this.setSellprice(sellprice);
    this.setStatus(status);
    this.setDiscount(discount);
}


//setter and getter methods
public void setItemCode(String itemCode){
    this.itemCode = itemCode;
}

public String getItemCode(){
    return this.itemCode;
}

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

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

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

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

public void setCostprice(double costprice){
    this.costprice = costprice;
}

public double getCostprice(){
    return this.costprice;
}

public void setSellprice(double sellprice){
    this.sellprice = sellprice;
}

public double getSellprice(){
    return this.sellprice;
}

public void setStatus(String status){

    this.status = status;
}

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

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

public double getDiscount(){
    return this.discount;
}

public void setCheck(boolean check){
    this.check = check;
}

public boolean getCheck(){
    return this.check;
}

public boolean addItem(Item[] iArray, String itemCode){

    boolean c = false;


    for(int i=0; i<iArray.length; i++){
        if(iArray[i].getItemCode().equals(itemCode)){
            c = true;
        }
        else{
            c = false;
            numberofobject++;
        }
    }

    return c;
}


    public void displaymenu(){
    System.out.println("Menu");
    System.out.println("1. Add New Item");
    System.out.println("2. Search");
    System.out.println("3. Edit Details");
    System.out.println("4. Edit Quantity");
    System.out.println("5. Stop Sell");
    System.out.println("6. List");
    System.out.println("7. Exit");
}*/



public String toString(){
    String msg = "";
    msg = this.getItemCode()+"\t\t\t\t"+this.getDescription()+"\t\t\t\t"+this.getQuantity()+"\t\t\t\t"+this.getCostprice()+"\t\t\t\t"+this.getSellprice()+"\t\t\t\t"+this.getDiscount()+"\t\t\t\t"+this.getStatus();
    return msg;
}

这是我的Item类。


import java.util.*;
public class Driver {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    int choice,quantity,NOI=0;
    double cprice,sprice,discount;
    String itc,name,status = "Available";
    boolean check = true;

    Item[] book = new Item[100];



    Scanner s1 = new Scanner(System.in);
    do{
        Item display = new Item();


        display.displaymenu();
        System.out.print("Please Select Menu: ");
        choice = s1.nextInt();

        if(choice==1){

            do{


            System.out.print("Please Enter the Item Code: ");
            itc =  s1.next();

                //for(int i=0; i<book.length; i++){
                //book[i].addItem(book, itc);

                if(display.addItem(book, itc)==true){
                    System.out.println("the book item code already exist."+NOI);
                    check = false;
                }
                else
                {
                    check = true;
                } //This is the question where i faced. 
                //}

            }while(check==false);

                   System.out.print("Please Enter the Description: ");
            name = s1.next();
            System.out.print("Please Enter the Quantity: ");
            quantity = s1.nextInt();
            System.out.print("Please Enter the Cost Price: ");
            cprice = s1.nextDouble();
            System.out.print("Please Enter the Sell Price: ");
            sprice = s1.nextDouble();
            System.out.print("Please Enter the Discount: ");
            discount = s1.nextDouble();*/

            book[NOI] =  new Item(itc,name,quantity,cprice,sprice,discount,status);
            NOI++;

        }

当我添加第二个项目时,出现错误(线程“ main”中的异常java.lang.NullPointerException),

怎么解决呢?

您的方法不会执行您想要的操作,因为即使找到了项目代码,循环仍会继续。 您可能想要这样的东西:

public boolean addItem(Item[] iArray, String itemCode){
    for (Item item : iArray) {
        if (item.getItemCode().equals(itemCode)) {
            return true;
        }
    }
    return false;
}

请注意,您发布的方法名称似乎很奇怪,因为它不会在任何地方添加任何内容。

您可能还考虑使用List<Item>ArrayList等)代替Item[]

我不确定我是否了解您要查找的内容,因此,如果我的答案与您的答案无关,请对其进行评论,然后将其删除。

我假设您正在尝试存储信息:将带有其代码的新项目添加到数组中。 但我不确定您是否是:

  • 在插入之前尝试确保项目在数组中的唯一性:

    也许您可以使用一组代码,它将简化您的问题,只需使用.contains()检查,然后添加或不添加

  • 尝试将其添加到列表中,如果已经存在,请执行某些操作(增加代码书的数量?)

    也许您可以使用HashMap,其中代码作为键,书作为项目。

在当前状态下,方法addItem不会添加任何内容,如果数组中的最后一本书与您的代码匹配,则仅返回...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM