/** * Model some details of a product sold by a company. * * @author David J. Barnes and Michael Kolling modified by Bob Broeg * @version 2003.01.10 */ 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; } /** * Alternate constructor for objects of class Product. * @param id The product's identifying number. * @param name The product's name. * @param quantity The product's intial quantity. */ public Product(int id, String name, int quantity) { this.id = id; this.name = name; this.quantity = quantity; } /** * @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. * @param amount The number of new items added to the stock. * The current quantity is incremented by the amount. */ 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); } } }