Reputation: 1045
I am currently working on a program where I must collect data and calculate the correct output by adding digits and modifying them according to the following number for each. The class I made for transactions is this:
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;
}
}
I'm having trouble with the TotalAmount method when I call it in the main like this:
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);
}
I'm trying to calculate the sum of all the parts "val" of the array of my created class. The problem is I cannot call it using an array of the objects on the "int tot" line. I've tried calling it for obj[i] but that doesn't give me the correct calculation. How would I go about calling the TotalAmount method from my array of class objects
For more information, I am supposed to modify numbers by the following letters, such as "234K" and "1.3M". When the code is run, input would look like "1001;Online;Target Corp;234K", "1002;Store;Advantec Corp;1.3M", and "1003;Online;Marvel Brothers;2.1M".
Upvotes: 0
Views: 47
Reputation: 594
1) Please do not name a funtion starting by apper case likepublic int TotalAmount(ArrayList<String> x)
name it like public int totalAmount(ArrayList<String> x)
2) try to move this method public int totalAmount(ArrayList<String> x)
to main class and make it static.
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;
}
}
}
Upvotes: 2