|
-
February 28th, 2011, 07:37 PM
#1
C# Program ??
I need help looping this program as well as storing and updating the "new balance" of the acct. after transactions have been made.
This is what I have so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Bank //name of class
{
public void A()//selection
{
Console.WriteLine("\nWhat would you like to do: \n \nDeposit - Press 1\n \nWithdrawl - Press 2\n \nShow Balance - Press 3");
Console.WriteLine("\nPlease Enter A Number");
}
public void B()//deposit
{
double z = 1000;
double b;
double c;
Console.WriteLine("\nYour balance is {0:C}", z);
Console.WriteLine("\nEnter deposit amount");
b = Convert.ToDouble(Console.ReadLine());
c = z + b;
Console.WriteLine("\nYour new balance is {0:C}", c);
}
public void C()//withdrawl
{
double z = 1000;
double b;
double c;
Console.WriteLine("\nYour balance is {0:C}", z);
Console.WriteLine("\nEnter withdrawl amount");
b = Convert.ToDouble(Console.ReadLine());
c = z - b;
Console.WriteLine("\nYour new balance is {0:C}", c);
}
public void D()//show bal.
{
double z = 1000;
Console.WriteLine("\nYour balance is {0:C}", z);
}
}
class Program
{
static void Main(string[] args)
{
int a = 9999;
int x;
int s;//user selection
int g;
Bank myBank = new Bank();//creating the object
Console.WriteLine(a);
Console.WriteLine("\nWelcome to Boatman's Bank! Please enter your 4 digit PIN #");
x = Convert.ToInt16(Console.ReadLine());
if (x != a)
{
Console.WriteLine("Incorrect Pin..Please enter your 4 digit PIN #");
x = Convert.ToInt32(Console.ReadLine());
if (x != a)
{
Console.WriteLine("Incorrect Pin..Please enter your 4 digit PIN #");
x = Convert.ToInt32(Console.ReadLine());
if (x != a)
{
Console.WriteLine("Third Incorrect Pin..Access DENIED!");
Console.ReadLine();
}
else
{
myBank.A();
s = Convert.ToInt32(Console.ReadLine());
switch (s)
{
case 1:
myBank.B();
Console.ReadLine();
break;
case 2:
myBank.C();
Console.ReadLine();
break;
case 3:
myBank.D();
Console.ReadLine();
break;
}
}
}
else
{
myBank.A();
s = Convert.ToInt32(Console.ReadLine());
switch (s)
{
case 1:
myBank.B();
Console.ReadLine();
break;
case 2:
myBank.C();
Console.ReadLine();
break;
case 3:
myBank.D();
Console.ReadLine();
break;
}
}
}
else
{
myBank.A();
s = Convert.ToInt32(Console.ReadLine());
switch (s)
{
case 1:
myBank.B();
Console.ReadLine();
break;
case 2:
myBank.C();
Console.ReadLine();
break;
case 3:
myBank.D();
Console.ReadLine();
break;
}
}
Console.WriteLine("\nIf you would like to make another transaction: Press 0");
Console.WriteLine("\nIf you're done banking: Press 5");
g = Convert.ToInt16(Console.ReadLine());
while (g != 5)
{
myBank.A();
s = Convert.ToInt32(Console.ReadLine());
switch (s)
{
case 1:
myBank.B();
Console.ReadLine();
break;
case 2:
myBank.C();
Console.ReadLine();
break;
case 3:
myBank.D();
Console.ReadLine();
break;
}
Console.WriteLine("\nIf you would like to make another transaction: Press 0");
Console.WriteLine("\nIf you're done banking: Press 5");
g = Convert.ToInt16(Console.ReadLine());
}
}
}
-
March 1st, 2011, 07:14 AM
#2
Re: C# Program ??
The program that you've written is structurally and conceptually flawed.
I think you have a few misconceptions, and I recommend that you find a good introductory text on OO principles, and a guide to coding style. This is not meant as snark. I'd google about for some, but I'm supposed to be working 
For your program, I would suggest the following considerations:
1. Meaningful variable naming. There is no need to name a variable "a" and then give a comment saying what the variable does. You can simply call the variable PIN, or PINumber, or whatever.
2. Functions like Withdraw() should take arguments such as "decimal amount".
3. The general idea of OO is that you encapsulate all the data and functions ("methods") associated with an entity, within a class. This gives a good framework for structuring code.
The class that's missing from your program is "Account". The "Bank" class can contain a list of accounts but it should not directly access account information, and Program defininitely shouldn't.
Roughly, Account should look like this:
Code:
public class Account
{
// Hide account data from other classes
private decimal m_balance;
private int m_PIN; // It's actually not ideal to keep PIN as an int, this is a simplification
public Account (int PIN, decimal initialBalance)
{
m_PIN = PIN;
m_balance = initialBalance;
}
// Check whether an attempt is correct without explicitly giving out the correct PIN
public bool PINIsCorrect (int PINAttempt)
{
return (PINAttempt == m_PIN);
}
public decimal Balance
{
get { return m_balance; }
}
// With a withdrawal, there is the extra complication of whether the account is allowed to go
// negative. Also, it's important that the calling function is aware of whether a withdrawal
// fails. So, instead of passing back a bool, we force the calling class to pass a success
// parameter with the 'out' tag. This is harder to ignore!
public void Withdraw (decimal cashAmount, bool allowNegativeBalance, out bool success)
{
if (m_balance >= cashAmount|| allowNegativeBalance)
{
m_balance -= cashAmount;
success = true;
}
else
success = false;
}
public void Deposit (decimal cashAmount)
{
m_balance += cashAmount;
}
}
4. The if else for pin attempts is flawed. What if we want to allow 100 attempts?
It should be more like this:
Code:
bool correctPINEntered = false;
const int kMaxAttempts = 3;
for (int attempt = 0; !correctPinEntered && attempt < kMaxAttempts; attempt++)
{
int PINAttempt = Convert.ToInt32(Console.ReadLine());
correctPINEntered = account.PINIsCorrect(PINAttempt);
}
if (correctPINEntered)
{
// Present user with account options
}
else
{
// Present user with overall system options (e.g. quit)
}
Happy coding!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|