Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Assign2.pdf Assign2Test.java Assign2Test.java import java.util.Scanner; /** * This class contains Assignment 1 with main method * and create a reference of Inventory class * */...

1 answer below »
Assign2.pdf
Assign2Test.java
Assign2Test.java
import java.util.Scanner;
**
 * This class contains Assignment 1 with main method
 * and create a reference of Inventory class
 
 * 
 *
public class Assign2Test {
    public static void main(String[] args) {
        
        
creating object of Inventory class
        Inventory test=new Inventory();
        
initializing option variable
        int option=0;
        
creating object of Scanner class
        Scanner scan=new Scanner (System.in);
        do {
            try {
try statement 
                
            
    
        System.out.println( 
epresent the option menu to read
                "1: Add item to Inventory\r\n" +
                "2: Display cu
ent inventory\r\n"+
                "3: Buy item(s)\r\n"+
                "4: Sell items(s)\r\n"+
                "5: Convert A
ayList to Linkedlist and print it\r\n"+
                "6: To exit\r\n");
        
    
            System.out.print("> ");
user prompt to enter intege
            option = scan.nextInt();
        /**switch statement for the different option*
        switch (option) {
        
        case 1:
    
            if (!test.addItem(scan))
                System.out.print("Item already exist");
            
            
eak;
            
        case 2:
            
            System.out.println(test);
            
eak;
        
        case 3:
            
            test.buyItem(scan);
            
            
eak;
            
        case 4:
        test.sellitem(scan);
            
            
eak;
            
        case 5:
            test.printLL();
            
eak;
        case 6:
            System.out.println("Exiting....");
            System.exit(0);
            
eak;
            
        
            
        default:
            System.out.println("Please select a valid selection between 1 - 6");
            
        }
         
              }catch (Exception e) {
                    System.out.println();
                    scan.next();
                }
        
    }while (option!=6);
        }
end of main 
}
end of class
        
        
        
        
        
        
        
        
        
        
        
        
        
        
FoodItem.JAVA
FoodItem.JAVA
**InputMismatchException class*
import java.util.InputMismatchException;
**Scanner class*
import java.util.Scanner;
**
 * This class contains FoodItem and its items
 
 * 
 *
public class FoodItem {
    /** The item code. *
    protected int itemCode;
    /** The item name. *
     protected String itemName;
     /** The item price. *
     protected float itemPrice;
     /** The item quantity. *
     protected int quantityInStock;
     /** The item cost. *
     protected float itemCost;
     /**
        * parameterized constructo
       * Instantiates a new food item.
       * @param itemCode represents item code
       * @param itemName represents name of the item
       * @param itemPrice represents price of the item
       * @param quantityInStock represents the quantity
       * @param itemCost represents the cost of the item
       *
     
     public FoodItem(int itemCode, String itemName, float itemPrice, int
    quantityInStock, float itemCost) {
     this.itemCode = itemCode;
     this.itemName = itemName;
     this.itemPrice = itemPrice;
     this.quantityInStock = quantityInStock;
     this.itemCost = itemCost;
     }
     /**
        * no-argument constructo
       * Instantiates a new food item.
       *
     public FoodItem() {
     this.itemCode = 0;
     this.itemName = null;
     this.itemPrice = 0.00f;
     this.quantityInStock = 0;
     this.itemCost = 0.00f;
     }
     
     /**method getter to get item code 
         * @return itemCode
         */ 
          public int getItemCode() { 
              return itemCode; }
          
          /**method setter to set item code 
             * @param itemCode represents the code of the item
             */ 
          public void setItemCode(int itemCode) { 
              this.itemCode = itemCode; } 
          /**method getter to get item name 
             * @return itemName
             */ 
          public String getItemName() { 
              return itemName; } 
          /**method setter to set item name 
             * @param itemName represents the name of the item
             */ 
          public void setItemName(String itemName) {
              this.itemName =itemName; } 
          /**method getter to get item price 
             * @return itemPrice
             */ 
          public float getItemPrice() { 
              return itemPrice; } 
          /**method setter to set item price 
             * @param itemPrice represents the price of the item
             */ 
          public void setItemPrice(float itemPrice) { 
              this.itemPrice = itemPrice; } 
          /**method getter to get quantity 
             * @return quantityInStock
             */ 
          public int getQuantityInStock() { 
              return quantityInStock; } 
          /**method setter to set item quantity 
             * @param quantityInStock represents the code of the item
             */ 
          public void setQuantityInStock(int quantityInStock) { 
              this.quantityInStock = quantityInStock; } 
          /**method getter to get item cost 
             * @return itemCost
             */ 
          public float getItemCost() { 
              return itemCost; } 
          /**method setter to set item cost 
             * @param itemCost represents the cost of the item
             */ 
          public void setItemCost(float itemCost) { 
              this.itemCost = itemCost; }
          /**method to print into String 
             * @return Item 
             */ 
     @Ove
ide
     public String toString() {
           
          
           return "Item:" + this.itemCode + "  " + this.itemName + " " + this.quantityInStock  +" price: $"+ this.itemPrice + " cost: $"+ this.itemCost ;
       }
end of toString method
     /**
       * Method Input code.
       *
       * @param scan the scanne
       * @return true, if successful
       *
     public boolean inputCode(Scanner scan) {
           do {
           try {
         System.out.print("Enter the code for the item: ");
          itemCode = scan.nextInt();
          if (itemCode==0)
            System.out.print("Invalid entry\n"); 
           
       } catch (InputMismatchException e) {
               System.out.print("Invalid entry\n");
               scan.nextLine();
       } }while(itemCode==0);
           return true;
            
            
       }
end of inputCode method
     /**
       * Checks if is equal.
       *
       * @param code the item
       * @return true, if is equal
       *
     public boolean equals(FoodItem code) {
     return this.itemCode==code.itemCode;
     }
end of equals method
     /**
       * Method addItem
       *
       * @param scan the scanne
       * @return true, if successful
       *
     public boolean  addItem(Scanner scan){
    
    
     scan.nextLine();
     System.out.print("Enter the name of the item: ");
user prompt to enter String
      itemName = scan.nextLine();
      do {
           try {
     System.out.print("Enter the quantity of the item: ");
user prompt to enter intege
      quantityInStock = scan.nextInt();
      if (quantityInStock<=0)
checking condition
        System.out.print("Invalid entry\n");
           } catch (InputMismatchException e) {
                   System.out.print("Invalid entry\n");
catch exception
                   scan.nextLine();
               } } while(quantityInStock<=0);
     scan.nextLine();
     do {
           try {
     System.out.print("Enter the cost of the item: ");
user prompt to enter intege
      itemCost = scan.nextFloat();
      if (itemCost<=0)
checking condition
        System.out.print("Invalid entry\n");
           } catch (InputMismatchException e) {
catch exception
               System.out.print("Invalid entry\n");
               scan.nextLine();
       } } while(itemCost<=0);
     do {
           try {
     System.out.print("Enter the sales price of the item: ");
user prompt to enter intege
      itemPrice = scan.nextFloat();
      if (itemPrice<=0)
checking condition
        System.out.print("Invalid entry\n");
 } catch (InputMismatchException e) {
catch exception
       System.out.print("Invalid entry\n");
       scan.nextLine();
 } } while(itemPrice<=0);
     return true;
}
end of addItem method
    }
end of class
Fruit.JAVA
Fruit.JAVA
**Scanner class*
import java.util.Scanner;
**
 * This class contains Fruit and its items and 
 * it extends class FoodItem
 * 
 * 
 *
public class Fruit extends FoodItem {
    /**orchard name*
    protected String orchardName;
     /**
         * Default Constructo
         *
        public Fruit() {
            
calling no arg constructor of super class by using super keyword
            super();
            orchardName = "";
        }
end of constructo
        
     
     /**method to print into String 
         * ove
ide method in super class by using super keyword and
         * adding orchardName item
         * @return Item 
         */ 
        @Ove
ide
        public String toString() {
            return super.toString() + " orchard supplier: " + this.orchardName;
        }
end of toString method
     /**
       * Ove
iding Method addItem of the super class by using supe
       * keyword and adding orchard name
       *
       * @param scan the scanne
       * @return true, if successful
       *
    @Ove
ide
    public boolean addItem(Scanner scan) {
        
        super.addItem(scan);
                System.out.print("Enter the name of the orchard supplier: ");
user prompt to enter String
            orchardName =scan.next();
    
  }
        return true;
    }
end of addItem method
    }
end of class
Inventory.java
Inventory.java
**A
ayList class*
import java.util.A
ayList;
**LinkedList class*
import java.util.LinkedList;
**Scanner class*
import java.util.Scanner;
**
 * This class contains Inventory in an A
aylist and LinkedList
 
 * 
 *
public class Inventory {
    /** A
aylist of inventory *
    private A
ayList inventory;
    /** represents the index *
    private int index;
    /** object of class FoodItem *
    private FoodItem food= new FoodItem();
**default constructor*
       public Inventory() {
           inventory=new A
ayList(20);
           
inventory=new FoodItem (size);
           
           food=null;
           
           
       }
end of default constructo
       /**method to print into String 
             * @return print
             */ 
       @Ove
ide
       public String toString() {
    String print=" ";
            for (int i=0; iprinting all elements
                
                print+=inventory.get(i).toString()+"\n";
            }
end of for loop
            return "Inventory in A
ayList:\n"+print;
            
        }
end of toString method
       /**method to check if itemCode is equal in the inventory a
aylist
        * @param item for the itemCode
             * @return index 
             */ 
       public int alreadyExists(FoodItem item) {
          
 int index=-1;
           for (int i = 0; i < inventory.size(); i++) {
              
               
               if (inventory.get(index).itemCode == food.itemCode) {
                   index = i;
                   
eak;
               }
           }
end of for loop
           return index;
       }
end of alreadyExists method
       
       /**
           * Method addItem
           *
           * @param scan the scanne
           * @return true, if successful
           *
       public boolean addItem(Scanner scan) {
           String choice=null;
           do {
               try {
                   
                   
                  
                 
           System.out.print("Do you wish to add a fruit(f), vegetable(v), sweetener (s) or a preserve(p)?:  ");
user prompt to enter String
             choice = scan.next();      
                  switch (choice.toLowerCase()) { 
                  case "f": 
                food = new Fruit(); 
creating object of Fruit 
                  
eak; 
                  case "v": 
                      food = new Vegetable();
creating object of Vegetable
                      
eak; 
                  case "s":
                      food=new Sweetener();
creating object of Sweetene
                      
eak;
                      case "p": 
                          food = new Preserve(); 
creating object of Preserve
                          
eak; 
            
                         
                          default: 
                              System.out.print("Invalid entry\n");
                  }
                  
            
                    food.inputCode(scan);
                       boolean flag=false;
                  
                  
                    for (FoodItem foodItem : inventory) {
checking if itemCode is in inventory
                        if (foodItem.itemCode==food.itemCode) {
                            food=foodItem;
                            flag=true;
                        }
                    }
                    
                    if (flag==true) {
if itemCode is inventory
                        System.out.print("Item already exist\n");
                    }else {
if not creating item
                        inventory.add(food); 
                        food.addItem(scan);
                    }                 
eak;
                     
              
               }catch (NullPointerException npe) {
                   
               }catch (IndexOutOfBoundsException iob) {
                   
               }
             
           
           }while(choice!="f"&&choice!="v"&&choice!="p"&&choice!="s");   
checking condition is true
        return true;
       }
end of addItem method
       /**
           * Method buyItem checks if the item code is valid 
           * then buy quantity
           *
           * @param scan the scanne
           * 
           *
     public void buyItem(Scanner scan) {
     System.out.println("Enter valid item code: ");
     int code = scan.nextInt();
     boolean response = false;
     FoodItem result = null;
     for (Object o : inventory) {
     if (((FoodItem) o).getItemCode() == code) {
     result = (FoodItem) o;
     response = true;
     }
     }
     if (response) {
     System.out.print("Enter valid quantity to buy: ");
     int quantity = scan.nextInt();
     if (quantity >= 1 && result.getQuantityInStock() >= quantity) {
     int old = result.getQuantityInStock();
     result.setQuantityInStock(old + quantity);
     System.out.println("Item buyed");
     } else {
     System.out.println("Invalid quantity...\n" +
     "E
or...could not buy item");
     }
     } else {
     System.out.println("Code not found in inventory...\n" +
     "E
or...could not buy item");
     }
     }
end of buyItem method
     /**
       * Method printLL to print LinkedList object
       * then buy quantity
       *
       * 
       * 
       *
     public void printLL() {
     LinkedList ll = new LinkedList();
     System.out.print("Inventory in Linked List:\n");
     for (Object o : inventory) {
     ll.add(o);
     }
     for (Object o : ll) {
     System.out.print(o + " \n ");
     }
     System.out.println();
     }
end of printLL method
     /**
       * Method sellitem checks if the item code is valid 
       * then sell quantity
       *
       * @param scan the scanne
       * 
       *
     public void sellitem(Scanner scan) {
     System.out.println("Enter valid item code: ");
     
     int code = scan.nextInt();
     boolean response = false;
     FoodItem result = null;
     for (Object o : inventory) {
     if (((FoodItem) o).getItemCode() == code) {
     result = (FoodItem) o;
     response = true;
     }
     }
     if (response) {
     System.out.print("Enter valid quantity to sell: ");
     int quantity = scan.nextInt();
     if (quantity <= 0) {
     System.out.println("Invalid quantity...\n" +
     "E
or...could not sell item");
     } else if (result.getQuantityInStock() >= quantity) {
     int old = result.getQuantityInStock();
     result.setQuantityInStock(old - quantity);
     System.out.println("Item buyed");
     } else {
     System.out.println("Insufficient stock in inventory...\n" +
     "E
or...could not sell item");
     }
     } else {
     System.out.println("Code not found in inventory...\n" +
     "E
or...could not buy item");
     }
     }
end of sell item method
    }
end of class
Preserve.java
Preserve.java
**InputMismatchException class*
import java.util.InputMismatchException;
**Scanner class*
import java.util.Scanner;
**
* This class contains Preserve and its items and 
* it extends class FoodItem

*
public class Preserve extends FoodItem{
    /**size of the jar*
    private int jarSize;
    /**
     * Default Constructo
     *
    public Preserve() {
        super();
        jarSize = 0;
    }
end of default constructo
    
     /**method to print into String 
         * ove
ide method in super class by using super keyword and
         * adding jarSize item
         * @return Item 
         */ 
        @Ove
ide
        public String toString() {
            return super.toString() + " size: " + jarSize + "mL";
        }
end of toString method
    
         /**
           * Ove
iding Method addItem of the super class by using supe
           * keyword and adding jar size 
           *
           * @param scan the scanne
           * @return true, if successful
           *
        @Ove
ide
        public boolean addItem(Scanner scan) {
            boolean valid = false;
            if (super.addItem(scan)) {
        
                if (!valid) {
                    do {
                        try {
                    System.out.print("Enter the size of the jar in millilitres: ");
                    
                    
                      
user prompt to enter intege
                    jarSize = scan.nextInt();
                        if (jarSize<=0)
checking condition
                            System.out.print("Invalid entry\n");
                           } catch (InputMismatchException e) {
catch exception
                               System.out.print("Invalid entry\n");
                               scan.nextLine();
                           } } while(jarSize<=0);
                    return true;
                }
            }
            return true;
        }
end of addItem method
     
    
    }
end of class
Sweetener.java
Sweetener.java
**Scanner class*
import java.util.Scanner;
**
 * This class contains Sweetener and it's items and 
 * it extends class FoodItem
 
 * 
 *
public class Sweetener extends FoodItem {
     /** The farm name String farmName. *
        protected String farmName; 
 Input [Name of supplier]
        
        /**default constructor*
        public Sweetener() {
            
calling default constructor of super class by using super keyword
            super();
            farmName = "";
            
            
            
        }
end of default constructo
        /**method to print into String 
         * ove
ide method in super class by using super keyword and
         * adding farmName item
         * @return Item 
         */ 
        @Ove
ide
        public String toString() {
            return super.toString() + " Sweetener supplier: " + farmName;
        }
end of toString method
        /**
           * Ove
iding Method addItem of the super class by using supe
           * keyword and adding farm name
           *
           * @param scan the scanne
           * @return true, if successful
           *
        @Ove
ide
        public boolean addItem( Scanner scan) {
             boolean isvalid = false;
                if (super.addItem(scan)) {
                    if (!isvalid)
                        System.out.print("Enter the name of the farm supplier: ");
user prompt to enter intege
                    farmName = scan.next();
                }
                return true;
            
    }
end of addItem method
    
    }
end of class
Vegetable.java
Vegetable.java
**Scanner class*
import java.util.Scanner;
**
 * This class contains Vegetable and it's items and 
 * it extends class FoodItem
 
 * 
 *
public class Vegetable extends FoodItem {
protected String farmName; 
 Input [Name of supplier]
    
    /**default constructor*
    public Vegetable() {
calling default constructor of super class by using super keyword
        super();
        farmName = "";
        
        
        
    }
end of default constructo
    /**method to print into String 
     * ove
ide method in super class by using super keyword and
     * adding farmName item
     * @return Item 
     */ 
    @Ove
ide
    public String toString() {
        return super.toString() + " farm name: " + farmName;
    }
end of toString method
    /**
       * Ove
iding Method addItem of the super class by using supe
       * keyword and adding farm name
       *
       * @param scan the scanne
       * @return true, if successful
       *
    @Ove
ide
    public boolean addItem( Scanner scan) {
         boolean isvalid = false;
            if (super.addItem(scan)) {
                if (!isvalid)
                    System.out.print("Enter the name of the farm supplier: ");
user prompt to enter String
                farmName = scan.next();
            }
            return true;
        
}
end of addItem method
    
    
    }
end of class
Answered 4 days After Mar 23, 2021

Solution

Kshitij answered on Mar 27 2021
159 Votes
Inventory System/Base class/FoodItem.java
Inventory System/Base class/FoodItem.java
*   Created by IntelliJ IDEA.
 *   Author: Kshitij Varshney (kshitijvarshne1)
 *   Date: 27-Mar-21
 *   Time: 12:59 PM
 *   File: FoodItem.java
 *
package March.mar27_21.BaseClass;
import java.util.Scanner;
public class FoodItem {private int itemCode;
    private String itemName;
    private float itemPrice;
    private int quantityInStock;
    private float itemCost;
    private String expiryDate;
    public FoodItem(int itemCode, String itemName, float itemPrice, int quantityInStock, float itemCost, String expiryDate) {
        this.itemCode = itemCode;
        this.itemName = itemName;
        this.itemPrice = itemPrice;
        this.quantityInStock = quantityInStock;
        this.itemCost = itemCost;
        this.expiryDate = expiryDate;
    }
    public FoodItem() {
        this.itemCode = 0;
        this.itemName = null;
        this.itemPrice = 0;
        this.quantityInStock = 0;
        this.itemCost = 0;
        this.expiryDate=null;
    }
    public int getItemCode() {
        return itemCode;
    }
    public void setItemCode(int itemCode) {
        this.itemCode = itemCode;
    }
    public String getItemName() {
        return itemName;
    }
    public void setItemName(String itemName) {
        this.itemName = itemName;
    }
    public float getItemPrice() {
        return itemPrice;
    }
    public void setItemPrice(float itemPrice) {
        this.itemPrice = itemPrice;
    }
    public int getQuantityInStock() {
        return quantityInStock;
    }
    public void setQuantityInStock(int quantityInStock) {
        this.quantityInStock = quantityInStock;
    }
    public float getItemCost() {
        return itemCost;
    }
    public void setItemCost(float itemCost) {
        this.itemCost = itemCost;
    }
    public String getExpiryDate() {
        return expiryDate;
    }
    public void setExpiryDate(String expiryDate) {
        this.expiryDate = expiryDate;
    }
    @Ove
ide
    public String toString() {
        return "itemCode=" + itemCode +
                ", itemName='" + itemName + '\'' +
                ", itemPrice= $" + itemPrice +
                ", quantityInStock=" + quantityInStock +
                ", itemCost= $" + itemCost +
                ", expiryDate= " + expiryDate +" , ";
    }
    public boolean equals(int code) {
        return this.getItemCode()==code;
    }
    public FoodItem addItem(int itemCode){
        Scanner sc= new Scanner(System.in);
        System.out.println("Enter the name for the item: ");
        String itemName = sc.next();
        System.out.println("Enter the quantity for the item: ");
        int quantityInStock= sc.nextInt();
        System.out.println("Enter the cost of the item: ");
        int itemCost = sc.nextInt();
        System.out.println("Enter the sell price of the item: ");
        int itemPrice= sc.nextInt();
        System.out.println("Enter the expiry date (yyyy-mm-dd)");
        sc.nextLine();
        String expiryDate = sc.nextLine();
        String finalDate=null;
        if(expiryDate.length()!=10){
            finalDate="No expiry";
        }
        else{
            finalDate=expiryDate;
        }
        FoodItem obj = new FoodItem(itemCode,itemName,itemPrice,quantityInStock,itemCost,finalDate);
        return obj;
    }
}
Inventory System/Base class/FruitItem.java
Inventory System/Base class/FruitItem.java
*   Created by IntelliJ IDEA.
 *   Author: Kshitij Varshney (kshitijvarshne1)
 *   Date: 27-Mar-21
 *   Time: 1:00 PM
 *   File: FruitItem.java
 *
package March.mar27_21.BaseClass;
import java.util.Scanner;
public class FruitItem extends FoodItem {private String orchardname;
    public FruitItem(int itemCode, String itemName, float itemPrice, int quantityInStock, float itemCost, String expiryDate, String orchardname) {
        super(itemCode, itemName, itemPrice, quantityInStock, itemCost, expiryDate);
        this.orchardname = orchardname;
    }
    public FruitItem(String orchardname) {
        this.orchardname = orchardname;
    }
    public FruitItem() {
        this.orchardname = null;
    }
    public String getOrchardname() {
        return orchardname;
    }
    public void setOrchardname(String orchardname) {
        this.orchardname = orchardname;
    }
    @Ove
ide
    public String toString() {
        return super.toString()+" orchardname= " + orchardname + " }";
    }
    public FruitItem addItem(FoodItem obj){
        System.out.println("Enter the name of the orchard supplier: ");
        Scanner sc= new Scanner(System.in);
        this.orchardname = sc.nextLine();
        FruitItem ftBucket = new FruitItem(obj.getItemCode(),obj.getItemName(),obj.getItemPrice(),obj.getQuantityInStock(),obj.getItemCost(),obj.getExpiryDate(),this.orchardname);
        return ftBucket;
    }
}
Inventory System/Base class/Inventory.java
Inventory System/Base...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here