Monday, July 25, 2016

Simple Budget Calculator in C#



Here's another word problem similar to the calculator with word problem and solution included!

I would like a budget calculator for this month to see how much over or under budget I was last month. The application will prompt me for my total budget for last month when I start the application.

Once I enter my total budget, it will ask me the following:
  • How much did you spend on groceries?
  • How much did you spend eating out?
  • How much did you spend on fuel?
  • How much did you spend on entertainment?
At the end, the application will tell me how much money I either over-spent or under-spent. Make sure it works for both over- and under- spending. For example, the console should look like this:


This is the Program.cs file below.

namespace SimpleCalc
{
    public class Program
    {

        static void Main(string[] args)
        {
            BudgetClass budgetClass = new BudgetClass();
            budgetClass.Budget();
        }

    }
}


This is some class file, for ex. BudgetClass.cs below:

using System;

namespace SimpleCalc
{
    public class BudgetClass
    {
        private int budget;
        private int spent;
        private int result;

        public void Budget()
        {
            Console.WriteLine("What is your budget?");
            int newBudget = int.Parse(Console.ReadLine());
            budget = newBudget;

            Console.WriteLine("How much did you spend on groceries?");
            Prompt();

            Console.WriteLine("How much did you spend eating out?");
            Prompt();

            Console.WriteLine("How much did you spend on fuel?");
            Prompt();

            Console.WriteLine("How much did you spend on entertainment?");
            Prompt();

            string overOrUnder = OverOrUnder(result);
            Console.WriteLine("You spent $" + result + " " + overOrUnder + " your budget.");
            Console.ReadLine();
        }

        public void Prompt()
        {
            spent = int.Parse(Console.ReadLine());
            result = Calculate(spent);
        }

        public int Calculate(int spent)
        {
            budget = budget - spent;
            return budget;
        }

        public string OverOrUnder(int result)
        {
            if (result >= 0) return "under";

            return "over";
        }
    }
}


Simple Calculator without Order of Operations in C#



I have recently started a new job, and with it I decided to do mentoring for a junior developer program. Because of this, I will be working on puzzles / mini-apps with them and I will be posting my solutions to them here for anyone else to use :). It is easiest for me to guide when I solve the problem beforehand. Below is a C# calculator without order of operations (to keep it simple this is a good starting place). Next will be one with order of operations using the tree method.

This is the Program.cs file below.

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            Calculator _calculator = new Calculator();
            _calculator.InitialPrompt();
        }
    }
}


This is some class file, for ex. Calculator.cs below:

using System;

namespace ConsoleApplication1
{
    public class Calculator
    {
        public void InitialPrompt()
        {
            Console.WriteLine("1st number");
            decimal firstNum = int.Parse(Console.ReadLine());

            Console.WriteLine("operation");

            SharedPrompt(firstNum);
        }

        public void ContinuedPrompt(decimal previousSolution)
        {
            decimal firstNum = previousSolution;

            Console.WriteLine("Your previous solution was: " + previousSolution);
            Console.WriteLine("What operation would you like to do to this previous solution?");

            SharedPrompt(firstNum);
        }

        public void SharedPrompt(decimal firstNum)
        {
            string oper = Console.ReadLine();

            Console.WriteLine("2nd number");
            decimal secondNum = int.Parse(Console.ReadLine());

            decimal solution = Calculate(firstNum, oper, secondNum);

            Console.WriteLine("Your answer is: " + solution);
            Console.ReadLine();

            Console.WriteLine("Would you like to continue, do a new problem, or quit? C/N/Q?");
            string nextStep = Console.ReadLine();

            // Continue with same problem
            if (nextStep == "C")
            {
                ContinuedPrompt(solution);
            }

            // New problem
            else if (nextStep == "N")
            {
                InitialPrompt();
            }
        }

        public decimal Calculate(decimal firstNum, string oper, decimal secondNum)
        {
            decimal solution = 0;

            switch (oper)
            {
                case "+":
                    solution = firstNum + secondNum;
                    break;
                case "-":
                    solution = firstNum - secondNum;
                    break;
                case "*":
                    solution = firstNum * secondNum;
                    break;
                case "/":
                    solution = firstNum / secondNum;
                    break;
            }

            return solution;
        }
    }
}