Java implementing Exception Handling
- by user69514
I am trying to implement an OutOfStockException for when the user attempts to buy more items than there are available. I'm not sure if my implementation is correct. Does this look OK to you?
public class OutOfStockException extends Exception {
public OutOfStockException(){
super();
}
public OutOfStockException(String s){
super(s);
}
}
This is the class where I need to test it:
import javax.swing.JOptionPane;
public class SwimItems {
static final int MAX = 100;
public static void main (String [] args)
{
Item [] items = new Item[MAX];
int numItems;
numItems = fillFreebies(items);
numItems += fillTaxable(items,numItems);
numItems += fillNonTaxable(items,numItems);
sellStuff(items, numItems);
}
private static int num(String which) {
int n = 0;
do {
try{
n=Integer.parseInt(JOptionPane.showInputDialog("Enter number of "+which+" items to add to stock:"));
}
catch(NumberFormatException nfe){
System.out.println("Number Format Exception in num method");
}
} while (n < 1 || n > MAX/3);
return n;
}
private static int fillFreebies(Item [] list)
{
int n = num("FREEBIES");
for (int i = 0; i < n; i++)
try{
list [i] = new Item(JOptionPane.showInputDialog("What freebie item will you give away?"),
Integer.parseInt(JOptionPane.showInputDialog("How many do you have?")));
}
catch(NumberFormatException nfe){
System.out.println("Number Format Exception in fillFreebies method");
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Array Index Out Of Bounds Exception in fillFreebies method");
}
return n;
}
private static int fillTaxable(Item [] list, int number)
{
int n = num("Taxable Items");
for (int i = number ; i < n + number; i++)
try{
list [i] = new TaxableItem(JOptionPane.showInputDialog("What taxable item will you sell?"),
Double.parseDouble(JOptionPane.showInputDialog("How much will you charge (not including tax) for each?")),
Integer.parseInt(JOptionPane.showInputDialog("How many do you have?")));
}
catch(NumberFormatException nfe){
System.out.println("Number Format Exception in fillTaxable method");
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Array Index Out Of Bounds Exception in fillTaxable method");
}
return n;
}
private static int fillNonTaxable(Item [] list, int number)
{
int n = num("Non-Taxable Items");
for (int i = number ; i < n + number; i++)
try{
list [i] = new SaleItem(JOptionPane.showInputDialog("What non-taxable item will you sell?"),
Double.parseDouble(JOptionPane.showInputDialog("How much will you charge for each?")),
Integer.parseInt(JOptionPane.showInputDialog("How many do you have?")));
}
catch(NumberFormatException nfe){
System.out.println("Number Format Exception in fillNonTaxable method");
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Array Index Out Of Bounds Exception in fillNonTaxable method");
}
return n;
}
private static String listEm(Item [] all, int n, boolean numInc)
{
String list = "Items: ";
for (int i = 0; i < n; i++)
{
try{
list += "\n"+ (i+1)+". "+all[i].toString() ;
if (all[i] instanceof SaleItem) list += " (taxable) ";
if (numInc) list += " (Number in Stock: "+all[i].getNum()+")";
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Array Index Out Of Bounds Exception in listEm method");
}
catch(NullPointerException npe){
System.out.println("Null Pointer Exception in listEm method");
}
}
return list;
}
private static void sellStuff (Item [] list, int n) {
int choice;
do {
try{
choice = Integer.parseInt(JOptionPane.showInputDialog("Enter item of choice: "+listEm(list, n, false)));
}
catch(NumberFormatException nfe){
System.out.println("Number Format Exception in sellStuff method");
}
}while (JOptionPane.showConfirmDialog(null, "Another customer?")==JOptionPane.YES_OPTION);
JOptionPane.showMessageDialog(null, "Remaining "+listEm(list, n, true));
}
}