Vending Machine JAVA program

profilebhihu45
__macosx.zip

__MACOSX/._VendingMachine

__MACOSX/VendingMachine/._.classpath

__MACOSX/VendingMachine/._.DS_Store

__MACOSX/VendingMachine/._.project

__MACOSX/VendingMachine/._.settings

__MACOSX/VendingMachine/._bin

__MACOSX/VendingMachine/._currencyHandler.txt

__MACOSX/VendingMachine/._currencyInventory

__MACOSX/VendingMachine/._currencyInventory.txt

__MACOSX/VendingMachine/._inventory

__MACOSX/VendingMachine/._inventory.txt

__MACOSX/VendingMachine/._src

__MACOSX/VendingMachine/.settings/._org.eclipse.jdt.core.prefs

__MACOSX/VendingMachine/bin/._.DS_Store

__MACOSX/VendingMachine/bin/._vending

__MACOSX/VendingMachine/bin/vending/._.DS_Store

__MACOSX/VendingMachine/bin/vending/._Currency.class

__MACOSX/VendingMachine/bin/vending/._CurrencyHandler.class

__MACOSX/VendingMachine/bin/vending/._CurrencyInventory.class

__MACOSX/VendingMachine/bin/vending/._InventoryItem.class

__MACOSX/VendingMachine/bin/vending/._Item.class

__MACOSX/VendingMachine/bin/vending/._Main.class

__MACOSX/VendingMachine/bin/vending/._Purchase.class

__MACOSX/VendingMachine/bin/vending/._VendingMachine.class

__MACOSX/VendingMachine/src/._.DS_Store

__MACOSX/VendingMachine/src/._vending

__MACOSX/VendingMachine/src/vending/._Currency.java

__MACOSX/VendingMachine/src/vending/._CurrencyHandler.java

__MACOSX/VendingMachine/src/vending/._CurrencyInventory.java

__MACOSX/VendingMachine/src/vending/._InventoryItem.java

__MACOSX/VendingMachine/src/vending/._Item.java

__MACOSX/VendingMachine/src/vending/._Main.java

__MACOSX/VendingMachine/src/vending/._Purchase.java

__MACOSX/VendingMachine/src/vending/._VendingMachine.java

VendingMachine/.classpath

VendingMachine/.DS_Store

VendingMachine/.project

Vending Machine org.eclipse.jdt.core.javabuilder org.eclipse.jdt.core.javanature

VendingMachine/.settings/org.eclipse.jdt.core.prefs

eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve org.eclipse.jdt.core.compiler.compliance=1.8 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.source=1.8

VendingMachine/bin/.DS_Store

VendingMachine/bin/vending/.DS_Store

VendingMachine/bin/vending/Currency.class

package vending;
public synchronized class Currency {
    private String name;
    private double value;
    private String size;
    private String material;
    private int quantity;
    private String curencyinventorytype;
    public void Currency();
    public String getName();
    public void setName(String);
    public double getValue();
    public void setValue(double);
    public String getSize();
    public void setSize(String);
    public String getMaterial();
    public void setMaterial(String);
    public int getQuantity();
    public void setQuantity(int);
    public String getCurencyinventorytype();
    public void setCurencyinventorytype(String);
    public String toString();
    public String available();
    public String record();
}

VendingMachine/bin/vending/CurrencyHandler.class

package vending;
public synchronized class CurrencyHandler {
    private String handler;
    private boolean coins;
    private boolean paper;
    private boolean paperchange;
    private boolean coinchange;
    public void CurrencyHandler();
    public String getHandler();
    public void setHandler(String);
    public boolean isCoins();
    public void setCoins(boolean);
    public boolean isPaper();
    public void setPaper(boolean);
    public boolean isPaperchange();
    public void setPaperchange(boolean);
    public boolean isCoinchange();
    public void setCoinchange(boolean);
    public String toString();
}

VendingMachine/bin/vending/CurrencyInventory.class

package vending;
public synchronized class CurrencyInventory {
    private java.util.List currency;
    public void CurrencyInventory();
    public void addItem(Currency);
    public String printInventory();
    public String availableInventory();
    public String getCurrencyInventory();
    public double getCurrencyValue(int);
    public int getInventorySize();
    public boolean printChange(double);
    public void recordCurrencyAdded(java.util.List);
}

VendingMachine/bin/vending/InventoryItem.class

package vending;
public synchronized class InventoryItem {
    private java.util.List items;
    public void InventoryItem();
    public void addItem(Item);
    public String printInventory();
    public int getInventorySize();
    public String getCurrentItemsRecord();
    public double getItemPrice(int);
    public void recordStockSold(int);
}

VendingMachine/bin/vending/Item.class

package vending;
public synchronized class Item {
    private String name;
    private String container;
    private String unit;
    private double size;
    private int quantity;
    private double price;
    public void Item();
    public String getName();
    public void setName(String);
    public String getContainer();
    public void setContainer(String);
    public String getUnit();
    public void setUnit(String);
    public double getSize();
    public void setSize(double);
    public int getQuantity();
    public void setQuantity(int);
    public double getPrice();
    public void setPrice(double);
    public String toString();
    public String record();
}

VendingMachine/bin/vending/Main.class

package vending;
public synchronized class Main {
    public void Main();
    public static void main(String[]);
}

VendingMachine/bin/vending/Purchase.class

package vending;
public synchronized class Purchase {
    public void Purchase();
    public static void updateItemsFile(String, InventoryItem);
    public static void updateCurrencyFile(String, CurrencyInventory);
}

VendingMachine/bin/vending/VendingMachine.class

package vending;
public synchronized class VendingMachine {
    private InventoryItem inventory;
    private CurrencyHandler currencyhandler;
    private CurrencyInventory currencyinventory;
    public void VendingMachine();
    public void loadInventory(String);
    public void loadCurrencyInventory(String);
    public String printInventoryLoad();
    public String printCurrencyInventoryLoad();
    public void loadCurrencyHandler(String);
    public void loadCommands();
    public void makePurchase(int, int);
    public int itemNumberInput();
    public int typesOfCurrencyInput();
    public int CurrencyTypeInput();
    public void listCommands();
    public int readInput();
}

VendingMachine/currencyHandler.txt

USD,true,true,true,true

VendingMachine/currencyInventory

PaperCurrency 100 Dollar Bill 100.0 medium paper -1
PaperCurrency 50 Dollar Bill 50.0 medium paper 0
PaperCurrency 20 Dollar Bill 200.0 medium paper 0
PaperCurrency 10 Dollar Bill 10.0 medium paper 4
PaperCurrency 5 Dollar Bill 5.0 medium paper 8
PaperCurrency 1 Dollar Bill 1.0 medium paper 16
CoinCurrency 50 cent piece 0.5 large metal 10
CoinCurrency Quarter 0.25 medium metal 20
CoinCurrency Dime 0.1 small metal 30
CoinCurrency Nickel 0.05 small metal 40
CoinCurrency penny 0.01 small metal 50
PaperCurrency 100 Dollar Bill 100.0 medium paper 0
PaperCurrency 50 Dollar Bill 50.0 medium paper 0
PaperCurrency 20 Dollar Bill 200.0 medium paper 0
PaperCurrency 10 Dollar Bill 10.0 medium paper 4
PaperCurrency 5 Dollar Bill 5.0 medium paper 7
PaperCurrency 1 Dollar Bill 1.0 medium paper 16
CoinCurrency 50 cent piece 0.5 large metal 10
CoinCurrency Quarter 0.25 medium metal 20
CoinCurrency Dime 0.1 small metal 30
CoinCurrency Nickel 0.05 small metal 40
CoinCurrency penny 0.01 small metal 50

VendingMachine/currencyInventory.txt

PaperCurrency 100 Dollar Bill 100.0 medium paper 0
PaperCurrency 50 Dollar Bill 50.0 medium paper 0
PaperCurrency 20 Dollar Bill 200.0 medium paper 0
PaperCurrency 10 Dollar Bill 10.0 medium paper 4
PaperCurrency 5 Dollar Bill 5.0 medium paper 8
PaperCurrency 1 Dollar Bill 1.0 medium paper 16
CoinCurrency 50 cent piece 0.5 large metal 10
CoinCurrency Quarter 0.25 medium metal 20
CoinCurrency Dime 0.1 small metal 30
CoinCurrency Nickel 0.05 small metal 40
CoinCurrency penny 0.01 small metal 50

VendingMachine/inventory

