• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calculating multiple variables on easier way  RSS feed

 
Milos Gojic
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, I'm trying to do my Android application. A rather small one
About it ... So application imitates the menu from the restaurant, when the user checks the breakfast, such as Bacon and eggs, enter his name , application on the screen repeats this information. If we want to take two different breakfasts, applications, of course, calculate Prices and prints on the screen.



But what if the user wants to select a number of different dishes, I can not go on forever adding if loops .Or, I can?

Now where I am stuck ... in principle I did not stuck, but I was wondering if there is an easier( and more beautiful to the eye) way to calculate several variables? My code works quite ok but it would look very ugly with   dozens of  if loops. I tried to use switch / case but it can't be used with boolean.I do not know if I can do smething with lists?

How my app looks for now:


My code:


I  will eventually  add more dishes so this is problematic.
Thank you and sorry for my poor english.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!
I was wondering if there is an easier( and more beautiful to the eye) way to calculate several variables

Yes, there is a more elegant way to do the calculation. In fact, there are at least two options that are more elegant.

First, if you want to make your method flexible, then the parameters you declare in its signature should not have to change when you add more items. This means that the way you've written it now needs to be changed.

Next, instead of individual independent items, you need to put the things you want to treat as a group into a collection.  You can use a Map with the id as the key and the price as the value. Alternatively, you can define a MenuItem class and use that as the map value.  You may need two Maps: one for the menu items, one for the items selected by the user.  Alternatively, if you use a MenuItem class and override its equals() and hashCode() methods, you can use a List to hold the items that were selected.

In your onCreate() method, you'd configure your checkboxes to respond to click events by adding an onClick listener.  Search for "Android add onClick listeners for checkboxes"  You only need one listener method. It would use the view.getId() method to look up the id of the checkbox that was clicked and use that id to find a MenuItem object in you menu map. Then you can either add/remove that MenuItem object into your selected item list or you can simply add/subtract its price from a running total variable.

BTW, they are if-statements, not if-loops. A loop does something multiple times. An if-statement's body is executed only once and only when its conditional expression is true.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and welcome to the Ranch
 
Milos Gojic
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:  You can use a Map with the id as the key and the price as the value. Alternatively, you can define a MenuItem class and use that as the map value.  You may need two Maps: one for the menu items, one for the items selected by the user.  Alternatively, if you use a MenuItem class and override its equals() and hashCode() methods, you can use a List to hold the items that were selected.


Would you believe me that i thought this also! This is huge step for me,because before, i did not know from where to start, now i at least had some (vague) idea

BTW, they are if-statements, not if-loops. A loop does something multiple times. An if-statement's body is executed only once and only when its conditional expression is true.


I know that,but was so focused on my english ;)

Thanks i will try to fix this issue with maps
 
Giovanni Montano
Ranch Hand
Posts: 434
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Outside Janilu comments.
I would warmly invite you to consider the so called polymorphism, especially when you have bigger ifs conditions.
so that every if(condition) has a condition class.  And on top of that you have an abstract class that is implemented by all the condition1 to condition n classes.
In the case of android you have to manage the concept of Context learning how to move some Android superclasses into the constructor, following DI principle, better if with this nice library that makes your code much more testable, although has a steep learning curve.

In the end I would warmly recommend to have a look at this fantastic blog on MVP pattern for Android that explains why most of the if statements can be detached by the view, following the loose coupling principle that allow you to separate in phase of testing the Junit part with the Android( Instrumentation) one.

this is a stub of how to refactor If / switch case  conditional logic
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!