当用户尝试购买比可用商品更多的商品时,我正在尝试实现OutOfStockException。我不确定我的实现是否正确。您觉得还好吗?

public class OutOfStockException extends Exception {


    public OutOfStockException(){
        super();
    }

    public OutOfStockException(String s){
        super(s);
    }
}


这是我需要测试的课程:

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));

        }

}

最佳答案

实现看起来不错;您不必在异常类中做很多事情。您可能会考虑添加成员,以了解发出请求时缺货的情况,所请求的数量以及有多少库存,以便捕获异常的代码可以访问该信息。例如,这里有一个库存项目代码:

public class OutOfStockException extends Exception {

    private int stockCode;

    public OutOfStockException(int stockCode){
        super();
        this.stockCode = stockCode;
    }

    public OutOfStockException(String s){
        super(s);
        this.stockCode = stockCode;
    }

    public int getStockCode() {
        return this.stockCode;
    }
}


然后,您可以像这样创建一个:

throw new OutOfStockException(StockCodes.WIDGET, "Out of widgets");


但这取决于您,到那时,它就像其他任何东西一样只是类设计。

很多时候,通过这些事情,我只将构造器包括在各个部分中,然后让类本身为基础Exception getMessage消息生成消息。所以:

public class OutOfStockException extends Exception {

    private int stockCode;

    public OutOfStockException(int stockCode){
        super("Out of " + StockCodes.getDescription(stockCode));
        this.stockCode = stockCode;
    }

    public int getStockCode() {
        return this.stockCode;
    }
}

// Throwing one:
throw new OutOfStockException(StockCodes.WIDGETS);


但同样,那只是类设计。

撇开所有这些,这是有点题外话,但在我看来,一件商品缺货是正常现象,不是一个例外。您确定例外确实是正确的建模方法吗?

关于java - Java实现异常处理,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2488109/

10-16 23:29