glaukon
Chapter 3: Java - Part 12
Of course, since we haven't created an updateTotal method for the Diner class yet, Eclipse will give you an error. So let's go and write the updateTotal method now. Go to the Diner class, and declare a new double variable.
public double total;
Whereas integers have to be whole numbers, doubles don't have to be. They can have fractional amounts in them. For instance, a double can be 5.24. Another common data type used for non-whole numbers is a float. We don't really need to know the difference between a double and a float, so I'm not going into that here. Honestly, I don't even remember the difference, and I'm too lazy to look it up. You can Google it for yourself if you're curious. Anyway, set this variable to 0.0 in the constructor.
total = 0.0;
The total variable will keep track of a particular Diner's total bill. When a Diner is instantiated, their total is 0. I like using 0.0 instead of just 0, since it reminds me that I'm dealing with a double, and not an integer. Next, create the updateTotal method, as below:

public void updateTotal() {

total = 0.0;

for (int i = 0; i < orderList.size(); i++) {

total += Double.parseDouble(orderList.get(i).getText().toString());

}

tvSplitBill.setText("$" + String.format("%,.2f", total));

}

Remember, this gets called every time an EditText for an order loses focus. Its job is to update the tvSplitBill of a Diner to show the proper total for that Diner. First, it sets the total of the Diner back to 0. Then, it runs through ever order in a Diner's orderList using a for loop. For ever order, we get its Text, turn it into a string, then we turn it into a double using Double.parseDouble. This is a new method. Double.parseDouble takes a string as an argument, and turns it into a double. So if you gave it a string like "5", it will return the double version of that string (5.0). The thing is, you can only pass this method strings that can be turned into numbers. In other words, if you pass Double.parseDouble a string like "apple", or even "five", you'll get an error. You have to give it numerical characters.
We need to do this because we're adding the returned double to the Diner's total variable. Since we can't add strings to doubles, we have to first turn the string into a double. Note also that we can't parse Editables, which is why we have to turn the Editable we got from the getText method into a string first. So what the whole for loop does is, it runs through every order in a Diner's orderList. For each order EditText, it gets the Text from it (which is an Editable), turns it into a string, turns the string into a double, then adds that to the total variable. As a result, by the end of the loop, the new total variable should be the sum of all the orders.
Glossary
Double: A double is one data type that can hold floating point numbers, or rational numbers that can have a fractional part.
Double.parseDouble(String s): The parseDouble method is used to convert a string into a double.
Float: A float is another data type that can hold floating point numbers. For beginners, any time a double can be used, a float can also be used.