TaxRateTable.java
上传用户:yinzh02
上传日期:2021-05-28
资源大小:63k
文件大小:7k
- /*
- * %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 java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import myException.OutOfRangException;
- import myException.TableIllegalException;
- /**
- * class TaxRateTable contains the infomation of all the tax's ranks rate.
- *
- * @version 1.0 09 March 2008
- * @author Rongzhi-Liu
- */
- public class TaxRateTable {
-
- /**
- * Custom Comparator, using it to sort the Tax Rate Table.
- */
- private class MyComparator implements Comparator<Rate>{
- public int compare(Rate o1, Rate o2) {
- Rate r1 = (Rate)o1;
- Rate r2 = (Rate)o2;
-
- if(r1.getStart() > r2.getStart())
- return 1;
- else
- return 0;
- }
- }
-
- /** The number of tax rank. */
- private int nRank;
-
- /** The infomation of all the tax ranks. */
- private ArrayList<Rate> rate = new ArrayList<Rate>();
-
- /** Total tax for the level. */
- private double[] fullRate;
-
- /** Default constructor. Create the TaxRateTable with default tax rate. */
- public TaxRateTable(){
- nRank = 5;
-
- rate.add(new Rate(0,0.05));
- rate.add(new Rate(500,0.1));
- rate.add(new Rate(2000,0.15));
- rate.add(new Rate(5000,0.2));
- rate.add(new Rate(20000,0.25));
-
- calFullRate();
- }
-
- /** Constructor. Create the TaxRateTable with customer's tax rate. */
- public TaxRateTable(int n, double startAndRate[][]){
- nRank = n;
-
- for(int i = 0; i < n; i++){
- rate.add(new Rate(startAndRate[i][0],startAndRate[i][1]));
- }
-
- calFullRate();
- }
-
- /**
- * Calculate the Full Rate Tax before Rank i;
- */
- private void calFullRate(){
- fullRate = new double[nRank];
- fullRate[0] = 0;
- for(int i = 1; i < nRank; i++){
- fullRate[i] = fullRate[i-1] +
- (((Rate)rate.get(i)).getStart() - ((Rate)rate.get(i-1)).getStart())
- * ((Rate)rate.get(i-1)).getRate();
- }
- }
-
- /**
- * Sort the Tax Rate Table with the starting money.
- */
- private void sortTaxRateTable(){
-
- Comparator comp = new MyComparator();
- Collections.<Rate>sort(rate,comp);
- }
- /**
- * Test if the Tax Rate Table is legal.
- *
- * @return If the Table is legal, return true, else return false.
- */
- private boolean test(){
-
- sortTaxRateTable();
- for(int i=0; i<nRank-1; i++){
- if(((Rate)rate.get(i)).getStart() == ((Rate)rate.get(i+1)).getStart() ||
- ((Rate)rate.get(i)).getRate() >= ((Rate)rate.get(i+1)).getRate()){
- return false;
- }
- }
- return true;
- }
-
- /**
- * Insert an Rank to the Tax Rate Table.
- *
- * @param s New Rank's starting money.
- * @param r New Rank's rate.
- */
- public void insertRank(double s, double r) throws TableIllegalException{
-
- rate.add(new Rate(s,r));
- if(!test()){
- rate.remove(new Rate(s,r));
- throw new TableIllegalException();
- }
- ++nRank;
- calFullRate();
- }
-
- /**
- * Delete a Rank in the Tax Rate Table.
- *
- * @param i The index of the Rank to be deleted.
- */
- public void deleteRank(int i) throws OutOfRangException{
-
- if(i <= 0 || i > nRank){
- throw new OutOfRangException(nRank);
- }
-
- rate.remove(i-1);
- --nRank;
- }
-
- /**
- * Change rank i, including starting money and rate.
- *
- * @param i Index of rank.
- * @param s Rank(i)'s new starting money.
- * @param r Rank(i)'s new rate.
- * @throws myException.OutOfRangException
- * @throws myException.TableIllegalException
- */
- public void changeRank(int i, double s, double r)
- throws OutOfRangException, TableIllegalException{
-
- if(i > nRank){
- throw new OutOfRangException(nRank);
- }
-
- Rate tmp = (Rate)rate.get(i-1);
-
- rate.set(i-1, new Rate(s,r));
- if(!test()){
- rate.remove(new Rate(s,r));
- rate.add(tmp);
- sortTaxRateTable();
- throw new TableIllegalException();
- }
- calFullRate();
- }
-
- /**
- * Override toString()
- *
- * @return Fomated Tax Rate Table
- */
- @Override
- public String toString(){
- String table = "* * * * * * * * * * * * *n";
- table += "* RanktStartt Ratet*n";
- for(int i=0; i<nRank; i++){
- table += "* " + (i + 1) + "t" + (Rate)rate.get(i) + "t*n";
- }
- table += "* * * * * * * * * * * * *n";
- return table;
- }
-
- /**
- * Calculate the tax.
- *
- * @param AmountOfTax Money that should be taxed.
- * @return The tax.
- */
- public double calculate(double amountOfTax){
- int i;
- for(i = 0; i < nRank; i++){
- if(amountOfTax <= ((Rate)rate.get(i)).getStart()){
- break;
- }
- }
-
- return (fullRate[i-1]
- + (amountOfTax - ((Rate)rate.get(i-1)).getStart())
- * ((Rate)rate.get(i-1)).getRate());
- }
-
- /**
- * Main Function. Use for Regression testing.
- * @param Args
- */
-
- public static void main(String[] args){
-
- System.out.println("Class Tax Rate Table testing.....");
-
- TaxRateTable t = new TaxRateTable();
-
- System.out.println("Current Table:");
- System.out.print(t);
- try{
- System.out.println("Insert (800,0.12)");
- t.insertRank(800, 0.12);
- System.out.println(t);
-
- System.out.println("Delete rank(6)");
- t.deleteRank(6);
- System.out.println(t);
-
- System.out.println("Change Rank(4,3000,0.18)");
- t.changeRank(3,3000,0.18);
- System.out.println(t);
-
- System.out.println("Error Testing...");
- System.out.println("Insert (1000,0.3)");
- t.insertRank(1000, 0.3);
- System.out.println(t);
-
- }catch(TableIllegalException e){
- e.printStackTrace();
- }catch(OutOfRangException e){
- e.printStackTrace();
- }
- System.out.print(t);
- }
- }