« Simple DOA with Base class | ^ Main | Using .ini Settings »


1st C# App: Project Calculator

Ok. I started on my book and couldn’t help but to try something on my own.

The first part of the book talked about console applications and a very in-depth look at programming in general (seriously…it is pretty thorough). We built a simple “hello world-ish” app first (so yes, this is my 2nd app) which did nothing but print to the console window some text. Whoop-di-doo! I instantly was hungry for more though.

The next app was a simple calculator. You enter a number and it did some math on it blah blah blah. I took this time to briefly look over the code and branch into doing my own thing. So, in comes the Project Calculator.

First off, Microsoft C# 2005 Express Edition is nothing short of phenominal. It is super slick and growing on me every time I type “{class}.” (where {class} is a class…duh). The overall flow of this IDE is superb and it opens SUPER fast, unlike Dreamweaver which is my normal IDE of choice for any code.

So, what does the app do?

————————————————————
How much is the total project?
100

Send Katapult Media $75.00, keep $15.00, and give the rest ($10.00) to charity.

Want to try again? y=yes, n=no
y

How much is the total project?
1000

Send Katapult Media $750.00, keep $150.00, and give the rest ($100.00) to charit
y.

Want to try again? y=yes, n=no
n
We didn’t want you to play anymore anyway!
BYE!
————————————————————

1. It asks the total project costs.
2. Tells you what to do with your money. Katapult Media is deserving of 75% because this app is so crucial in the overall success of your project.
3. Asks you if you want to try again. If you say yes it repeats. If you say no…keep going.
4. The app gets made.

I couldn’t stop there. Since I typed something wrong once I had to put a failsafe in so you couldn’t break the app.

————————————————————
How much is the total project?
100

Send Katapult Media $75.00, keep $15.00, and give the rest ($10.00) to charity.

Want to try again? y=yes, n=no
no you dumb program

Stop playing and read the available answers! y=yes, n=no
Please answer again.

Want to try again? y=yes, n=no
————————————————————

Holy flippin’ failsafes Batman! You try to type something other than y or n and it spits on you, in a sense.

The only thing I didn’t bother looking up was how to validate the project #’s as a number. I’m sure there is a method something but I don’t necessarily know what it is and it is 3 AM so I don’t care.

All-in-all, I’m pretty excited about C# now. I can’t wait to graduate from console apps and move on to WinForms. That’s when the good stuff hits the fan, I assume.

Feel free to kick my code around. I am absolutely still learning and accept all criticisms, constructive or otherwise. One day Katapult may release this app on the market for a handsom fee.

using System;

namespace ProjectCalculator
{
    class Program
    {
        static void Main()
        {
            askEm();

            Console.ReadLine();
        }

        static void askEm()
        {
            double total;

            Console.WriteLine("How much is the total project?");
            total = Convert.ToDouble(Console.ReadLine());
            Result(total);

            again();
        }

        static void Result(double total)
        {
            Console.WriteLine("");
            Console.WriteLine("Send Katapult Media " + String.Format("{0:C}", total * .75) + ", keep " + String.Format("{0:C}", total * .15) + ", and give the rest (" + String.Format("{0:C}", total * .1) + ") to charity.");
        }

        static void again()
        {
            string choice;

            Console.WriteLine("");
            Console.WriteLine("Want to try again? y=yes, n=no");
            choice = Console.ReadLine();

            if (choice  "y")
            {
                Console.WriteLine("");
                askEm();
            }
            else if (choice  "n")
            {
                Console.WriteLine("We didn't want you to play anymore anyway!");
                Console.WriteLine("BYE!");
            }
            else
            {
                Console.WriteLine("");
                Console.WriteLine("Stop playing and read the available answers! y=yes, n=no");
                Console.WriteLine("Please answer again.");
                again();
            }
        }
    }
}

Posted by John C. Bland II on January 22, 2006 3:10 AM |

TrackBack

TrackBack URL for this entry:
http://mt.katapultmedia.com/mt-tb.cgi/3

Post a comment