Android Community
Results 1 to 4 of 4

Thread: Need help with discount calculator

  1. #1
    Join Date
    Jun 2011
    Posts
    4

    Default Need help with discount calculator


    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

  2. #2
    Join Date
    Jun 2011
    Posts
    4

    Default Re: Need help with discount calculator

    ok this is what I have done till now:

    I created an Android Project names as :MyDC

    MyDC.java

    Code:
    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:
    <?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:
    <?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:





    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. #3
    Join Date
    Jun 2011
    Posts
    4

    Default Re: Need help with discount calculator

    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:
    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:
    <?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:
    <?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>

  4. #4
    Join Date
    Jun 2011
    Posts
    4

    Default Re: Need help with discount calculator


    Anyone ? Please Help ?

Similar Threads

  1. New app: PG Calculator - scientific and financial calculator for Android
    By aquarius777 in forum All About Andorid Software
    Replies: 0
    Last Post: 05-24-2011, 09:12 AM
  2. business discount
    By crazythunder in forum Off-topic
    Replies: 4
    Last Post: 12-16-2010, 08:01 PM
  3. Sick Student Discount on Evo
    By mrtechgeek22 in forum EVO 4G
    Replies: 0
    Last Post: 11-09-2010, 02:44 PM
  4. Get a Discount on your T-Mobile Bill
    By AndroidUser in forum Off-topic
    Replies: 18
    Last Post: 01-08-2009, 01:45 AM
  5. G1 Corporate Discount
    By docprego in forum General HTC Chat
    Replies: 26
    Last Post: 11-05-2008, 08:42 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •