-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathCargoService.cs
More file actions
119 lines (102 loc) · 2.62 KB
/
CargoService.cs
File metadata and controls
119 lines (102 loc) · 2.62 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
namespace Examples.E1;
public class CargoService
{
public ICargo BookProduct(byte type, string? origin = null, string? destination = null)
{
CargoFactory? factory = null;
if (type == (byte)CargoType.Air)
{
factory = new AirFactory();
}
if (type == (byte)CargoType.Ship)
{
if (string.IsNullOrWhiteSpace(origin))
throw new ArgumentException("Origin should not be null or empty.", nameof(origin));
if (string.IsNullOrWhiteSpace(destination))
throw new ArgumentException("Destination should not be null or empty.", nameof(destination));
factory = new ShipFactory(origin, destination);
}
if (type == (byte)CargoType.Air)
{
factory = new TrainFactory();
}
if (factory is null)
throw new Exception("cargotype is wrong please enter a new type");
return factory.Create(origin, destination);
}
}
public abstract class CargoFactory
{
public abstract ICargo Create(string? origin = null, string? destination = null);
}
public class TrainFactory : CargoFactory
{
public override ICargo Create(string? origin = null, string? destination = null)
{
var cargo = new Train();
TrainMethod();
return cargo;
}
private void TrainMethod() => Console.WriteLine("train is new");
}
public class ShipFactory : CargoFactory
{
public ShipFactory(string origin, string destination)
{
Origin = origin;
Destination = destination;
}
public string Origin { get; set; }
public string Destination { get; set; }
public override ICargo Create(string? origin, string? destination)
{
return new Ship(origin, destination);
}
}
public class AirFactory : CargoFactory
{
private static readonly Lazy<Air> _air = new Lazy<Air>(() => new Air());
public override ICargo Create(string? origin, string? destination)
{
return _air.Value;
}
}
public class Train : ICargo
{
public decimal SetPayment()
{
return 500;
}
}
public class Ship : ICargo
{
public Ship(string origin, string destination)
{
Origin = origin;
Destination = destination;
}
public string Origin { get; set; }
public string Destination { get; set; }
public decimal SetPayment()
{
return 2000;
}
}
public class Air : ICargo
{
public decimal SetPayment()
{
return 1000;
}
}
public interface ICargo
{
public decimal SetPayment();
}
public enum CargoType
{
NotSet = 0,
Air = 1,
Ship = 2,
Train = 3,
}