0% found this document useful (0 votes)
14 views4 pages

Assignment 3 MC 007

Uploaded by

sujata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Assignment 3 MC 007

Uploaded by

sujata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

SIES College of Management Studies SYMCA (Revised), Sem III,

Roll No: 07

Assignment - 3

Currency converter (Using spinner to list the currencies)


Code:
MainActivity.java:
package com.example.p3;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.Objects;

public class MainActivity extends AppCompatActivity {

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

Spinner spinner1, spinner2;


EditText editText;
Button btn1;
TextView textView4;
String[] currencies;

currencies = getResources().getStringArray(R.array.currencies);

spinner1 = findViewById(R.id.spinner1);
spinner2 = findViewById(R.id.spinner2);
btn1 = findViewById(R.id.button);
editText = findViewById(R.id.editText);
textView4 = findViewById(R.id.textView4);

Subject: MCAL34 Mobile Computing Lab Academic Year


First Half 2023_24
SIES College of Management Studies SYMCA (Revised), Sem III,
Roll No: 07

ArrayAdapter<String> adapter = new ArrayAdapter<>(this,


android.R.layout.simple_spinner_item, currencies);

// Set the adapter for both Spinners


spinner1.setAdapter(adapter);
spinner2.setAdapter(adapter);

// Set an onClick listener for the button


btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

// Get the selected currency from each Spinner


String fromCurrency = currencies[spinner1.getSelectedItemPosition()];
String toCurrency = currencies[spinner2.getSelectedItemPosition()];

// Get the amount entered in the EditText field


float amount = Float.parseFloat(editText.getText().toString());

// Convert the amount


double convertedAmount = amount * getConversionRate(fromCurrency,
toCurrency);

// Display the converted amount in the TextView


textView4.setText(String.valueOf(convertedAmount));
}

});
}
private double getConversionRate(String fromCurrency, String toCurrency) {
double con = 0;
if(Objects.equals(fromCurrency, "INR") && Objects.equals(toCurrency, "USD")) {
con = 0.012;
} else if (Objects.equals(fromCurrency, "INR") && Objects.equals(toCurrency, "JPY")) {
con = 1.79;
} else if (Objects.equals(fromCurrency, "USD") && Objects.equals(toCurrency, "INR")) {
con = 83.13;
} else if (Objects.equals(fromCurrency, "USD") && Objects.equals(toCurrency, "JPY")) {
con = 148.80;
} else if (Objects.equals(fromCurrency, "JPY") && Objects.equals(toCurrency, "INR")) {
con = 0.56;
Subject: MCAL34 Mobile Computing Lab Academic Year
First Half 2023_24
SIES College of Management Studies SYMCA (Revised), Sem III,
Roll No: 07

} else if (Objects.equals(fromCurrency, "JPY") && Objects.equals(toCurrency, "USD")) {


con = 0.0067;
} else if (Objects.equals(fromCurrency, "INR") && Objects.equals(toCurrency, "INR")
|| Objects.equals(fromCurrency, "USD") && Objects.equals(toCurrency, "USD")
|| Objects.equals(fromCurrency, "JPY") && Objects.equals(toCurrency, "JPY" )) {
con = 1;
}
return con;
}
}

Currencies.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="currencies">
<item>INR</item>
<item>USD</item>
<item>JPY</item>
</string-array>
</resources>

Subject: MCAL34 Mobile Computing Lab Academic Year


First Half 2023_24
SIES College of Management Studies SYMCA (Revised), Sem III,
Roll No: 07

Output:

Subject: MCAL34 Mobile Computing Lab Academic Year


First Half 2023_24

You might also like