Skip to content

Commit 50fae45

Browse files
author
Zahra Usefi
committed
fixCargoService
1 parent fd3e52e commit 50fae45

File tree

2 files changed

+118
-90
lines changed

2 files changed

+118
-90
lines changed

Examples/E1/CargoService.cs

Lines changed: 86 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,119 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
namespace Examples.E1;
62

7-
namespace Examples.E1
3+
public class CargoService
84
{
9-
public class CargoService
5+
public ICargo BookProduct(byte type, string? origin = null, string? destination = null)
106
{
11-
private CargoFactory CargoFactory { get; set; }
12-
private string Origin =string.Empty;
13-
private string Destination = string.Empty;
14-
public ICargo BookProduct(string type)
7+
CargoFactory? factory = null;
8+
if (type == (byte)CargoType.Air)
159
{
16-
17-
return CargoFactory.CreateCargoFactory(type,Origin,Destination);
18-
10+
factory = new AirFactory();
1911
}
20-
}
21-
22-
internal abstract class CargoFactory
23-
{
24-
public ICargo CreateCargoFactory(string type,string origin,string destination)
12+
if (type == (byte)CargoType.Ship)
2513
{
26-
CargoFactory factory =null ;
27-
if (type == "Air")
28-
{
29-
factory= new AirFactory();
30-
}
31-
if (type == "Ship")
32-
{
33-
factory = new ShipFactory(origin,destination);
34-
35-
}
36-
if (type == "Train")
37-
{
38-
factory = new TrainFactory();
39-
40-
}
41-
if (factory is null)
42-
throw new Exception("type is wrong");
43-
44-
return factory.CreateCargo();
45-
}
14+
if (string.IsNullOrWhiteSpace(origin))
15+
throw new ArgumentException("Origin should not be null or empty.", nameof(origin));
4616

47-
public abstract ICargo CreateCargo();
48-
49-
}
17+
if (string.IsNullOrWhiteSpace(destination))
18+
throw new ArgumentException("Destination should not be null or empty.", nameof(destination));
19+
factory = new ShipFactory(origin, destination);
5020

51-
internal class TrainFactory : CargoFactory
52-
{
53-
public override ICargo CreateCargo()
54-
{
55-
var cargo = new Train();
56-
TrainMethod();
57-
return cargo;
5821
}
59-
60-
private void TrainMethod()
22+
if (type == (byte)CargoType.Air)
6123
{
62-
throw new NotImplementedException();
24+
factory = new TrainFactory();
25+
6326
}
27+
if (factory is null)
28+
throw new Exception("cargotype is wrong please enter a new type");
29+
return factory.Create(origin, destination);
6430
}
31+
}
6532

66-
internal class ShipFactory : CargoFactory
67-
{
68-
public ShipFactory(string origin, string destination)
69-
{
70-
Origin = origin;
71-
Destination = destination;
72-
}
33+
public abstract class CargoFactory
34+
{
35+
public abstract ICargo Create(string? origin = null, string? destination = null);
36+
}
7337

74-
public string Origin { get; set; }
75-
public string Destination { get; set; }
76-
public override ICargo CreateCargo()
77-
{
78-
return new Ship(Origin, Destination);
79-
}
38+
public class TrainFactory : CargoFactory
39+
{
40+
public override ICargo Create(string? origin = null, string? destination = null)
41+
{
42+
var cargo = new Train();
43+
TrainMethod();
44+
return cargo;
8045
}
8146

82-
internal class AirFactory : CargoFactory
47+
private void TrainMethod() => Console.WriteLine("train is new");
48+
}
49+
50+
public class ShipFactory : CargoFactory
51+
{
52+
public ShipFactory(string origin, string destination)
8353
{
84-
private static Air Air { get; set; }
85-
public override ICargo CreateCargo()
86-
{
87-
if (Air is null)
88-
{
89-
Air = new Air();
90-
91-
}
92-
return Air;
93-
}
54+
Origin = origin;
55+
Destination = destination;
9456
}
9557

96-
internal class Train : ICargo
58+
public string Origin { get; set; }
59+
public string Destination { get; set; }
60+
public override ICargo Create(string? origin, string? destination)
9761
{
98-
62+
return new Ship(origin, destination);
9963
}
64+
}
10065

101-
internal class Ship : ICargo
102-
{
103-
public Ship(string origin, string destination)
104-
{
105-
Origin = origin;
106-
Destination = destination;
107-
}
66+
public class AirFactory : CargoFactory
67+
{
68+
private static readonly Lazy<Air> _air = new Lazy<Air>(() => new Air());
10869

109-
public string Origin { get; }
110-
public string Destination { get; }
70+
public override ICargo Create(string? origin, string? destination)
71+
{
72+
return _air.Value;
11173
}
74+
}
11275

113-
internal class Air : ICargo
76+
public class Train : ICargo
77+
{
78+
public decimal SetPayment()
11479
{
80+
return 500;
81+
}
82+
}
11583

84+
public class Ship : ICargo
85+
{
86+
public Ship(string origin, string destination)
87+
{
88+
Origin = origin;
89+
Destination = destination;
11690
}
11791

118-
public interface ICargo
92+
public string Origin { get; set; }
93+
public string Destination { get; set; }
94+
public decimal SetPayment()
11995
{
96+
return 2000;
97+
}
98+
}
12099

100+
public class Air : ICargo
101+
{
102+
public decimal SetPayment()
103+
{
104+
return 1000;
121105
}
122106
}
107+
108+
public interface ICargo
109+
{
110+
public decimal SetPayment();
111+
}
112+
public enum CargoType
113+
{
114+
NotSet = 0,
115+
Air = 1,
116+
Ship = 2,
117+
Train = 3,
118+
}
119+

UserCode/Program.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,32 @@
1-
namespace UserCode;
1+
using Examples.E1;
2+
3+
namespace UserCode;
4+
class Program
5+
{
6+
static void Main()
7+
{
8+
var cargoService = new CargoService();
9+
Console.WriteLine("please enter cargo type ");
10+
Console.WriteLine($"air = " + CargoType.Air);
11+
Console.WriteLine($"ship = " + CargoType.Ship);
12+
Console.WriteLine($"train = " + CargoType.Train);
13+
byte type;
14+
Console.WriteLine("Please enter a number between 0 and 255:");
15+
while (!byte.TryParse(Console.ReadLine(), out type))
16+
{
17+
Console.WriteLine("Invalid input. Please enter a valid byte value (0-255):");
18+
}
19+
string? origin = null;
20+
string? destination = null;
21+
if (type == (byte)CargoType.Ship)
22+
{
23+
Console.Write("please enter origin ");
24+
origin = Console.ReadLine();
25+
Console.Write("please enter destination ");
26+
destination = Console.ReadLine();
27+
}
28+
29+
30+
cargoService.BookProduct(type, origin, destination);
31+
}
32+
}

0 commit comments

Comments
 (0)