Skip to content

Commit 8226089

Browse files
committed
Added second finale exercises.
1 parent 7a7d99b commit 8226089

4 files changed

Lines changed: 91 additions & 4 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
3+
abstract class Article
4+
{
5+
public string Id { get; set; }
6+
private decimal price;
7+
8+
public void SetPrice(decimal priceP)
9+
{
10+
if (priceP < 0)
11+
{
12+
throw new ArgumentOutOfRangeException();
13+
}
14+
else
15+
{
16+
price = priceP;
17+
}
18+
}
19+
20+
public decimal GetPrice()
21+
{
22+
return price;
23+
}
24+
25+
public Article(string idP, decimal priceP)
26+
{
27+
Id = idP;
28+
SetPrice(priceP);
29+
}
30+
31+
public abstract decimal ShippingCosts();
32+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Book : Article
2+
{
3+
public string Title { get; set; }
4+
5+
public Book(string idP, decimal priceP, string titleP)
6+
: base(idP, priceP)
7+
{
8+
Title = titleP;
9+
}
10+
11+
public override decimal ShippingCosts()
12+
{
13+
decimal tenp = .1M * GetPrice();
14+
if (tenp > 5M)
15+
{
16+
tenp = 5M;
17+
}
18+
return tenp;
19+
}
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
class Program
4+
{
5+
public static void Main()
6+
{
7+
Book test = new Book(
8+
"AAA001",
9+
12.5M,
10+
"What it's like to be a bird."
11+
);
12+
Console.WriteLine($"{test.ShippingCosts():C}");
13+
Console.WriteLine(test.Id);
14+
}
15+
}

source/solutions/oop/inheritance.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ tags:
1919

2020
## Problem
2121

22-
#. Consider the following diagram:
22+
#. Consider the diagram representing the "Room", "ClassRoom", "Office" classes and their relations.
2323

2424
!include uml/cla/RoomClassroomOffice.md
2525

@@ -56,21 +56,41 @@ tags:
5656
```
5757
</details>
5858

59-
#. Consider the following diagram:
59+
#. Consider the diagram representing the "Article", "Book" classes and their relations.
6060

6161
!include uml/cla/Article.md
6262

6363
#. Write a (partial) implementation of the `Article` abstract class:
6464

65-
#. Write an implementation for the `price` attribute: you can either use a getter and a setter (as pictured in the UML diagram), or a property. However, in both cases, setting the price to a negative value should result in an `ArgumentOutOfRangeException` (that you can shorten to `AOORE`) exception being thrown.
65+
#. Write an implementation for the `price` attribute: you can either use a getter and a setter (as pictured in the UML diagram), or a property. However, in both cases, setting the price to a negative value should result in an `ArgumentOutOfRangeException` exception being thrown.
6666
#. Write an *abstract* `ShippingCosts()` method.
6767

68+
<details><summary>Solution</summary>
69+
```
70+
!include code/projects/Article/Article/Article.cs
71+
```
72+
</details>
73+
74+
6875
#. Now, assume given a complete implementation of the `Article` abstract class. Write a **complete** implementation of the `Book` class (header included), containing:
6976
#. An implementation of the `Title` property using auto-properties.
7077
#. A `Book` constructor that passes the `idP` and `priceP` arguments to the `Article` constructor. The `titleP` argument should be assigned to the `Title` property.
71-
#. A `ShippingCosts()` method that returns either $5.0$, or 10% of the Book's price, whichever is smallest.
78+
#. A `ShippingCosts()` method that returns either 5.0, or 10% of the Book's price, whichever is smallest.
79+
80+
<details><summary>Solution</summary>
81+
```
82+
!include code/projects/Article/Article/Book.cs
83+
```
84+
</details>
7285

7386
#. Write statements that, if placed in a `Main` method, would
7487
#. Create a `Book` with `Id` "AAA001", `price` $12.5, titled "What it's like to be a bird".
7588
#. Display (nicely) its shipping costs.
7689
#. Display its `Id` (as retrieved from the object).
90+
91+
<details><summary>Solution</summary>
92+
```{download="code/projects/Article.zip"}
93+
!include code/projects/Article/Article/Program.cs
94+
```
95+
</details>
96+

0 commit comments

Comments
 (0)