Diet Coke 1.0 Bottle-soda liter 0.5 10
Coke 1.0 Bottle-soda liter 0.5 9
Apple Juice 1.0 Carton-thinRectangle ounces 6.0 10
Peanuts 0.5 Bag-small ounces 2.5 10
Potato Chips 0.5 Bag-medium ounces 6.0 10
Hersey Chocolate 1.0 Bag-medium ounces 4.0 10
Diet Coke 1.0 Bottle-soda liter 0.5 10
Coke 1.0 Bottle-soda liter 0.5 9
Apple Juice 1.0 Carton-thinRectangle ounces 6.0 10
Peanuts 0.5 Bag-small ounces 2.5 10
Potato Chips 0.5 Bag-medium ounces 6.0 10
Hersey Chocolate 1.0 Bag-medium ounces 4.0 10

VendingMachine/inventory.txt

Diet Coke,1.0,Bottle-soda,liter,0.5,10 Coke,1.0,Bottle-soda,liter,0.5,10 Apple Juice,1.0,Carton-thinRectangle,ounces,6.0,10 Peanuts,0.5,Bag-small,ounces,2.5,10 Potato Chips,0.5,Bag-medium,ounces,6.0,10 Hersey Chocolate,1.0,Bag-medium,ounces,4.0,10

VendingMachine/src/.DS_Store

VendingMachine/src/vending/Currency.class

package vending;
public synchronized class Currency {
    private String name;
    private double value;
    private String size;
    private String material;
    private int quantity;
    private String curencyinventorytype;
    public void Currency();
    public String getName();
    public void setName(String);
    public double getValue();
    public void setValue(double);
    public String getSize();
    public void setSize(String);
    public String getMaterial();
    public void setMaterial(String);
    public int getQuantity();
    public void setQuantity(int);
    public String getCurencyinventorytype();
    public void setCurencyinventorytype(String);
    public String toString();
    public String available();
    public String record();
}

VendingMachine/src/vending/Currency.java

VendingMachine/src/vending/Currency.java

package  vending ;

/**
 *
 *  @author  DCCLXXVII
 * This is the currency class whic is used
 * to create currency objects with the given characteristics
 *
 */
public   class   Currency   {

     private   String  name ;
     private   double  value ;
     private   String  size ;
     private   String  material ;
     private   int  quantity ;
     private   String  curencyinventorytype ;

     /**
     * constuctor
     */
     public   Currency (){



     /**
     * setters set   @param  fieldvalue
     *  and
     *  getters
     *  @return  fieldValue
     */
     public   String  getName ()   {
         return  name ;
     }

     public   void  setName ( String  name )   {
         this . name  =  name ;
     }

     public   double  getValue ()   {
         return  value ;
     }

     public   void  setValue ( double  value )   {
         this . value  =  value ;
     }

     public   String  getSize ()   {
         return  size ;
     }

     public   void  setSize ( String  size )   {
         this . size  =  size ;
     }

     public   String  getMaterial ()   {
         return  material ;
     }

     public   void  setMaterial ( String  material )   {
         this . material  =  material ;
     }

     public   int  getQuantity ()   {
         return  quantity ;
     }

     public   void  setQuantity ( int  quantity )   {
         this . quantity  =  quantity ;
     }

     public   String  getCurencyinventorytype ()   {
         return  curencyinventorytype ;
     }

     public   void  setCurencyinventorytype ( String  curencyinventorytype )   {
         this . curencyinventorytype  =  curencyinventorytype ;
     }

     /**
     *  @return  class fields contents
     */
     public   String  toString (){

         return   "CurrencyInventory: " + curencyinventorytype  + " CurrencyUnit: Name: " +
        name +   " Value: " + value +   " Size: " + size  + " Material: " + material +
         " Quantity: " + quantity  + "\n" ;
     }
     /**
     *  @return  class fields contents
     */
public   String  available (){

         return  curencyinventorytype  + " CurrencyUnit: Name: " +
        name +   " Value: " + value +   " Size: " + size  + " Material: " + material + "\n" ;
     }

/**
 *  @return  class fields contents
 */

public   String  record (){
     return  curencyinventorytype + "," + name + "," + value + "," + size + "," + material + "," + quantity ;
}

}
}

VendingMachine/src/vending/CurrencyHandler.class

