Created
September 11, 2024 17:15
-
-
Save EsinShadrach/aa80ddee400e89d2ecc927cb5d78b3ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| enum Length { | |
| millimeter, | |
| centimeter, | |
| meter, | |
| kilometer, | |
| inch, | |
| foot, | |
| yard, | |
| mile, | |
| } | |
| enum Area { | |
| squareMillimeter, | |
| squareCentimeter, | |
| squareMeter, | |
| squareKilometer, | |
| squareInch, | |
| squareFoot, | |
| squareYard, | |
| squareMile, | |
| } | |
| enum Mass { | |
| milligram, | |
| gram, | |
| kilogram, | |
| metricTon, | |
| ounce, | |
| pound, | |
| stone, | |
| shortTon, | |
| longTon, | |
| } | |
| enum Temperature { | |
| celsius, | |
| fahrenheit, | |
| kelvin, | |
| } | |
| enum Volume { | |
| milliliter, | |
| liter, | |
| cubicMeter, | |
| cubicInch, | |
| cubicFoot, | |
| cubicYard, | |
| teaspoon, | |
| tablespoon, | |
| fluidOunce, | |
| cup, | |
| pint, | |
| quart, | |
| gallon, | |
| } | |
| enum Time { | |
| nanosecond, | |
| microsecond, | |
| millisecond, | |
| second, | |
| minute, | |
| hour, | |
| day, | |
| week, | |
| month, | |
| year, | |
| decade, | |
| century, | |
| millennium, | |
| } | |
| enum Data { | |
| bit, | |
| byte, | |
| kilobit, | |
| kilobyte, | |
| megabit, | |
| megabyte, | |
| gigabit, | |
| gigabyte, | |
| terabit, | |
| terabyte, | |
| petabit, | |
| petabyte, | |
| exabit, | |
| exabyte, | |
| zettabit, | |
| zettabyte, | |
| yottabit, | |
| yottabyte, | |
| } | |
| enum Conversions { | |
| length, | |
| area, | |
| mass, | |
| temperature, | |
| volume, | |
| time, | |
| data, | |
| } | |
| class ConversionEnumHandler { | |
| // Method to get enum values based on conversion type | |
| static List<dynamic> getSubEnum(Conversions conversion) { | |
| return switch (conversion) { | |
| Conversions.length => Length.values, | |
| Conversions.area => Area.values, | |
| Conversions.mass => Mass.values, | |
| Conversions.temperature => Temperature.values, | |
| Conversions.volume => Volume.values, | |
| Conversions.time => Time.values, | |
| Conversions.data => Data.values, | |
| }; | |
| } | |
| // Method to get the appropriate unit suffix for each enum value | |
| static String getSuffix(dynamic value) { | |
| return switch (value) { | |
| // Length | |
| Length.millimeter => "mm", | |
| Length.centimeter => "cm", | |
| Length.meter => "m", | |
| Length.kilometer => "km", | |
| Length.inch => "in", | |
| Length.foot => "ft", | |
| Length.yard => "yd", | |
| Length.mile => "mi", | |
| // Area | |
| Area.squareMillimeter => "mm²", | |
| Area.squareCentimeter => "cm²", | |
| Area.squareMeter => "m²", | |
| Area.squareKilometer => "km²", | |
| Area.squareInch => "in²", | |
| Area.squareFoot => "ft²", | |
| Area.squareYard => "yd²", | |
| Area.squareMile => "mi²", | |
| // Mass | |
| Mass.milligram => "mg", | |
| Mass.gram => "g", | |
| Mass.kilogram => "kg", | |
| Mass.metricTon => "t", | |
| Mass.ounce => "oz", | |
| Mass.pound => "lb", | |
| Mass.stone => "st", | |
| Mass.shortTon => "ton (short)", | |
| Mass.longTon => "ton (long)", | |
| // Temperature | |
| Temperature.celsius => "°C", | |
| Temperature.fahrenheit => "°F", | |
| Temperature.kelvin => "K", | |
| // Volume | |
| Volume.milliliter => "mL", | |
| Volume.liter => "L", | |
| Volume.cubicMeter => "m³", | |
| Volume.cubicInch => "in³", | |
| Volume.cubicFoot => "ft³", | |
| Volume.cubicYard => "yd³", | |
| Volume.teaspoon => "tsp", | |
| Volume.tablespoon => "tbsp", | |
| Volume.fluidOunce => "fl oz", | |
| Volume.cup => "cup", | |
| Volume.pint => "pt", | |
| Volume.quart => "qt", | |
| Volume.gallon => "gal", | |
| // Time | |
| Time.nanosecond => "ns", | |
| Time.microsecond => "μs", | |
| Time.millisecond => "ms", | |
| Time.second => "s", | |
| Time.minute => "min", | |
| Time.hour => "h", | |
| Time.day => "d", | |
| Time.week => "wk", | |
| Time.month => "mo", | |
| Time.year => "yr", | |
| Time.decade => "dec", | |
| Time.century => "cent", | |
| Time.millennium => "mill", | |
| // Data | |
| Data.bit => "b", | |
| Data.byte => "B", | |
| Data.kilobit => "kb", | |
| Data.kilobyte => "kB", | |
| Data.megabit => "Mb", | |
| Data.megabyte => "MB", | |
| Data.gigabit => "Gb", | |
| Data.gigabyte => "GB", | |
| Data.terabit => "Tb", | |
| Data.terabyte => "TB", | |
| Data.petabit => "Pb", | |
| Data.petabyte => "PB", | |
| Data.exabit => "Eb", | |
| Data.exabyte => "EB", | |
| Data.zettabit => "Zb", | |
| Data.zettabyte => "ZB", | |
| Data.yottabit => "Yb", | |
| Data.yottabyte => "YB", | |
| _ => "", | |
| }; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import "enums.dart"; | |
| double performConversion(double value, dynamic fromUnit, dynamic toUnit) { | |
| if (fromUnit == toUnit) { | |
| return value; | |
| } | |
| // Length conversion | |
| if (fromUnit is Length && toUnit is Length) { | |
| return _convertLength(value, fromUnit, toUnit); | |
| } | |
| // Area conversion | |
| if (fromUnit is Area && toUnit is Area) { | |
| return _convertArea(value, fromUnit, toUnit); | |
| } | |
| // Mass conversion | |
| if (fromUnit is Mass && toUnit is Mass) { | |
| return _convertMass(value, fromUnit, toUnit); | |
| } | |
| // Temperature conversion | |
| if (fromUnit is Temperature && toUnit is Temperature) { | |
| return _convertTemperature(value, fromUnit, toUnit); | |
| } | |
| // Volume conversion | |
| if (fromUnit is Volume && toUnit is Volume) { | |
| return _convertVolume(value, fromUnit, toUnit); | |
| } | |
| // Time conversion | |
| if (fromUnit is Time && toUnit is Time) { | |
| return _convertTime(value, fromUnit, toUnit); | |
| } | |
| // Data conversion | |
| if (fromUnit is Data && toUnit is Data) { | |
| return _convertData(value, fromUnit, toUnit); | |
| } | |
| // If conversion is not handled, return the original value | |
| return value; | |
| } | |
| double _convertLength(double value, Length from, Length to) { | |
| // Conversion factors relative to meter | |
| final conversionFactors = { | |
| Length.millimeter: 0.001, | |
| Length.centimeter: 0.01, | |
| Length.meter: 1.0, | |
| Length.kilometer: 1000.0, | |
| Length.inch: 0.0254, | |
| Length.foot: 0.3048, | |
| Length.yard: 0.9144, | |
| Length.mile: 1609.34, | |
| }; | |
| double valueInMeters = value * conversionFactors[from]!; | |
| return valueInMeters / conversionFactors[to]!; | |
| } | |
| double _convertArea(double value, Area from, Area to) { | |
| // Conversion factors relative to square meter | |
| final conversionFactors = { | |
| Area.squareMillimeter: 1e-6, | |
| Area.squareCentimeter: 1e-4, | |
| Area.squareMeter: 1.0, | |
| Area.squareKilometer: 1e6, | |
| Area.squareInch: 0.00064516, | |
| Area.squareFoot: 0.092903, | |
| Area.squareYard: 0.836127, | |
| Area.squareMile: 2.59e6, | |
| }; | |
| double valueInSquareMeters = value * conversionFactors[from]!; | |
| return valueInSquareMeters / conversionFactors[to]!; | |
| } | |
| double _convertMass(double value, Mass from, Mass to) { | |
| // Conversion factors relative to gram | |
| final conversionFactors = { | |
| Mass.milligram: 0.001, | |
| Mass.gram: 1.0, | |
| Mass.kilogram: 1000.0, | |
| Mass.metricTon: 1e6, | |
| Mass.ounce: 28.3495, | |
| Mass.pound: 453.592, | |
| Mass.stone: 6350.29, | |
| Mass.shortTon: 907185.0, | |
| Mass.longTon: 1.016e6, | |
| }; | |
| double valueInGrams = value * conversionFactors[from]!; | |
| return valueInGrams / conversionFactors[to]!; | |
| } | |
| double _convertTemperature(double value, Temperature from, Temperature to) { | |
| // Temperature conversion formulas | |
| if (from == Temperature.celsius) { | |
| if (to == Temperature.fahrenheit) { | |
| return value * 9 / 5 + 32; | |
| } else if (to == Temperature.kelvin) { | |
| return value + 273.15; | |
| } | |
| } else if (from == Temperature.fahrenheit) { | |
| if (to == Temperature.celsius) { | |
| return (value - 32) * 5 / 9; | |
| } else if (to == Temperature.kelvin) { | |
| return (value - 32) * 5 / 9 + 273.15; | |
| } | |
| } else if (from == Temperature.kelvin) { | |
| if (to == Temperature.celsius) { | |
| return value - 273.15; | |
| } else if (to == Temperature.fahrenheit) { | |
| return (value - 273.15) * 9 / 5 + 32; | |
| } | |
| } | |
| return value; // Return the original value if no conversion is needed | |
| } | |
| double _convertVolume(double value, Volume from, Volume to) { | |
| // Conversion factors relative to liter | |
| final conversionFactors = { | |
| Volume.milliliter: 0.001, | |
| Volume.liter: 1.0, | |
| Volume.cubicMeter: 1000.0, | |
| Volume.cubicInch: 0.0163871, | |
| Volume.cubicFoot: 28.3168, | |
| Volume.cubicYard: 764.555, | |
| Volume.teaspoon: 0.00492892, | |
| Volume.tablespoon: 0.0147868, | |
| Volume.fluidOunce: 0.0295735, | |
| Volume.cup: 0.24, | |
| Volume.pint: 0.473176, | |
| Volume.quart: 0.946353, | |
| Volume.gallon: 3.78541, | |
| }; | |
| double valueInLiters = value * conversionFactors[from]!; | |
| return valueInLiters / conversionFactors[to]!; | |
| } | |
| double _convertTime(double value, Time from, Time to) { | |
| // Conversion factors relative to second | |
| final conversionFactors = { | |
| Time.nanosecond: 1e-9, | |
| Time.microsecond: 1e-6, | |
| Time.millisecond: 0.001, | |
| Time.second: 1.0, | |
| Time.minute: 60.0, | |
| Time.hour: 3600.0, | |
| Time.day: 86400.0, | |
| Time.week: 604800.0, | |
| Time.month: 2.628e6, // Approximation | |
| Time.year: 3.154e7, // Approximation | |
| Time.decade: 3.154e8, // Approximation | |
| Time.century: 3.154e9, // Approximation | |
| Time.millennium: 3.154e10, // Approximation | |
| }; | |
| double valueInSeconds = value * conversionFactors[from]!; | |
| return valueInSeconds / conversionFactors[to]!; | |
| } | |
| double _convertData(double value, Data from, Data to) { | |
| // Conversion factors relative to bit | |
| final conversionFactors = { | |
| Data.bit: 1, | |
| Data.byte: 8, | |
| Data.kilobit: 1e3, | |
| Data.kilobyte: 8e3, | |
| Data.megabit: 1e6, | |
| Data.megabyte: 8e6, | |
| Data.gigabit: 1e9, | |
| Data.gigabyte: 8e9, | |
| Data.terabit: 1e12, | |
| Data.terabyte: 8e12, | |
| Data.petabit: 1e15, | |
| Data.petabyte: 8e15, | |
| Data.exabit: 1e18, | |
| Data.exabyte: 8e18, | |
| Data.zettabit: 1e21, | |
| Data.zettabyte: 8e21, | |
| Data.yottabit: 1e24, | |
| Data.yottabyte: 8e24, | |
| }; | |
| double valueInBits = value * conversionFactors[from]!; | |
| return valueInBits / conversionFactors[to]!; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment