From 061d717fedf92063cc0909fcadbe612c3b43ab84 Mon Sep 17 00:00:00 2001 From: Exoucia Mukubay Date: Tue, 8 Nov 2022 22:37:34 +0100 Subject: [PATCH] 'Final homework with tests' --- lib/model/EnergyModelProvider.dart | 60 +++++++++++++++++++++++++++++ test/EnergyModelProvider_test.dart | 62 ++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 lib/model/EnergyModelProvider.dart create mode 100644 test/EnergyModelProvider_test.dart diff --git a/lib/model/EnergyModelProvider.dart b/lib/model/EnergyModelProvider.dart new file mode 100644 index 0000000..bdac229 --- /dev/null +++ b/lib/model/EnergyModelProvider.dart @@ -0,0 +1,60 @@ +import 'package:flutter/material.dart'; + +class EnergyModelProvider with ChangeNotifier { + double potentialEnergy = 0, + kineticEnergy = 0, + energyInJoule = 0, + literWaterToHeat = 0, + kilogrammIronToHeat = 0; + + EnergyModelProvider() {} + + /// E = kg * (m^2 / s^2) + double calculateEnergyInJoule(String weightValue, String velocity) { + double weightValueInDouble = double.parse(weightValue); + double velocityInDouble = double.parse(velocity); + + energyInJoule = (velocityInDouble * velocityInDouble) / weightValueInDouble; + notifyListeners(); + + return energyInJoule; + } + + /// E = m * g * h in Joule + void calculateEnergyPotential(String weightValue, String altitudeChange) { + double weightValueInDouble = double.parse(weightValue); + double altitudeChangeInDouble = double.parse(altitudeChange); + + potentialEnergy = weightValueInDouble * 9.81 * altitudeChangeInDouble; + notifyListeners(); + } + + /// E = 1/2 * m * v^2 in Joule + void calculateEnergyKinetic(String weightValue, String velocity) { + double weightValueInDouble = double.parse(weightValue); + double velocityInDouble = double.parse(velocity); + + kineticEnergy = + 0.5 * weightValueInDouble * (velocityInDouble * velocityInDouble); + notifyListeners(); + } + + /** + * E = c * m * delta T + * c = spezifische Wärmekapazität Wasser 4,182 kJ/l + * m = Masse + * delta T = Temperaturunterschied + */ + void calculateLiterToHeatWater() { + // 4182 J = 1l = 1000kg Waasser um 1° zu erwaermen + // E/c/delta T = m + //Total Energie = Potentielle Energie + Kinetische Energie + literWaterToHeat = (potentialEnergy + kineticEnergy) / (4182 * 80); + notifyListeners(); + } + + void calculateKgToHeatIron() { + kilogrammIronToHeat = (potentialEnergy + kineticEnergy) / (460 * 680); + notifyListeners(); + } +} diff --git a/test/EnergyModelProvider_test.dart b/test/EnergyModelProvider_test.dart new file mode 100644 index 0000000..5e34216 --- /dev/null +++ b/test/EnergyModelProvider_test.dart @@ -0,0 +1,62 @@ +import 'package:energy/model/EnergyModelProvider.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + group('EnergyModelProviderTest1', () { + final provider = EnergyModelProvider(); + + test('Tests calculateEnergyInJoule', () { + provider.calculateEnergyInJoule('100', '500'); + expect(provider.energyInJoule, 2500.0); + }); + + test('Tests calculateEnergyPotential', () { + provider.calculateEnergyPotential('10', '50'); + expect(provider.potentialEnergy, 4905.0); + }); + + test('Tests calculateEnergyPotential', () { + provider.calculateEnergyKinetic('10', '50'); + expect(provider.kineticEnergy, 12500.0); + }); + + test('Tests calculateEnergyPotential', () { + provider.calculateLiterToHeatWater(); + expect(provider.literWaterToHeat, 0.05202355332376853); + }); + + test('Tests calculateKgToHeatIron', () { + provider.calculateKgToHeatIron(); + expect(provider.kilogrammIronToHeat, 0.055642583120204606); + }); + }); + + group('EnergyProviderTest2', () { + final provider = EnergyModelProvider(); + + test('Tests calculateEnergyInJoule', () { + provider.calculateEnergyInJoule('5000', '180'); + expect(provider.energyInJoule, 6.48); + }); + + test('Tests calculateEnergyPotential', () { + provider.calculateEnergyPotential('5000', '60'); + expect(provider.potentialEnergy, 2943000.0); + }); + + test('Tests calculateEnergyPotential', () { + provider.calculateEnergyKinetic('2000', '60'); + expect(provider.kineticEnergy, 3600000.0); + }); + + test('Tests calculateEnergyPotential', () { + provider.calculateLiterToHeatWater(); + expect(provider.literWaterToHeat, 19.557030129124822); + }); + + test('Tests calculateKgToHeatIron', () { + provider.calculateKgToHeatIron(); + expect(provider.kilogrammIronToHeat, 20.91751918158568); + }); + }); +}