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 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...
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...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence;...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence; Message() { sentence=""; } Message(String text) { setSentence(text); } void setSentence(String text) { sentence=text; } String getSentence() { return sentence; } int getVowels() { int count=0; for(int i=0;i<sentence.length();i++) { char ch=sentence.charAt(i); if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') { count=count+1; } } return count; } int getConsonants() { int count=0; for(int i=0;i<sentence.length();i++)...
In Java, please write a tester code. Here's my code: public class Bicycle {     public...
In Java, please write a tester code. Here's my code: public class Bicycle {     public int cadence; public int gear;   public int speed;     public Bicycle(int startCadence, int startSpeed, int startGear) {         gear = startGear;   cadence = startCadence; speed = startSpeed;     }     public void setCadence(int newValue) {         cadence = newValue;     }     public void setGear(int newValue) {         gear = newValue;     }     public void applyBrake(int decrement) {         speed -= decrement;    ...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at it. - What should the Class object represent? (What is the “real life object” to represent)? - What properties should it have? (let’s hold off on the methods/actions for now – unless necessary for the constructor) - What should happen when a new instance of the Class is created? - Question: What are you allowed to do in the constructor? - Let’s test this...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT