-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sample6.cs
99 lines (86 loc) · 3.66 KB
/
Sample6.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using CommercialRegistration;
using ConsumerVehicleRegistration;
using LiveryRegistration;
namespace csharp9.Test_PatternMatch
{
public static class Sample6
{
public static void Run()
{
Console.Write($"{nameof(Sample6)}\n{"".PadRight(80, '_')}\n");
var rnd = new Random().NextDouble();
object v = rnd > 0.7
? new Car { Passengers = 2 }
: rnd > 0.5 ? new DeliveryTruck { GrossWeightClass = 3500 }
: rnd > 0.1 ? new Taxi { Fares = 42 } : null!;
var repo = "https://github.com/dmorosinotto/XE_dotNETConf_HotTopics_CSharp9";
var ch = repo[new Random().Next(repo.Length)];
Console.WriteLine($"{rnd} -> {v}");
var c = new TollCalculator();
Console.WriteLine($"TOLL {v} -> {c.CalculateToll(v)}");
Console.WriteLine($"{ch} -> { IsLetterOrSeparator(ch)}");
try
{ //? ESEMPI PIU' CONCRETI / BASICI DI UTILIZZO PATTERN MATCH + CONTROL FLOW ANALYSI
if (!(v is not null)) throw new ArgumentNullException("EQUIVALE AL VECCHIO v == null");
if (v is not Taxi t) throw new Exception($"{v} NON E' UN TAXI!");
//TODO SE ARRIVO QUI t E' SICURAMENTE !=null E DI TIPO TAXI QUINDI
Console.WriteLine($"{v == t} {v} FARE={t.Fares}"); //POSSO LEGGERE Fares
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
}
finally
{
Console.Write($"{"".PadRight(80, '=')}\n\n\n");
}
}
public static bool IsLetterOrSeparator(this char c) =>
c is (>= 'a' and <= 'z') or (>= 'A' and <= 'Z') or '.' or ',';
}
public class TollCalculator
{ //ESEMPIO PRESO DA Pattern Matching Tutorial https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/pattern-matching
public decimal CalculateToll(object vehicle) =>
vehicle
switch
{
Car c => c.Passengers
switch
{
0 => 2.00m + 0.5m,
1 => 2.0m,
2 => 2.0m - 0.5m,
_ => 2.00m - 1.0m
},
Taxi t => t.Fares
switch
{
0 => 3.50m + 1.00m,
1 => 3.50m,
2 => 3.50m - 0.50m,
_ => 3.50m - 1.00m
},
Bus b when ((double)b.Riders / (double)b.Capacity) < 0.50 => 5.00m + 2.00m,
Bus b when ((double)b.Riders / (double)b.Capacity) > 0.90 => 5.00m - 1.00m,
Bus b => 5.00m,
//TODO QUESTE ERANO LE RIGHE ORIGINALI -> ORA SFRUTTA reletional-pattern
// DeliveryTruck t when (t.GrossWeightClass > 5000) => 10.00m + 5.00m,
// DeliveryTruck t when (t.GrossWeightClass < 3000) => 10.00m - 2.00m,
// DeliveryTruck t => 10.00m,
DeliveryTruck t => t.GrossWeightClass
switch
{
< 3000 => 10.00m - 2.00m,
>= 3000 and <= 5000 => 10.00m,
> 5000 => 10.00m + 5.00m,
// _ => 10.00m
},
// { } => //TODO RIGA ORIGINALE -> ORA SFRUTA SIMPLE logical-pattern
not null =>
throw new ArgumentException(message: "Not a known vehicle type", paramName: nameof(vehicle)),
null =>
throw new ArgumentNullException(nameof(vehicle))
};
}
}