Question
Here is my code for my main class. I am receiving the following message: Exception in thread main java.lang.ClassCastException: subshop.Beverage cannot be cast to subshop.Sub
Here is my code for my main class. I am receiving the following message: Exception in thread "main" java.lang.ClassCastException: subshop.Beverage cannot be cast to subshop.Sub at subshop.DisplayGUI.main(DisplayGUI.java:117) What is causing this?
package subshop;
import javax.swing.*;
import java.util.*;
/** * Creates a GUI to provide ordering options to the customer *
@author mikem
*/
class FoodEnum
{
public static enum BevType{Water,Tea,Coke,Sprite,Fanta,Coffee};
public static enum Bevsize{Small,Medium,Large};
public static enum Bread{Italian,Wheat,Asiago_Cheese}; public static enum SubType{Ham_and_Swiss,Turkey_and_Provolone,Tuna,Meatball,Cold_Cut_Combo,Chicken_Parmesan}; public static enum SubSize{six_inch,Footlong}; public static String convertDrink(Integer num) { return BevType.values()[num-1].toString(); } public static String convertDrinkSize(Integer size) { return Bevsize.values()[size-1].toString(); } public static String convertBread(Integer breadChoice) { return Bread.values()[breadChoice-1].toString(); } public static String convertSubType(Integer subChoice) { return SubType.values()[subChoice-1].toString(); } public static String convertSubSize(Integer sizeChoice) { return SubType.values()[sizeChoice-1].toString(); } } public class DisplayGUI { /** * @param args the command line arguments */ public static void main(String[] args) { // Jframe to display GUI JFrame frame = new JFrame(); Vector v = new Vector(10,1); // prompt the user to enter their name String name = JOptionPane.showInputDialog(frame, " Welcome to ***Famous Favorite Subs***! Please enter your name"); // get the user's input. note that if they press Cancel, 'name' will be null String address = JOptionPane.showInputDialog(frame, "What's your delivery address?"); // get the user's input. note that if they press Cancel, 'name' will be null // prompt for type of beverage from displayed list String beverage,beveragesize; beveragesize = null; Beverage b; // prompt for type of bread String breadType,subType,subSize; subType = null; subSize = null; Sub s; //statement to prompt for multiple items do{ beverage = JOptionPane.showInputDialog(frame, "Select the number for the beverage would you like? 1. Water 2. Tea 3. Coke 4. Sprite 5. Fanta 6. Coffee 7. To continue to next item"); if(!beverage.equals("7")){ // prompt for size of beverage beveragesize = JOptionPane.showInputDialog(frame, "Select the number for the size you would like? 1. Small (12oz.) 2. Medium (21oz.) 3. Large (32oz.)"); } // variable to hold beverage size and type calls Beverage method from Beverage class b = new Beverage(beverage, beveragesize); // add Beverage objects to vector v.addElement(b); }while(!beverage.equals("7")); do{ breadType = JOptionPane.showInputDialog(frame, "Select the number for the type of bread you would like? 1. Italian 2. Wheat 3. Asiago Cheese 4. To continue to next item"); if(!breadType.equals("4")){ // prompt for type of sub subType = JOptionPane.showInputDialog(frame, "Select the number for the type of sub you would like? 1. Ham and Swiss 2. Turkey and Provolone 3. Tuna 4. Meatball 5. Cold Cut Combo 6. Chicken Parmesan 7. To continue to next item"); } if(!subType.equals("7")){ // prompt for sub size subSize = JOptionPane.showInputDialog(frame, "Select the number for the size sub you would like? 1. six inch 2. Footlong 3. To complete your order"); } s = new Sub(breadType, subType, subSize); // variable to hold bread type, subtype, and subsize and calls Sub method from Sub class v.addElement(s); }while(!subSize.equals("3")); for(int i=0;i<=v.capacity();i++) { Beverage b1; b1 = (Beverage)v.elementAt(i); FoodEnum.convertDrink(Integer.parseInt(b1.getType())); FoodEnum.convertDrinkSize(Integer.parseInt(b1.getSize())); Sub s1; s1 = (Sub)v.elementAt(i); //FoodEnum.convertBread(Integer.parseInt(s1.getBread())); FoodEnum.convertSubType(Integer.parseInt(s1.getType())); FoodEnum.convertSubSize(Integer.parseInt(s1.getSize())); //} // Displays order summary to user in well formatted GUI JOptionPane.showMessageDialog(frame, "Thank you for visiting us "+name +" Below is your order Summary: Beverage: "+ b1.getType() +" Beverage Size: "+ b1.getSize()+ " Sub Type: " + s1.getType() + " Sub Size: " + s1.getSize() ); System.exit(0); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started