Memu.java
上传用户:yinzh02
上传日期:2021-05-28
资源大小:63k
文件大小:10k
源码类别:

家庭/个人应用

开发平台:

Java

  1. /*
  2.  * %W% %E% Rongzhi Liu
  3.  * 
  4.  * Copyright (c) 2008 Rongzhi-Liu SUN YAT-SEN UNIVERSITY. All Rights Reserved.
  5.  * 
  6.  * This software is to calculate personal tax.
  7.  * To make its use wider, the base of tax and the Tax Rate Table can be change
  8.  * if necessary.
  9.  */
  10. package UI;
  11. import java.io.*;
  12. import personaltax.*;
  13. import myException.*;
  14. /**
  15.  * Main class: Interacte with customers.
  16.  * 
  17.  * @author Rongzhi-Liu
  18.  */
  19. public class Memu {
  20.     
  21.     /** Instance of TaxCalculator. */
  22.     static private TaxCalculator taxCalculator = new TaxCalculator();
  23.     
  24.     /**
  25.      * Default Constructor. Do nothing.
  26.      */
  27.     public Memu(){}
  28.   
  29.     /**
  30.      * Custom integer Input function, including illegal input dealing.
  31.      * 
  32.      * @return If the input is an positive integer, return the integer, else return -1.
  33.      */
  34.     private int inputI(){
  35.         BufferedReader sRead = new BufferedReader(new InputStreamReader(System.in));
  36.         String sInput;
  37.         int cIndex;
  38.         
  39.         try{
  40.             sInput = (String)sRead.readLine();
  41.             
  42.             for(cIndex=0; cIndex < sInput.length(); cIndex++){
  43.                 if(sInput.charAt(cIndex) < '0' || sInput.charAt(cIndex) > '9'){
  44.                     return -1;
  45.                 }
  46.             }
  47.             if(cIndex == sInput.length()){
  48.                 return Integer.parseInt(sInput);
  49.             }
  50.         }catch(IOException e){
  51.             e.printStackTrace();
  52.         }
  53.         return 0;
  54.     }
  55.     
  56.      /**
  57.      * Custom double Input function, including illegal input dealing.
  58.      * 
  59.      * @return If the input is an positive double, return the double, else return -1.
  60.      */
  61.     private double inputD(){
  62.         BufferedReader sRead = new BufferedReader(new InputStreamReader(System.in));
  63.         String sInput;
  64.         int cIndex;
  65.         
  66.         try{
  67.             sInput = (String)sRead.readLine();
  68.             for(cIndex = 0; cIndex < sInput.length(); cIndex++){
  69.                 if((sInput.charAt(cIndex) < '0' || sInput.charAt(cIndex) > '9')
  70.                     && sInput.charAt(cIndex) != '.'){
  71.                     return -1;
  72.                 }
  73.             }
  74.             if(cIndex == sInput.length()){
  75.                 return Double.parseDouble(sInput);
  76.             }
  77.         }catch(IOException e){
  78.             e.printStackTrace();
  79.         }
  80.         return 0;
  81.     }
  82.     
  83.     /**
  84.      * @param args the command line arguments
  85.      */
  86.     public static void main(String[] args) {
  87.         // TODO code application logic here
  88.         Memu memu = new Memu();
  89.         int choice;                  // Maim memu choice.
  90.         int changeTableChoice;       // Function 2-subMemu choice.
  91.         double income;
  92.         int nRank;
  93.         double base;
  94.         double[][] startAndRate;
  95.         boolean flag = true;
  96.         boolean flag2 = true;
  97.     
  98.         do{
  99.             try{
  100.                 System.out.println("****************************");
  101.                 System.out.println("*  Personal Tax Caculatro  *");                
  102.                 System.out.println("****************************");
  103.                 System.out.println("Functions For You to Choose: ");
  104.                 System.out.println("1. Calculate your personal tax.");
  105.                 System.out.println("2. Change the Tax Rate Table.");
  106.                 System.out.println("3. Change the Tax Base.");
  107.                 System.out.println("0. Exit.");
  108.                 System.out.print(taxCalculator);
  109.                 System.out.print("What do you want to do?(0-3): ");
  110.                 choice = memu.inputI();
  111.                 
  112.                 switch(choice){
  113.                     case 1:
  114.                         System.out.print("Enter Your Income: ");
  115.                         income = memu.inputD();
  116.                         System.out.println("Your Tax is : " + taxCalculator.calculate(income) + " yuan.n");
  117.                         break;
  118.                     case 2:
  119.                         do{                        
  120.                             System.out.print(taxCalculator);
  121.                             System.out.println("1. Insert a new Rank.");
  122.                             System.out.println("2. Delete a Rank.");
  123.                             System.out.println("3. Change a Rank.");
  124.                             System.out.println("4. Change the whole Table.");
  125.                             System.out.println("0. Return to the Main Menu.");
  126.                             System.out.print("How do you want to change the Table: ");
  127.          
  128.                             changeTableChoice = memu.inputI();
  129.                             
  130.                             double s;
  131.                             double r;
  132.                             int i;
  133.                             switch(changeTableChoice){
  134.                                 case 1:                                    
  135.                                     System.out.println("Input the new rank :");
  136.                                     System.out.print("Start: ");
  137.                                     s = memu.inputD();
  138.                                     System.out.print(" Rate: ");
  139.                                     r = memu.inputD();
  140.                                     taxCalculator.getTaxRateTable().insertRank(s, r);
  141.                                     break;
  142.                                     
  143.                                 case 2:                                    
  144.                                     System.out.print("The rank you want to delete is : ");
  145.                                     i = memu.inputI();
  146.                                     taxCalculator.getTaxRateTable().deleteRank(i);
  147.                                     break;
  148.                                     
  149.                                 case 3:
  150.                                     System.out.print("The rank you want to change is : ");
  151.                                     i = memu.inputI();
  152.                                     System.out.print("New start: ");
  153.                                     s = memu.inputD();
  154.                                     System.out.print("New rate: ");
  155.                                     r = memu.inputD();
  156.                                     taxCalculator.getTaxRateTable().changeRank(i, s, r);
  157.                                     break;
  158.                                     
  159.                                 case 4:
  160.                                     System.out.print("How many ranks dose your Tax Rate Table have? : ");
  161.                                     nRank = memu.inputI();
  162.                                     startAndRate = new double[nRank][2];
  163.                                     System.out.println("Input the new Tax Rate Table:");
  164.                                     System.out.println("Level 1:");
  165.                                     System.out.println("  From: 0");
  166.                                     startAndRate[0][0] = 0;
  167.                                     System.out.print("  To: ");
  168.                                     startAndRate[1][0] = memu.inputD();
  169.                                     System.out.print("  Rate: ");
  170.                                     startAndRate[0][1] = memu.inputD();
  171.                                     for(i=1; i<nRank - 1; i++){
  172.                                         System.out.println("Level " + (i + 1) + ":");
  173.                                         System.out.println("  From :" + startAndRate[i][0]);
  174.                                         System.out.print("  To :");
  175.                                         startAndRate[i+1][0] = memu.inputD();
  176.                                         System.out.print("  Rate: ");
  177.                                         startAndRate[i][1] = memu.inputD();
  178.                                     }
  179.                                     System.out.println("Levle " + nRank + ":");
  180.                                     System.out.println("  More than " + startAndRate[nRank-1][0]);
  181.                                     System.out.print("  Rate: ");
  182.                                     startAndRate[nRank-1][1] = memu.inputD();
  183.                                     
  184.                                     for(int j=0; j<nRank-1; j++){
  185.                                         for(int k=j+1; k<nRank; k++){
  186.                                             if((startAndRate[j][0] - startAndRate[k][0])
  187.                                                * (startAndRate[j][1] - startAndRate[k][1]) < 0 ){
  188.                                                 throw new TableIllegalException();
  189.                                             }
  190.                                         }
  191.                                     }
  192.                                     taxCalculator.rebuildTaxRateTable(nRank, startAndRate);
  193.                                     break;
  194.                                     
  195.                                 case 0:
  196.                                     flag2 = false;
  197.                                     break;
  198.                                     
  199.                                 default:
  200.                                     System.out.println("Error: You can only choose 0-3!n");
  201.                                     break;
  202.                             }                            
  203.                         }while(flag2);
  204.                         break;
  205.                     case 3:
  206.                         System.out.print("Input the new base: ");
  207.                         base = memu.inputD();
  208.                         taxCalculator.setBase(base);
  209.                         System.out.println("Success changing Base of Tax!n");
  210.                         break;
  211.                     case 0:
  212.                         flag = false;
  213.                         break;
  214.                     default:
  215.                         System.out.println("Error: You can only choose 0-3!n");
  216.                         break;                    
  217.                 }
  218.             }catch(NegativeException e){
  219.                 e.printStackTrace();
  220.             }catch(OutOfRangException e){
  221.                 e.printStackTrace();
  222.             }catch(TableIllegalException e){
  223.                 e.printStackTrace();
  224.             }
  225.         }while(flag);
  226.     }
  227. }