Thread: Need Help On a Simple Bank Program

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    97

    Need Help On a Simple Bank Program

    Hi. Im new to C# and want to make a bank program. Its a simple thing, with a Deposit textbox and a Withdraw textbox, and a Balance textbox.

    I want to have it so you start with 50$ on the Balance part. I think I have done that, not sure if it works though.

    I also want it to be like this: I type a number in the Deposit box, hit enter, and it will add that amount to Balance. And when I type a number in the Withdraw box, and hit enter, it will subtract that amount from the Balance.

    I cant find ANY good tutorials for this VISUAL C# app. It is a Windows App, and I am using Microsoft Visual Studio.NET 2003.

    It seems I cant attach zip files, so how can I show you guys what I currently have done? Can anybody help me out with this PLEASE!?!

    Thanks
    -Ryan

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Sure, you paste a relevant section of source code which you've thought about and consider to be the problem, rather than relying on quickly zipping up the whole goddam mess and hoping someone else will sort it out.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    First of all , think very well before just adding controls onto your form.

    Like you explained the way you have implemented the controls its already going wrong...

    If i was a bank I would rather have the balance be in a label then in a textbox - unless you have it selected that you cannot edit the balance textbox. The withdraw and deposit textbox are ok.

    After that, make sure you are verifying input from your textboxes, so make sure a user can not enter a string or a negative number. Also you should take into account that unless you can go into red with the account a user should not be able to withdraw more then it has on his bankaccount and if they can go under 0 then make sure they can not withdraw more then "balance+limit under zero".

    Then if you want to attach an action like hitting enter you can have an on keyDown event in your form , then in the event check if they hit enter if so , proceed else do not proceed. Or you could look into using something that is called mnemonics and add these to your textboxes.

    But unless you give us some code we cant really help you out, like Salem already said.

    It seems I cant attach zip files, so how can I show you guys what I currently have done?
    Read this
    Last edited by GanglyLamb; 08-07-2005 at 10:47 AM.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    97
    Ok, lemme do this. Also, the Balance is a non editable textbox.

    I was thinking that the Visuals would be needed to see, but I guess code sounds more better.

    Code:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    
    namespace WindowsApplication2
    {
    	/// <summary>
    	/// Summary description for Form1.
    	/// </summary>
    	public class Form1 : System.Windows.Forms.Form
    	{
    		private System.Windows.Forms.TextBox deposit1;
    		private System.Windows.Forms.TextBox withdraw1;
    		private System.Windows.Forms.TextBox balance1;
    		private System.Windows.Forms.Label label1;
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		private System.ComponentModel.Container components = null;
    
    		public Form1()
    		{
    			//
    			// Required for Windows Form Designer support
    			//
    			InitializeComponent();
    
    			//
    			// TODO: Add any constructor code after InitializeComponent call
    			//
    
    		float balance1 = 50;	
    		}
    
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		protected override void Dispose( bool disposing )
    		{
    			if( disposing )
    			{
    				if (components != null) 
    				{
    					components.Dispose();
    				}
    			}
    			base.Dispose( disposing );
    		}
    
    		#region Windows Form Designer generated code
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		private void InitializeComponent()
    		{
    			this.deposit1 = new System.Windows.Forms.TextBox();
    			this.label1 = new System.Windows.Forms.Label();
    			this.withdraw1 = new System.Windows.Forms.TextBox();
    			this.balance1 = new System.Windows.Forms.TextBox();
    			this.SuspendLayout();
    			// 
    			// deposit1
    			// 
    			this.deposit1.AcceptsReturn = true;
    			this.deposit1.AcceptsTab = true;
    			this.deposit1.Location = new System.Drawing.Point(16, 72);
    			this.deposit1.Name = "deposit1";
    			this.deposit1.TabIndex = 0;
    			this.deposit1.Text = "Deposit";
    			this.deposit1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
    			this.deposit1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
    			// 
    			// label1
    			// 
    			this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    			this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
    			this.label1.Location = new System.Drawing.Point(72, 8);
    			this.label1.Name = "label1";
    			this.label1.Size = new System.Drawing.Size(176, 23);
    			this.label1.TabIndex = 1;
    			this.label1.Text = "Ryans Bank Program";
    			this.label1.Click += new System.EventHandler(this.label1_Click);
    			// 
    			// withdraw1
    			// 
    			this.withdraw1.AcceptsTab = true;
    			this.withdraw1.Location = new System.Drawing.Point(200, 72);
    			this.withdraw1.Name = "withdraw1";
    			this.withdraw1.TabIndex = 2;
    			this.withdraw1.Text = "Withdraw";
    			this.withdraw1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
    			this.withdraw1.TextChanged += new System.EventHandler(this.textBox1_TextChanged_1);
    			// 
    			// balance1
    			// 
    			this.balance1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
    			this.balance1.Location = new System.Drawing.Point(104, 160);
    			this.balance1.Name = "balance1";
    			this.balance1.ReadOnly = true;
    			this.balance1.TabIndex = 3;
    			this.balance1.Text = "$50";
    			this.balance1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
    			this.balance1.TextChanged += new System.EventHandler(this.balance1_TextChanged);
    			// 
    			// Form1
    			// 
    			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    			this.ClientSize = new System.Drawing.Size(312, 262);
    			this.Controls.Add(this.balance1);
    			this.Controls.Add(this.withdraw1);
    			this.Controls.Add(this.label1);
    			this.Controls.Add(this.deposit1);
    			this.Name = "Form1";
    			this.Text = "Form1";
    			this.Load += new System.EventHandler(this.Form1_Load);
    			this.ResumeLayout(false);
    
    		}
    		#endregion
    
    		/// <summary>
    		/// The main entry point for the application.
    		/// </summary>
    		[STAThread]
    		static void Main() 
    		{
    			Application.Run(new Form1());
    		
    		}
    
    		private void deposit1_TextChanged(object sender, System.EventArgs e)
    		{
    		
    		}
    
    		private void label1_Click(object sender, System.EventArgs e)
    		{
    		
    		}
    
    		private void withdraw1_TextChanged_1(object sender, System.EventArgs e)
    		{
    			
    		}
    
    		private void balance1_TextChanged(object sender, System.EventArgs e)
    		{
    
    		}
    
    	}
    }
    Also, heres a picture of what it looks like now::

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    97
    What I want it to do:

    I want to have it so you start with 50$ on the Balance part. I think I have done that, not sure if it works though.

    I also want it to be like this: I type a number in the Deposit box, hit enter, and it will add that amount to Balance. And when I type a number in the Withdraw box, and hit enter, it will subtract that amount from the Balance.

  6. #6
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    Im in a good mood today so:

    Code:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    
    namespace Bank
    {
    	/// <summary>
    	/// Summary description for Form1.
    	/// </summary>
    	public class Form1 : System.Windows.Forms.Form
    	{ 
    	private Account a;
       
    	private System.Windows.Forms.Label balance_lbl;
    	private System.Windows.Forms.TextBox deposit_TextBox;
    	private System.Windows.Forms.TextBox withdraw_TextBox;
    	private System.Windows.Forms.Label label2;
    	private System.Windows.Forms.Label label3;
    	private System.Windows.Forms.Label label1;
    	private System.Windows.Forms.Label label4;
    	private System.Windows.Forms.Label limit_lbl;
    	private System.Windows.Forms.Label info_lbl;
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		private System.ComponentModel.Container components = null;
    
    		public Form1()
    		{
    			//
    			// Required for Windows Form Designer support
    			//
    			InitializeComponent();
    
    			//
    			// TODO: Add any constructor code after InitializeComponent call
    			//
    		}
    
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		protected override void Dispose( bool disposing )
    		{
    			if( disposing )
    			{
    				if (components != null) 
    				{
    					components.Dispose();
    				}
    			}
    			base.Dispose( disposing );
    		}
    
    		#region Windows Form Designer generated code
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		private void InitializeComponent()
    		{
    	  this.balance_lbl = new System.Windows.Forms.Label();
    	  this.deposit_TextBox = new System.Windows.Forms.TextBox();
    	  this.withdraw_TextBox = new System.Windows.Forms.TextBox();
    	  this.label2 = new System.Windows.Forms.Label();
    	  this.label3 = new System.Windows.Forms.Label();
    	  this.label1 = new System.Windows.Forms.Label();
    	  this.label4 = new System.Windows.Forms.Label();
    	  this.limit_lbl = new System.Windows.Forms.Label();
    	  this.info_lbl = new System.Windows.Forms.Label();
    	  this.SuspendLayout();
    	  // 
    	  // balance_lbl
    	  // 
    	  this.balance_lbl.Location = new System.Drawing.Point(64, 200);
    	  this.balance_lbl.Name = "balance_lbl";
    	  this.balance_lbl.TabIndex = 0;
    	  // 
    	  // deposit_TextBox
    	  // 
    	  this.deposit_TextBox.Location = new System.Drawing.Point(64, 88);
    	  this.deposit_TextBox.Name = "deposit_TextBox";
    	  this.deposit_TextBox.TabIndex = 1;
    	  this.deposit_TextBox.Text = "";
    	  this.deposit_TextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox_KeyDown);
    	  // 
    	  // withdraw_TextBox
    	  // 
    	  this.withdraw_TextBox.Location = new System.Drawing.Point(216, 88);
    	  this.withdraw_TextBox.Name = "withdraw_TextBox";
    	  this.withdraw_TextBox.TabIndex = 2;
    	  this.withdraw_TextBox.Text = "";
    	  this.withdraw_TextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox_KeyDown);
    	  // 
    	  // label2
    	  // 
    	  this.label2.Location = new System.Drawing.Point(64, 56);
    	  this.label2.Name = "label2";
    	  this.label2.TabIndex = 3;
    	  this.label2.Text = "Deposit";
    	  // 
    	  // label3
    	  // 
    	  this.label3.Location = new System.Drawing.Point(216, 56);
    	  this.label3.Name = "label3";
    	  this.label3.TabIndex = 4;
    	  this.label3.Text = "Withdraw";
    	  // 
    	  // label1
    	  // 
    	  this.label1.Location = new System.Drawing.Point(216, 176);
    	  this.label1.Name = "label1";
    	  this.label1.Size = new System.Drawing.Size(100, 24);
    	  this.label1.TabIndex = 5;
    	  this.label1.Text = "Limit";
    	  // 
    	  // label4
    	  // 
    	  this.label4.Location = new System.Drawing.Point(64, 176);
    	  this.label4.Name = "label4";
    	  this.label4.TabIndex = 6;
    	  this.label4.Text = "Balance";
    	  // 
    	  // limit_lbl
    	  // 
    	  this.limit_lbl.Location = new System.Drawing.Point(216, 200);
    	  this.limit_lbl.Name = "limit_lbl";
    	  this.limit_lbl.TabIndex = 7;
    	  // 
    	  // info_lbl
    	  // 
    	  this.info_lbl.Location = new System.Drawing.Point(64, 240);
    	  this.info_lbl.Name = "info_lbl";
    	  this.info_lbl.Size = new System.Drawing.Size(256, 64);
    	  this.info_lbl.TabIndex = 8;
    	  // 
    	  // Form1
    	  // 
    	  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    	  this.ClientSize = new System.Drawing.Size(384, 317);
    	  this.Controls.Add(this.info_lbl);
    	  this.Controls.Add(this.limit_lbl);
    	  this.Controls.Add(this.label4);
    	  this.Controls.Add(this.label1);
    	  this.Controls.Add(this.label3);
    	  this.Controls.Add(this.label2);
    	  this.Controls.Add(this.withdraw_TextBox);
    	  this.Controls.Add(this.deposit_TextBox);
    	  this.Controls.Add(this.balance_lbl);
    	  this.Name = "Form1";
    	  this.Text = "Form1";
    	  this.Load += new System.EventHandler(this.Form1_Load);
    	  this.ResumeLayout(false);
    
    	}
    		#endregion
    
    		/// <summary>
    		/// The main entry point for the application.
    		/// </summary>
    		[STAThread]
    		static void Main() 
    		{
    			Application.Run(new Form1());
    		}
    
    	private void textBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
    	  if(e.KeyCode == Keys.Enter){
    		try{
    		  a.addToBalance(float.Parse(deposit_TextBox.Text));
    		  if(!a.subtractFromBalance(float.Parse(withdraw_TextBox.Text))){
    			balance_lbl.Text = "Credit Exceeded"; 
    		  } else {
    			balance_lbl.Text = ""+a.getBalance();
    		  }
    		  info_lbl.Text = "";
    		} catch(FormatException fException){
    		  info_lbl.Text = "Verify that withdraw and deposit are numbers.";
    		}
    	  }
    	}
    
    	private void Form1_Load(object sender, System.EventArgs e) {
    	  a = new Account(50,100);
    	  balance_lbl.Text = a.getBalance().ToString();
    	  limit_lbl.Text = a.getLimit().ToString();
    	}
    
    	}
    }
    and Account class:

    Code:
    using System;
    
    namespace Bank
    {
    	/// <summary>
    	/// Summary description for Account.
    	/// </summary>
    	public class Account
    	{
    		private float balance;
    	private float limit;
    	
    	public Account(float startBalance,float limit) {
    	  this.balance = startBalance;
    	  this.limit = -limit;
    		}
    
    	public float getBalance() {
    	  return this.balance;
    	}
    	
    	public float getLimit() {
    	  return this.limit;
    	}
    
    	public void addToBalance(float amount) {
    	  this.balance += amount;
    	}
    
    	public bool subtractFromBalance(float amount) {
    	  if((this.balance - amount)<this.limit){
    		return false;
    	  } else {
    		this.balance -= amount;
    		return true;
    	  }
    	}
    	
    
    	}
    }
    Notice how i seperated the account things from the form things...
    Also note how I set one event Handler for both my textBoxes.

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    97
    Darn...I just wanted tips on how to make what I wanted, or a tutorial Oh well, I will just skim and see whats new in it. Thanks

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    97
    Just tried to compile your code. I made new project, Windows Application. I replaced Form1.cs with your first code, and then clicked Add New Class and copied your second code to that, rewriting everything that was there before on both things.

    These errors came up:::

    Code:
    C:\Documents and Settings\Ryan Sands\My Documents\Visual Studio Projects\Solution1\Bank Learning Prog\Form1.cs(88): The designer cannot process the code at line 88: 
    
    );
    
    The code within the method 'InitializeComponent' is generated by the designer and should not be manually modified.  Please remove any changes and try opening the designer again.
    
    C:\Documents and Settings\Ryan Sands\My Documents\Visual Studio Projects\Solution1\Bank Learning Prog\Form1.cs(88): 
          ) expected
    
    C:\Documents and Settings\Ryan Sands\My Documents\Visual Studio Projects\Solution1\Bank Learning Prog\Form1.cs(88): 
    
    ; expected
    
    C:\Documents and Settings\Ryan Sands\My Documents\Visual Studio Projects\Solution1\Bank Learning Prog\Form1.cs(88): Invalid expression term      ')'
    
    C:\Documents and Settings\Ryan Sands\My Documents\Visual Studio Projects\Solution1\Bank Learning Prog\Form1.cs(96): 
          ) expected
    
    C:\Documents and Settings\Ryan Sands\My Documents\Visual Studio Projects\Solution1\Bank Learning Prog\Form1.cs(96): 
          ; expected
    
    C:\Documents and Settings\Ryan Sands\My Documents\Visual Studio Projects\Solution1\Bank Learning Prog\Form1.cs(96): Invalid expression term       ')'
    
    C:\Documents and Settings\Ryan Sands\My Documents\Visual Studio Projects\Solution1\Bank Learning Prog\Form1.cs(177): 
          ) expected
    
    C:\Documents and Settings\Ryan Sands\My Documents\Visual Studio Projects\Solution1\Bank Learning Prog\Form1.cs(177): 
          ; expected
    
    C:\Documents and Settings\Ryan Sands\My Documents\Visual Studio Projects\Solution1\Bank Learning Prog\Form1.cs(177): Invalid expression term       ')'
    
    C:\Documents and Settings\Ryan Sands\My Documents\Visual Studio Projects\Solution1\Bank Learning Prog\Form1.cs(177): 
          ; expected
    
    C:\Documents and Settings\Ryan Sands\My Documents\Visual Studio Projects\Solution1\Bank Learning Prog\Form1.cs(177): Invalid expression term       ')'
    
    C:\Documents and Settings\Ryan Sands\My Documents\Visual Studio Projects\Solution1\Bank Learning Prog\Form1.cs(177): 
          ; expected
    
    C:\Documents and Settings\Ryan Sands\My Documents\Visual Studio Projects\Solution1\Bank Learning Prog\Form1.cs(177): Invalid expression term       ')'
    
    C:\Documents and Settings\Ryan Sands\My Documents\Visual Studio Projects\Solution1\Bank Learning Prog\Form1.cs(177): 
          ; expected
    
    C:\Documents and Settings\Ryan Sands\My Documents\Visual Studio Projects\Solution1\Bank Learning Prog\Form1.cs(181): Invalid expression term       'else'
    
    C:\Documents and Settings\Ryan Sands\My Documents\Visual Studio Projects\Solution1\Bank Learning Prog\Form1.cs(181): 
          ; expected

  9. #9
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    Its because Visual Studio does somehow strange about editing anything he puts their automated.

    Ill link you the whole project including the executable ...
    The link will be working untill somewhere next wednesday so be quick to download it.

    Download here

    [edit]
    I see, during copy paste some got mixed up
    (this.textBox_ KeyDown);
    Should be (this.textBox_KeyDown); //without the spaces the same mixe up happened some lines below.
    Last edited by GanglyLamb; 08-08-2005 at 05:00 AM.

  10. #10
    Registered User
    Join Date
    Jul 2003
    Posts
    97
    Thanks so much man, I will try it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  2. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  3. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM