'Final homework with tests'
parent
a6729cf3c3
commit
061d717fed
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue