Lektion 10: lösningsförslag
- class Person
- {
- private int age;
- public int Age
- {
- get
- {
- return age;
- }
- set
- {
- if (value >= 0)
- {
- age = value;
- }
- else
- {
- throw new ArgumentException("Invalid age");
- }
- }
- }
- }
- class Account
- {
- private double balance;
- public void Deposit(double amount)
- {
- balance += amount;
- }
- public void Withdraw(double amount)
- {
- if (amount <= balance)
- {
- balance -= amount;
- }
- else
- {
- throw new ArgumentException("Balance too low");
- }
- }
- }
- class Account2
- {
- private double balance;
- public void Deposit(double amount)
- {
- balance += amount;
- }
- public void Withdraw(double amount)
- {
- if (amount > balance)
- {
- balance -= 100;
- }
- balance -= amount;
- }
- }
- class SquareLike
- {
- private double width;
- private double height;
- public SquareLike(double width, double height)
- {
- if (width / height <= 1.2 && height / width <= 1.2)
- {
- this.width = width;
- this.height = height;
- }
- else
- {
- throw new ArgumentException("Width and height too different");
- }
- }
- }
- class SquareLike2
- {
- private double width;
- private double height;
- public SquareLike2(double width, double height)
- {
- ChangeSize(width, height);
- }
- public void ChangeSize(double width, double height)
- {
- if (width / height <= 1.2 && height / width <= 1.2)
- {
- this.width = width;
- this.height = height;
- }
- else
- {
- throw new ArgumentException("Width and height too different");
- }
- }
- }
- class Temperature
- {
- public double Celsius { get; set; }
- public double Fahrenheit
- {
- get
- {
- return (Celsius / (5.0 / 9)) + 32;
- }
- set
- {
- Celsius = (value - 32) * (5.0 / 9);
- }
- }
- }