Gab*_*l_W 1 java arrays multidimensional-array
我正在研究一个税收计算器,并且想要将税率括号放入多维数组中.这是我第一次尝试这样的事情,并想知道这是否是正确的做法?
private static double[][][] taxBrackets;
static {
taxBrackets = new double[20][14][14];
/*Single*/ /*Married*/ /*Head of Household*/
//TierOne //TierOne //TierOne
taxBrackets[0][0][0] = 0; taxBrackets[0][1][0] = 0; taxBrackets[0][0][1] = 0; //MIN
taxBrackets[1][0][0] = 9075; taxBrackets[0][2][0] = 18150; taxBrackets[0][0][2] = 12950; //MAX
taxBrackets[2][0][0] = 0.10; //Tax Rate
//TierTwo
taxBrackets[3][0][0] = 9076; taxBrackets[0][3][0] = 18151; taxBrackets[0][0][3] = 12951; //MIN
taxBrackets[4][0][0] = 36900; taxBrackets[0][4][0] = 73800; taxBrackets[0][0][4] = 49400; //MAX
taxBrackets[5][0][0] = 0.15; //Tax Rate
//TierThree
taxBrackets[6][0][0] = 36901; taxBrackets[0][5][0] = 73801; taxBrackets[0][0][5] = 49401; //MIN
taxBrackets[7][0][0] = 89350; taxBrackets[0][6][0] = 148850; taxBrackets[0][0][6] = 127550; //MAX
taxBrackets[8][0][0] = 0.25; //Tax Rate
//TierFour
taxBrackets[9][0][0] = 89351; taxBrackets[0][7][0] = 148851; taxBrackets[0][0][7] = 127551; //MIN
taxBrackets[10][0][0] = 186350; taxBrackets[0][8][0] = 226850; taxBrackets[0][0][8] = 206600; //MAX
taxBrackets[11][0][0] = 0.28; //Tax Rate
//TierFive
taxBrackets[12][0][0] = 186351; taxBrackets[0][9][0] = 226851; taxBrackets[0][0][9] = 206601; //MIN
taxBrackets[13][0][0] = 405100; taxBrackets[0][10][0] = 405100; taxBrackets[0][0][10] = 405100; //MAX
taxBrackets[14][0][0] = 0.33; //Tax Rate
//TierSix
taxBrackets[15][0][0] = 405101; taxBrackets[0][11][0] = 405101; taxBrackets[0][0][11] = 405101; //MIN
taxBrackets[16][0][0] = 406750; taxBrackets[0][12][0] = 457600; taxBrackets[0][0][12] = 432200; //MAX
taxBrackets[17][0][0] = 0.35; //Tax Rate
//TierSeven
taxBrackets[18][0][0] = 406751; taxBrackets[0][13][0] = 457601; taxBrackets[0][0][13] = 432201; //MIN
taxBrackets[19][0][0] = 0.396; //Tax Rate
}
Run Code Online (Sandbox Code Playgroud)
我们的想法是在左侧数组中使用单个文件管理器,在中间使用Married/Joint文件管理器,在右侧数组中使用Household文件管理器头.我是否正确地做到了这一点还是完全错过了这一点?
更新:1/30/2017在遵循Nhouser9的建议后,我最终得到了更易于管理的内容.
private static class TaxBracket {
final double minSalary;
final double maxSalary;
final double taxRate;
TaxBracket(double minSalary, double maxSalary, double taxRate) {
this.minSalary = minSalary;
this.maxSalary = maxSalary;
this.taxRate = taxRate;
}
}
//This is the data structure which holds the values for each tier of each filing status
//Changing values in these arrays will affect the output of the entire program
private static TaxBracket[] singleFiler;
static {
singleFiler = new TaxBracket[]
{
new TaxBracket(0, 9075, 0.10), //Index 0 TierOne
new TaxBracket(9076, 36900, 0.15), //Index 1 TierTwo
new TaxBracket(36901, 89350, 0.25), //Index 2 TierThree
new TaxBracket(89351, 186350, 0.28),//Index 3 TierFour
new TaxBracket(186351, 405100, 0.33),//Index 4 TierFive
new TaxBracket(405101, 406750, 0.35),//Index 5 TierSix
new TaxBracket(406751, Double.MAX_VALUE, 0.396)//Index 6 TierSeven
};
}
private static TaxBracket[] jointFiler;
static {
jointFiler = new TaxBracket[]
{
new TaxBracket(0, 18150, 0.10), //Index 0 TierOne
new TaxBracket(18151, 73800, 0.15), //Index 1 TierTow
new TaxBracket(73801, 148850, 0.25), //Index 2 TierThree
new TaxBracket(148851, 226850, 0.28),//Index 3 TierFour
new TaxBracket(226851, 405100, 0.33),//Index 4 TierFive
new TaxBracket(405101, 457600, 0.35),//Index 5 TierSix
new TaxBracket(457601, Double.MAX_VALUE, 0.396)//Index 6 TierSeven
};
}
private static TaxBracket[] hohFiler;
static {
hohFiler = new TaxBracket[]
{
new TaxBracket(0, 12950, 0.10), //Index 0 TierOne
new TaxBracket(12951, 49400, 0.15), //Index 1 TierTow
new TaxBracket(49401, 127550, 0.25), //Index 2 TierThree
new TaxBracket(127551, 206600, 0.28),//Index 3 TierFour
new TaxBracket(206601, 405100, 0.33),//Index 4 TierFive
new TaxBracket(405101, 432200, 0.35),//Index 5 TierSix
new TaxBracket(432201, Double.MAX_VALUE, 0.396)//Index 6 TierSeven
};
}
Run Code Online (Sandbox Code Playgroud)
这是一种可能的解决方案.如果它有效,那很好.
由于Java是面向对象的语言,因此最好使用Objects.例如,TaxBracket像这样的对象:
public class TaxBracket {
public static final int SINGLE = 1;
public static final int MARRIED = 2;
public static final int HEAD_OF_HOUSE = 3;
public final int mStatus;
public final int minSalary;
public final int maxSalary;
public final double rate;
public TaxBracket(int mStatus, int minSalary, int maxSalary, double rate) {
this.mStatus = mStatus;
this.minSalary = minSalary;
this.maxSalary = maxSalary;
this.taxRate = taxRate;
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以声明这些对象的单个数组而不是多维数组,像这样初始化它:
taxBrackets[0] = new TaxBracket(TaxBracket.SINGLE, 0, 9075, .1);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
124 次 |
| 最近记录: |