1. Download our Official Android App: Forums for Android!

Apps Need help with discount calculator

Discussion in 'Android Development' started by iamsuper123, Jun 23, 2011.

  1. iamsuper123

    iamsuper123 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    4
    Joined:
    Jun 23, 2011

    Jun 23, 2011
    4
    0
    5
    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
     

    Advertisement

  2. iamsuper123

    iamsuper123 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    4
    Joined:
    Jun 23, 2011

    Jun 23, 2011
    4
    0
    5
    ok this is what I have done till now:

    I created an Android Project names as :MyDC

    MyDC.java

    Code (Text):
    1.  
    2. package com.super.mydc;
    3.  
    4.  
    5.  
    6. import android.app.Activity;
    7. import android.os.Bundle;
    8. import android.widget.EditText;
    9. import android.widget.TextView;
    10. import android.widget.Button;
    11. import android.view.View;
    12.  
    13. public class MyDC extends Activity {
    14.    
    15.     private EditText amount1;
    16.     private double x=0;
    17.       private double y=2.0;
    18.       private double z=0;
    19.       private TextView tt;
    20.       private Button calculate;
    21.     /** Called when the activity is first created. */
    22.     @Override
    23.     public void onCreate(Bundle icicle) {
    24.         super.onCreate(icicle);
    25.         // We want to view some very simple text, so we need a TextView
    26.         TextView tv = new TextView(this);
    27.         // Put some text to the newly created TextVIew
    28.         tv.setText("Test");
    29.         // Tell our Application to display the textView
    30.        
    31.        
    32.         this.setContentView(tv);
    33.        
    34.         super.onCreate(icicle);
    35.         setContentView(R.layout.main);
    36.         initControls();
    37.        
    38.             }
    39.    
    40.     private void initControls()
    41.     {
    42.         amount1=(EditText)findViewById(R.id.amount1);
    43.        
    44.         tt=(TextView)findViewById(R.id.tt);
    45.         calculate=(Button)findViewById(R.id.calculate);
    46.         calculate.setOnClickListener(new Button.OnClickListener()
    47.         {public void onClick
    48.         (View  v) { calculate();}});
    49.     }
    50.    
    51.     private void calculate()
    52.     {
    53.         x=Double.parseDouble(amount1.getText().toString());
    54.         z=x-(x*y/100);
    55.         tt.setText(Double.toString(z));
    56.     }
    57. }
    58.  

    This is main.xml

    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     android:orientation="vertical"
    5.     android:layout_width="fill_parent"
    6.     android:layout_height="fill_parent"
    7.     >
    8.     <Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/spinner1"></Spinner>
    9. <TextView  
    10.     android:layout_width="fill_parent"
    11.     android:layout_height="wrap_content"
    12.     android:text="Enter the price"
    13.     />
    14.     <EditText android:layout_height="wrap_content" android:id="@+id/amount1" android:text="" android:layout_width="match_parent"></EditText>
    15.     <Button android:text="Calculate Result" android:id="@+id/calculate" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    16.  
    17. <EditText android:layout_height="wrap_content" android:id="@+id/tt" android:text="" android:layout_width="match_parent"></EditText>
    18.    
    19. </LinearLayout>
    20.  
    21.  

    This is string.xml

    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <resources>
    4.     <string name="hello">Hi, MyDC!</string>
    5.     <string name="app_name">My, DC</string>
    6.     <string name="spinner1">Choose discount type</string>
    7.    
    8.    
    9. </resources>
    10.  
    11.  
    This is what I am able to get:

    [​IMG]


    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

     
  3. rwagnes

    rwagnes Newbie
    Rank:
    None
    Points:
    36
    Posts:
    28
    Joined:
    Jun 4, 2011

  4. iamsuper123

    iamsuper123 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    4
    Joined:
    Jun 23, 2011

    Jun 23, 2011
    4
    0
    5
    thanks for the link, I will try that code.

    Could you PLEASE help me with rest of the code for this problem ? Even if I am able to get the dropdown menu working, I don't know how to make it work in the logic for calculating discount.

    I will deeply appreciate any kind of help.
     
  5. rwagnes

    rwagnes Newbie
    Rank:
    None
    Points:
    36
    Posts:
    28
    Joined:
    Jun 4, 2011

    Follow this tutorial: Spinner | Android Developers

    Once you get the current spinner value, do some sort of if or switch statement to set the multiplier (i.e. the number to multiply times price)
     
  6. iamsuper123

    iamsuper123 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    4
    Joined:
    Jun 23, 2011

    Jun 23, 2011
    4
    0
    5
    Ok 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 (Text):
    1.  
    2. package com.super.mydc;
    3.  
    4. import android.app.Activity;
    5. import android.os.Bundle;
    6. import android.widget.ArrayAdapter;
    7. import android.widget.EditText;
    8. import android.widget.Spinner;
    9. import android.widget.TextView;
    10. import android.widget.Button;
    11.  
    12. import android.view.View;
    13.  
    14. public class MyDC extends Activity {
    15.    
    16.     private EditText amount1;
    17.     private double x=0;
    18.       private double y=2.0;
    19.       private double z=0;
    20.       private TextView tt;
    21.       private Button calculate;
    22.     /** Called when the activity is first created. */
    23.     @Override
    24.     public void onCreate(Bundle icicle) {
    25.         super.onCreate(icicle);
    26.         // We want to view some very simple text, so we need a TextView
    27.         TextView tv = new TextView(this);
    28.         // Put some text to the newly created TextVIew
    29.         tv.setText("Test");
    30.         // Tell our Application to display the textView
    31.        
    32.        
    33.         this.setContentView(tv);
    34.        
    35.         super.onCreate(icicle);
    36.         setContentView(R.layout.main);
    37.        
    38.         Spinner spinner = (Spinner) findViewById(R.id.spinner);
    39.         ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
    40.                 this, R.array.planets_array, android.R.layout.simple_spinner_item);
    41.         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    42.         spinner.setAdapter(adapter);
    43.  
    44.        
    45.        
    46.         initControls();
    47.        
    48.        
    49.             }
    50.    
    51.    
    52.    
    53.     private void initControls()
    54.     {
    55.         amount1=(EditText)findViewById(R.id.amount1);
    56.        
    57.         tt=(TextView)findViewById(R.id.tt);
    58.         calculate=(Button)findViewById(R.id.calculate);
    59.         calculate.setOnClickListener(new Button.OnClickListener()
    60.         {public void onClick
    61.         (View  v) { calculate();}});
    62.     }
    63.    
    64.     private void calculate()
    65.     {
    66.         x=Double.parseDouble(amount1.getText().toString());
    67.         z=x-(x*y/100);
    68.         tt.setText(Double.toString(z));
    69.     }
    70. }
    71.  


    main.xml


    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     android:orientation="vertical"
    5.     android:layout_width="fill_parent"
    6.     android:layout_height="fill_parent"
    7.     >
    8.     <Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/spinner1"></Spinner>
    9.     <TextView
    10.         android:layout_width="fill_parent"
    11.         android:layout_height="wrap_content"
    12.         android:layout_marginTop="10dip"
    13.         android:text="@string/planet_prompt"
    14.     />
    15.     <Spinner
    16.         android:id="@+id/spinner"
    17.         android:layout_width="fill_parent"
    18.         android:layout_height="wrap_content"
    19.         android:prompt="@string/planet_prompt"
    20.     />
    21.    
    22. <TextView  
    23.     android:layout_width="fill_parent"
    24.     android:layout_height="wrap_content"
    25.     android:text="Enter the price"
    26.     />
    27.     <EditText android:layout_height="wrap_content" android:id="@+id/amount1" android:text="" android:layout_width="match_parent"></EditText>
    28.     <Button android:text="Calculate Result" android:id="@+id/calculate" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    29.  
    30. <EditText android:layout_height="wrap_content" android:id="@+id/tt" android:text="" android:layout_width="match_parent"></EditText>
    31.    
    32. </LinearLayout>
    33.  
    34.  

    strings.xml

    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <resources>
    4.     <string name="hello">Hi, MyDC!</string>
    5.     <string name="app_name">My, DC</string>
    6.     <string name="spinner1">Choose a planet</string>
    7.    
    8.     <string name="planet_prompt">Choose a planet</string>
    9.     <string-array name="planets_array">
    10.         <item>10% discount A</item>
    11.         <item>15% discount B</item>
    12.         <item>18% discount C</item>
    13.        
    14.     </string-array>
    15.    
    16. </resources>
    17.  
    18.  
     

Share This Page

Loading...