I'm working on a program that creates a file for a specific embosser machine (machine that creates plastic bank card for example). I have 3 embosser machines and all of them create different files. Every file contains a card number, last and first name, address, etc. The difference between the files is that they contain specific characters for the delimiter.
I want to implement this project with a factory pattern.
For simplicity sake, let's say that I have these embossers: DataCard
, CIM
and DataCard2
.
This is how I created the files in my project, and I'd like to know if this is a good way of doing it.
public enum eEmbosserType
{
Datacard,
Cim,
Datacard2
}
public interface IEmbosser
{
eEmbosserType EmbosserType { get; }
List<EmbosserData> Data { get; set; }
void CreateFileForEmbosser(string path);
void PrepareDataForEmboser();
}
public class DataCardEmbosser : Emboser.IEmbosser
{
public eEmbosserType EmbosserType => eEmbosserType.Datacard;
public List<EmbosserData> Data { get ; set; }
public void CreateFileForEmbosser(string path)
{
using (StreamWriter sw = new StreamWriter(path))
{
foreach (DataCard item in Data)
{
sw.Write(item.cardNumber + "_!");
sw.Write(item.city + "_?");
sw.Write(item.name + "_^");
sw.Write(item.expDate + "_:");
}
}
}
public void PrepareDataForEmboser()
{
Data = new List<EmbosserData>();
List<CardData> cards = GetCardData();
foreach (CardData card in cards)
{
DataCard dc = new DataCard();
dc.cardNumber = card.PAN;
dc.city = card.City;
dc.expDate = card.Expire;
dc.name = card.Owner;
Data.Add(dc);
}
}
}
public class CimEmbosser : Emboser.IEmbosser
{
public eEmbosserType EmbosserType => eEmbosserType.Datacard2;
public List<EmbosserData> Data { get; set; }
public void CreateFileForEmbosser(string path)
{
using (StreamWriter sw = new StreamWriter(path))
{
foreach (Cim item in Data)
{
sw.Write(item.cardNumber + ";");
sw.Write(item.city + ";");
sw.Write(item.name + ";");
sw.Write(item.expDate + ";");
sw.Write(item.version + ";");
}
}
}
public void PrepareDataForEmboser()
{
Data = new List<EmbosserData>();
List<CardData> cards = GetCardData();
foreach (CardData card in cards)
{
Cim dc = new Cim();
dc.cardNumber = card.PAN;
dc.city = card.City;
dc.expDate = card.Expire;
dc.name = card.Owner;
dc.version = DateTime.Now.ToString() + GetNewVersionNumber();
Data.Add(dc);
}
}
}
public class DataCard2Embosser : Emboser.IEmbosser
{
public eEmbosserType EmbosserType => eEmbosserType.Datacard2;
public List<EmbosserData> Data { get; set; }
public void CreateFileForEmbosser(string path)
{
using (StreamWriter sw = new StreamWriter(path))
{
foreach (DataCard2 item in Data)
{
sw.Write(item.cardNumber + ";_");
sw.Write(item.city + "?");
sw.Write(item.name + ";");
sw.Write(item.expDate + "#");
sw.Write(item.f1 + " ");
sw.Write(item.f2 + " ");
sw.Write(item.f3 + " ");
}
}
}
public void PrepareDataForEmboser()
{
Data = new List<EmboserData>();
List<CardData> cards = GetCardData();
foreach (CardData card in cards)
{
DataCard2 dc = new DataCard2();
dc.cardNumber = card.PAN;
dc.city = card.City;
dc.expDate = card.Expire;
dc.name = card.Owner;
dc.f1 = card.PAN + " " + card.Expire;
dc.f2 = card.Owner + " " + card.Address;
dc.f3 = cards.Count().ToString().Trim();
Data.Add(dc);
}
}
}
And factory creator
public class EmbosserCreator
{
public static IEmbosser GetEmboser(eEmbosserType type)
{
switch (type)
{
case eEmbosserType.Datacard:
return new DataCardEmbosser();
case eEmbosserType.Cim:
return new CimEmbosser();
case eEmbosserType.Datacard2:
return new DataCard2Embosser();
default:
throw new Exception("");
}
}
}
For data I thought I should create an abstract class, EmbosserData
, which inherits classes DataCard
, CIM
, and DataCard2
.
For example:
public abstract class EmbosserData
{
public string cardNumber { get; set; }
public string name { get; set; }
public string city { get; set; }
public string expDate { get; set; }
}
public class DataCard : EmbosserData
{
}
public class Cim : EmbosserData
{
public string version { get; set; }
}
public class DataCard2 : EmbosserData
{
public string f1 { get; set; }
public string f2 { get; set; }
public string f3 { get; set; }
}
Is this a good approach for my project? If you have any advice for other implementations, I'd love to know more.
PS. I hope that code is now ok to understand my idea.
GetCardData
defined? \$\endgroup\$