为类对象的ArrayList调用一个加法方法

时间:2017-10-09 15:06:35

标签: java arrays class object

我目前正在开发一个程序,我必须通过添加数字来收集数据并计算正确的输出,并根据每个数字修改它们。我为交易所做的课程是这样的:

public static class OrderTransactions {
    String no;
    String loc;
    String co;
    String val;

    public OrderTransactions(String n, String l, String c, String v) {
        no = n;
        loc = l;
        co = c;
        val = v;
    }

    public int TotalAmount(ArrayList<String> x) {
        HashMap<Character,Double> hm = new HashMap<Character,Double>();
        for(int i = 0; i < x.size(); i++) {
            String[] s = x.get(i).split("(?<=\\d)(?=[a-zA-Z])");
            hm.put(s[1].charAt(0), Double.parseDouble(s[0]));
        }

        double[] nums = new double[5];
        int i = 0;
        for(Map.Entry<Character,Double> pair : hm.entrySet()) {
            switch(pair.getKey()) {
                case 'M' : 
                    nums[i] = pair.getValue() * 1000000;
                    break;
                case 'K' : 
                    nums[i] = pair.getValue() * 1000;
                    break;
                case 'H' : 
                    nums[i] = pair.getValue() * 100;
                    break;
                default : 
                    System.out.println("Info did not have correct following letter");
                    break;
            }
            System.out.println(nums[i]);
            i++;
        }

        int tot = 0;
        for(int j = 0; j < nums.length; j++)
            tot += nums[j];

        return tot;
    }
}

当我在主要方法中调用TotalAmount方法时,我遇到了这样的问题:

public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    OrderTransactions[] obj = new OrderTransactions[5];
    String info;

    for(int i = 0; i < obj.length; i++) {
        System.out.println("Enter input as: 'num;loc;co;val'");
        info = sc.nextLine();
        String[] parts = info.split(";");
        obj[i] = new OrderTransactions(parts[0], parts[1], parts[2], parts[3]);
    }

    ArrayList<String> costs = new ArrayList<String>();
    for(int i = 0; i < obj.length; i++)
        costs.add(obj[i].val);
    int tot = obj.TotalAmount(costs);

    System.out.println("Total Amount: " + tot);
}

我试图计算所有部分的总和&#34; val&#34;我创建的类的数组。问题是我不能使用&#34; int tot&#34;上的对象数组来调用它。线。我试过把它叫做obj [i],但这并没有给我正确的计算。我如何从我的类对象数组中调用TotalAmount方法

有关更多信息,我应该通过以下字母修改数字,例如&#34; 234K&#34;和&#34; 1.3M&#34;。当代码运行时,输入看起来像&#34; 1001; Online; Target Corp; 234K&#34;,&#34; 1002; Store; Advantec Corp; 1.3M&#34;和&#34; 1003;在线;漫威兄弟; 2.1M&#34;。

1 个答案:

答案 0 :(得分:2)

1)请不要public int TotalAmount(ArrayList<String> x)等apper案例开始命名功能 将其命名为public int totalAmount(ArrayList<String> x)

2)尝试将此方法public int totalAmount(ArrayList<String> x)移动到主类并使其成为静态。

public class Main {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    OrderTransactions[] obj = new OrderTransactions[5];
    String info;

    for(int i = 0; i < obj.length; i++) {
        System.out.println("Enter input as: 'num;loc;co;val'");
        info = sc.nextLine();
        String[] parts = info.split(";");
        obj[i] = new OrderTransactions(parts[0], parts[1], parts[2], parts[3]);
    }

    ArrayList<String> costs = new ArrayList<String>();
    for(int i = 0; i < obj.length; i++)
        costs.add(obj[i].val);
    int tot = totalAmount(costs);

    System.out.println("Total Amount: " + tot);
}
public static int totalAmount(ArrayList<String> x) {
    HashMap<Character,Double> hm = new HashMap<Character,Double>();
    for(int i = 0; i < x.size(); i++) {
        String[] s = x.get(i).split("(?<=\\d)(?=[a-zA-Z])");
        hm.put(s[1].charAt(0), Double.parseDouble(s[0]));
    }

    double[] nums = new double[5];
    int i = 0;
    for(Map.Entry<Character,Double> pair : hm.entrySet()) {
        switch(pair.getKey()) {
            case 'M' :
                nums[i] = pair.getValue() * 1000000;
                break;
            case 'K' :
                nums[i] = pair.getValue() * 1000;
                break;
            case 'H' :
                nums[i] = pair.getValue() * 100;
                break;
            default :
                System.out.println("Info did not have correct following letter");
                break;
        }
        System.out.println(nums[i]);
        i++;
    }

    int tot = 0;
    for(int j = 0; j < nums.length; j++)
        tot += nums[j];

    return tot;
}
public static class OrderTransactions {
    String no;
    String loc;
    String co;
    String val;

    public OrderTransactions(String n, String l, String c, String v) {
        no = n;
        loc = l;
        co = c;
        val = v;
    }


}
}