user3436488
user3436488

Reputation: 13

Writing Java methods that use a combination of an interface and a separate class

This is an assignment working with Java interfaces. In total there are 3 classes and an interface: Seller, SellerImpl, SellerTestDriver, and Product. I am to implement the Seller interface in the SellerImpl class and test the output with code I write in the SellerTestDriver class. So far only one method gives the result I want – getName(). The other methods seem to call on the Product class but I have been unable write any code that will work. What I want are the methods to read:

name – Baker (it does so already);

product - Bread; provide product – true;

sellProduct() – just a line of text summarizing the seller and product.

Any assistance will be appreciated!

The Seller Interface:

     public interface Seller
{
public String getName();
public Product getProduct();
public boolean provideProduct(String productName);
public Product sellProduct();
}

The SellerImpl class:

    public class SellerImpl implements Seller
{
protected String name;
protected Product product;

// TODO: Complete this constructor
public SellerImpl(String name, Product product)
    {
    this.name = name;
    this.product = product;
    }

// TODO: Complete these methods 
public String getName()
    {
    return name;
    }

public Product getProduct()
    {
    return product;
    }
public boolean provideProduct(String productName)
    {
    if (productName.length() == 0) {
    return null != null;
    }
    else {
    return true;
    }
    }


public Product sellProduct()
    {
    return product;
    }

}

SellerTestDriver class:

    public class SellerImplTestDriver
{
    public static void main(String[] args)
        {
        Seller seller = new SellerImpl("Baker", new Product("Bread", 1.95));
        // TODO: Write a test for the getName() method
        System.out.println("Seller is a: " + seller.getName());

        // TODO: Write a test for the getProduct() method
        System.out.println("Seller offers: " + seller.getProduct());

        // TODO: Write a test for the provideProduct() method
        System.out.println("Does this seller supply this item? " + seller.provideProduct("Bread"));

        // TODO: Write a test for the sellProduct() method
        System.out.println(seller.getName() +" sells " );           }

Product class (that I am not instructed to touch at all):

    public class Product
{
protected String itemName;
protected double itemPrice;

public Product(String name, double price)
    {
    itemName = name;
    itemPrice = price;
    }
public String getName()
    {
    return itemName;
    }
public void setName(String itemName)
    {
    this.itemName = itemName;
    }

public double getPrice()
    {
    return itemPrice;
    }
public void setPrice(double itemPrice)
    {
    this.itemPrice = itemPrice;
    }
}

My current results:

Seller is a: Baker - I like this, it is correct

Seller offers: seller.Product@7a3e72

Does this seller supply this item? false

Buyer will purchase seller.Product@7a3e72

Upvotes: 1

Views: 545

Answers (2)

ErstwhileIII
ErstwhileIII

Reputation: 4843

Let's look at the high level. Don't you want the implementation of the seller class to have a way to hold multiple Products, and to have an inventory of each Product? (So the SellerImpl class should keep a list of Products and an inventory count for each product. Perhaps provideProduct means that you have to look through the list of products and see if you have the right product name and an inventory > 1 (to be true); and the sellProduct class would return the product to the user and reduct the inventory count?

  • Seller getName .. ok ..
  • Seller getProduct .. implementation ok, test wrong. You want to get the Product and then see if tne name of the product matches "bread". Something like
theProduct = seller.getProduct();
theProductName = theProduct.getName();
theProductPrice = theProduct.getPrice();

Then test to see if the product name and product price are what you want

  • Seller provideProduct .. Don't you want to test to see if the Seller's product is a particular value? You are testing the input argument. You want something like in the SellerImpl class

    if (productName == null) return false; return product.getName().equalsIgnoreCase(productName)

  • sellProduct looks ok, but remember to have the test make sure the returned value is not null and that the name and price are what you expect.

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

With this: Product@7a3e72

You're seeing the String returned by Product's toString() method which is inherited from Object, the parent class of all Java classes. This default toString() method from Object displays the class name, the @ symbol, and the object's hashCode, and all classes automatically inherit this method, unless they or one of their parent classes choose to override it.

To fix this you need to do just that: give your Product class a decent public String toString() override method, one that returns a String that fully describes the Product's state. Likely your toString() method will return both the Product's name and price. Something like:

@Override
public String toString() {
  return "Product: " + name + ", " + price;
}

Or even better,

@Override
public String toString() {
  return "Product: " + name + ", " + currencyFormat.format(price);
}

Where decimalFormat is a NumberFormat variable that uses a currency instance:

private NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();

Upvotes: 3

Related Questions