package vending;
public synchronized class CurrencyHandler {
    private String handler;
    private boolean coins;
    private boolean paper;
    private boolean paperchange;
    private boolean coinchange;
    public void CurrencyHandler();
    public String getHandler();
    public void setHandler(String);
    public boolean isCoins();
    public void setCoins(boolean);
    public boolean isPaper();
    public void setPaper(boolean);
    public boolean isPaperchange();
    public void setPaperchange(boolean);
    public boolean isCoinchange();
    public void setCoinchange(boolean);
    public String toString();
}

VendingMachine/src/vending/CurrencyHandler.java

VendingMachine/src/vending/CurrencyHandler.java

package  vending ;

public   class   CurrencyHandler   {
    
     private   String  handler ;
     private   boolean  coins ;
     private   boolean  paper ;
     private   boolean  paperchange ;
     private   boolean  coinchange ;
    
     public   CurrencyHandler (){
        
        handler =   "Not set" ;
        coins =   false ;
        paper =   false ;
        paperchange =   false ;
        coinchange =   false ;
        
     }

     public   String  getHandler ()   {
         return  handler ;
     }

     public   void  setHandler ( String  handler )   {
         this . handler  =  handler ;
     }

     public   boolean  isCoins ()   {
         return  coins ;
     }

     public   void  setCoins ( boolean  coins )   {
         this . coins  =  coins ;
     }

     public   boolean  isPaper ()   {
         return  paper ;
     }

     public   void  setPaper ( boolean  paper )   {
         this . paper  =  paper ;
     }

     public   boolean  isPaperchange ()   {
         return  paperchange ;
     }

     public   void  setPaperchange ( boolean  paperchange )   {
         this . paperchange  =  paperchange ;
     }

     public   boolean  isCoinchange ()   {
         return  coinchange ;
     }

     public   void  setCoinchange ( boolean  coinchange )   {
         this . coinchange  =  coinchange ;
     }
    
     public   String  toString (){
        
         return   "CurrencyHandler: " +  handler  + " Handles Paper Currrency: " +  paper  +  
                 " Handles Coins: " + coins +   "Provides Change Paper: " + paperchange +
                 " Provides Change Coins: " + coinchange ;
     }
    

}

VendingMachine/src/vending/CurrencyInventory.java

VendingMachine/src/vending/CurrencyInventory.java

package  vending ;

import  java . util . ArrayList ;
import  java . util . List ;
/**
 * 
 *  @author  DCCLXXVII
 * this class handles the collection of currencies
 *
 */

public   class   CurrencyInventory   {
    
     private   List < Currency >  currency ;
    
     public   CurrencyInventory (){
        
        currency =   new   ArrayList <> ();
     }
    
     public   void  addItem ( Currency  curr ){
        
        currency . add ( curr );
     }
    
     /**
     * 
     *  @return  currencyList This is the list of currency loaded from txt file
     */
     public   String  printInventory (){
         String  inventory = "" ;
        
         for ( int  i = 0 ; i < currency . size (); i ++ ){
            
            inventory  +=  currency . get ( i ). toString ();
         }
        
         return  inventory ;
     }
    
    
     /**
     * 
     *  @return  currencyList This is the list of currency available
     */
     public   String  availableInventory (){
         String  inventory = "Available Currency Denominations. \n" ;
        
         for ( int  i = 0 ; i < currency . size (); i ++ ){
            
            inventory  +=   "Currency #: " + i + ": " + currency . get ( i ). available ();
         }
        
         return  inventory ;
     }
    
     /**
     * 
     *  @return  currencyList this is the current list of currency 
     */
    
     public   String  getCurrencyInventory (){
String  inventory = "" ;
        
         for ( int  i = 0 ; i < currency . size (); i ++ ){
            
            inventory  += currency . get ( i ). record () + "\n" ;
         }
        
         return  inventory ;
        
     }
    
    
     /**
     * get the value of a single currency
     *  @param  currencyType
     *  @return
     */
     public   double  getCurrencyValue ( int  currencyType ){
        
        
         return  currency . get ( currencyType ). getValue ();
        
     }
    
     /**
     * 
     *  @return  currencyList size
     */
    
public   int  getInventorySize (){
        
         return  currency . size ();
     }

/**
 * 
 *  @param  change
 *  @return  boolean This is to check whether there is sufficient amount to give change
 */

public   boolean  printChange ( double  change ){
     boolean  state =   false ;
     double  checkChange =  change ;
    
     for ( int  i = 0 ; i < currency . size (); i ++ ){
        
         double  amountPerCurency =  currency . get ( i ). getValue () * currency . get ( i ). getQuantity ();
         if ( checkChange % currency . get ( i ). getValue () == 0   &&  amountPerCurency >= checkChange  &&  checkChange  !=   0.0 ){
            
            state = true ;
             int  printControl =    ( int )   (( int )  checkChange / currency . get ( i ). getValue ());
             for ( int  j = 0 ;  j < printControl ; j ++ ){
                 System . out . print ( currency . get ( i ). available ());
             }
            
             double  remainingCurrencyAmount = amountPerCurency - checkChange ;
            currency . get ( i ). setQuantity (( int )   ( remainingCurrencyAmount / currency . get ( i ). getValue ()));
            checkChange = 0.0 ;
            
         }
        
        
     }
    
    
    
    
     return  state ;
}


/**
 * record currency added after a purchase
 *  @param  count
 */

public   void  recordCurrencyAdded ( List < Integer >  count ){
     for ( int  i = 0 ;  i < count . size (); i ++ ){
        
        currency . get ( count . get ( i )). setQuantity ( currency . get ( count . get ( i )). getQuantity () + 1 );
     }
    
}


}

VendingMachine/src/vending/InventoryItem.java

VendingMachine/src/vending/InventoryItem.java

package  vending ;

import  java . util . ArrayList ;
import  java . util . List ;


/**
 *
 *  @author  DCCLXXVII
 *Hanldes the collection of items
 */
public   class   InventoryItem   {

     private   List < Item >  items ;

     public   InventoryItem (){

        items =   new   ArrayList <> ();
     }

     public   void  addItem ( Item  item ){

        items . add ( item );
     }


     /**
     *
     *  @return  itemsList This is the list of all items loaded from the txt file
     */
     public   String  printInventory (){
         String  inventory = "" ;

         for ( int  i = 0 ; i < items . size (); i ++ ){

            inventory  +=  i + ": " +  items . get ( i ). toString ();
         }

         return  inventory ;
     }

     /**
     *
     *  @return  itemsList size
     */
     public   int  getInventorySize (){

         return  items . size ();
     }

     /**
     *
     *  @return  itemsList in record form
     */
     public   String  getCurrentItemsRecord (){
         String  inventory = "" ;

                 for ( int  i = 0 ; i < items . size (); i ++ ){

                    inventory  +=  items . get ( i ). record () + "\n" ;
                 }

                 return  inventory ;

     }


     /**
     *
     *  @param  itemNo
     *  @return  price of a single item
     */
     public   double  getItemPrice ( int  itemNo ){
         return  items . get ( itemNo ). getPrice ();
     }

     public   void  recordStockSold ( int  itemNO ){
        items . get ( itemNO ). setQuantity ( items . get ( itemNO ). getQuantity () - 1 );
     }

}

VendingMachine/src/vending/Item.class

package vending;
public synchronized class Item {
    private String name;
    private String container;
    private String unit;
    private double size;
    private int quantity;
    private double price;
    public void Item();
    public String getName();
    public void setName(String);
    public String getContainer();
    public void setContainer(String);
    public String getUnit();
    public void setUnit(String);
    public double getSize();
    public void setSize(double);
    public int getQuantity();
    public void setQuantity(int);
    public double getPrice();
    public void setPrice(double);
    public String toString();
    public String record();
}

VendingMachine/src/vending/Item.java

VendingMachine/src/vending/Item.java

package  vending ;

/**
 *
 *  @author  DCCLXXVII
 *Handles item objects
 */
public   class   Item   {

     private    String  name ;
     private    String  container ;
     private   String  unit ;
     private   double  size ;
     private    int  quantity ;
     private   double  price ;


     public   Item (){

     }
     /**
     * setters
     *  @param  fieldValue
     *
     * getters
     *  @return  fieldValue
     */
     public   String  getName ()   {
         return  name ;
     }
     public   void  setName ( String  name )   {
         this . name  =  name ;
     }
     public   String  getContainer ()   {
         return  container ;
     }
     public   void  setContainer ( String  container )   {
         this . container  =  container ;
     }
     public   String  getUnit ()   {
         return  unit ;
     }
     public   void  setUnit ( String  unit )   {
         this . unit  =  unit ;
     }
     public   double  getSize ()   {
         return  size ;
     }
     public   void  setSize ( double  size )   {
         this . size  =  size ;
     }
     public   int  getQuantity ()   {
         return  quantity ;
     }
     public   void  setQuantity ( int  quantity )   {
         this . quantity  =  quantity ;
     }


     public   double  getPrice ()   {
         return  price ;
     }
     public   void  setPrice ( double  price )   {
         this . price  =  price ;
     }

     /**
     *  @return  classFieldsVlaues
     */
     public   String  toString (){

         return   "InventoryItem: Name: " + name + " Price: "   + price +
                 " Container: " + container +   " Unit: " +  unit  + " Size:  " +
                size +   " Quantity: " + quantity  + " IN STOCK \n" ;
     }

     /**
     *  @return  classFieldsVlaues
     */

     public   String  record (){
         return  name  + "," + price + "," + container + "," + unit + "," + size + "," + quantity ;
     }





}

VendingMachine/src/vending/Main.java

VendingMachine/src/vending/Main.java

package  vending ;

/**
 * 
 *  @author  DCCLXXVII
 * this is the main class where the program execution is 
 * called from
 *
 */

public   class   Main   {

     public   static   void  main ( String []  args )   {
    
        
         VendingMachine  machine  =   new   VendingMachine ();
      
       
            /**
            * loadInventory from a text file
            */
            machine . loadInventory ( "inventory.txt" );
            
             /**
             * loadCurrencyHandler from a text file
             */
            
            machine . loadCurrencyHandler ( "currencyHandler.txt" );
            
             /**
             * loadCurencyInventory from a text file
             */
            
            machine . loadCurrencyInventory ( "currencyInventory.txt" );
            
             /**
             * list commands option
             */
          machine . listCommands ();
          
           /**
           * process command input
           */
            machine . loadCommands ();
            
           
            
         
           
        
       
     }
}

VendingMachine/src/vending/Purchase.java

VendingMachine/src/vending/Purchase.java

package  vending ;

import  java . io . File ;
import  java . io . FileWriter ;
import  java . io . IOException ;
import  java . io . PrintWriter ;

/**
 * 
 *  @author  DCCLXXVII
 * 
 * This class updates the test files of both currency and items
 *
 */

public   class   Purchase {
    
    
    
    
    
     /**
     * upadte the items after a successfull purchase
     *  @param  filepath
     *  @param  items
     */
    
     public   static   void  updateItemsFile ( String  filepath ,   InventoryItem  items ){
        
         try {
            
            
             File  file =   new   File ( filepath );
             PrintWriter  out =   new   PrintWriter (
                     new   FileWriter ( file , false ));
                out . println (  items . getCurrentItemsRecord ());
            out . close ();
        
            
            
            
            
             }
             catch   ( IOException  e ){
                
                 System . out . println ( e . getMessage ());
             }
                
     }
    
     /**
     * update currency file after a successful purchase
     *  @param  filepath
     *  @param  currency
     */
    
     public    static   void  updateCurrencyFile ( String  filepath ,   CurrencyInventory  currency ){
        
try {
            
            
             File  file =   new   File ( filepath );
             PrintWriter  out =   new   PrintWriter (
                     new   FileWriter ( file , false ));
                out . println (  currency . getCurrencyInventory ());
            out . close ();
        
            
            
            
            
             }
             catch   ( IOException  e ){
                
                 System . out . println ( e . getMessage ());
             }
                
                
             }

}

VendingMachine/src/vending/VendingMachine.java

VendingMachine/src/vending/VendingMachine.java

package  vending ;

import  java . io . BufferedReader ;
import  java . io . File ;
import  java . io . FileReader ;
import  java . io . IOException ;
import  java . util . ArrayList ;
import  java . util . List ;
import  java . util . Scanner ;

/**
 *
 *  @author  DCCLXXVII
 * this is the vens=ding machine class whic does all the functionality
 * of a vending machine
 *
 */
public   class   VendingMachine   {
     private    InventoryItem  inventory ;
     private    CurrencyHandler  currencyhandler ;
     private    CurrencyInventory  currencyinventory ;

     /**
     * constructor
     */
     public   VendingMachine ()   {
        inventory =   new   InventoryItem ();
        currencyhandler =   new   CurrencyHandler ();
        currencyinventory =   new   CurrencyInventory ();


     }

     /**
     * loadInventory from a text file
     */
     public   void  loadInventory ( String  filePath ){

         try {
         File  file =   new   File ( filePath );

         FileReader  fileReader  =   new   FileReader ( "/Users/justin/Documents/JHU/VendingMachine/inventory.txt" );
         BufferedReader  bufferedReader  =   new   BufferedReader ( fileReader );
                     String  temp  = null ;

                     String []  list ;

                     while   (( temp  =  bufferedReader . readLine ())   !=   null )   {

                        list =  temp . split ( "," );
                         if ( list . length  > 1 ){
                         Item  item =   new   Item ();
                        item . setName ( list [ 0 ]);
                        item . setPrice (   Double . parseDouble (  list [ 1 ]));
                        item . setContainer ( list [ 2 ]);
                        item . setUnit ( list [ 3 ]);
                        item . setSize ( Double . parseDouble ( list [ 4 ]));
                        item . setQuantity ( Integer . parseInt ( list [ 5 ]));


                        inventory . addItem ( item );
                         }

                     }

         }
         catch   ( IOException  e ){

             System . out . println ( e . getMessage ());
         }
     }


     /**
     * loadCurencyInventory from a text file
     */

public   void  loadCurrencyInventory ( String  filePath ){

         try {
         File  file =   new   File ( "/Users/justin/Documents/JHU/VendingMachine/currencyInventory.txt" );
         //File file = new File("c:/example/filedata.txt");
         FileReader  fileReader  =   new   FileReader ( file );
         BufferedReader  bufferedReader  =   new   BufferedReader ( fileReader );
                     String  temp  = null ;

                     String []  list ;

                     while   (( temp  =  bufferedReader . readLine ())   !=   null )   {

                        list =  temp . split ( "," );
                         if ( list . length  > 1 ){
                         Currency  currency =   new   Currency ();
                        currency . setCurencyinventorytype ( list [ 0 ]);
                        currency . setName ( list [ 1 ]);
                        currency . setValue ( Double . parseDouble ( list [ 2 ]));
                        currency . setSize ( list [ 3 ]);
                        currency . setMaterial ( list [ 4 ]);
                        currency . setQuantity ( Integer . parseInt ( list [ 5 ]));



                        currencyinventory . addItem ( currency );
                         }

                     }

         }
         catch   ( IOException  e ){

             System . out . println ( e . getMessage ());
         }
     }


     /**
     *
     *  @return   itemslist of all items in stock
     */
     public   String  printInventoryLoad (){

         return  inventory . printInventory ();
     }

     /**
     *
     *  @return  Currencylist of all currency available
     */
  public   String  printCurrencyInventoryLoad (){

         return  currencyhandler . toString () + "\n" +  currencyinventory . printInventory ();
     }


  /**
  * loadCurrencyHandler from a text file
  */
     public   void  loadCurrencyHandler ( String  filePath ){

         try {
         File  file =   new   File ( "/Users/justin/Documents/JHU/VendingMachine/currencyInventory.txt" );
         //File file = new File("c:/example/filedata.txt");
         FileReader  fileReader  =   new   FileReader ( file );
         BufferedReader  bufferedReader  =   new   BufferedReader ( fileReader );
                     String  temp  = null ;

                     String []  list ;

                     while   (( temp  =  bufferedReader . readLine ())   !=   null )   {

                        list =  temp . split ( "," );

                        currencyhandler . setHandler ( list [ 0 ]);
                        currencyhandler . setPaper ( Boolean . parseBoolean ( list [ 1 ]));
                        currencyhandler . setCoins ( Boolean . parseBoolean ( list [ 2 ]));
                        currencyhandler . setPaperchange ( Boolean . parseBoolean ( list [ 3 ]));
                        currencyhandler . setCoinchange ( Boolean . parseBoolean ( list [ 4 ]));

                     }

         }
         catch   ( IOException  e ){

             System . out . println ( e . getMessage ());
         }



     }


