Esempio n. 1
0
        public static void Main()
        {
            USCurrency currency = new USCurrency();
            CoinJar    jar      = new CoinJar(32f);
            Coin       newCoin  = null;

            Console.WriteLine("Total Amount: " + jar.TotalAmount);
            Console.WriteLine("Used Volume: " + jar.UsedVolume);

            newCoin = currency.ManufactureCoinOfAmount("USPenny");
            Console.WriteLine("Coin successfully created: " + (newCoin != null));

            newCoin = currency.ManufactureCoinOfAmount("AusPenny");
            Console.WriteLine("Coin successfully created: " + (newCoin != null));

            jar.AddCoin(currency.ManufactureCoinOfAmount("USHalfDollar"));
            jar.AddCoin(currency.ManufactureCoinOfAmount("USHalfDollar"));
            jar.AddCoin(currency.ManufactureCoinOfAmount("USPenny"));
            jar.AddCoin(currency.ManufactureCoinOfAmount("USQuarter"));
            jar.AddCoin(currency.ManufactureCoinOfAmount("USDollar"));
            jar.AddCoin(currency.ManufactureCoinOfAmount("USDollar"));
            Console.WriteLine("Total Amount: " + jar.TotalAmount);
            Console.WriteLine("Used Volume: " + jar.UsedVolume);

            List <Coin> myCoins = jar.Empty();

            Console.WriteLine("Total Amount: " + jar.TotalAmount);
            Console.WriteLine("Used Volume: " + jar.UsedVolume);
        }
Esempio n. 2
0
    {   //Very simple POC of the implementation of the interfaces that keep adding one of a coin type until the coinjar is full.
        //NB - Adding "Coin" will result in an infinite loop since they will all be rejected unless defined specifically for US currency. See AmericanCoinValidator.cs for implementation details.
        static void Main(string[] args)
        {
            var     validator = new AmericanCoinValidator();
            CoinJar coinJar   = new CoinJar(validator);
            Random  rand      = new Random(Guid.NewGuid().GetHashCode());
            bool    notFull   = true;

            while (notFull)
            {
                try
                {
                    var coin = new Penny();
                    coinJar.AddCoin(coin);
                    Console.WriteLine("Total amount : " + coinJar.TotalAmount);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error occured -> Total Amount " + coinJar.TotalAmount);
                    notFull = false;
                }
            }
            Console.ReadLine();
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            ICoinFactory factory  = null;
            CoinJar      coinJar  = new CoinJar();
            string       coinType = string.Empty;

            do
            {
                Console.WriteLine("Valid Coin Types: [cent/penny, nickel, dime, quarter, halfdollar, dollar]");
                Console.WriteLine("Enter Coin Type (q to quit):");
                coinType = Console.ReadLine();
                Console.Clear();
                switch (coinType.ToLower())
                {
                case "cent":
                case "penny":
                    factory = new PennyFactory();
                    break;

                case "nickel":
                    factory = new NickelFactory();
                    break;

                case "dime":
                    factory = new DimeFactory();
                    break;

                case "quarter":
                    factory = new QuarterFactory();
                    break;

                case "halfdollar":
                    factory = new HalfDollarFactory();
                    break;

                case "dollar":
                    factory = new DollarFactory();
                    break;

                default:
                    Console.WriteLine("Invalid Coin Type");
                    factory = null;
                    break;
                }

                if (factory != null)
                {
                    try
                    {
                        coinJar.AddCoin(factory.GetCoin());
                        Console.WriteLine("Coin added!");
                    }
                    catch (JarFullException ex)
                    {
                        Console.WriteLine($"** {ex.Message} **");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"ERROR: {ex.Message}");
                    }
                }
                Console.WriteLine($"Total Amount: {coinJar.TotalAmount}");
            } while (coinType.ToLower() != "q");
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            CoinJar coinJar = new CoinJar();

            if (args.Length == 0)
            {
                Console.WriteLine(GenerateHelpText());
                return;
            }

            foreach (string arg in args)
            {
                string coinName = string.Empty;
                int    numCoins = 0;

                string[] values = arg.ToLower().Split('x');

                if (values.Length != 2)
                {
                    Console.WriteLine($"Number of values specified for {arg} is incorrect");
                    return;
                }

                coinName = values[0];

                if (!int.TryParse(values[1], out numCoins))
                {
                    Console.WriteLine($"The value specified for the number of coins ({values[1]}) is invalid.");
                    return;
                }

                if (AvailableCoins.Keys.Contains(coinName))
                {
                    try
                    {
                        for (int i = 1; i <= numCoins; i++)
                        {
                            coinJar.AddCoin(AvailableCoins[coinName]);
                        }
                    }
                    catch (Exception coinAdditionException)
                    {
                        Console.WriteLine($"Error: {coinAdditionException.Message}");
                        break;
                    }
                }
                else
                {
                    Console.WriteLine($"Could not find coin: {coinName}, please check the coin parameters");
                    return;
                }
            }

            Console.WriteLine("Current Coin Jar Contents:\r\n");
            Console.WriteLine($" Total Amount: {coinJar.TotalAmount}");
            Console.WriteLine($" Volume: {coinJar.Volume:F3} fluid ounces");

            Console.ReadLine();

            Console.WriteLine("Resetting Coin Jar ...\r\n");
            coinJar.Reset();
            Console.WriteLine($" Total Amount: {coinJar.TotalAmount}");
            Console.WriteLine($" Volume: {coinJar.Volume:F3} fluid ounces");
        }