views:

132

answers:

2

Before I delve into it, I'm very new to Android and I have just started learning Java last month. I've hit bumps while trying to develop my first simple app. Most of these hurdles were jumped thanks to random tutorials online. MY CODE IS VERY MESSY. Any tips are appreciated.

The question above is quite broad, but this is what I want to do: It's a essentially a blood alcohol content calculator / drink keeper-tracker. Basic layout: http://i.imgur.com/JGuh7.jpg

The buttons along the bottom are just regular buttons, not ImageButtons (had problems with that) Here's some example code of one:

<Button android:id="@+id/Button01"
                    android:layout_width="wrap_content"
                    android:layout_marginRight="5dp"
                    android:background="@drawable/addbeer"/>

The buttons and TextView are all in main.xml.

I have variables defined in a class called Global.java:

package com.dantoth.drinkingbuddy;

import android.app.Activity;

public class Global extends Activity{

public static double StandardDrinks = 0;
public static double BeerOunces = 12;
public static double BeerPercentAlcohol = .05;
public static double BeerDrink = BeerOunces * BeerPercentAlcohol;
public static double BeerDrinkFinal = BeerDrink * 1.6666666;
public static double ShotOunces = 1.5;
public static double ShotPercentAlcohol = .4;
public static double ShotDrink = ShotOunces * ShotPercentAlcohol;
public static double ShotDrinkFinal = ShotDrink * 1.6666666;
public static double WineOunces = 5;
public static double WinePercentAlcohol = .12;
public static double WineDrink = WineOunces * WinePercentAlcohol;
public static double WineDrinkFinal = WineDrink * 1.6666666;
public static double OtherOunces;
public static double OtherPercentAlcohol;
public static double OtherDrink = OtherOunces * (OtherPercentAlcohol * .01);
public static double OtherDrinkFinal = OtherDrink * 1.6666666;
public static double GenderConstant = 7.5; //9 for female
public static double Weight = 180;
public static double TimeDrinking = 60;
public static double Hours = TimeDrinking / 60;
public static double Bac = ((StandardDrinks / 2) * (GenderConstant / Weight)) - (0.017 * Hours);

}

The last variable is the important part. It calculates your BAC based on the factors involved.

When I press the add beer button (Button01) I make it add 1 to StandardDrinks, simulating drinking one beer. The other variables in the Bac formula have values assigned to them in Global.java.

The code that makes the beer button do stuff is in my regular class, drinkingbuddy.java:

public class DrinkingBuddy extends Activity {
/** Called when the activity is first created. */


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button button = (Button) findViewById(R.id.Button01);
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Global.StandardDrinks = Global.StandardDrinks + Global.BeerDrinkFinal;
            Toast.makeText(DrinkingBuddy.this, "Mmmm... Beer", Toast.LENGTH_SHORT).show();

        }
        });

By my perception, StandardDrinks should now have a value of 1. However, when I click the Calculate BAC button (Button05) it merely outputs the variable Bac as if StandardDrinks was still set to 0. Here is the code for the Calculate BAC button (Button05):

Button button4 = (Button) findViewById(R.id.Button05); button4.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) {

            TextView texty;

            texty = (TextView) findViewById(R.id.texty1);

            texty.setText("Your BAC is " + Global.Bac );

        }
        });

It outputs the following to the text view: "Your BAC is -0.017". This is the Bac value for if StandardDrinks was still 0, so obviously there is some problem communicating between the classes. Can anyone help me??

The other elements of the formula (weight, time spent drinking, and the alcohol %'s and such) are variables because I will ultimately allow the user to change those values in the settings.

I've heard around the water cooler that global variables are not good programming style, but this is the closest I've come to getting it to work. Any other ways of doing it are very much welcomed!

+1  A: 

There could be a very long response in regards to coding style and structure but i'll skip that and keep to the upfront issues.

Just because you update the value of a variable does not mean that any calculation that has ever taken place with that variable is updated. In this context changing the value of StandardDrinks does not cause Bac to be recalculated automatically. Specifically the code to assign the value of class static member variables is run once during class setup although i don't actually know exactly when the dalvik VM does this. Point is every time StandardDrinks changes value you need to recalculate Bac.

Mark
Point 1 is wrong. I think you mean that variables defined as 'final' cannot be changed. Declaring 'static' means there is one variable for the class, instead of one for the instance.Point 2 is correct.
Skip Head
+1  A: 

There are some logical errors in your program.

public static double Bac = ((StandardDrinks / 2) * (GenderConstant / Weight)) - (0.017 * Hours);

This variable Bac would be initialized with the value of the formula. The further changes in the variables used in the formula to calculate BAC wouldn't reflect back to it unless you explicitly do so. I would recommend having a function to update BAC as follows

You could do all the above in a single activity.

       public class DrinkingBuddy extends Activity {
/** Called when the activity is first created. */

 double StandardDrinks = 0;
 double BeerOunces = 12;
double BeerPercentAlcohol = .05;
 double BeerDrink = BeerOunces * BeerPercentAlcohol;
 double BeerDrinkFinal = BeerDrink * 1.6666666;
 double ShotOunces = 1.5;
 double ShotPercentAlcohol = .4;
 double ShotDrink = ShotOunces * ShotPercentAlcohol;
 double ShotDrinkFinal = ShotDrink * 1.6666666;
 double WineOunces = 5;
 double WinePercentAlcohol = .12;
double WineDrink = WineOunces * WinePercentAlcohol;
double WineDrinkFinal = WineDrink * 1.6666666;
 double OtherOunces;
 double OtherPercentAlcohol;
 double OtherDrink = OtherOunces * (OtherPercentAlcohol * .01);
 double OtherDrinkFinal = OtherDrink * 1.6666666;
 double GenderConstant = 7.5; //9 for female
 double Weight = 180;
 double TimeDrinking = 60;
 double Hours = TimeDrinking / 60;
 double Bac;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button button = (Button) findViewById(R.id.Button01);
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            StandardDrinks = StandardDrinks + BeerDrinkFinal;
            Toast.makeText(DrinkingBuddy.this, "Mmmm... Beer", Toast.LENGTH_SHORT).show();

        }
        });

Button button4 = (Button) findViewById(R.id.Button05); 
button4.setOnClickListener(new OnClickListener() 
{ 
@Override 
public void onClick(View v) {

            TextView texty;
            Bac = ((StandardDrinks / 2) * (GenderConstant / Weight)) - (0.017 * Hours);
            texty = (TextView) findViewById(R.id.texty1);
            texty.setText("Your BAC is " + Bac );

        }
        });

MY CODE IS VERY MESSY.

Very messy indeed. I had a hard time removing all the unnecessary public and static declarations. This document may help on conventions to follow while writing java code.

As per quoted from that document

Variables should be initialized where they are declared and they should be declared in the smallest scope possible.

Hope it helps a bit in the right direction.

primalpop