In: Computer Science
Please write code in java and comment . thanks
Item class
BadAmountException Class
Inventory class
Program
//Item class
public class Item
{
//private data member
private String name; //name of the item
//constructor
public Item(String name)
{
this.name = name;
}
//return the name of item
public String name()
{
return name;
}
//return name of the Item
public String toString()
{
return name;
}
}
//BadAmountException class
class BadAmountException extends RuntimeException
{
//constructor
public BadAmountException(String message)
{
super(message);
}
}
//InventoryException class
class InventoryException extends Exception
{
//constructor
public InventoryException(String message)
{
super(message);
}
}
//Inventory class
class Inventory
{
//private data members
private String type; //item type
private int num;
//private data member
private int limit;
//limit to the number of items
//constructor
public Inventory(String type, int num, int
limit)
{
this.type = type;
if(num<0)
throw new
BadAmountException("Cannot keep a negative amount of items in
stock");
this.num = num;
this.limit = limit;
}
//another constructor
public Inventory(String type, int num)
{
this.type = type;
if(num<0)
throw new
BadAmountException("Cannot keep a negative amount of items in
stock");
this.num = num;
}
//method to return limit
public int getLimit()
{
return limit;
}
//method to set limit
public void setLimit(int limit)
{
this.limit = limit;
}
//method to return num
public int getNum()
{
return num;
}
//method to set num
public void setNum(int num)
{
this.num = num;
}
//method to return type
public String getType()
{
return type;
}
//method to set type
public void setType(String type)
{
this.type = type;
}
//method restocks by adding the input amount to the
current stock
public void restock(int quantity)
{
if(quantity<0)
throw new
BadAmountException("Cannot stock a negative amount of
items");
num = num + quantity;
}
//method to purchase n number of items
public Item[] purchase(int n) throws
InventoryException
{
if(n<0)
throw new
BadAmountException("Cannot purchase a negative amount of
items");
if(n>num)
throw new
InventoryException("Not enough items in stock");
if(limit==0)
throw new
InventoryException("Purchasing freeze on item");
if(n>limit)
throw new
InventoryException("Exceeded limit of LIMIT");
//update num
num = num - n;
//array of Item
Item items[] = new Item[n];
for(int i=0; i<n; i++)
{
items[i] = new
Item(type);
}
//return
return items;
}
}