0

i've made an app with android studio, to count money. The first row is for 1 cent, the secont row is for 2 cents.

theres a picture how it should be

everithing is fine until i got the counter to 35, then my total money makes something stupid

and i dont know why.. it's just an double that counts my money

The second row is not finished yet, first i want to know why my first row doesn't work

Heres my code:

package com.tom.dennis.kassensturz;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class kasse_geld_eingeben extends AppCompatActivity {
public double geld=0;

//money
private int cent_ein;
private int cent_zwei;

//EditText views
private EditText textout_cent_ein;
private EditText textout_cent_zwei;

private TextView textout_geld;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_kasse_geld_eingeben);

    textout_cent_ein=(EditText)findViewById(R.id.ausgabe_cent_ein);
    textout_cent_zwei=(EditText)findViewById(R.id.ausgabe_cent_zwei);
    textout_geld=(TextView)findViewById(R.id.geld_ausgabe);
    textout_cent_ein.addTextChangedListener(mTextEditorWatcher); // sollte jetzt watchen
}

private final TextWatcher  mTextEditorWatcher = new TextWatcher() //hmm
{
    public void beforeTextChanged(CharSequence s, int start, int count, int after)
    {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        try
        {
            cent_ein = Integer.parseInt(textout_cent_ein.getText().toString()); // wandelt meinen string in int um
            geld=geld_ausrechnen();
            textout_geld.setText(String.valueOf(geld));
        } catch(NumberFormatException nfe)
        { // fängt fehler ab
            System.out.println("Could not parse " + nfe);
        }

    }

    @Override
    public void afterTextChanged(Editable s)
    {

    }
};

private double  geld_ausrechnen()
{
    double betrag;
    betrag=cent_ein*0.01+cent_zwei*0.02;
    return betrag;
}


@Override
public View findViewById(int id) {
    return super.findViewById(id);
}


public void afterTextChanged(Editable s)
{
}


public void buttonOnClick_geld_plus_cent_ein(View V) {
    cent_ein = cent_ein + 1;
    textout_cent_ein.setText(String.valueOf(cent_ein));
    textout_geld.setText(String.valueOf(geld));
}

public void buttonOnClick_geld_minus_cent_ein(View V)
{
    cent_ein=cent_ein-1;
    textout_cent_ein.setText(String.valueOf(cent_ein));
    textout_geld.setText(String.valueOf(geld));
}

public void buttonOnClick_geld_plus_cent_zwei(View V) {
    cent_zwei = cent_zwei + 1;
    textout_cent_zwei.setText(String.valueOf(cent_zwei));
    geld = geld + 0.02;
    textout_geld.setText(String.valueOf(geld));
}

public void buttonOnClick_geld_minus_cent_zwei(View V)
{
    cent_zwei=cent_zwei-1;
    geld=geld-0.02;
    textout_cent_zwei.setText(String.valueOf(cent_zwei));
    textout_geld.setText(String.valueOf(geld));
}
}

and there is the code from my xml file, if someone want to try this out

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.tom.dennis.kassensturz.kasse_geld_eingeben">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/geld_zeile">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Kassen inhalt:"
            android:id="@+id/gesamtes_geld"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="122"
            android:id="@+id/geld_ausgabe"
            android:layout_toRightOf="@+id/gesamtes_geld"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="€"
            android:id="@+id/euro_zeichen"
            android:layout_toRightOf="@+id/geld_ausgabe"
            />
    </RelativeLayout>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/scrollView"
        android:layout_below="@+id/geld_zeile">
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/cent_ein">

                <ImageView
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:id="@+id/bild_cent_ein"
                    android:src="#050505"
                     />

                <!-- inputType und ems damit nur zahlen eingegeben werden können -->
                <EditText
                    android:layout_width="0dp"
                    android:minWidth="100dp"
                    android:layout_height="wrap_content"
                    android:inputType="number"
                    android:ems="10"
                    android:id="@+id/ausgabe_cent_ein"
                    android:layout_weight=".30"
                    android:editable="false" />

                <Button
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:text="+"
                    android:id="@+id/button_geld_plus_cent_ein"
                    android:onClick="buttonOnClick_geld_plus_cent_ein" />

                <Button
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:text="-"
                    android:id="@+id/button_geld_minus_cent_ein"
                    android:layout_toRightOf="@+id/button_geld_plus_cent_ein"
                    android:onClick="buttonOnClick_geld_minus_cent_ein" />

            </LinearLayout>


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/cent_zwei"
                android:layout_below="@id/cent_ein"
                android:paddingTop="10dp">

                <ImageView
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:id="@+id/bild_cent_zwei"
                    android:src="#050505"
                    />

                <!-- inputType und ems damit nur zahlen eingegeben werden können -->
                <EditText
                    android:layout_width="match_parent"
                    android:minWidth="260dp"
                    android:layout_height="wrap_content"
                    android:inputType="number"
                    android:ems="10"
                    android:id="@+id/ausgabe_cent_zwei"
                    android:layout_weight=".4"
                    android:editable="false" />

                <Button
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:text="+"
                    android:id="@+id/button_geld_plus_cent_zwei"
                    android:onClick="buttonOnClick_geld_plus_cent_zwei" />

                <Button
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:text="-"
                    android:id="@+id/button_geld_minus_cent_zwei"
                    android:layout_toRightOf="@+id/button_geld_plus_cent_ein"
                    android:onClick="buttonOnClick_geld_minus_cent_zwei" />

            </LinearLayout>
            <!--
            <LinearLayout
                android:layout_below="@id/cent_ein"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="10dp">

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:id="@+id/bild_cent_zwei"
                android:src="#050505"
                android:layout_below="@+id/cent_ein"
                />

            <EditText
                android:layout_width="0dp"
                android:minWidth="100dp"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:ems="10"
                android:layout_weight=".30"
                android:editable="false"
                android:id="@+id/ausgabe_cent_zwei"
                />

            <Button
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:text="+"
                android:id="@+id/button_geld_plus_cent_zwei"
                />

            <Button
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:text="-"
                android:id="@+id/button_geld_minus_cent_zwei"
            />

            </LinearLayout>-->
        </RelativeLayout>
    </ScrollView>




</RelativeLayout>
  • Why you dont use integer and make total in cents. Then to display it you can use the total of cents divided by 100. Also it is good practice to us camelcase for class name, method, and variable in java. – Kevin LE GOFF Nov 24 '15 at 19:45

1 Answers1

0

In floating point units 0.1 != 0.1. Fab Sa wrote a perfect explanation about the reason.

See his answer of this post.

My suggestion, save the result in two separate variable. One for the integer part and one for the decimal part.

Community
  • 1
  • 1
malefiz
  • 39
  • 4