Question: Need this program to handle invalid input ( Letters specifically ) Thank you import java.util.Scanner; public class PocketMain { public static void main(String args[]) throws
Need this program to handle invalid input (Letters specifically)
Thank you
import java.util.Scanner;
public class PocketMain {
public static void main(String args[]) throws NoSuchCoin
{
Pocket pocket = new Pocket(); //loop goes on until valid input
boolean exit =false;
while(!exit)
{
System.out.println("Choose one operation: "
+ "1.Add a coin (must specify type) "
+ "2.Remove a coin (must specify type) "
+ "3.Display a coin count "
+ "4.Display a total value in the pocket "
+ "5.exit the program");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
switch(choice){
case 1:
System.out.println("Enter type of coin "
+ "1.Dime "
+ "2.Nickle "
+ "3.Penny "
+ "4.Quarter");
int selection =sc.nextInt();
switch(selection){ //case executes depending on user's choice
case 1:pocket.addCoin( new Dime());
System.out.println("dime added to pocket successfully");
break;
case 2:pocket.addCoin(new Nickle());
System.out.println("Nickle added to pocket successfully");
break;
case 3:pocket.addCoin(new Penny());
System.out.println("Penny added to pocket successfully");
break;
case 4:pocket.addCoin(new Quarter());
System.out.println("Quarter added to pocket successfully");
break;
default:throw new NoSuchCoin();
}
break;
case 2:
System.out.println("Enter type of coin "
+ "1.Dime "
+ "2.Nickle "
+ "3.Penny "
+ "4.Quarter");
selection =sc.nextInt();
switch(selection){
case 1:pocket.removeCoin(Coin.DIME);
System.out.println("Dime removed from pocket successfully");
break;
case 2:pocket.removeCoin(Coin.NICKLE);
System.out.println("Nickle removed from pocket successfully");
break;
case 3:pocket.removeCoin(Coin.PENNY);
System.out.println("Penny removed from pocket successfully");
break;
case 4:pocket.removeCoin(Coin.QUARTER);
System.out.println("Quarter removed from pocket successfully");
break;
default:throw new NoSuchCoin();
}
break;
case 3:
System.out.println("Enter type of coin "
+ "1.Dime "
+ "2.Nickle "
+ "3.Penny "
+ "4.Quarter");
selection =sc.nextInt();
switch(selection){
case 1:System.out.println("Dimes:"+pocket.countCoins(Coin.DIME));
break;
case 2:System.out.println("Nickles:"+pocket.countCoins(Coin.NICKLE));
break;
case 3:System.out.println("Pennys:"+pocket.countCoins(Coin.PENNY));
break;
case 4:System.out.println("Quarters:"+pocket.countCoins(Coin.QUARTER));
break;
default:throw new NoSuchCoin();
}
break;
case 4:
System.out.println("Total value of the Pocket:"+pocket.totalValue());
break;
case 5:
exit=true;
break;
default : System.out.println("Invalid input exiting program");
exit=true;
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
