Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace TowelShop
- {
- class Program
- {
- /// <summary>
- /// Sell Towels Command
- /// </summary>
- /// <param name="stk">Stack</param>
- /// <param name="amount">Amount of towels</param>
- /// <param name="price">Price of towels</param>
- static void Sell(Stack.Stack<string> stk, int amount, double price)
- {
- //str = string in stack
- //S_amount = Stack_amount (amount of towels in stack) //temp_amount = keeps amount to sell //x = index of x in string
- //S_price = Stack_price (price of towels in stack) //profit = overall profit from sale
- //sale = if a sale was made - true, if not - false
- string str;
- int S_amount, temp_amount, x;
- double S_price, profit;
- bool sale = false;
- profit = 0;
- temp_amount = amount;
- while (!stk.IsEmpty()) //Checks if there is inventory
- {
- if (amount > 0 && price >= 0) //Checks if the amount / price entered is valid
- {
- sale = true;
- str = stk.Pop();
- x = str.IndexOf("x"); //Find X representing distinction between amount and price
- S_amount = Convert.ToInt32(str.Substring(0, x)); //Turn amount in string to int
- S_price = Convert.ToDouble(str.Substring(x + 1)); //Turn price in string to double
- if (amount >= S_amount) //Checks if amount to sell is greater or equal to amount in stack
- {
- profit += ((price * S_amount) - (S_price * S_amount)); //Calculate profit
- amount -= S_amount;
- }
- else if (amount < S_amount) //Checks if amount to sell is lower than amount in stack
- {
- profit += ((price * amount) - (S_price * S_amount)); //Calculate profit
- S_amount -= amount;
- stk.Push(S_amount + "x" + S_price); //Return remaining towels into stack
- amount = 0;
- break;
- }
- }
- else //Breaks the while loop if amount / price entered is invalid
- break;
- }
- if (sale) //Prints detailed message if sale was made
- {
- amount = temp_amount - amount;
- Console.WriteLine("Sold " + amount + " towels for: $" + (amount * price) + " (Profit: $" + profit + ")");
- }
- else //Prints error message if invalid
- Console.WriteLine("Sale not made - No inventory / Invalid towel amount or price.");
- }
- /// <summary>
- /// Buy Towels Command
- /// </summary>
- /// <param name="stk">Stack</param>
- /// <param name="amount">Amount of towels</param>
- /// <param name="price">Price of towels</param>
- static void Buy(Stack.Stack<string> stk, int amount, double price)
- {
- if (amount > 0 && price >= 0) //Checks if the amount / price entered is valid & prints detailed message if purchase was made
- {
- stk.Push(amount + "x" + price);
- Console.WriteLine("Added " + amount + " towels for: $" + (amount * price) + " ($" + price + " each)");
- }
- else //Prints error message if invalid
- Console.WriteLine("Purchase not made - Invalid towel amount or price");
- }
- /// <summary>
- /// Main program & User Interface
- /// </summary>
- /// <param name="args"></param>
- static void Main(string[] args)
- {
- Stack.Stack<string> stk = new Stack.Stack<string>();
- int amount; //Amount of towels
- double price; //Price of towels
- char x; //Command holder
- do
- {
- Console.Clear(); //Clear the console
- Console.WriteLine("---Towel Manager Program---");
- Console.WriteLine("What would you like to do?\n");
- Console.WriteLine("B: Buy Towels\nS: Sell Towels\nI: Show Inventory\nQ: Quit Program"); //Available commands
- x = char.Parse(Console.ReadLine()); //Enter command
- Console.WriteLine();
- switch (x)
- {
- case 'B': //Checks if buy / sale commands activated
- case 'b':
- case 'S':
- case 's':
- Console.WriteLine("Enter amount of towels:");
- amount = int.Parse(Console.ReadLine());
- Console.WriteLine("Enter price of each towel:");
- price = double.Parse(Console.ReadLine());
- if (x == 'B' || x == 'b') //Checks if buy command
- Buy(stk, amount, price);
- else //Checks if sell command
- Sell(stk, amount, price);
- Console.ReadKey();
- break;
- case 'I': //Checks if inventory command activated
- case 'i':
- Console.WriteLine("AMOUNTxPRICE");
- Console.WriteLine(stk);
- Console.ReadKey();
- break;
- case 'Q': //Checks if quit command activated
- case 'q':
- Console.WriteLine("Are you sure you want to exit? (y/n)"); //Safety measure to prevent accidental quit
- x = char.Parse(Console.ReadLine());
- if (x == 'Y' || x == 'y') //Chekcs if yes / no
- x = 'Q';
- break;
- default: //Prints error message if invalid command
- Console.WriteLine("ERROR: Invalid Command");
- Console.ReadKey();
- break;
- }
- }
- while (x != 'Q' && x != 'q'); //Infinite loop until Quit command is activated
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement