mirror of
https://github.com/LucasVbr/convertisseur-app.git
synced 2026-05-13 17:12:09 +00:00
feat: Add Réaumur and Rankine units, javadoc
This commit is contained in:
@@ -13,7 +13,7 @@
|
||||
android:theme="@style/Theme.Convertisseur"
|
||||
tools:targetApi="31">
|
||||
<activity
|
||||
android:name=".ConvertisseurMainActivity"
|
||||
android:name=".MainActivity"
|
||||
android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
+12
-1
@@ -1,3 +1,9 @@
|
||||
/*
|
||||
* ConvertisseurMainActivity.java, 24/10/2024
|
||||
* UPPA M2 TI 2024-2025
|
||||
* MIT License (MIT)
|
||||
*/
|
||||
|
||||
package fr.univpau.convertisseur;
|
||||
|
||||
import android.os.Bundle;
|
||||
@@ -9,7 +15,12 @@ import androidx.core.graphics.Insets;
|
||||
import androidx.core.view.ViewCompat;
|
||||
import androidx.core.view.WindowInsetsCompat;
|
||||
|
||||
public class ConvertisseurMainActivity extends AppCompatActivity {
|
||||
/**
|
||||
* Main activity of the application.
|
||||
*
|
||||
* @author LucasVbr
|
||||
*/
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -1,3 +1,10 @@
|
||||
/*
|
||||
* OnConvertListener.java, 24/10/2024
|
||||
* UPPA M2 TI 2024-2025
|
||||
* MIT License (MIT)
|
||||
*/
|
||||
|
||||
|
||||
package fr.univpau.convertisseur;
|
||||
|
||||
import android.util.Log;
|
||||
@@ -5,13 +12,32 @@ import android.view.View;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Spinner;
|
||||
|
||||
/**
|
||||
* Listener for the convert button.
|
||||
*
|
||||
* @author LucasVbr
|
||||
*/
|
||||
public class OnConvertListener implements View.OnClickListener {
|
||||
|
||||
/** The spinner for the unit to convert from. */
|
||||
private final Spinner spinnerFrom;
|
||||
|
||||
/** The spinner for the unit to convert to. */
|
||||
private final Spinner spinnerTo;
|
||||
|
||||
/** The input field for the value to convert. */
|
||||
private final EditText inputFrom;
|
||||
|
||||
/** The input field for the result of the conversion. */
|
||||
private final EditText inputTo;
|
||||
|
||||
/**
|
||||
* Create a new listener for the convert button.
|
||||
* @param spinnerFrom The spinner for the unit to convert from.
|
||||
* @param spinnerTo The spinner for the unit to convert to.
|
||||
* @param inputFrom The input field for the value to convert.
|
||||
* @param inputTo The input field for the result of the conversion.
|
||||
*/
|
||||
public OnConvertListener(Spinner spinnerFrom, Spinner spinnerTo, EditText inputFrom, EditText inputTo) {
|
||||
this.spinnerFrom = spinnerFrom;
|
||||
this.spinnerTo = spinnerTo;
|
||||
@@ -35,12 +61,22 @@ public class OnConvertListener implements View.OnClickListener {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value from an input field.
|
||||
* @param input The input field.
|
||||
* @return The value from the input field as a double.
|
||||
*/
|
||||
private double getInputValue(EditText input) {
|
||||
String inputFromText = input.getText().toString();
|
||||
if (inputFromText.isEmpty()) return 0;
|
||||
return Double.parseDouble(inputFromText);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the unit from a spinner.
|
||||
* @param spinner The spinner.
|
||||
* @return The unit from the spinner.
|
||||
*/
|
||||
private Temperature.Units getUnit(Spinner spinner) {
|
||||
int unitIndex = spinner.getSelectedItemPosition();
|
||||
return Temperature.Units.values()[unitIndex];
|
||||
|
||||
@@ -1,22 +1,52 @@
|
||||
/*
|
||||
* Temperature.java, 24/10/2024
|
||||
* UPPA M2 TI 2024-2025
|
||||
* MIT License (MIT)
|
||||
*/
|
||||
|
||||
package fr.univpau.convertisseur;
|
||||
|
||||
/**
|
||||
* Class representing a temperature value.
|
||||
*
|
||||
* @author LucasVbr
|
||||
*/
|
||||
public class Temperature {
|
||||
|
||||
/** Enumeration of the units for temperature. */
|
||||
public enum Units {
|
||||
CELSIUS, FAHRENHEIT, KELVIN
|
||||
CELSIUS, FAHRENHEIT, KELVIN, RANKINE, REAUMUR
|
||||
}
|
||||
|
||||
/** The value of the temperature in Kelvin. */
|
||||
double valueInKelvin;
|
||||
|
||||
/**
|
||||
* Create a new temperature value.
|
||||
*
|
||||
* @param value The value of the temperature.
|
||||
* @param from The unit of the temperature value.
|
||||
*/
|
||||
public Temperature(double value, Temperature.Units from) {
|
||||
if (from == Units.CELSIUS) this.valueInKelvin = value + 273.15;
|
||||
if (from == Units.FAHRENHEIT) this.valueInKelvin = (value - 32) * 5 / 9 + 273.15;
|
||||
if (from == Units.KELVIN) this.valueInKelvin = value;
|
||||
if (from == Units.RANKINE) this.valueInKelvin = (value - 491.67) * 5 / 9;
|
||||
if (from == Units.REAUMUR) this.valueInKelvin = value * 5 / 4 + 273.15;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the temperature to the given unit.
|
||||
*
|
||||
* @param to The unit to convert to.
|
||||
* @return The value of the temperature in the given unit.
|
||||
*/
|
||||
public double convert(Units to) {
|
||||
if (to == Units.CELSIUS) return this.valueInKelvin - 273.15;
|
||||
if (to == Units.FAHRENHEIT) return (this.valueInKelvin - 273.15) * 9 / 5 + 32;
|
||||
if (to == Units.RANKINE) return (this.valueInKelvin) * 9 / 5;
|
||||
if (to == Units.REAUMUR) return (this.valueInKelvin - 273.15) * 4 / 5;
|
||||
|
||||
return this.valueInKelvin;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
android:id="@+id/main"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".ConvertisseurMainActivity">
|
||||
tools:context=".MainActivity">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
android:id="@+id/main"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".ConvertisseurMainActivity">
|
||||
tools:context=".MainActivity">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
|
||||
@@ -8,5 +8,7 @@
|
||||
<item>Celsius</item>
|
||||
<item>Fahrenheit</item>
|
||||
<item>Kelvin</item>
|
||||
<item>Rankine</item>
|
||||
<item>Réaumur</item>
|
||||
</string-array>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user