- /*
- * %W% %E% Rongzhi Liu
- *
- * Copyright (c) 2008 Rongzhi-Liu SUN YAT-SEN UNIVERSITY. All Rights Reserved.
- *
- * This software is to calculate personal tax.
- * To make its use wider, the base of tax and the Tax Rate Table can be change
- * is necessary.
- */
- package personaltax;
- import myException.NegativeException;
- /**
- * Class TaxCalculator calculate the personal tax with the infomation in TaxRateTable.
- *
- * @version 1.0 09 March 2008
- * @author Rongzhi-Liu
- */
- public class TaxCalculator {
- /** The base of tax. */
- private double base;
- /** TaxRateTable contains all the tax rate infomation. */
- private TaxRateTable taxRateTable;
- /** Default constructor. Create the TaxCalculator with default base. */
- public TaxCalculator(){
- base = 1600;
- taxRateTable = new TaxRateTable();
- }
- /**
- * Return taxRateTable.
- *
- * @return Tax Rate Table.
- */
- public TaxRateTable getTaxRateTable() {
- return taxRateTable;
- }
- /**
- * Rebuild the whole TaxRateTable, including the number of tax rank, starting money & rate.
- *
- * @param n: the number of ranks.
- * @param startAndRate: contains the infomation of each tax level(staring money & rate).
- */
- public void rebuildTaxRateTable(int n, double startAndRate[][]){
- taxRateTable = new TaxRateTable(n, startAndRate);
- }
- /**
- * Set the base of the tax.
- *
- * @param b the new base.
- */
- public void setBase(double b) throws NegativeException{
- if(base < 0){
- throw new NegativeException("Base");
- }
- base = b;
- }
- /**
- * Override toString()
- *
- * @return fomated Calculator.
- */
- @Override
- public String toString(){
- String fomatedCalculator = "--------------------------------n";
- fomatedCalculator += "Current Tax Base is : " + base + "n";
- fomatedCalculator += "Current Tax Rate Table: n";
- fomatedCalculator += taxRateTable;
- fomatedCalculator += "--------------------------------n";
- return fomatedCalculator;
- }
- /**
- * Calculate the tax.
- *
- * @param amountOfTax: Money that should be taxed.
- * @return the tax.
- */
- public double calculate(double income) throws NegativeException{
- if(income < 0){
- throw new NegativeException("Income");
- }else if(income <= base){
- return 0;
- }else{
- return taxRateTable.calculate(income - base);
- }
- }
- public static void main(String[] args){
- try{
- TaxCalculator t = new TaxCalculator();
- System.out.println("Class TaxCalculator testing ......");
- System.out.println("Current Tax Calculator:");
- System.out.print(t);
- System.out.println("Calculate(4300)......");
- System.out.println("The tax is " + t.calculate(4300) + "nn");
- System.out.println("setBase(1800)........");
- t.setBase(1800);
- System.out.println("Now the Tax Calculator:");
- System.out.println(t);
- System.out.println("n");
- System.out.println("Calculate(4300)......");
- System.out.println("The tax is " + t.calculate(4300));
- System.out.println("Calculate(0) = " + t.calculate(0));
- System.out.println("Calculate(0.01) = " + t.calculate(0.01));
- System.out.println("Calculate(1800) = " + t.calculate(1800) + "n");
- System.out.println("Error testing: Calculator(-10)...");
- System.out.println("Calculate(-10) = " + t.calculate(-10));
- }catch(NegativeException e){
- e.printStackTrace();
- }
- }
- }