Example #1
0
 public PrimesApp_IsPrime()
 {
     // I'm not sure if this is proper TDD practice,
     // I feel like you're meant to start fresh in every test function.
     // But I'm creating the Primes object once and reusing it.
     _primes = new PrimesFinder();
 }
Example #2
0
        static void Main(string[] args)
        {
            int primeCount = 0;

            // A little sanity testing.
            try
            {
                primeCount = Int32.Parse(args[0]);
            }
            catch (FormatException)
            {
                Console.WriteLine($"{args[0]}: Bad Format");
                return;
            }
            catch (OverflowException)
            {
                Console.WriteLine($"{args[0]}: Overflow");
                return;
            }

            if (primeCount < 1)
            {
                Console.WriteLine("Please enter an integer greater than 0");
                return;
            }

            // The required objects
            var          primeFinder = new PrimesFinder();
            IPrimeOutput outputGenerator;

            int[] primes = primeFinder.FindPrimes(primeCount);
            // It's a shame you can't just print the array directly like Swift and Python
            string primesDisplay = String.Join(", ", primes);

            Console.WriteLine($"The first {primeCount} primes are {primesDisplay}");

            // What to do with the primes though?
            // Coding against an interface really is lovely.
            if (args.Length > 1)
            {
                if (args[1] == "-csv")
                {
                    outputGenerator = new CsvSaver();
                }
                else
                {
                    Console.WriteLine($"The argument {args[1]} cannot be parsed, did you mean -csv ?");
                    return;
                }
            }
            else
            {
                outputGenerator = new GridRender();
            }
            outputGenerator.Output(primes);
        }
 public CPULoadHostedService(
     PrimesFinder primesFinder,
     JsonWorker jsonWorker,
     IHostApplicationLifetime hostApplicationLifetime,
     IOptionsSnapshot <Settings> settingsSnapshot,
     ILogger <CPULoadHostedService> logger)
 {
     _primesFinder            = primesFinder ?? throw new ArgumentNullException(nameof(primesFinder));
     _jsonWorker              = jsonWorker;
     _hostApplicationLifetime = hostApplicationLifetime;
     _logger   = logger;
     _settings = settingsSnapshot?.Value ?? throw new ArgumentNullException(nameof(settingsSnapshot));
     _timer    = new Timer(Callback, null, System.Threading.Timeout.InfiniteTimeSpan, System.Threading.Timeout.InfiniteTimeSpan);
 }
 public PrimesApp_GridRender()
 {
     _renderer    = new GridRender();
     _primeFinder = new PrimesFinder();
 }