Memu.java
上传用户:yinzh02
上传日期:2021-05-28
资源大小:63k
文件大小:10k
- /*
- * %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
- * if necessary.
- */
- package UI;
- import java.io.*;
- import personaltax.*;
- import myException.*;
- /**
- * Main class: Interacte with customers.
- *
- * @author Rongzhi-Liu
- */
- public class Memu {
-
- /** Instance of TaxCalculator. */
- static private TaxCalculator taxCalculator = new TaxCalculator();
-
- /**
- * Default Constructor. Do nothing.
- */
- public Memu(){}
-
- /**
- * Custom integer Input function, including illegal input dealing.
- *
- * @return If the input is an positive integer, return the integer, else return -1.
- */
- private int inputI(){
- BufferedReader sRead = new BufferedReader(new InputStreamReader(System.in));
- String sInput;
- int cIndex;
-
- try{
- sInput = (String)sRead.readLine();
-
- for(cIndex=0; cIndex < sInput.length(); cIndex++){
- if(sInput.charAt(cIndex) < '0' || sInput.charAt(cIndex) > '9'){
- return -1;
- }
- }
- if(cIndex == sInput.length()){
- return Integer.parseInt(sInput);
- }
- }catch(IOException e){
- e.printStackTrace();
- }
- return 0;
- }
-
- /**
- * Custom double Input function, including illegal input dealing.
- *
- * @return If the input is an positive double, return the double, else return -1.
- */
- private double inputD(){
- BufferedReader sRead = new BufferedReader(new InputStreamReader(System.in));
- String sInput;
- int cIndex;
-
- try{
- sInput = (String)sRead.readLine();
- for(cIndex = 0; cIndex < sInput.length(); cIndex++){
- if((sInput.charAt(cIndex) < '0' || sInput.charAt(cIndex) > '9')
- && sInput.charAt(cIndex) != '.'){
- return -1;
- }
- }
- if(cIndex == sInput.length()){
- return Double.parseDouble(sInput);
- }
- }catch(IOException e){
- e.printStackTrace();
- }
- return 0;
- }
-
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- Memu memu = new Memu();
- int choice; // Maim memu choice.
- int changeTableChoice; // Function 2-subMemu choice.
- double income;
- int nRank;
- double base;
- double[][] startAndRate;
- boolean flag = true;
- boolean flag2 = true;
-
- do{
- try{
- System.out.println("****************************");
- System.out.println("* Personal Tax Caculatro *");
- System.out.println("****************************");
- System.out.println("Functions For You to Choose: ");
- System.out.println("1. Calculate your personal tax.");
- System.out.println("2. Change the Tax Rate Table.");
- System.out.println("3. Change the Tax Base.");
- System.out.println("0. Exit.");
- System.out.print(taxCalculator);
- System.out.print("What do you want to do?(0-3): ");
- choice = memu.inputI();
-
- switch(choice){
- case 1:
- System.out.print("Enter Your Income: ");
- income = memu.inputD();
- System.out.println("Your Tax is : " + taxCalculator.calculate(income) + " yuan.n");
- break;
- case 2:
- do{
- System.out.print(taxCalculator);
- System.out.println("1. Insert a new Rank.");
- System.out.println("2. Delete a Rank.");
- System.out.println("3. Change a Rank.");
- System.out.println("4. Change the whole Table.");
- System.out.println("0. Return to the Main Menu.");
- System.out.print("How do you want to change the Table: ");
-
- changeTableChoice = memu.inputI();
-
- double s;
- double r;
- int i;
- switch(changeTableChoice){
- case 1:
- System.out.println("Input the new rank :");
- System.out.print("Start: ");
- s = memu.inputD();
- System.out.print(" Rate: ");
- r = memu.inputD();
- taxCalculator.getTaxRateTable().insertRank(s, r);
- break;
-
- case 2:
- System.out.print("The rank you want to delete is : ");
- i = memu.inputI();
- taxCalculator.getTaxRateTable().deleteRank(i);
- break;
-
- case 3:
- System.out.print("The rank you want to change is : ");
- i = memu.inputI();
- System.out.print("New start: ");
- s = memu.inputD();
- System.out.print("New rate: ");
- r = memu.inputD();
- taxCalculator.getTaxRateTable().changeRank(i, s, r);
- break;
-
- case 4:
- System.out.print("How many ranks dose your Tax Rate Table have? : ");
- nRank = memu.inputI();
- startAndRate = new double[nRank][2];
- System.out.println("Input the new Tax Rate Table:");
- System.out.println("Level 1:");
- System.out.println(" From: 0");
- startAndRate[0][0] = 0;
- System.out.print(" To: ");
- startAndRate[1][0] = memu.inputD();
- System.out.print(" Rate: ");
- startAndRate[0][1] = memu.inputD();
- for(i=1; i<nRank - 1; i++){
- System.out.println("Level " + (i + 1) + ":");
- System.out.println(" From :" + startAndRate[i][0]);
- System.out.print(" To :");
- startAndRate[i+1][0] = memu.inputD();
- System.out.print(" Rate: ");
- startAndRate[i][1] = memu.inputD();
- }
- System.out.println("Levle " + nRank + ":");
- System.out.println(" More than " + startAndRate[nRank-1][0]);
- System.out.print(" Rate: ");
- startAndRate[nRank-1][1] = memu.inputD();
-
- for(int j=0; j<nRank-1; j++){
- for(int k=j+1; k<nRank; k++){
- if((startAndRate[j][0] - startAndRate[k][0])
- * (startAndRate[j][1] - startAndRate[k][1]) < 0 ){
- throw new TableIllegalException();
- }
- }
- }
- taxCalculator.rebuildTaxRateTable(nRank, startAndRate);
- break;
-
- case 0:
- flag2 = false;
- break;
-
- default:
- System.out.println("Error: You can only choose 0-3!n");
- break;
- }
- }while(flag2);
- break;
- case 3:
- System.out.print("Input the new base: ");
- base = memu.inputD();
- taxCalculator.setBase(base);
- System.out.println("Success changing Base of Tax!n");
- break;
- case 0:
- flag = false;
- break;
- default:
- System.out.println("Error: You can only choose 0-3!n");
- break;
- }
- }catch(NegativeException e){
- e.printStackTrace();
- }catch(OutOfRangException e){
- e.printStackTrace();
- }catch(TableIllegalException e){
- e.printStackTrace();
- }
- }while(flag);
- }
- }