hello
i need help on comparing two bank accounts and finding out if the accounts have the more less or equals amounts of money in them. i am truly lost, my teacher went to fast in class and im not sure where i can find this information out
public class AccountC
{
//Data declarations
protected string accountName;
protected double accountBalance;
protected double transactionAmount;
protected char transactionType;
protected static double penalty = 40.0;
//constructors
public AccountC()
{
this.accountName ="";
this.accountBalance = 0.0;
this.transactionAmount =0.0;
this.transactionType = ' ';
}
// constructor
public AccountC (string an, double ab, double ta, char tt)
{
this.AccountNumber = an;
this.accountBalance = ab;
this.transactionAmount = ta;
this.transactionType = tt;
}
// properties
public String AccountNumber
{
get{return this.AccountNumber;}
set{ this.AccountNumber = value;}
}
public double Balance
{
get{return this.accountBalance;}
set{this.accountBalance = value;}
}
public double TransActionAmount
{
get{ return this.transactionAmount;}
set{this.transactionAmount = value;}
}
public char TransactionType
{
get{ return this.TransactionType;}
set{this.transactionType = value;}
}
//processing methods
//process tranactiontransaction
public double Tranaction()
{
if(this.transactionType =='w'|| this.transactionType =='W')
if(this.accountBalance>= this.TransActionAmount)
this.accountBalance= this.accountBalance - this.TransActionAmount;
else
{
this.accountBalance=this.accountBalance - AccountC.penalty;
console.WriteLine("\n\tInsufficient funds.A penalty appplied\n\tCurrent balnce= "+ this.Balance);
}
else if(this.transactionAmount == 'd'|| this.transactionType =='D')
{
this.accountBalance = this.accountBalance + this.transactionAmount;
console.WriteLine(this.transactionAmount + "\tadded to current balance");
}
else Console.WriteLine("Bad transaction code, balance was not chaned\n");
return this.accountBalance;
}
//compare two accounts; used for sorting and searching
public int CompareTo(AccountC account) // a reference of type object can refer to any object type
{
return this.accountNumber.CompareTo(account.AccountNumber);
}
//used for outputing an account data members
public override string ToString()
{
return this.accountName.ToString() + '\n'
+ this.accountBalance.ToString() + '\n'
+ this.transactionAmount.ToString() + '\n'
+ this.transactionType.ToString() + '\n';
}
Loading
Kirtan PatelPosted Feb 21, 2010, 4:47 PM
Download Attached Application for reference ..
Class Code
------------
class AccountC
{
//Data declarations
protected string accountName;
protected double accountBalance;
protected double transactionAmount;
protected char transactionType;
protected static double penalty = 40.0;
protected string AccountNumber;
//constructors
public AccountC()
{
this.accountName = "";
this.accountBalance = 0.0;
this.transactionAmount = 0.0;
this.transactionType = ' ';
}
// constructor
public AccountC(string an, double ab, double ta, char tt)
{
this.AccountNumber = an;
this.accountBalance = ab;
this.transactionAmount = ta;
this.transactionType = tt;
}
// properties
public string AccNumber
{
get { return this.AccountNumber; }
set { this.AccountNumber = value; }
}
public double Balance
{
get { return this.accountBalance; }
set { this.accountBalance = value; }
}
public double TransActionAmount
{
get { return this.transactionAmount; }
set { this.transactionAmount = value; }
}
public char TransactionType
{
get { return this.transactionType; }
set { this.transactionType = value; }
}
//processing methods
//process tranactiontransaction
public double Tranaction()
{
if (this.transactionType == 'w' || this.transactionType == 'W')
if (this.accountBalance >= this.TransActionAmount)
this.accountBalance = this.accountBalance - this.TransActionAmount;
else
{
this.accountBalance = this.accountBalance - AccountC.penalty;
Console.WriteLine("\n\tInsufficient funds.A penalty appplied\n\tCurrent balnce= " + this.Balance);
}
else if (this.transactionAmount == 'd' || this.transactionType == 'D')
{
this.accountBalance = this.accountBalance + this.transactionAmount;
Console.WriteLine(this.transactionAmount + "\tadded to current balance");
}
else Console.WriteLine("Bad transaction code, balance was not chaned\n");
return this.accountBalance;
}
//compare two accounts; used for sorting and searching
public static int CompareAcccounts(AccountC accountA,AccountC AccountB) // a reference of type object can refer to any object type
{
if (accountA.Balance > AccountB.Balance)
{
//return 1 if A has more money
return 1;
}
else if (AccountB.Balance > accountA.Balance)
{
//return 2 if B hase more money
return 2;
}
else
{
//return 0 if both are equals
return 0;
}
}
//used for outputing an account data members
public override string ToString()
{
return this.accountName.ToString() + '\n' + this.accountBalance.ToString() + '\n' + this.transactionAmount.ToString() + '\n' + this.transactionType.ToString() + '\n';
}
}
How to Use Coded Class in Program
----------------------------
static void Main(string[] args)
{
AccountC acc = new AccountC("100", 5000, 5000, 'w');
AccountC acc2 = new AccountC("101", 1000, 500, 'w');
Console.WriteLine("\n\nAccount 100 Detials ");
Console.WriteLine("********************** \n");
Console.WriteLine("Account Balance :"+acc.Balance);
Console.WriteLine("Transaction Amount:" + acc.TransActionAmount);
Console.WriteLine("Transaction Type :" + acc.TransactionType);
Console.WriteLine("\n\nAccount 101 Detials ");
Console.WriteLine("**********************\n ");
Console.WriteLine("Account Balance :" + acc2.Balance);
Console.WriteLine("Transaction Amount:" + acc2.TransActionAmount);
Console.WriteLine("Transaction Type :" + acc2.TransactionType+"\n\n");
//Compare 2 Account
int Result = AccountC.CompareAcccounts(acc, acc2);
if (Result == 1)
{
Console.WriteLine("Account A has More Money ");
}
else if (Result == 2)
{
Console.WriteLine("Account B has More Money ");
}
else if(Result ==0)
{
Console.WriteLine("Both Account have Same Money ");
}
Console.ReadKey();
}
Sam HobbsPosted Feb 21, 2010, 5:06 PM
Sam HobbsPosted Feb 21, 2010, 5:02 PM
I see that you have a property for AccountNumber that is getting and setting this.AccountNumber; that won't work. You must have different names; you cannot have a member variable to store the data for a property with the same name (AccountNumber).