有没有更简单的方法来配对JCheckBox和JTextField中的值,而不是使用HashMap

时间:2017-06-30 16:26:20

标签: java swing collections hashmap

所以我的目标是能够存储成绩水平和金额的价值对。现在,我不确定集合是否是解决问题的最佳方法。

所以我的GUI看起来像这样。

enter image description here

对于每增加一个费用,它可能有1对或更多对等级和金额。

所以我所做的就是填充选中的复选框,以确定需要将哪个GradeLevel和Amount插入到MANY-to-MANY数据库表中。

这是我填充它的方式。你知道如何减少代码吗?也许是另一种方式,而不是使用Maps

Map<Integer, Double> map = new HashMap<>();
Integer key = null;
Double value = null;

Component[] comp = jpnlFeeAmount.getComponents();
for (Component c : comp) {
    if (c instanceof JCheckBox) {
        if (((JCheckBox) c).isSelected() && !((JCheckBox) c).getText().equals("Kindergarten")) {
            String glStr = ((JCheckBox) c).getText().replaceAll("[^-?0-9]+", " "); //takes out all numeric from string 
            String aGradeLevel = glStr.replaceAll("\\s+", ""); // removes all whitespace
            if (!aGradeLevel.isEmpty()) {
                int aGradeLevelInt = Integer.parseInt(aGradeLevel);
                switch (aGradeLevelInt) {
                    case 1:
                        key = 1;
                        value = Double.parseDouble(jtfGrade1Amount.getText().trim());
                        break;
                    case 2:
                        key = 2;
                        value = Double.parseDouble(jtfGrade2Amount.getText().trim());
                        break;
                    case 3:
                        key = 3;
                        value = Double.parseDouble(jtfGrade3Amount.getText().trim());
                        break;
                    case 4:
                        key = 4;
                        value = Double.parseDouble(jtfGrade4Amount.getText().trim());
                        break;
                    case 5:
                        key = 5;
                        value = Double.parseDouble(jtfGrade5Amount.getText().trim());
                        break;
                    case 6:
                        key = 6;
                        value = Double.parseDouble(jtfGrade6Amount.getText().trim());
                        break;
                    case 7:
                        key = 7;
                        value = Double.parseDouble(jtfGrade7Amount.getText().trim());
                        break;
                    case 8:
                        key = 8;
                        value = Double.parseDouble(jtfGrade8Amount.getText().trim());
                        break;
                    case 9:
                        key = 9;
                        value = Double.parseDouble(jtfGrade9Amount.getText().trim());
                        break;
                    case 10:
                        key = 10;
                        value = Double.parseDouble(jtfGrade10Amount.getText().trim());
                        break;
                }
                map.put(key, value);
            }
        } else if (((JCheckBox) c).isSelected() && ((JCheckBox) c).getText().equals("Kindergarten")) {
            key = 0;
            value = Double.parseDouble(jtfKindergartenAmount.getText().trim());
            map.put(key, value);
        }
    }
}

我打算获得等级的ID,也许是getId(int gradelevel)

有什么建议吗?

0 个答案:

没有答案