Here you will find a small simple J# example. It does not use a SQL database
or transactions. It's a one user bank. This is probably what they are asking
for in an introduction text/course.
/*
* This is our banking "database".
* We can do:
* - deposit
* - withdrawl
* - balance
*
* Our small bank can hold up to 100 accounts. These accounts
* are stored in an static array called "account[]". The next version
* sould use a dynamic array, but I will use a static array, because
* this is what an introduction book/text will use.
*
*/
public class bank
{
// This is the maximum number of accounts in the bank.
int NoOfAccounts = 0;
// The static array that holds the 100 accounts.
int account[];
// A startup constructor that will initialize the 100 accounts.
public bank( int MaxAccounts )
{
NoOfAccounts = MaxAccounts;
// Make a static array.
account = new int[NoOfAccounts];
// Loop through the 100 accounts and set the balance to 0.
for (int counter = 0; counter < NoOfAccounts; counter++)
this.account[counter] = 0;
}
// The deposit method. "accountNo" is the account number, "inMoney" is
the amount.
// The method throws an exception if the user tries to access an account
that is outside
// the account array. An exception is an error.
public int deposit( int accountNo, int inMoney ) throws System.Exception
{
int new_value = 0;
if (( accountNo < NoOfAccounts ) || (accountNo < 0))
{
// Get the old value in the account
int old_value = account[ accountNo ];
// Add the new amount to the balance.
new_value = old_value + inMoney;
// Store the new value back in the array.
account[ accountNo ]= new_value;
}
else // This will give the user an error if they try to access the
array out of range.
throw new System.Exception("Deposit : Out of account range!");
// return the new balance.
return new_value;
}
// The withdrawl method. "accountNo" is the account number, "inMoney" is
the amount.
// The method throws an exception if the user tries to access an account
that is outside
// the account array. An exception is an error.
public int withdrawl( int accountNo, int outMoney ) throws
System.Exception
{
int new_value = 0;
if ( (accountNo < NoOfAccounts) || (accountNo < 0) )
{
// Get the old value in the account
int old_value = account[ accountNo ];
// Calculate the new account value
new_value = old_value - outMoney;
// Store the new value in the account
account[ accountNo ] = new_value;
}
else // Error, out of array range!
throw new System.Exception("Withdrawl : Out of account range!");
// Return the new balance.
return new_value;
}
// This method returns the balance of a specified account.
public int balance( int accountNo ) throws System.Exception
{
int value = 0;
// Check to see if the account number is within the account range.
if ( (accountNo < NoOfAccounts) || (accountNo < 0) )
{
// Get the amount in the account
value = account[ accountNo ];
}
else // Error, out of account array range. Eg. 1001 or -34
throw new System.Exception("Balance : Out of account range!");
// Return the value.
return value;
}
}
// The entry point of the application.
public class Class1
{
public Class1()
{
// Use try - catch, because we throw exceptions in the bank object.
try
{
// Make a bank with 100 accounts. The bank is called
"ThrumpCorp".
bank ThrumpCorp = new bank( 100 );
// Deposit $100000 on the account 1
ThrumpCorp.deposit( 1, 100000 );
// Get the balance of the account 1
System.Console.WriteLine("Balance account No 1
:"+ThrumpCorp.balance(1) );
// Withdrawl $200 on the account 1
ThrumpCorp.withdrawl( 1, 200 );
// Get the balance of the account 1
System.Console.WriteLine("Balance account No 1
:"+ThrumpCorp.balance(1) );
// Deposit $200 on the account 2
System.Console.WriteLine("Balance account No 2
:"+ThrumpCorp.deposit(2, 200) );
// Withdrawl $400 on the account 3
System.Console.WriteLine("Balance account No 3
:"+ThrumpCorp.withdrawl(3, 400) );
// !!!This will bring an exception. We are trying to acces
account 300!!!
System.Console.WriteLine("Balance account No 4
:"+ThrumpCorp.balance( 300 ) );
}
catch ( System.Exception error )
{
System.Console.WriteLine(" Error: "+error.get_Message() );
}
}
/** @attribute System.STAThread() */
public static void main(String[] args)
{
new Class1();
}
}
Regards,
Lars-Inge Tønnessen
www.larsinge.com