[Challenge] ATM Console Application - Stage 1

Editor
Feb 22, 2013 at 1:23 AM
Edited Feb 22, 2013 at 4:39 AM
Hi All,

I think we should get back to basics and have a go at implementing a console based ATM application.

Hopefully this will teach us:
  • How to read input from the console window
  • How to output to the console window
  • How to use while loops
  • The basics of try-catch blocks
  • and How to convert a string to an Int
At stage one our goal is to simply display a welcome message, ask the user for an input and display it back to them.

We should also correctly Convert the string input of the account number into an Int type as Console.ReadLine() will read in a String type. This, however, could throw an exception and cause our application to crash, so you should surround your Convert.ToInt32() method in a Try-Catch block and catching a FormatException exception.

This operation should also be surrounded by a loop which will continue to ask for a numerical input if an exception is thrown. How could we do that? I would suggest the use of a while loop and a boolean variable that can be tested and flipped on success causing the loop to finish.

The methods we will use for this application will be:
  • Console.Write() - used to output to the console window
  • Console.WriteLine() - used to output to the console window, followed by a line terminator
  • Console.ReadLine() - used to read the users input
  • Convert.ToInt32() - converts one type (string, float, double etc) into an Int
A sample output of such an application could look like this:
Image

A sample output with an incorrect account number entered:
Image

Also, to make the console window stick around after the last input is complete you can use Console.ReadLine();

An example of outputting to the console:
Console.WriteLine("Welcome to CSharp Begin ATM Facility");
An example of reading from the console:
accountNumber = Console.ReadLine();
An example of while loop:
while (tryAgain)
{
   DoSomething();
}
The skeleton of a try-catch block:
try
{
   tryToDoThis();
}
catch (Exception)
{
   doThisToHandleException();
}
To declare a variable of type Int and Boolean (true / false) and give it a value:
int accountNumber = 0;
bool tryAgain = true;
string clientName = "";
I hope this gets you started and remember please ask questions if you are not sure. our goal here is to learn c#. Learn together. Most of all have fun doing it!

I will post an example of this shortly, I would like to see a few other submissions first as I don't want to give the game away too easily! :)

When you are done, you can paste your code into a reply to this discussion thread. When you do be sure to insert your code using the "Insert Code" button on the top of the new reply form. It is the 8th button from the left or you can CTRL+K.

Happy Coding

Regards,
GK
Editor
Feb 22, 2013 at 1:50 AM
Hi,

I thought I would offer up some links to provide you with some further reading for the methods we will be using for this challenge.

Casting and Type Conversions:
http://msdn.microsoft.com/en-us/library/ms173105.aspx

Console Class:
http://msdn.microsoft.com/en-us/library/43zwz7ys.aspx

Try-Catch:
http://msdn.microsoft.com/en-us/library/0yd65esw.aspx

While loop:
http://msdn.microsoft.com/en-us/library/0yd65esw.aspx

Regards,
GK
Editor
Feb 22, 2013 at 3:03 AM
Great challenge!

-Casey
Coordinator
Feb 22, 2013 at 1:13 PM
Hi
If anyone is completely new to Visual Studio c# please check this tutorial on how to get started:-
c# Complete Beginner tutorial video by Matt

Sorry, its not the complete answer to the ATM Challenge - I had to cut it short but its a good place to watch if you've never done any c# before.

Cheers
Matt
Editor
Feb 26, 2013 at 8:44 AM
Hi!

I have problems with the upload. But I will buy a new computer and I can install the visual studio 2012. After that I can upload my source codes.
I copied here my solution. I supposed that we are using 4 digit ID. I checked the length of the pressed buttons at checked that the string contains only digit characters.


static void Main(string[] args)
    {
        //declare method variables, the boolean is for testing during the loop
        int intInput = -1;
        string strInput = "-1";
        bool tryagain = false;

        //welcome message
        Console.WriteLine("Welcome to the C# B2E ATM Machine");
        Console.WriteLine("Please enter your 4 digit ID number");
       //ask the ID, checking if the ID contains only numbers
        do
        {
            strInput = Console.ReadLine();
            foreach (char karakter in strInput)
            {
                if (!Char.IsDigit(karakter))
                {
                    tryagain = true;

                }
            }


            if (tryagain == true) Console.WriteLine("This is not only numbers, please try again");
            if (strInput.Length != 4) Console.WriteLine("The ID number shall be 4 digits long");
        } while (tryagain == true || strInput.Length!=4);

        intInput = Convert.ToInt32(strInput);


        Console.WriteLine("Your ID is: {0}", intInput);
        Console.ReadKey();
}
BR JozsefG
Editor
Feb 26, 2013 at 9:25 AM
Sorry people. I kind of missed the discussions. (First time on CodePlex)

