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";
        }
    }
}