Finding the total Income of 3 re-occurring names (people) in a text file

108 Views Asked by At

I have been trying to tackle this problem for a fair few days now and I have only managed to solve half of it, the next part that is troubling me seems to be a little more challenging and was wondering if I could be pointed in the right direction as to how I can tackle it.

I have 3 names that keep re-occurring in a text file on each line (in a random order) and with each of those names 2 numbers next to them that represent Price and Quantity. (As shown below)

Jack 8 2
Joe 4 2
Mike 14 5
Jack 3 3
Jack 9 1
Jack 2 2
Mike 20 6
Sofia 11 3
Jack 13 6
Mike 8 5
Joe 8 4
Sofia 8 1
Sofia 1 6
Sofia 9 4
  • I have managed to multiply those 2 numbers on each line for my answer next to each name. (First problem solved)

  • The next part I am having difficulty tackling, is how I am going to add together all the numbers next to each of the 3 individual names that appear into a total.

I have pondered whether I should use Switch, While, if, else loops and/or arrays but I can't seem to get my head around how to achieve my desired result. I have started to doubt whether or not my current code (Shown below) has gone a step in the wrong direction for getting the total income of the 3 names.

String name;
int leftNum, rightNum;

//Scan the text file
Scanner scan = new Scanner(Explore.class.getResourceAsStream("pay.txt"));

while (scan.hasNext()) { //finds next line
  name = scan.next(); //find the name on the line
  leftNum = scan.nextInt(); //get price
  rightNum = scan.nextInt(); //get quantity
  int ans = leftNum * rightNum; //Multiply Price and Quanity
  System.out.println(name + " : " + ans);
}

// if name is Jack,
//  get that number next to his name
//  and all the numbers next to name Jack are added together
// get total of the numbers added together for Jack

// if else name is Mike,
// Do the same steps as above for Jack and find total

// if else name is Joe,
// same as above and find total

My latest thoughts were pondering the use of an if, if else loop but I can't seem to think of a way to get Java to read a name and get the number next to it. Then find all lines with the same name for its number, finally adding all the numbers next to that persons name. Repeat for the 3 names.

My apologies if I've made this seem more complicated than it may be but I've gotten quite lost recently and feel I've hit another brick wall.

3

There are 3 best solutions below

2
On BEST ANSWER

What about a Map<String, Long>:

Map<String, Long> nameSumMap = new HashMap<>(3);
while (scan.hasNext()) {       //finds next line
    name = scan.next();        //find the name on the line
    leftNum = scan.nextInt();  //get price
    rightNum = scan.nextInt(); //get quantity

    Long sum = nameSumMap.get(name);
    if(sum == null) {          // first time we see "name"
        nameSumMap.put(name, Long.valueOf(leftNum + rightNum));
    } else {
        nameSumMap.put(name, sum + leftNum + rightNum);
    }
}

At the end, the map contains the sum associated with each name.

0
On

You can use a Map with a key String (the name) and value Double (the money spent).

Whenever you read a line :

  1. you check if the name is in the map with containsKey
  2. if it does not exist, you put the current amount to the map
  3. Otherwise, if it exists, you get the current value with get, you add the current amount and you put the result back in the map.
0
On

You could take a look at a HashMap.

You would create 2 of them, one for the quantity and one for the price. When you find the name the first time, you place the price there. When you find the same name again, you simply add the current value with the new one.

A more OOP way of doing it, could be the following:

  • Create an object with the following properties: Name, Price, Quantity.
  • As you go through your file, create an object per line, denoting what each person bought.
  • Once that the object is created, add it to the list.

This would allow you to go through the objects and do whatever calculations you may need.