I'll delete my implementation of an ATM.

I thought this was a place to drop examples of stuff. I wanted to drop an example of a WPF application as I am learning it, but this is probably not the best place to drop it.

Cheers for the patience!

K
Coordinator
Feb 26, 2013 at 11:23 AM
Hi Klagon
Feel free to drop it here if you like - this might give everyone an idea of what WPF (Windows Presentation Foundation) can do - even if its just to a basic level.
Eventually, we all might want to start doing more WPF stuff alongside our normal c# code.
Kind Regards
Matt

On 26 February 2013 09:28, klagan <notifications@codeplex.com> wrote:

From: klagan

Sorry people. I kind of missed the discussions. (First time on CodePlex)

I'll delete my implementation of an ATM.

I thought this was a place to drop examples of stuff. I wanted to drop an example of a WPF application as I am learning it, but this is probably not the best place to drop it.

Cheers for the patience!

K

Read the full discussion online.

To add a post to this discussion, reply to this email (cSharpbegin@discussions.codeplex.com)

To start a new discussion for this project, email cSharpbegin@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Coordinator
Feb 26, 2013 at 11:25 AM
Edited Feb 26, 2013 at 3:16 PM
Hi Jozsef Its possible to use Team Explorer for visual studio 2010 also. I've not tried it myself yet but might be worth a go. I think you can download it from :- http://www.microsoft.com/en-us/download/details.aspx?id=329

You will also need a writable DVD player to save the .IOS download file and then to install/configure it into Express but might be worth a try. Alternatively you can always use TortoiseSVN which does the same job but is not part of Visual Studio :)

Kind Regards
Matt

P.s I had an email from CodePlex 2 days ago to say that their were issues with the SVN Servers so if you had problems uploading your solutions then please give it another go - its should be fine again now.
Feb 28, 2013 at 6:55 AM
ok guys i thiknk catching exceptions is a good thing but i think in this application just avoid using that cause the efficiency of the applications depends on how low the memory is used and using a try-catch code could actualy increase that ... so i think ou should actually use IF statements on Strings to handle the exceptions
Editor
Feb 28, 2013 at 8:24 PM
I'm a bit new to this; are entries still accepted to do this?

I've got some free time and I'd love to contribute to this kind of thing, just not sure how to sign up.
Coordinator
Feb 28, 2013 at 8:33 PM
Hi Formless
I've approved you as a Developer in this project group now.
For all goals its extremely good to set a deadline date or time by when you intend to finish something so I set it for 24th Feb but I also mentioned somewhere that its quite open ended.
The more solutions people put forward, the better. I think its fascinating that people will put in completely different solutions for the same project.
As well as downloading what others have done, please check out YouTube and this groups Homepage on some useful links I created to make the c# Web Browser easier to get started.
Please can you and anyone else still interest please submit their c# Web Browser - theres no definite deadline date - its about your own personal development so please choose your own deadline date.
Look forward to seeing your solution soon.
Kind Regards
Matt
Editor
Feb 28, 2013 at 8:56 PM
Edited Feb 28, 2013 at 9:52 PM
So am I doing a web browser or a Console-based ATM machine? I can do both if you want.


EDIT: When I get home, I'll probably look into making a step-by-step for the ATM machine as an example...I've got a bit of free time. If I get even more free time, I'll make it a fancy Console application!
Coordinator
Mar 1, 2013 at 7:04 AM
Hi Form
I'd do both but mainly whatever you think you can handle.
The more solutions we keep building up the better as it will help thus group to gain more momentum and motivation which is partly the battle when learning something over a long period.
Cheers
Matt

Sent from my iPhone

On 28 Feb 2013, at 20:24, "formlesstree4" <notifications@codeplex.com> wrote:

From: formlesstree4

I'm a bit new to this; are entries still accepted to do this?

I've got some free time and I'd love to contribute to this kind of thing, just not sure how to sign up.