Skip to content

Add weight and length conversion functions #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions Conversions/Lengthconversions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
/**
* Class for converting length between different units.
*/
class LengthConversions {
/**
* Validates input for conversion methods
*
* @param mixed $value The value to check
* @param string $method The method name for error message
* @throws InvalidArgumentException
*/
private static function validateInput($value, $method) {
// Make sure we have a numeric value
if (!is_numeric($value)) {
throw new InvalidArgumentException("Invalid input for $method");
}
}

/**
* Converts meters to kilometers.
*
* @param float $meters The length in meters.
* @return float The equivalent length in kilometers.
*/
public static function mToKm($meters) {
self::validateInput($meters, 'mToKm');
return round($meters / 1000, 4);
}

/**
* Converts kilometers to meters.
*
* @param float $kilometers The length in kilometers.
* @return float The equivalent length in meters.
*/
public static function kmToM($kilometers) {
self::validateInput($kilometers, 'kmToM');
return round($kilometers * 1000, 4);
}

/**
* Converts meters to miles.
*
* @param float $meters The length in meters.
* @return float The equivalent length in miles.
*/
public static function mToMiles($meters) {
self::validateInput($meters, 'mToMiles');
return round($meters / 1609.34, 6);
}

/**
* Converts miles to meters.
*
* @param float $miles The length in miles.
* @return float The equivalent length in meters.
*/
public static function milesToM($miles) {
self::validateInput($miles, 'milesToM');
return round($miles * 1609.34, 4);
}

/**
* Converts inches to centimeters.
*
* @param float $inches The length in inches.
* @return float The equivalent length in centimeters.
*/
public static function inToCm($inches) {
self::validateInput($inches, 'inToCm');
return round($inches * 2.54, 4);
}

/**
* Converts centimeters to inches.
*
* @param float $centimeters The length in centimeters.
* @return float The equivalent length in inches.
*/
public static function cmToIn($centimeters) {
self::validateInput($centimeters, 'cmToIn');
return round($centimeters / 2.54, 2);
}

/**
* Converts kilometers to miles.
*
* @param float $km The length in kilometers.
* @return float The equivalent length in miles.
*/
public static function kmToMiles($km) {
self::validateInput($km, 'kmToMiles');
return round($km / 1.609, 5);
}

/**
* Converts miles to kilometers.
*
* @param float $miles The length in miles.
* @return float The equivalent length in kilometers.
*/
public static function milesToKm($miles) {
self::validateInput($miles, 'milesToKm');
return round($miles * 1.609, 4);
}
}
87 changes: 87 additions & 0 deletions Conversions/Weightconversions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Class for converting weight between different units.
*/
class WeightConversions {
/**
* Validates input for conversion methods
*
* @param mixed $value The value to check
* @param string $method The method name for error message
* @throws InvalidArgumentException
*/
private static function validateInput($value, $method) {
// Make sure we have a numeric value
if (!is_numeric($value)) {
throw new InvalidArgumentException("Invalid input for $method");
}
}

/**
* Converts kilograms to pounds.
*
* @param float $kg The weight in kilograms.
* @return float The equivalent weight in pounds.
* @see https://en.wikipedia.org/wiki/Kilogram
*/
public static function kgToLbs($kg) {
self::validateInput($kg, 'kgToLbs');
return round($kg * 2.20462, 4);
}

/**
* Converts pounds to kilograms.
*
* @param float $lbs The weight in pounds.
* @return float The equivalent weight in kilograms.
* @see https://en.wikipedia.org/wiki/Pound_(mass)
*/
public static function lbsToKg($lbs) {
self::validateInput($lbs, 'lbsToKg');
return round($lbs / 2.20462, 4);
}

/**
* Converts grams to kilograms.
*
* @param float $grams The weight in grams.
* @return float The equivalent weight in kilograms.
*/
public static function gToKg($grams) {
self::validateInput($grams, 'gToKg');
return round($grams / 1000, 4);
}

/**
* Converts kilograms to grams.
*
* @param float $kg The weight in kilograms.
* @return float The equivalent weight in grams.
*/
public static function kgToG($kg) {
self::validateInput($kg, 'kgToG');
return round($kg * 1000, 4);
}

/**
* Converts ounces to pounds.
*
* @param float $oz The weight in ounces.
* @return float The equivalent weight in pounds.
*/
public static function ozToLbs($oz) {
self::validateInput($oz, 'ozToLbs');
return round($oz / 16, 4);
}

/**
* Converts pounds to ounces.
*
* @param float $lbs The weight in pounds.
* @return float The equivalent weight in ounces.
*/
public static function lbsToOz($lbs) {
self::validateInput($lbs, 'lbsToOz');
return round($lbs * 16, 4);
}
}
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* [Octaltodecimal](./Conversions/OctalToDecimal.php)
* [Speedconversion](./Conversions/SpeedConversion.php)
* [Temperatureconversions](./Conversions/TemperatureConversions.php)
* [Weightconversions](./Conversions/Weightconversions.php)
* [Lengthconversions](./Conversions/Lengthconversions.php)

## Datastructures
* Avltree
Expand Down
109 changes: 109 additions & 0 deletions tests/Conversions/ConversionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
require_once __DIR__ . '/../../Conversions/HexadecimalToDecimal.php';
require_once __DIR__ . '/../../Conversions/SpeedConversion.php';
require_once __DIR__ . '/../../Conversions/TemperatureConversions.php';
require_once __DIR__ . '/../../Conversions/Weightconversions.php';
require_once __DIR__ . '/../../Conversions/Lengthconversions.php';

class ConversionsTest extends TestCase
{
Expand Down Expand Up @@ -142,4 +144,111 @@ public function testFahrenheitToKelvin()
$this->expectExceptionMessage('Temperature (Fahrenheit) must be a number');
FahrenheitToKelvin("non-numeric");
}
/**
* Helper method to test invalid input handling in conversion methods
*
* @param string $method The conversion method to test (e.g. 'WeightConversions::kgToLbs')
* @param mixed $value The invalid input value
* @param string $expectedMessage The expected exception message
*/
protected function assertInvalidInputConversion($method, $value, $expectedMessage)
{
try {
// Call the method with the invalid input
$parts = explode('::', $method);
$className = $parts[0];
$methodName = $parts[1];
$className::$methodName($value);

// If we get here, no exception was thrown
$this->fail("Expected exception was not thrown for $method with invalid input");
} catch (InvalidArgumentException $e) {
// Check if the exception message matches the expected message
$this->assertEquals($expectedMessage, $e->getMessage());
}
}
public function testKgToLbs()
{
$this->assertEquals(220.462, WeightConversions::kgToLbs(100), 0.001);
$this->assertInvalidInputConversion('WeightConversions::kgToLbs', "invalid string", 'Invalid input for kgToLbs');
}

public function testLbsToKg()
{
$this->assertEquals(45.3593, WeightConversions::lbsToKg(100), 0.001);
$this->assertInvalidInputConversion('WeightConversions::lbsToKg', "invalid string", 'Invalid input for lbsToKg');
}

public function testGToKg()
{
$this->assertEquals(0.5, WeightConversions::gToKg(500), 0.001);
$this->assertInvalidInputConversion('WeightConversions::gToKg', "invalid string", 'Invalid input for gToKg');
}

public function testKgToG()
{
$this->assertEquals(1000, WeightConversions::kgToG(1), 0.001);
$this->assertInvalidInputConversion('WeightConversions::kgToG', "invalid string", 'Invalid input for kgToG');
}

public function testOzToLbs()
{
$this->assertEquals(3.5, WeightConversions::ozToLbs(56), 0.001);
$this->assertInvalidInputConversion('WeightConversions::ozToLbs', "invalid string", 'Invalid input for ozToLbs');
}

public function testLbsToOz()
{
$this->assertEquals(64, WeightConversions::lbsToOz(4), 0.001);
$this->assertInvalidInputConversion('WeightConversions::lbsToOz', "invalid string", 'Invalid input for lbsToOz');
}
public function testMToKm()
{
$this->assertEquals(1, LengthConversions::mToKm(1000), 0.001);
$this->assertInvalidInputConversion('LengthConversions::mToKm', "invalid string", 'Invalid input for mToKm');
}

public function testKmToM()
{
$this->assertEquals(5000, LengthConversions::kmToM(5), 0.001);
$this->assertInvalidInputConversion('LengthConversions::kmToM', "invalid string", 'Invalid input for kmToM');
}

public function testMToMiles()
{
$this->assertEquals(0.621373, LengthConversions::mToMiles(1000), 0.001);
$this->assertInvalidInputConversion('LengthConversions::mToMiles', "invalid string", 'Invalid input for mToMiles');
}

public function testMilesToM()
{
$this->assertEquals(1609.34, LengthConversions::milesToM(1), 0.001);
$this->assertInvalidInputConversion('LengthConversions::milesToM', "invalid string", 'Invalid input for milesToM');
}

public function testInToCm()
{
$this->assertEquals(25.4, LengthConversions::inToCm(10), 0.001);
$this->assertInvalidInputConversion('LengthConversions::inToCm', "invalid string", 'Invalid input for inToCm');
}

public function testCmToIn()
{
$this->assertEquals(39.37, LengthConversions::cmToIn(100), 0.001);
$this->assertInvalidInputConversion('LengthConversions::cmToIn', "invalid string", 'Invalid input for cmToIn');
}

public function testKmToMiles()
{
$this->assertEquals(6.21504, LengthConversions::kmToMiles(10), 0.001);
$this->assertInvalidInputConversion('LengthConversions::kmToMiles', "invalid string", 'Invalid input for kmToMiles');
}

public function testMilesToKm()
{
$this->assertEquals(16.09, LengthConversions::milesToKm(10), 0.001);
$this->assertInvalidInputConversion('LengthConversions::milesToKm', "invalid string", 'Invalid input for milesToKm');
}


}
Loading