添加子类来计算销售税?

时间:2013-12-23 03:03:25

标签: java uml calculator sales-tax

这是作业: 对未征税的物品的帐户。您将下面的UML图示为草图,将Tax类拆分为两个子类PercentageTax和NoTax。您有一个计划,并且您的主管批准,因此是时候实施它了。  使用项目SaleEvenBetter作为起点,实现新设计,如上面的UML图所述。验证PercentageTax中的tax方法返回与当前版本的SaleEvenBetter中的getPriceWithTax方法相同的值,而NoTax中的tax方法返回零值。

我正在使用bluej进行编译。类的连接不是问题。我只是想知道子类中的代码是否正确。

到目前为止,这是我原来的课程。

    public class PercentageTax extends Tax
    {
    // instance variables - replace the example below with your own

    private String name;
    private double price;
    private Tax tax;

    /**
     * Constructor for objects of class PercentageTax
     */
    public PercentageTax()
    {
        this.name = name;
        this.price = price;
        tax = new Tax();
    }


    public double getPrice()
    {
        return price;
    }


    /**
     * return the price including salesTax
     */
    public double priceWithTax(double price, String state) {
        return price*tax.tax(price, state);
    }

    public class NoTax extends Tax

    // instance variables - replace the example below with your own
     private double price;
     private Tax tax;

    /**
     * Constructor for objects of class NoTax
     */
    public NoTax()
    {
        // initialise instance variables
        this.price = price;
        tax = new Tax();
    }


    public double Notax() 
    {
        return 0;
    }
     /**
     * return the price including salesTax
     */
    public double priceWithNoTax(double price) {
        return price;
    }
}

原始代码:

    public class SalesLine
    {
    private int number;
    private Item item;


    /**
     * Constructor for objects of class SalesLine
     */
    public SalesLine(int number, Item item)
    {
        this.item = item;
        this.number = number;
    }

    /**
     * returns the total sum of the saleline
     */
    public double price(String state)
    {

        return number*item.priceWithTax(state);

    }

    public class Sale
    {
    private ArrayList<SalesLine> saleLines;
    private String state;
    private double price;


    /**
     * Constructor for objects of class SaleBad
     */
    public Sale(String state)
    {
        saleLines = new ArrayList<SalesLine>();
    }
    public Sale()
    {
        saleLines = new ArrayList<SalesLine>();
    }
    /**
     * add an item to the sale
     */
    public void addItem(int number, Item i)
    {
        saleLines.add(new SalesLine(number,i));
    }

    /**
     * return the total of the sale including sales tax for the state
     */
    public double total() 
    {
        double sum = 0;
        for (SalesLine s: saleLines) {
            sum = sum + s.price(state);
        }
        return sum;
    }
}

    public class Item
    {
    private String name;
    private double price;
    private Tax tax;

    /**
     * Constructor for objects of class Item
     */
    public Item(String name, double price)
    {
        this.name = name;
        this.price = price;
        tax = new Tax();
    }

    public double getPrice()
    {
        return price;
    }

    public void setPrice(double newPrice)
    {
        price = newPrice;
    }

    public String getName()
    {
        return name;
    }

    public void setPrice(String newName)
    {
        name = newName;
    }

    /**
     * return the price including salesTax
     */
    public double priceWithTax(String state) {
        return price*tax.tax(price, state);
    }

}

    public class Tax
    {

    /**
     * Constructor for objects of class Tax
     */
    public Tax()
    {

    }

    /**
     * returns the tax for the given amount of money. If the state is unknown, zero is returned
     */
    public double tax(double price, String state) {
        if (state.equals( "NY")) return price * 0.05; else
        if (state.equals( "AL")) return price * 0.07; else
        if (state.equals( "AK")) return price * 0.04; else
        if (state.equals( "AS")) return price * 0.12; else
        if (state.equals( "AZ")) return price * 0.13; else
        if (state.equals( "AR")) return price * 0.08; else
        if (state.equals( "CA")) return price * 0.04; else
        if (state.equals( "CO")) return price * 0.05; else
        if (state.equals( "CT")) return price * 0.07; else
        if (state.equals( "DE")) return price * 0.08; else
        if (state.equals( "DC")) return price * 0.09; else
        if (state.equals( "FM")) return price * 0.17; else
        if (state.equals( "FL")) return price * 0.05; else
        if (state.equals( "GA")) return price * 0.05; else
        if (state.equals( "GU")) return price * 0.05; else
        if (state.equals( "HI")) return price * 0.05; else
        if (state.equals( "ID")) return price * 0.065; else
        if (state.equals( "IL")) return price * 0.05; else
        if (state.equals( "IN")) return price * 0.05; else
        if (state.equals( "IA")) return price * 0.07; else
        if (state.equals( "KS")) return price * 0.07; else
        if (state.equals( "KY")) return price * 0.05; else
        if (state.equals( "LA")) return price * 0.05; else
        if (state.equals( "ME")) return price * 0.098; else
        if (state.equals( "MH")) return price * 0.06; else
        if (state.equals( "MD")) return price * 0.05; else
        if (state.equals( "MA")) return price * 0.04; else
        if (state.equals( "MI")) return price * 0.05; else
        if (state.equals( "MN")) return price * 0.08; else
        if (state.equals( "MS")) return price * 0.05; else
        if (state.equals( "MO")) return price * 0.18; else
        if (state.equals( "MT")) return price * 0.25; else
        if (state.equals( "NE")) return price * 0.05; else
        if (state.equals( "NV")) return price * 0.05; else
        if (state.equals( "NH")) return price * 0.05; else
        if (state.equals( "NJ")) return price * 0.08; else
        if (state.equals( "NM")) return price * 0.07; else
        if (state.equals( "NY")) return price * 0.03; else
        if (state.equals( "NC")) return price * 0.05; else
        if (state.equals( "ND")) return price * 0.06; else
        if (state.equals( "MP")) return price * 0.05; else
        if (state.equals( "OH")) return price * 0.05; else
        if (state.equals( "OK")) return price * 0.09; else
        if (state.equals( "OR")) return price * 0.05; else
        if (state.equals( "PW")) return price * 0.15; else
        if (state.equals( "PA")) return price * 0.05; else
        if (state.equals( "PR")) return price * 0.05; else
        if (state.equals( "RI")) return price * 0.04; else
        if (state.equals( "SC")) return price * 0.05; else
        if (state.equals( "SD")) return price * 0.06; else
        if (state.equals( "TN")) return price * 0.05; else
        if (state.equals( "TX")) return price * 0.07; else
        if (state.equals( "UT")) return price * 0.05; else
        if (state.equals( "VT")) return price * 0.08; else
        if (state.equals( "VI")) return price * 0.05; else
        if (state.equals( "VA")) return price * 0.05; else
        if (state.equals( "WA")) return price * 0.04; else
        if (state.equals( "WV")) return price * 0.06; else
        if (state.equals( "WI")) return price * 0.05; else
        if (state.equals( "WY")) return price * 0.08; 
        // if the state was not found then the prices is returned
        return 0;
    }

}

1 个答案:

答案 0 :(得分:1)

我理解了你给出的代码。这是我的实现,请原谅我破坏你的类名:

class Tax
{

    public double getTax()
    {
        //noTax implementation.
    } 
    public double getTax(String state, double percentageTax)
    {
        //With tax implementation
    }
}


Class WithOutTax extends Tax
{
    //use your no tax method. 
    //use that value to do further manipulation
}

class WithTax extends Tax
{
    //use your with tax method.
    //use that value to do further manipulation
}