开发者

Bill Splitter Calculator Android App

开发者 https://www.devze.com 2023-03-14 08:50 出处:网络
I\'m trying to create an Android app to help to calculate split the bill if let\'s say you\'re eating out in a party of 2 people or more.

I'm trying to create an Android app to help to calculate split the bill if let's say you're eating out in a party of 2 people or more.

You're supposed to enter the subtotal of the bill, enter the number of people in the party, enter applicable discount if any, there are 2 checkboxes for 7% tax, and 10% service charges if it hasn't been included in the bill yet. Finally you just need to click on the "calculate button" for the app to calculate how much each person has to pay.

My Questions are:

  1. for subtotal, it's supposed to be double instead of int, but I'm not sure how to parse String into a double. Is there a way to do this?
  2. I'm not sure if that is the best way to activate the Checkboxes for the tax and 10% tips
  3. When I click on the calculate button, it is supposed to display the Toast message with the result of the calculation, but nothing appears. I'm not sure if the problem is with parseInteger, checkBoxes, or if the onClick method is wrong, or all of them.

Here's the code that I wrote:

    package com.kevinw.BillSplitter;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class BillSplitter extends Activity implements OnClickListener {
/** Declares XML Widgets */
private EditText numberDiners;
private EditText enterAmount;
private EditText enterDiscount;
private CheckBox gst;
private CheckBox tips;
private CheckBox cess;
double result;
private Button calculate;
private TextView resultAmount;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


//Initialize Widgets
numberDiners = (EditText) findViewById(R.id.numberDiners);
enterAmount = (EditText) findViewById(R.id.EnterAmount);
enterDiscount = (EditText) findViewById(R.id.EnterDiscount);
calculate = (Button) findViewById(R.id.calculate);   

//Initialize CheckBoxes
gst = (CheckBox) findViewById(R.id.cbCheck1);
gst.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (gst.isChecked()) {
        result = result + (0.07 * result);
}
else {
    result = result;
        }
    }
});


tips = (CheckBox) findViewById(R.id.cbCheck2);
tips.setOnCheckedChangeListener(new开发者_C百科 CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (tips.isChecked()) {
            result = result + (0.1 * result);       
        }
        else {
            result = result;
        }   
    }
});

}  


@Override
public void onClick(View v) {

//Initialize EditTexts
String amount = enterAmount.getText().toString();
int subtotal = Integer.parseInt(amount);

String diners = numberDiners.getText().toString();
int people = Integer.parseInt(diners);

String disc = enterDiscount.getText().toString();
int discount = Integer.parseInt(disc);
double discounted = discount / 100;
result = (1 - discounted) * (subtotal / people);    

switch (v.getId()) {
case(R.id.calculate):
    Toast.makeText(this, "The Amount a Person has to pay: $" + result,     Toast.LENGTH_LONG).show();
break;
    }
}
}    

and if it helps, this is the XML code for the layout:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
    android:id="@+id/dinersView"  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"/>
<TextView
    android:id="@+id/Enter"  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/dinersView" 
    android:layout_alignLeft="@+id/EnterAmount"
    android:text="@string/enter"/>
<EditText
    android:id="@+id/numberDiners"
    android:layout_height="wrap_content"
    android:layout_below="@+id/dinersView"
    android:layout_width="100dip"/>
<EditText
    android:id="@+id/EnterAmount"  
    android:layout_height="wrap_content"
    android:layout_below="@+id/Enter"
    android:layout_toRightOf="@+id/numberDiners" 
    android:layout_width="220dip"/>
<TextView
    android:id="@+id/Discount"  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_below="@+id/EnterAmount" 
    android:layout_alignLeft="@+id/EnterAmount"
    android:text="@string/discount"/>
<EditText
    android:id="@+id/EnterDiscount"  
    android:layout_height="wrap_content"
    android:layout_below="@+id/Discount"
    android:layout_alignLeft="@+id/Discount"
    android:layout_width="220dip"/>
<CheckBox
    android:id="@+id/cbCheck1"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_below="@+id/EnterDiscount" />
<CheckBox
    android:id="@+id/cbCheck2"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_below="@+id/cbCheck1" /> 
<TextView
    android:id="@+id/gst"  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/cbCheck1" 
    android:layout_alignTop="@+id/cbCheck1"
    android:layout_alignLeft="@+id/enterDiscount"   
    android:layout_marginTop="10dip"
    android:text="@string/GST"/>    
<TextView
    android:id="@+id/tips"  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/cbCheck2" 
    android:layout_alignTop="@+id/cbCheck2"
    android:layout_marginTop="10dip"
    android:text="@string/tips"/>
<Button 
    android:id="@+id/calculate"
    android:layout_below="@+id/cbCheck2"
    android:layout_width="wrap_content" 
    android:text="@string/calculate" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true"/>
</RelativeLayout>

Thank you for all of the help. Really appreciate it.


For string to double conversion you can use Double.valueOf("").

you need to add clicklistener to your calculator button, either add calculate.setOnClickListener(this);

or move your onClick code without switch case inside this block.

    calculate.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });


for subtotal, it's supposed to be double instead of int, but I'm not sure how to parse String into a double. Is there a way to do this?

Yes there is.

I'm not sure if that is the best way to activate the Checkboxes for the tax and 10% tips

That is not a question but, moving on, no, that is not the right way to write checkbox code.

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (gst.isChecked()) {
        result = result + (0.07 * result);
    } else {
        result = result;
    }
}

This code will not work as you expect it to. The price will just keep going up and up on each second tick of the checkbox. You may want to step through what will actually happen in your head or on paper and then try and rethink it.

When I click on the calculate button, it is supposed to display the Toast message with the result of the calculation, but nothing appears. I'm not sure if the problem is with parseInteger, checkBoxes, or if the onClick method is wrong, or all of them.

I think that you will find that the onClick function is never called because you have not called the setOnClickListener function. I think that might be the problem but I'm not sure.

0

精彩评论

暂无评论...
验证码 换一张
取 消