Skip to content

Commit

Permalink
fixCargoService
Browse files Browse the repository at this point in the history
  • Loading branch information
Zahra Usefi committed Dec 2, 2024
1 parent fd3e52e commit 50fae45
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 90 deletions.
175 changes: 86 additions & 89 deletions Examples/E1/CargoService.cs
Original file line number Diff line number Diff line change
@@ -1,122 +1,119 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Examples.E1;

namespace Examples.E1
public class CargoService
{
public class CargoService
public ICargo BookProduct(byte type, string? origin = null, string? destination = null)
{
private CargoFactory CargoFactory { get; set; }
private string Origin =string.Empty;
private string Destination = string.Empty;
public ICargo BookProduct(string type)
CargoFactory? factory = null;
if (type == (byte)CargoType.Air)
{

return CargoFactory.CreateCargoFactory(type,Origin,Destination);

factory = new AirFactory();
}
}

internal abstract class CargoFactory
{
public ICargo CreateCargoFactory(string type,string origin,string destination)
if (type == (byte)CargoType.Ship)
{
CargoFactory factory =null ;
if (type == "Air")
{
factory= new AirFactory();
}
if (type == "Ship")
{
factory = new ShipFactory(origin,destination);

}
if (type == "Train")
{
factory = new TrainFactory();

}
if (factory is null)
throw new Exception("type is wrong");

return factory.CreateCargo();
}
if (string.IsNullOrWhiteSpace(origin))
throw new ArgumentException("Origin should not be null or empty.", nameof(origin));

public abstract ICargo CreateCargo();

}
if (string.IsNullOrWhiteSpace(destination))
throw new ArgumentException("Destination should not be null or empty.", nameof(destination));
factory = new ShipFactory(origin, destination);

internal class TrainFactory : CargoFactory
{
public override ICargo CreateCargo()
{
var cargo = new Train();
TrainMethod();
return cargo;
}

private void TrainMethod()
if (type == (byte)CargoType.Air)
{
throw new NotImplementedException();
factory = new TrainFactory();

}
if (factory is null)
throw new Exception("cargotype is wrong please enter a new type");
return factory.Create(origin, destination);
}
}

internal class ShipFactory : CargoFactory
{
public ShipFactory(string origin, string destination)
{
Origin = origin;
Destination = destination;
}
public abstract class CargoFactory
{
public abstract ICargo Create(string? origin = null, string? destination = null);
}

public string Origin { get; set; }
public string Destination { get; set; }
public override ICargo CreateCargo()
{
return new Ship(Origin, Destination);
}
public class TrainFactory : CargoFactory
{
public override ICargo Create(string? origin = null, string? destination = null)
{
var cargo = new Train();
TrainMethod();
return cargo;
}

internal class AirFactory : CargoFactory
private void TrainMethod() => Console.WriteLine("train is new");
}

public class ShipFactory : CargoFactory
{
public ShipFactory(string origin, string destination)
{
private static Air Air { get; set; }
public override ICargo CreateCargo()
{
if (Air is null)
{
Air = new Air();

}
return Air;
}
Origin = origin;
Destination = destination;
}

internal class Train : ICargo
public string Origin { get; set; }
public string Destination { get; set; }
public override ICargo Create(string? origin, string? destination)
{

return new Ship(origin, destination);
}
}

internal class Ship : ICargo
{
public Ship(string origin, string destination)
{
Origin = origin;
Destination = destination;
}
public class AirFactory : CargoFactory
{
private static readonly Lazy<Air> _air = new Lazy<Air>(() => new Air());

public string Origin { get; }
public string Destination { get; }
public override ICargo Create(string? origin, string? destination)
{
return _air.Value;
}
}

internal class Air : ICargo
public class Train : ICargo
{
public decimal SetPayment()
{
return 500;
}
}

public class Ship : ICargo
{
public Ship(string origin, string destination)
{
Origin = origin;
Destination = destination;
}

public interface ICargo
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,
}

33 changes: 32 additions & 1 deletion UserCode/Program.cs
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
namespace UserCode;
using Examples.E1;

namespace UserCode;
class Program
{
static void Main()
{
var cargoService = new CargoService();
Console.WriteLine("please enter cargo type ");
Console.WriteLine($"air = " + CargoType.Air);
Console.WriteLine($"ship = " + CargoType.Ship);
Console.WriteLine($"train = " + CargoType.Train);
byte type;
Console.WriteLine("Please enter a number between 0 and 255:");
while (!byte.TryParse(Console.ReadLine(), out type))
{
Console.WriteLine("Invalid input. Please enter a valid byte value (0-255):");
}
string? origin = null;
string? destination = null;
if (type == (byte)CargoType.Ship)
{
Console.Write("please enter origin ");
origin = Console.ReadLine();
Console.Write("please enter destination ");
destination = Console.ReadLine();
}


cargoService.BookProduct(type, origin, destination);
}
}

1 comment on commit 50fae45

@Zusefi
Copy link

@Zusefi Zusefi commented on 50fae45 Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fix cargoService Example
Thanks for your comment 🙏🌻
@mohammadKarimi

Please sign in to comment.