     /**
     * process the command input choice
     */
     public   void  loadCommands (){




         int  navvalue = readInput ();

         if ( navvalue  ==   0 ){
            listCommands ();
            loadCommands ();

         } else   if ( navvalue  ==   1   ){
          System . out . println ( printInventoryLoad ());
          System . out . print ( "Command:" );
         loadCommands ();

         }
         else   if ( navvalue  ==   2 ){

             System . out . println ( printCurrencyInventoryLoad ());
             System . out . print ( "Command:" );
             loadCommands ();
                 }
         else   if ( navvalue  == 3 ){



             int  itemNo =  itemNumberInput ();

             int  typesOfCurrency =  typesOfCurrencyInput ();


            makePurchase ( itemNo ,  typesOfCurrency );

             System . out . print ( "Command:" );
             loadCommands ();

                 }
         else   if ( navvalue  ==   - 1   ){
             System . out . println ( "Goodbye!" );
             System . exit ( 0 );

                 }
         else {

             System . out . println ( "Invalid input, please try agian" );
            listCommands ();
            loadCommands ();

         }



     }


    /**
    *  process purchase
    *  @param  itemNo
    *  @param  typesOfCurrency
    */
public    void  makePurchase ( int  itemNo ,   int  typesOfCurrency ){

         Double  totalAmount = 0.0 ;
         List < Integer >  currencyPickCount =   new   ArrayList <> ();
         for ( int  i = 0 ;  i < typesOfCurrency ; i ++ ){

             System . out . println ( currencyinventory . availableInventory ());
             int  currencyPicked =   CurrencyTypeInput ();

            totalAmount +=  currencyinventory . getCurrencyValue ( currencyPicked );
            currencyPickCount . add ( currencyPicked );

         }


         if (  inventory . getItemPrice ( itemNo )   <=  totalAmount ){
                 double  change =  totalAmount - inventory . getItemPrice ( itemNo );

             System . out . printf ( "Change :%4f \n" , change );

             if ( currencyinventory . printChange ( change )){
            currencyinventory . recordCurrencyAdded ( currencyPickCount );

            inventory . recordStockSold ( itemNo );


             Purchase . updateItemsFile ( "inventory.txt" ,  inventory );

             Purchase . updateCurrencyFile ( "currencyInventory.txt" ,  currencyinventory );




             System . out . println ( "Item Purchased successful" );
             } else {
                 System . out . println ( "Vending machine out of cash" );
             }

         } else {
             System . out . println ( "Insufficient amount" );
         }


     }


     /**
     *
     *  @return  itemNo The item number of the item selected
     */
     public   int  itemNumberInput (){
         System . out . print ( "item #:" );

         int  itemNotest =   readInput ();

         if ( itemNotest ==   7   ||  itemNotest  >=  inventory . getInventorySize ()   ||  itemNotest  < 0 ){
             System . out . println ( "Invalid item input, please try agian" );
            itemNumberInput ();

         }

         return  itemNotest ;
     }


     /**
     *
     *  @return  currencyCount This is the currency count  selected
     */
     public   int  typesOfCurrencyInput (){
         System . out . print ( "How many differnt currency items?:" );

         int  typestest =   readInput ();

         if ( typestest ==   7   ||  typestest  >=  currencyinventory . getInventorySize ()   ||  typestest  <= 0 ){
             System . out . println ( "Invalid currency input, please try agian" );
            typesOfCurrencyInput ();

         }

         return  typestest ;
     }

     /**
     *
     *  @return  currencySelected This is the type of curency selected
     */
     public   int   CurrencyTypeInput (){
         System . out . print ( "Currency type:" );

         int  typestest =   readInput ();

         if ( typestest ==   7   ||  typestest  >=  currencyinventory . getInventorySize ()   ||  typestest  < 0 ){
             System . out . println ( "Invalid currency input, please try agian" );
            typesOfCurrencyInput ();

         }

         return  typestest ;
     }


     /**
     * list the commands on the console
     */
     public   void  listCommands (){

         System . out . println ( ""
                 +   "Available Commands \n" );

         System . out . printf ( "Show Commands:%8s\n"
                 +   "Display Inventory:%4s\n"
                 +   "Display Currency:%5s\n"
                 +   "Purchase Item:%8s\n"
                 +   "Exit:%17s\n" , "0" , "1" , "2" , "3" , "-1" );
         System . out . print ( "Command:" );
     }

     /**
     *
     *  @return  inputValue This is the command value inputed
     */
     public   int  readInput (){
         int  inputValue = 7 ;
         Scanner  in  =   new   Scanner ( System . in );
         String  input =  in . nextLine ();
         try {
         inputValue =   Integer . parseInt ( input );
          return  inputValue ;

         } catch ( NumberFormatException  e ){

             return  inputValue ;
         }




     }











}