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...
Please write code in java and comment. Thanks. I would appreciate that. Fitness Task: public interface...
Please write code in java and comment. Thanks. I would appreciate that. Fitness Task: public interface Fitness public Muscle [ ] muscleTargeted() A method that returns the muscle that is going to be affected by the fitness. Note that the return type of the method is Muscle. A human body has a finite number of muscle parts that can be affected by fitness exercises. Define an enum datatype called Muscle with the follwoing member values Abs,Back,Biceps,Chest,Arms,Glutes,Shoulders,Triceps,Legs,Cardio public String description() a...
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...
Please use Java language! with as much as comment! thanks! Write a program that displays a...
Please use Java language! with as much as comment! thanks! Write a program that displays a frame with a three labels and three textfields. The labels should be "width:", "height:", and "title:" and should each be followed by one textfield. The texfields should be initialized with default values (Example 400, 600, default title), but should be edited by the user. There should be a button (label it whatever you want, I don't care). If you click the button, a new...
Please use Java language! with as much as comment! thanks! Write a program that displays a...
Please use Java language! with as much as comment! thanks! Write a program that displays a frame with a three labels and three textfields. The labels should be "width:", "height:", and "title:" and should each be followed by one textfield. The texfields should be initialized with default values (Example 400, 600, default title), but should be edited by the user. There should be a button (label it whatever you want, I don't care). If you click the button, a new...
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;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT