feat: Add Réaumur and Rankine units, javadoc

This commit is contained in:
Lucàs
2024-10-24 15:09:51 +02:00
parent e17e2e136b
commit 4635a4328b
7 changed files with 84 additions and 5 deletions
+1 -1
View File
@@ -13,7 +13,7 @@
android:theme="@style/Theme.Convertisseur" android:theme="@style/Theme.Convertisseur"
tools:targetApi="31"> tools:targetApi="31">
<activity <activity
android:name=".ConvertisseurMainActivity" android:name=".MainActivity"
android:exported="true"> android:exported="true">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
@@ -1,3 +1,9 @@
/*
* ConvertisseurMainActivity.java, 24/10/2024
* UPPA M2 TI 2024-2025
* MIT License (MIT)
*/
package fr.univpau.convertisseur; package fr.univpau.convertisseur;
import android.os.Bundle; import android.os.Bundle;
@@ -9,7 +15,12 @@ import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat; import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat; import androidx.core.view.WindowInsetsCompat;
public class ConvertisseurMainActivity extends AppCompatActivity { /**
* Main activity of the application.
*
* @author LucasVbr
*/
public class MainActivity extends AppCompatActivity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { 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; package fr.univpau.convertisseur;
import android.util.Log; import android.util.Log;
@@ -5,13 +12,32 @@ import android.view.View;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Spinner; import android.widget.Spinner;
/**
* Listener for the convert button.
*
* @author LucasVbr
*/
public class OnConvertListener implements View.OnClickListener { public class OnConvertListener implements View.OnClickListener {
/** The spinner for the unit to convert from. */
private final Spinner spinnerFrom; private final Spinner spinnerFrom;
/** The spinner for the unit to convert to. */
private final Spinner spinnerTo; private final Spinner spinnerTo;
/** The input field for the value to convert. */
private final EditText inputFrom; private final EditText inputFrom;
/** The input field for the result of the conversion. */
private final EditText inputTo; 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) { public OnConvertListener(Spinner spinnerFrom, Spinner spinnerTo, EditText inputFrom, EditText inputTo) {
this.spinnerFrom = spinnerFrom; this.spinnerFrom = spinnerFrom;
this.spinnerTo = spinnerTo; 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) { private double getInputValue(EditText input) {
String inputFromText = input.getText().toString(); String inputFromText = input.getText().toString();
if (inputFromText.isEmpty()) return 0; if (inputFromText.isEmpty()) return 0;
return Double.parseDouble(inputFromText); 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) { private Temperature.Units getUnit(Spinner spinner) {
int unitIndex = spinner.getSelectedItemPosition(); int unitIndex = spinner.getSelectedItemPosition();
return Temperature.Units.values()[unitIndex]; 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; package fr.univpau.convertisseur;
/**
* Class representing a temperature value.
*
* @author LucasVbr
*/
public class Temperature { public class Temperature {
/** Enumeration of the units for temperature. */
public enum Units { public enum Units {
CELSIUS, FAHRENHEIT, KELVIN CELSIUS, FAHRENHEIT, KELVIN, RANKINE, REAUMUR
} }
/** The value of the temperature in Kelvin. */
double valueInKelvin; 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) { public Temperature(double value, Temperature.Units from) {
if (from == Units.CELSIUS) this.valueInKelvin = value + 273.15; if (from == Units.CELSIUS) this.valueInKelvin = value + 273.15;
if (from == Units.FAHRENHEIT) this.valueInKelvin = (value - 32) * 5 / 9 + 273.15; if (from == Units.FAHRENHEIT) this.valueInKelvin = (value - 32) * 5 / 9 + 273.15;
if (from == Units.KELVIN) this.valueInKelvin = value; 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) { public double convert(Units to) {
if (to == Units.CELSIUS) return this.valueInKelvin - 273.15; if (to == Units.CELSIUS) return this.valueInKelvin - 273.15;
if (to == Units.FAHRENHEIT) return (this.valueInKelvin - 273.15) * 9 / 5 + 32; 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; return this.valueInKelvin;
} }
} }
@@ -5,7 +5,7 @@
android:id="@+id/main" android:id="@+id/main"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".ConvertisseurMainActivity"> tools:context=".MainActivity">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
@@ -5,7 +5,7 @@
android:id="@+id/main" android:id="@+id/main"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".ConvertisseurMainActivity"> tools:context=".MainActivity">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
+2
View File
@@ -8,5 +8,7 @@
<item>Celsius</item> <item>Celsius</item>
<item>Fahrenheit</item> <item>Fahrenheit</item>
<item>Kelvin</item> <item>Kelvin</item>
<item>Rankine</item>
<item>Réaumur</item>
</string-array> </string-array>
</resources> </resources>