Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- program.cs ---
- namespace GoFFactoryMethod
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- ICreditCardFactory factory = new CreditCardFactory();
- Correntista correntista = new Correntista()
- {
- ID = 123,
- DataNascita = new DateTime(1970, 4, 22),
- StatoLavorativo = StatoLavorativo.Imprenditore,
- RedditoAnnuo = 157000,
- Saldo = 42000
- };
- correntista.CreditCard = factory.CreateCreditCard(correntista);
- if (correntista.CreditCard is null)
- {
- Console.WriteLine("Nessuna carta di credito disponibile");
- }
- else
- {
- Console.WriteLine($"Carta: {correntista.CreditCard.Nome} - Limite: {correntista.CreditCard.Limite:N2}");
- }
- Console.ReadKey();
- }
- }
- }
- -----------------------------------------------------------------------
- --- correntista.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace GoFFactoryMethod
- {
- public enum StatoLavorativo
- {
- Studente,
- Disoccupato,
- Lavoratore,
- Imprenditore,
- Pensionato
- }
- public interface ICorrentista
- {
- public StatoLavorativo StatoLavorativo { get; }
- public int Anni { get; }
- }
- public class Correntista : ICorrentista
- {
- public int ID { get; set; }
- /*
- * private int id = 0;
- *
- * public int ID() {return id;}
- */
- public DateTime DataNascita { get; set; }
- public StatoLavorativo StatoLavorativo { get; set; }
- public decimal RedditoAnnuo { get; set; }
- public ICreditCard? CreditCard { get; set; } = null;
- public decimal Saldo { get; set; }
- public int Anni
- {
- get
- {
- long ticks = DateTime.Now.Subtract(DataNascita).Ticks;
- return new DateTime(ticks).Year;
- }
- }
- }
- }
- -----------------------------------------------------------------------------
- --- ICreditCard.cs ---
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace GoFFactoryMethod
- {
- public interface ICreditCard
- {
- string Nome { get; }
- decimal Limite { get; }
- void Paga();
- }
- internal abstract class CreditCard : ICreditCard
- {
- protected string nome = String.Empty;
- protected decimal limite;
- public string Nome
- {
- get => nome;
- }
- public decimal Limite
- {
- get => limite;
- }
- public virtual void Paga() { }
- }
- internal class PrimaCreditCard : CreditCard
- {
- public PrimaCreditCard(decimal limite)
- {
- nome = "Carta Prima";
- this.limite = limite;
- }
- }
- internal class ClassicCreditCard : CreditCard
- {
- public ClassicCreditCard(decimal limite)
- {
- nome = "Carta Classic";
- this.limite = limite;
- }
- }
- }
- ------------------------------------------------------------------------------
- --- CreditCardFactory ---
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace GoFFactoryMethod
- {
- public interface ICreditCardFactory
- {
- ICreditCard? CreateCreditCard(ICorrentista correntista);
- }
- public abstract class AbstractCreditCardFactory : ICreditCardFactory
- {
- public abstract ICreditCard? CreateCreditCard(ICorrentista correntista);
- }
- public class CreditCardFactory : AbstractCreditCardFactory
- {
- public override ICreditCard? CreateCreditCard(ICorrentista correntista)
- {
- ICreditCard? card = null;
- switch (correntista.StatoLavorativo)
- {
- case StatoLavorativo.Disoccupato:
- break;
- case StatoLavorativo.Studente:
- card = new PrimaCreditCard(500);
- break;
- case StatoLavorativo.Lavoratore:
- card = new ClassicCreditCard(1000);
- break;
- case StatoLavorativo.Imprenditore:
- card = new ClassicCreditCard(2000);
- break;
- }
- return card;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement