Advertisement
Filkolev

Cake Tycoon

Feb 27th, 2016
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.05 KB | None | 0 0
  1. namespace CakeTycoon
  2. {
  3.     using System;
  4.  
  5.     public class CakeTycoon
  6.     {
  7.         public static void Main(string[] args)
  8.         {
  9.             /* Keep "magic numbers" out of your code by using constants
  10.              * Using a named constant/variable is easier to understand
  11.              */
  12.             const double ProfitMultiplier = 1.25;
  13.  
  14.             // Be very careful of the types you choose
  15.             ulong cakesWanted = ulong.Parse(Console.ReadLine());
  16.             double flourPerCakeNeeded = double.Parse(Console.ReadLine());
  17.             uint availableFlour = uint.Parse(Console.ReadLine());
  18.             uint trufflesAvailable = uint.Parse(Console.ReadLine());
  19.             uint priceOfTruffle = uint.Parse(Console.ReadLine());
  20.            
  21.             // How many cakes can we produce with the flour we have?
  22.             ulong maxCakes = (ulong)(availableFlour / flourPerCakeNeeded);
  23.  
  24.             // Two outcomes depending on whether we can produce as many cakes as we want
  25.             if (maxCakes < cakesWanted)
  26.             {
  27.                 /* Calculate how much flour we lack; this is only relevant in this case
  28.                  * so keep calculations here, no need to do them before we know if we'll
  29.                  * need them */
  30.                 double totalFlourNeeded = flourPerCakeNeeded * cakesWanted;
  31.                 double flourDeficit = totalFlourNeeded - availableFlour;
  32.  
  33.                 Console.WriteLine(
  34.                     "Can make only {0} cakes, need {1:F2} kg more flour",
  35.                     maxCakes,
  36.                     flourDeficit);
  37.             }
  38.             else
  39.             {
  40.                 // Calculate the price of each cake; again, only relevant in this case
  41.                 ulong totalTrufflePrice = (ulong)trufflesAvailable * priceOfTruffle;
  42.                 double priceOfCake = totalTrufflePrice * ProfitMultiplier / cakesWanted ;
  43.  
  44.                 Console.WriteLine(
  45.                     "All products available, price of a cake: {0:F2}",
  46.                     priceOfCake);
  47.             }
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement