Need help with discount calculator

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Need help with discount calculator

Postby super » Fri Jun 10, 2011 12:54 am

I am a newbie in android dev.
I want to develop a very small and simple discount rate calculator.
in which there will be drop down menu with options like:

calculate 10%
calculate 20%
calculate 30%

Based on the selection of option from the drop down menu the discount rate should change.

After selection, there will be a textbox which should ask user to enter price.
and on the click even of "Calculate" button, the resultant amount (discounted rate) should be displayed.

I guess it is very simple for you guys. If anyone can help me with the code, i will deeply appreciate.

thank you
super
Junior Developer
Junior Developer
 
Posts: 22
Joined: Sat May 21, 2011 12:56 pm

Top

Re: Need help with discount calculator

Postby super » Sat Jun 18, 2011 9:29 pm

super
Junior Developer
Junior Developer
 
Posts: 22
Joined: Sat May 21, 2011 12:56 pm

Re: Need help with discount calculator

Postby super » Thu Jun 23, 2011 4:57 am

ok this is what I have done till now:

I created an Android Project names as :MyDC

MyDC.java

Code: Select all
package com.super.mydc;



import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;

public class MyDC extends Activity {
   
   private EditText amount1;
   private double x=0;
     private double y=2.0;
     private double z=0;
     private TextView tt;
     private Button calculate;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
       super.onCreate(icicle);
        // We want to view some very simple text, so we need a TextView
        TextView tv = new TextView(this);
        // Put some text to the newly created TextVIew
        tv.setText("Test");
        // Tell our Application to display the textView
       
       
        this.setContentView(tv);
       
        super.onCreate(icicle);
        setContentView(R.layout.main);
        initControls();
       
            }
   
    private void initControls()
    {
        amount1=(EditText)findViewById(R.id.amount1);
       
        tt=(TextView)findViewById(R.id.tt);
        calculate=(Button)findViewById(R.id.calculate);
        calculate.setOnClickListener(new Button.OnClickListener()
        {public void onClick
        (View  v) { calculate();}});
    }
   
    private void calculate()
    {
        x=Double.parseDouble(amount1.getText().toString());
        z=x-(x*y/100);
        tt.setText(Double.toString(z));
    }
}



This is main.xml

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/spinner1"></Spinner>
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Enter the price"
    />
    <EditText android:layout_height="wrap_content" android:id="@+id/amount1" android:text="" android:layout_width="match_parent"></EditText>
    <Button android:text="Calculate Result" android:id="@+id/calculate" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

<EditText android:layout_height="wrap_content" android:id="@+id/tt" android:text="" android:layout_width="match_parent"></EditText>
   
</LinearLayout>




This is string.xml

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hi, MyDC!</string>
    <string name="app_name">My, DC</string>
    <string name="spinner1">Choose discount type</string>
   
   
</resources>




This is what I am able to get:

Image

I am able to calculate discount on the button click, BUT i have hardcoded the discount rate.

I want to select discount rate based on the dropdown menu (I don't know how to set dropdown values with different discount rate in this problem)

In the dropdown menu I would like to have values like this:
Type 1 discount
Type 2 discount
Type 3 discount

Here is the respective discount rate for the above drop-down values:
Type 1 discount - 10%
Type 2 discount - 15%
Type 3 discount - 18%

PLZ HELP ME
super
Junior Developer
Junior Developer
 
Posts: 22
Joined: Sat May 21, 2011 12:56 pm

Re: Need help with discount calculator

Postby rob michael » Fri Jun 24, 2011 8:08 pm

you can populate a spinner field as follows:

In your activity something like follows

Code: Select all
//init ArrayAdapter with values from String xml
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.dropdown_values, android.R.layout.simple_spinner_item );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
//set Adapter to be used on drop down box
dropDownField.setAdapter( adapter );



And in strings.xml in /res/values/
Code: Select all
<string-array name="dropdown_values">
   <item></item>
   <item>discount 1</item>
   <item>discount 2</item>
</string-array>
Check out my blog (tutorials, examples, etc): http://automateddeveloper.blogspot.com/
Check out my Android apps website http://www.mobile.thirdmindmedia.co.uk
rob michael
Junior Developer
Junior Developer
 
Posts: 12
Joined: Sat Jun 11, 2011 3:10 pm

Re: Need help with discount calculator

Postby super » Fri Jun 24, 2011 11:26 pm

Ok rob, somehow I was able to add spinner tool on my application.

Now the question is how to select discount rate based on selected value from spinner.

PLZ HELP ME WITH THE CODE

Here is my current code:

MyDC.java

Code: Select all
package com.super.mydc;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Button;

import android.view.View;

public class MyDC extends Activity {
   
   private EditText amount1;
   private double x=0;
     private double y=2.0;
     private double z=0;
     private TextView tt;
     private Button calculate;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
       super.onCreate(icicle);
        // We want to view some very simple text, so we need a TextView
        TextView tv = new TextView(this);
        // Put some text to the newly created TextVIew
        tv.setText("Test");
        // Tell our Application to display the textView
       
       
        this.setContentView(tv);
       
        super.onCreate(icicle);
        setContentView(R.layout.main);
       
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.planets_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

       
       
        initControls();
       
       
            }
   
   
   
    private void initControls()
    {
        amount1=(EditText)findViewById(R.id.amount1);
       
        tt=(TextView)findViewById(R.id.tt);
        calculate=(Button)findViewById(R.id.calculate);
        calculate.setOnClickListener(new Button.OnClickListener()
        {public void onClick
        (View  v) { calculate();}});
    }
   
    private void calculate()
    {
        x=Double.parseDouble(amount1.getText().toString());
        z=x-(x*y/100);
        tt.setText(Double.toString(z));
    }
}




main.xml


Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/spinner1"></Spinner>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="@string/planet_prompt"
    />
    <Spinner
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/planet_prompt"
    />
   
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Enter the price"
    />
    <EditText android:layout_height="wrap_content" android:id="@+id/amount1" android:text="" android:layout_width="match_parent"></EditText>
    <Button android:text="Calculate Result" android:id="@+id/calculate" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

<EditText android:layout_height="wrap_content" android:id="@+id/tt" android:text="" android:layout_width="match_parent"></EditText>
   
</LinearLayout>




strings.xml

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hi, MyDC!</string>
    <string name="app_name">My, DC</string>
    <string name="spinner1">Choose a planet</string>
   
    <string name="planet_prompt">Choose a planet</string>
    <string-array name="planets_array">
        <item>10% discount A</item>
        <item>15% discount B</item>
        <item>18% discount C</item>
       
    </string-array>
   
</resources>

super
Junior Developer
Junior Developer
 
Posts: 22
Joined: Sat May 21, 2011 12:56 pm

Re: Need help with discount calculator

Postby rob michael » Sat Jun 25, 2011 10:10 am

Ok, so in your calculate() method right now that triggers when you press the calculate button - in there you need code like:
Code: Select all
String discountSelected = discountDropDownField.getSelectedItem().toString();


That will give you a String telling you which item has been selected (Discount 1, Discount 2)

Then you can use that to drive which discount to apply - there are prob nicer ways, but something like this should work:
Code: Select all
//default discount rate to 10%
double discount = 0.1;

if ("15% discount B".equals(discountSelected)){
    discount = 0.15;
}
else
if ("18% discount C".equals(discountSelected)){
    discount = 0.18;
}

//now apply discount rate to value entered
...
Check out my blog (tutorials, examples, etc): http://automateddeveloper.blogspot.com/
Check out my Android apps website http://www.mobile.thirdmindmedia.co.uk
rob michael
Junior Developer
Junior Developer
 
Posts: 12
Joined: Sat Jun 11, 2011 3:10 pm

Top

Re: Need help with discount calculator

Postby super » Sat Jun 25, 2011 12:51 pm

ok rob,

I did what you mentioned above:

MyDC.java:

Code: Select all
package com.super.mydc;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Button;

import android.view.View;

public class MyDC extends Activity {
   
   private EditText amount1;
   private double x=0;
     private double y=2.0;
     private double z=0;
     private TextView tt;
     private Button calculate;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
       super.onCreate(icicle);
        // We want to view some very simple text, so we need a TextView
        TextView tv = new TextView(this);
        // Put some text to the newly created TextVIew
        tv.setText("Test");
        // Tell our Application to display the textView
       
       
        this.setContentView(tv);
       
        super.onCreate(icicle);
        setContentView(R.layout.main);
       
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.planets_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        initControls();
       
            }
       
    private void initControls()
    {
        amount1=(EditText)findViewById(R.id.amount1);
       
        tt=(TextView)findViewById(R.id.tt);
        calculate=(Button)findViewById(R.id.calculate);
        calculate.setOnClickListener(new Button.OnClickListener()
        {public void onClick
        (View  v) { calculate();}});
    }
   
    private void calculate()
    {
        x=Double.parseDouble(amount1.getText().toString());
        z=x-(x*y/100);
        tt.setText(Double.toString(z));
        String discountSelected = discountDropDownField.getSelectedItem().toString();
    }
   
   
}


In my Calculate(), this line:

String discountSelected = discountDropDownField.getSelectedItem().toString();

it was giving error so I right-clicked on discountDropDownField and create new class for it

Here it is: discountDropDownField.java


Code: Select all
package com.super.mydc;

public class discountDropDownField {
   
   double discount = 0.0;{

   if ("10% discount A".equals(discountSelected)){
       discount = 0.1;
   }
   if ("15% discount B".equals(discountSelected)){
       discount = 0.15;
   }
   else
   if ("18% discount C".equals(discountSelected)){
       discount = 0.18;
   }
   }
}



If you see the code above, I changed default discount to 0.0 since I don't want any value to be selected by default in dropdown menu, so I will just an item saying "Select discount type". (I hope I am doing correctly).

But after adding all these code you provided me, I am getting a lot of error messages like this:

Code: Select all
discountSelected cannot be resolved to a variable   discountDropDownField.java line 7   Java Problem
discountSelected cannot be resolved to a variable   discountDropDownField.java line 10   Java Problem
discountSelected cannot be resolved to a variable   discountDropDownField.java line 14   Java Problem
The method getSelectedItem() is undefined for the type discountDropDownField   MyDC.java


please help...
super
Junior Developer
Junior Developer
 
Posts: 22
Joined: Sat May 21, 2011 12:56 pm

Re: Need help with discount calculator

Postby rob michael » Sat Jun 25, 2011 2:45 pm

dont need a new Class for the discount field - that should be the spinner field.
Code: Select all
Spinner discountDropDownField = (Spinner) findViewById(R.id.spinner);
String discountSelected = discountDropDownField.getSelectedItem().toString();



then once you have the discountSelecet string you can use the if block as mentioned in earlier post
Check out my blog (tutorials, examples, etc): http://automateddeveloper.blogspot.com/
Check out my Android apps website http://www.mobile.thirdmindmedia.co.uk
rob michael
Junior Developer
Junior Developer
 
Posts: 12
Joined: Sat Jun 11, 2011 3:10 pm

Re: Need help with discount calculator

Postby super » Sat Jun 25, 2011 3:49 pm

so rob, you mean I don't need "discountDropDownField.java" that I created ?

So according to you everything should be in one page right ? in MyDC.java ?

This is MyDC.java

Code: Select all
package com.super.mydc;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Button;

import android.view.View;

public class MyDC extends Activity {
   
   private EditText amount1;
   private double x=0;
     private double y=2.0;
     private double z=0;
     private TextView tt;
     private Button calculate;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
       super.onCreate(icicle);
        // We want to view some very simple text, so we need a TextView
        TextView tv = new TextView(this);
        // Put some text to the newly created TextVIew
        tv.setText("Test");
        // Tell our Application to display the textView
       
       
        this.setContentView(tv);
       
        super.onCreate(icicle);
        setContentView(R.layout.main);
       
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        Spinner discountDropDownField = (Spinner) findViewById(R.id.spinner);
        String discountSelected = discountDropDownField.getSelectedItem().toString();
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.planets_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

       
       
        initControls();   
                     
            }
       
    private void initControls()
    {
        amount1=(EditText)findViewById(R.id.amount1);
       
        tt=(TextView)findViewById(R.id.tt);
        calculate=(Button)findViewById(R.id.calculate);
        calculate.setOnClickListener(new Button.OnClickListener()
        {public void onClick
        (View  v) { calculate();}});
    }
   
    private void calculate()
    {
        x=Double.parseDouble(amount1.getText().toString());
        z=x-(x*y/100);
        tt.setText(Double.toString(z));
     
    }
   
    private void discountDropDownField {
       
       double discount = 0.0;{

       if ("10% discount A".equals(discountSelected)){
           discount = 0.1;
       }
       if ("15% discount B".equals(discountSelected)){
           discount = 0.15;
       }
       else
       if ("18% discount C".equals(discountSelected)){
           discount = 0.18;
       }
       }
    }
     
       
}


And I am getting these error messages:

Code: Select all

Description   Resource   Path   Location   Type
discountSelected cannot be resolved to a variable   MyDC.java   c   line 81   Java Problem
discountSelected cannot be resolved to a variable   MyDC.java      line 84   Java Problem
discountSelected cannot be resolved to a variable   MyDC.java      line 88   Java Problem
Syntax error, insert ";" to complete FieldDeclaration   MyDC.java      line 77   Java Problem
void is an invalid type for the variable discountDropDownField   MyDC.java      line 77   Java Problem


rob, I will deeply appreciate if you can create this program (whatever I did above) at your end. By that it will be easy for me to follow your directions and to understand the how you want me to add the code and where.

thnx
super
Junior Developer
Junior Developer
 
Posts: 22
Joined: Sat May 21, 2011 12:56 pm

Re: Need help with discount calculator

Postby super » Tue Jun 28, 2011 5:59 am

PLEASE HELP ME!!!
super
Junior Developer
Junior Developer
 
Posts: 22
Joined: Sat May 21, 2011 12:56 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: Yahoo [Bot] and 6 guests