0

我从另一个类调用方法时出错。我在StockManager类中,想在Product类中调用toString方法。

但我收到错误:类 Product 中的构造函数产品不能应用于给定类型:必需:int.java.lang.String; 发现:没有参数;原因:实际参数列表和格式参数列表的长度不同

顺便说一句,我正在使用 BlueJ。

请注意下面的 printProductDetails() 方法。这就是我想要写的。

这是我在 StockManager 课程中获得的所有内容。我正在尝试在下面的 printProductDetails() 方法中打印出 Product 类中 toString 的详细信息:

import java.util.ArrayList;

/**
 * Manage the stock in a business.
* The stock is described by zero or more Products.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class StockManager
{
// A list of the products.
private ArrayList<Product> stock;

/**
 * Initialise the stock manager.
 */
public StockManager()
{
    stock = new ArrayList<Product>();
}

/**
 * Add a product to the list.
 * @param item The item to be added.
 */
public void addProduct(Product item)
{
    stock.add(item);
}

/**
 * Receive a delivery of a particular product.
 * Increase the quantity of the product by the given amount.
 * @param id The ID of the product.
 * @param amount The amount to increase the quantity by.
 */
public void delivery(int id, int amount)
{

}

/**
 * Try to find a product in the stock with the given id.
 * @return The identified product, or null if there is none
 *         with a matching ID.
 */
public Product findProduct(int id)
{
    return null;
}

/**
 * Locate a product with the given ID, and return how
 * many of this item are in stock. If the ID does not
 * match any product, return zero.
 * @param id The ID of the product.
 * @return The quantity of the given product in stock.
 */
public int numberInStock(int id)
{
    return 0;
}

/**
 * Print details of all the products.
 */
public void printProductDetails()
{
    Product string = new Product();
    string.toString(); {
                    System.out.println("Attempt to restock " +
                           name +
                           " with a non-positive amount: " +
                           amount);
                        }

}
}

关于如何解决这个问题的具体帮助以及建议的一些原因将不胜感激谢谢。

产品类别:

/**
 * Model some details of a product sold by a company.
 * 
 * @author David J. Barnes and Michael Kölling.
 * @version 2011.07.31
 */
public class Product
{
// An identifying number for this product.
private int id;
// The name of this product.
private String name;
// The quantity of this product in stock.
private int quantity;

/**
 * Constructor for objects of class Product.
 * The initial stock quantity is zero.
 * @param id The product's identifying number.
 * @param name The product's name.
 */
public Product(int id, String name)
{
    this.id = id;
    this.name = name;
    quantity = 0;
}

/**
 * @return The product's id.
 */
public int getID()
{
    return id;
}

/**
 * @return The product's name.
 */
public String getName()
{
    return name;
}

/**
 * @return The quantity in stock.
 */
public int getQuantity()
{
    return quantity;
}

/**
 * @return The id, name and quantity in stock.
 */
public String toString()
{
    return id + ": " +
           name +
           " stock level: " + quantity;
}

/**
 * Restock with the given amount of this product.
 * The current quantity is incremented by the given amount.
 * @param amount The number of new items added to the stock.
 *               This must be greater than zero.
 */
public void increaseQuantity(int amount)
{
    if(amount > 0) {
        quantity += amount;
    }
    else {
        System.out.println("Attempt to restock " +
                           name +
                           " with a non-positive amount: " +
                           amount);
    }
}

/**
 * Sell one of these products.
 * An error is reported if there appears to be no stock.
 */
public void sellOne()
{
    if(quantity > 0) {
        quantity--;
    }
    else {
        System.out.println(
            "Attempt to sell an out of stock item: " + name);
    }
}
}
4

3 回答 3

1

您需要在 Product 类中定义一个默认构造函数(不带参数),或者在获取 Product 实例时传递 int id、String name:

// ...
Product string = new Product(1, "test value");
string.toString();
// ...
于 2013-03-15T20:40:36.190 回答
1

该方法不应该printProductDetails打印所有产品stock吗?这就是文档所说的:

  • 打印所有产品的详细信息。

所以我的猜测是你需要实际迭代你的stock而不是尝试构建一个Product.

public void printProductDetails() {
    for (Product product:stock) {
        System.out.println(product);
    }
}
于 2013-03-15T20:46:39.433 回答
0

从你的问题来看,这就是我的想法。

您需要toString()在您的Product class. 将此方法添加到您的Product class

public String toString()
{
     String str = "\n******Product ******\n"+
                   "Name :"+this.name+
                   "\nType :"+this.type;
    return str;
}

您可以使用System.out.println(product)where productis instance ofProduct class

于 2013-03-15T20:39:48.873 回答