Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's no clear way to streamline that logic, afterall the process is what it is, unless you change to a SQLite SQL approach (see the latter part of my answer). It doesn't really matter too much if there is a lot of code, so long as it's easy to read and understand. I would put it in a method or class of its own to keep it tidy.</p> <p>It's never a good idea to use literal values (like the 20, 24 &amp; 30), in the main body of your code, that are at any risk of changing at some point in the future. When you need to change them it's a pain to find them all and all too easy to miss something and end up with a bug that's hard to find. If the values should very rarely change, the simplest option is to define all these constant values as static final, either at the top of the class that you are using them in, or in another static class. For example:</p> <pre><code>private static final int AGE_THRESHOLD_1_MIN = 20; private static final int AGE_THRESHOLD_1_MAX = 24; private static final int PUSHUPS_THRESHOLD_1_MIN = 20; private static final int PUSHUPS_THRESHOLD_1_MAX = 30; ... if (age &gt;= AGE_THRESHOLD_1_MIN &amp;&amp; age &lt;= AGE_THRESHOLD_1_MAX ){ if pushups &gt;= PUSHUPS_THRESHOLD_1_MIN &amp;&amp; pushup &lt;= PUSHUPS_THRESHOLD_1_MAX){ }} </code></pre> <p>Then when you need to change these values, you've got them all in one, easy to find, place. An alternative is to store them in an XML file (e.g. added to assets) that you read in at runtime. Both approaches of course require a new build when the values change.</p> <p>If you want more flexibility (and avoiding the need for rebuilding the app), you can store them in Shared Preferences (if a value is stored in Shared Prefs use that, otherwise use the static final). You could then have an option in your app to adjust the thresholds which you would save to Shared Prefs, which will persist between sessions.</p> <p>This could also be configured in a SQLite database. For example an Age table to hold each age range:</p> <pre><code>Table Age (age_id INT, minimum INT, maximum INT) 1 | 20 | 24 2 | 25 | 29 3 | 30 | 34 </code></pre> <p>Then a Pushups table holding a score for a given range of pushups, for a given age range. Note the age_id column is a foreign key back to the Age table.</p> <pre><code>Table PushUps (pushup_id INT, age_id INT, minimum INT, maximum INT, score INT) 1 | 1 | 20 | 30 | 100 2 | 1 | 31 | 40 | 200 3 | 2 | 20 | 25 | 120 4 | 2 | 26 | 34 | 240 </code></pre> <p>You could then use this query to get the score for someone aged 22 who did 35 pushups, which would return 200.</p> <pre><code>SELECT score FROM PushUps p INNER JOIN Age a ON a.age_id = p.age_id WHERE 22 &gt;= a.minimum AND 22 &lt;= a.maximum AND 35 &gt;= p.minimum and 35 &lt;= p.maximum </code></pre> <p>I feel that this is the most elegant solution. If you don't already use a SQLite database in your app, it is up to you to decide if it is worth the effort to configure one. A relational database such as SQLite is a powerful tool and worth investigating if you have not done so before.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload