Question

In: Computer Science

Please write code in java and comment . thanks Item class A constructor, with a String...

Please write code in java and comment . thanks

Item class

  • A constructor, with a String parameter representing the name of the item.
  • A name() method and a toString() method, both of which are identical and which return the name of the item.

BadAmountException Class

  • It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it.
  • It must have a default constructor.
  • It must have a constructor which takes an error message as a parameter. It can simply pass this error message up to the parent constructor.

Inventory class

  • A constructor which takes a String parameter indicating the item type, an int parameter indicating the initial number of items in stock, and an int parameter indicating the limit to the number of items which can be purchased in any transaction. If we attempt to initialize the inventory with a negative number of items, the constructor should throw a BadAmountException with the message "Cannot keep a negative amount of items in stock".
  • A constructor which takes a String parameter indicating the item type, and an int parameter indicating the initial number of items in stock. This constructor assumes no purchase limit, so initialize the limit appropriately. If we attempt to initialize the inventory with a negative number of items, the constructor should throw a BadAmountException with the message "Cannot keep a negative amount of items in stock".
  • A getter and setter for the purchase limit, getLimit and setLimit.
  • A getter for the number of items currently in stock, getNum.
  • A getter for the type of item (i.e. the name of the item) in stock, getType.
  • A restock method which takes an int parameter and which doesn't return anything. This method restocks by adding the input amount to the current stock. If the input amount is negative, this method should throw a BadAmountException with the message "Cannot stock a negative amount of items".
  • A purchase method which takes an int parameter and which returns an array of Item. The input value represents a number of items to purchase. However, before a purchase can be made, several checks need to be done to make sure it is a legal transaction:
    1. If the input is negative, then the method should throw a BadAmountException with the message "Cannot purchase a negative amount of items".
    2. If the purchase amount is greater than the number of items in stock, then we should throw an InventoryException with the text "Not enough items in stock".
    3. If the purchase limit is zero, that means that there is a purchase freeze, so we should throw an InventoryException with the text "Purchasing freeze on item".
    4. If there is a limit but we've been requested to purchase more than the limit, we should throw a InventoryException with the text "Exceeded limit of LIMIT", where LIMIT is a number representing the purchase limit.
    5. Otherwise the transaction is valid.
    If the transaction is valid, then we should purchase the specified number of items (updating the inventory accordingly), and return create and return an array of Item of the appropriate size and type (for example, if we request a purchase of two packages of "toilet paper", we would get back an array containing two Item objects whose name is "toilet paper").
    Note that since InventoryException is a checked exception, we may need to declare it to use it.

Solutions

Expert Solution

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;
   }
}


Related Solutions

Please write code in java and comment . thanksItem classA constructor, with a String...
Please write code in java and comment . thanksItem classA constructor, with a String parameter representing the name of the item.A name() method and a toString() method, both of which are identical and which return the name of the item.BadAmountException ClassIt must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it.It must have a default constructor.It must have a constructor which...
write code in java and comment. thanks. the program is about interface . Implement the basics...
write code in java and comment. thanks. the program is about interface . Implement the basics of Fitness and types of Fitness: Aerobic. Implement specific Fitness types such as Swimming, Cycling, Fitness Task: public interface Fitness (10pts) This will be used as a starting point for deriving any specific Fitness type. Every fitness exercise type has one or more muscle group it affects. Therefor a Fitness has the following abstarct method (Note that all methods in an interface are abstract...
Java and please have screenshots with source code and output \item[(1)] A palindrome is a string...
Java and please have screenshots with source code and output \item[(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
A constructor, with a String parameter representing the name of the item.
USE JAVA Item classA constructor, with a String parameter representing the name of the item.A name() method and a toString() method, both of which are identical and which return the name of the item.BadAmountException ClassIt must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it.Inventory classA constructor which takes a String parameter indicating the item type, an int parameter indicating the initial...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /**...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /** 2 * SameArray2.java 3 * @author Sherri Vaseashta4 * @version1 5 * @see 6 */ 7 import java.util.Scanner;8 public class SameArray29{ 10 public static void main(String[] args) 11 { 12 int[] array1 = {2, 4, 6, 8, 10}; 13 int[] array2 = new int[5]; //initializing array2 14 15 //copies the content of array1 and array2 16 for (int arrayCounter = 0; arrayCounter < 5;...
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should...
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should have a public instance method, getBMI() that returns a double reflecting the person's BMI (Body Mass Index = weight (kg) / height2 (m2) ). The class should have a public toString() method that returns a String like Fred is 1.9m tall and is 87.0Kg and has a BMI of 24.099722991689752Kg/m^2 (just print the doubles without special formatting). Implement this class (if you wish you...
please write the java code so it can run on jGRASP Thanks! 1 /** 2 *...
please write the java code so it can run on jGRASP Thanks! 1 /** 2 * PassArray 3 * @Sherri Vaseashta 4 * @Version 1 5 * @see 6 */ 7 import java.util.Scanner; 8 9 /** 10 This program demonstrates passing an array 11 as an argument to a method 12 */13 14 public class PassArray 15 { 16 public static void main(String[] args) 17 { 18 19 final int ARRAY_SIZE = 4; //Size of the array 20 // Create...
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor...
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Write a driver class to test that demonstrates that an exception happens for these scenarios 2.) Write a class named InvalidTestScore...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT