The source code for TestVendingMachinejava is showing an err

The source code for TestVendingMachine.java is showing an error when compiling, which has to do with the exception VendingMachineException never thrown in body of corresponding try statement, catch(VendingMachineException vme). I cannot figure out what I am doing wrong and any help would be greatly appreciated. I will provide the three seperate source codes (TestVendingMachine.java, VendingMachineException.java, and VendingMachine.java) that should all work together to recreate the sample output provided.

TestVendingMachine.java

import javax.swing.*;
import java.io.Console;

public class TestVendingMachine
{
   public static void main(String[] args) throws VendingMachineException
   {
       int deposit = 0;
       int change = 0;
       String choice = null;
        Console console = System.console();

        if (console != null)
        {
           // create VendingMachine object
           console.format(\"Beginning Vending Machine Simulation.%n%n\");

           VendingMachine vm1 = new VendingMachine();           // create a new vending machine object
           console.format(\"%s%n%n\",vm1);

           deposit = Integer.parseInt(console.readLine(\"Enter money as integer (e.g., 50) or 0 to exit: \"));

           while(deposit > 0)
           {
               try
               {
                   vm1.depositMoney(deposit);
                   choice = console.readLine(\"Please make a selection (e.g., A1): \");

                   change = vm1.buyItem(choice);

                   console.format(\"%nDispensing ...%n%n\");
                   console.format(\"%s%n\",vm1);
                   console.format(\"Change returned = %d%n%n\",change);
               }
               catch(VendingMachineException vme)
               {
                   System.out.println(\"Error: \"+vme.getMessage());
               }
               deposit = Integer.parseInt(console.readLine(\"\ Enter money as integer (e.g., 50) or 0 to exit: \"));
           }

           if(vm1.getDeposit() > 0)
               console.format(\"%nReturning change: $0.%d%n.\", vm1.getChange());

           console.format(\"%nEnd of program.\");
        }
        else
            System.err.println(\"No console.\");
   }
}

VendingMachine.java

import java.util.Random;
import java.util.regex.*;

public class VendingMachine
{
   // soft drinks candy bars snacks
   private String[][] items = { {\"Pepsi\",    \"Snickers\",    \"Potato Chips\"},
                                {\"Diet Pepsi\", \"Resse\'s Cup\", \"Ruffles\"},
                                {\"Dr. Pepper\", \"M&M chocolate\",\"Fritos\"},
                                {\"Diet Dr. P\", \"Peanut M&Ms\", \"Doritos\"},
                                {\"Sprite\",      \"Twisters\",    \"Almonds\"},
                                {\"Diet Sprite\",\"Hershey\'s\", \"Peanuts\"} };

   private int[][] prices = { {75,65,30},
                              {80,60,35},
                              {70,55,40},
                              {75,60,45},
                              {60,45,50},
                              {65,40,35} };

   private int[][] quantity = { {3,1,0},
                                {4,6,5},
                                {7,5,4},
                                {5,0,5},
                                {0,4,0},
                                {6,4,3} };

   private int depositedAmt = 0;
   private boolean haveValidSelection = false;
   private int selectedRow = 0;
   private char selectedCol = \'A\';

   // default constructor
   public VendingMachine()
   {
   }

   public int buyItem(String selection)
   {
      Pattern validChars = Pattern.compile(\"[^A-Ca-c1-6]+\");
      Matcher m1 = validChars.matcher(selection);

      Pattern selectionRange = Pattern.compile(\"[1-6]+\");
      Matcher m2;
      boolean illegal = false;
      int change = -1;

      if(depositedAmt > 0)
      {
         if(selection.length() != 2)
            illegal = true;

         if(!illegal)
            illegal = m1.find();

         if(!illegal)
            if( Character.getNumericValue(selection.charAt(1)) < 1)
               illegal = true;

         m2 = selectionRange.matcher(selection.substring(0,1));
         if(!illegal)
            illegal = m2.find();

         haveValidSelection = !illegal;

         if(haveValidSelection)
         {
            selectedRow = Character.getNumericValue(selection.charAt(1) - 1);
            selectedCol = Character.toUpperCase(selection.charAt(0)); // allows for upper- or lowercase entry
            int col = selectedCol - 65;

            if(quantity[selectedRow][col] > 0)
               if( depositedAmt >= prices[selectedRow][col])
               {
                  depositedAmt -= prices[selectedRow][col];
                  --quantity[selectedRow][col];
                  change = getChange();
               }
               else
                  System.out.print(String.format(\"Insufficient funds for item: %s%nCost: $0.%d, Deposited: $%.2f\",
                                                items[selectedRow][col], prices[selectedRow][col], getDeposit()/100.0));
            else
                  System.out.print(String.format(\"Out of stock on this item: %s%nCost: $0.%d\",
                                                items[selectedRow][col], prices[selectedRow][col]));
         }
         else
            System.out.print(\"Invalid item selection: \"+selection);
      }
      else
         System.out.print(\"Please enter proper funds before making selection: \"+selection);

      return change;
   }

   public void depositMoney(int payment)
   {
      if(payment > 0)
         depositedAmt += payment;
   }

   public int getChange()
   {
      int returned = depositedAmt;
      depositedAmt = 0;
      return returned;
   }

   public int getDeposit()
   {
      return depositedAmt;
   }

   // returns String representation of object
   public String toString()
   {
      // display items, quantity on hand, and prices
      StringBuffer display = new StringBuffer();

      // holds display contents
      display.append(\" Price A (in stock)    Price B (in stock)      Price C (in stock)\ \");
      display.append(\"1 $0.\"+prices[0][0]+\" Pepsi(\"+quantity[0][0]+\")        $0.\"
                     +prices[0][1]       +\" Snickers(\"+quantity[0][1]
                     +\")       $0.\"+prices[0][2]+\" Potato Chips(\"+quantity[0][2]+\")\ \");
      display.append(\"2 $0.\"+prices[1][0]+\" Diet Pepsi(\"+quantity[1][0]
                     +\")   $0.\"+prices[1][1]+\" Resse\'s Cup(\"+quantity[1][1]
                     +\")    $0.\"+prices[1][2]+\" Ruffles(\"+quantity[1][2]+\")\ \");
      display.append(\"3 $0.\"+prices[2][0]+\" Dr. Pepper(\"+quantity[2][0]
                     +\")   $0.\"+prices[2][1]+\" M&Ms (reg)(\"+quantity[2][1]
                     +\")     $0.\"+prices[2][2]+\" Fritos(\"+quantity[2][2]+\")\ \");
      display.append(\"4 $0.\"+prices[3][0]+\" Diet Dr. P(\"+quantity[3][0]
                     +\")   $0.\"+prices[3][1]+\" Peanut M&Ms(\"+quantity[3][1]
                     +\")    $0.\"+prices[3][2]+\" Doritos(\"+quantity[3][2]+\")\ \");
      display.append(\"5 $0.\"+prices[4][0]+\" Sprite(\"+quantity[4][0]
                     +\")       $0.\"+prices[4][1]+\" Twisters(\"+quantity[4][1]
                     +\")       $0.\"+prices[4][2]+\" Almonds(\"+quantity[4][2]+\")\ \");
      display.append(\"6 $0.\"+prices[5][0]+\" Diet Sprite(\"+quantity[5][0]
                     +\") $0.\"+prices[5][1]+\" Hershey\'s(\"+quantity[5][1]
                     +\")      $0.\"+prices[5][2]+\" Peanuts(\"+quantity[5][2]+\")\ \");

      return display.toString();
   }
}

VendingMachineException.java

public class VendingMachineException extends Exception
{
   public VendingMachineException(String ex)
   {
       super(ex);
   }
}

(Sample output that program should match)

Solution

Since you are not throwing this exception in your VendingMachine class code, you can\'t put in try catch as it will never occur.

For the error messages you need to throw it like: for no console

import javax.swing.*;
import java.io.Console;
public class TestVendingMachine
{
   public static void main(String[] args) throws VendingMachineException
   {
       int deposit = 0;
       int change = 0;
       String choice = null;
       Console console = System.console();
       if (console != null)
       {
           // create VendingMachine object
           console.format(\"Beginning Vending Machine Simulation.%n%n\");
           VendingMachine vm1 = new VendingMachine(); // create a new vending machine object
           console.format(\"%s%n%n\",vm1);
           deposit = Integer.parseInt(console.readLine(\"Enter money as integer (e.g., 50) or 0 to exit: \"));
           while(deposit > 0)
           {

               vm1.depositMoney(deposit);
               choice = console.readLine(\"Please make a selection (e.g., A1): \");
               change = vm1.buyItem(choice);
               console.format(\"%nDispensing ...%n%n\");
               console.format(\"%s%n\",vm1);
               console.format(\"Change returned = %d%n%n\",change);

               deposit = Integer.parseInt(console.readLine(\"\ Enter money as integer (e.g., 50) or 0 to exit: \"));
           }
           if(vm1.getDeposit() > 0)
               console.format(\"%nReturning change: $0.%d%n.\", vm1.getChange());
           console.format(\"%nEnd of program.\");
       }
       else
           throw new VendingMachineException(\"No Console.\");
   }
}

The source code for TestVendingMachine.java is showing an error when compiling, which has to do with the exception VendingMachineException never thrown in body
The source code for TestVendingMachine.java is showing an error when compiling, which has to do with the exception VendingMachineException never thrown in body
The source code for TestVendingMachine.java is showing an error when compiling, which has to do with the exception VendingMachineException never thrown in body
The source code for TestVendingMachine.java is showing an error when compiling, which has to do with the exception VendingMachineException never thrown in body
The source code for TestVendingMachine.java is showing an error when compiling, which has to do with the exception VendingMachineException never thrown in body

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site