Advertisement
DashWaLLker

Towel Shop

Sep 18th, 2014
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.28 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace TowelShop
  6. {
  7.     class Program
  8.     {
  9.         /// <summary>
  10.         /// Sell Towels Command
  11.         /// </summary>
  12.         /// <param name="stk">Stack</param>
  13.         /// <param name="amount">Amount of towels</param>
  14.         /// <param name="price">Price of towels</param>
  15.         static void Sell(Stack.Stack<string> stk, int amount, double price)
  16.         {
  17.             //str = string in stack
  18.             //S_amount = Stack_amount (amount of towels in stack) //temp_amount = keeps amount to sell //x = index of x in string
  19.             //S_price = Stack_price (price of towels in stack) //profit = overall profit from sale
  20.             //sale = if a sale was made - true, if not - false
  21.             string str;
  22.             int S_amount, temp_amount, x;
  23.             double S_price, profit;
  24.             bool sale = false;
  25.             profit = 0;
  26.             temp_amount = amount;
  27.             while (!stk.IsEmpty()) //Checks if there is inventory
  28.             {
  29.                 if (amount > 0 && price >= 0) //Checks if the amount / price entered is valid
  30.                 {
  31.                     sale = true;
  32.                     str = stk.Pop();
  33.                     x = str.IndexOf("x"); //Find X representing distinction between amount and price
  34.                     S_amount = Convert.ToInt32(str.Substring(0, x)); //Turn amount in string to int
  35.                     S_price = Convert.ToDouble(str.Substring(x + 1)); //Turn price in string to double
  36.                     if (amount >= S_amount) //Checks if amount to sell is greater or equal to amount in stack
  37.                     {
  38.                         profit += ((price * S_amount) - (S_price * S_amount)); //Calculate profit
  39.                         amount -= S_amount;
  40.                     }
  41.                     else if (amount < S_amount) //Checks if amount to sell is lower than amount in stack
  42.                     {
  43.                         profit += ((price * amount) - (S_price * S_amount)); //Calculate profit
  44.                         S_amount -= amount;
  45.                         stk.Push(S_amount + "x" + S_price); //Return remaining towels into stack
  46.                         amount = 0;
  47.                         break;
  48.                     }
  49.                 }
  50.                 else //Breaks the while loop if amount / price entered is invalid
  51.                     break;
  52.             }
  53.             if (sale) //Prints detailed message if sale was made
  54.             {
  55.                 amount = temp_amount - amount;
  56.                 Console.WriteLine("Sold " + amount + " towels for: $" + (amount * price) + " (Profit: $" + profit + ")");
  57.             }
  58.             else //Prints error message if invalid
  59.                 Console.WriteLine("Sale not made - No inventory / Invalid towel amount or price.");
  60.            
  61.         }
  62.         /// <summary>
  63.         /// Buy Towels Command
  64.         /// </summary>
  65.         /// <param name="stk">Stack</param>
  66.         /// <param name="amount">Amount of towels</param>
  67.         /// <param name="price">Price of towels</param>
  68.         static void Buy(Stack.Stack<string> stk, int amount, double price)
  69.         {
  70.             if (amount > 0 && price >= 0) //Checks if the amount / price entered is valid & prints detailed message if purchase was made
  71.             {
  72.                 stk.Push(amount + "x" + price);
  73.                 Console.WriteLine("Added " + amount + " towels for: $" + (amount * price) + " ($" + price + " each)");
  74.             }
  75.             else //Prints error message if invalid
  76.                 Console.WriteLine("Purchase not made - Invalid towel amount or price");
  77.         }
  78.         /// <summary>
  79.         /// Main program & User Interface
  80.         /// </summary>
  81.         /// <param name="args"></param>
  82.         static void Main(string[] args)
  83.         {
  84.             Stack.Stack<string> stk = new Stack.Stack<string>();
  85.             int amount; //Amount of towels
  86.             double price; //Price of towels
  87.             char x; //Command holder
  88.             do
  89.             {
  90.                 Console.Clear(); //Clear the console
  91.                 Console.WriteLine("---Towel Manager Program---");
  92.                 Console.WriteLine("What would you like to do?\n");
  93.                 Console.WriteLine("B: Buy Towels\nS: Sell Towels\nI: Show Inventory\nQ: Quit Program"); //Available commands
  94.                 x = char.Parse(Console.ReadLine()); //Enter command
  95.                 Console.WriteLine();
  96.                 switch (x)
  97.                 {
  98.                     case 'B': //Checks if buy / sale commands activated
  99.                     case 'b':
  100.                     case 'S':
  101.                     case 's':
  102.                         Console.WriteLine("Enter amount of towels:");
  103.                         amount = int.Parse(Console.ReadLine());
  104.                         Console.WriteLine("Enter price of each towel:");
  105.                         price = double.Parse(Console.ReadLine());
  106.                         if (x == 'B' || x == 'b') //Checks if buy command
  107.                             Buy(stk, amount, price);
  108.                         else //Checks if sell command
  109.                             Sell(stk, amount, price);
  110.                         Console.ReadKey();
  111.                         break;
  112.                     case 'I': //Checks if inventory command activated
  113.                     case 'i':
  114.                         Console.WriteLine("AMOUNTxPRICE");
  115.                         Console.WriteLine(stk);
  116.                         Console.ReadKey();
  117.                         break;
  118.                     case 'Q': //Checks if quit command activated
  119.                     case 'q':
  120.                         Console.WriteLine("Are you sure you want to exit? (y/n)"); //Safety measure to prevent accidental quit
  121.                         x = char.Parse(Console.ReadLine());
  122.                         if (x == 'Y' || x == 'y') //Chekcs if yes / no
  123.                             x = 'Q';
  124.                         break;
  125.                     default: //Prints error message if invalid command
  126.                         Console.WriteLine("ERROR: Invalid Command");
  127.                         Console.ReadKey();
  128.                         break;
  129.                 }
  130.             }
  131.             while (x != 'Q' && x != 'q'); //Infinite loop until Quit command is activated
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement