C# Program Help

tjmudder

Member
Dec 16, 2006
77
0
0
So I am working on a program for my C# class and the big thing that this program involves is inheritance. What the program is supposed to do is to let me input transactions for a single person and their account number and place the transactions under one of 3 bank accounts. Main() is supposed to set up the accounts for the people with their number and type of account and output their transactions to a text file. I have my Main() .cs file pretty much finished. What I am having trouble with are my class files. I am to have 4 class files, one for each account, and one bank account class that sends the information to one of the other 3 account classes. What I really need is more of a guide on what to do for these two classes.

The Bank Account Class is supposed to have:
a method to make deposits
a property to access the account number
a property to access the account balance
and data field members for an account number and account balance.

And one of the Account Classes is to have:
a constructor to initialize the checking account data members. the account number is initialized to 000 balance to 0, Minimum to 1000 and the charge to .50
a method that will cash a check by receiving a check amount and debit the account balance accordingly
and data field members for a minimum balance value that will dictate when a per-check charge is to be made and a value that will be charged on each check cashed when the account balance is less than the minimum required balance

Just to add My Main() consists of this;
output each person via streamwriter to their own individual file
input value 1-3 that will direct to the right account i.e. "Input (1) for Checking Account"
swith (choice) that uses a constructor that pulls back the values

 

tjmudder

Member
Dec 16, 2006
77
0
0
I thought I would input my Main() so it could help
static void Main(string[] args)
{
StreamWriter outBjarne = new StreamWriter("Bjarne.txt");
StreamWriter outGraceChecking = new StreamWriter("GraceChecking.txt");
StreamWriter outJohn = new StreamWriter("John.txt");
StreamWriter outGraceSavings = new StreamWriter("GraceSavings.txt");
MainMenu();

}

static void MainMenu()
{
string inputValue;
int choice = 0;
bool stop = false;
while (!stop)
{

// Main Menu
Console.WriteLine("\n");
Console.WriteLine("Please select an option below, or enter "
+ "any other number to quit.\n");
Console.WriteLine("(1) Checking Account");
Console.WriteLine("(2) Super-Now Checking Account");
Console.WriteLine("(3) Savings Account\n");
Console.Write("Enter Choice: ");
inputValue = Console.ReadLine();
try
{
choice = int.Parse(inputValue);
}
catch
{
// Handle problems with invalid input. Clears the console and
// restarts the loop
Console.Clear();
continue;
}
Console.WriteLine();

// Handle input
switch (choice)
{
case 1:
stop = true;
// go to Checking Account Menu
Console.WriteLine("Checking Account");
break;
case 2:
stop = true;
// go to Super-Now Account Menu
Console.WriteLine("Super-Now Checking Account");
break;
case 3:
stop = true;
// go to Savings Account Menu
Console.WriteLine("Savings Account");
break;
default:
Console.WriteLine("Program Terminated");
stop = true;
break;
}

}
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
I don't have time to read the code in detail tonight, but here's some perspective on the inheritance part of your problem, mainly concerning the meaning of the following sentence:

I am to have 4 class files, one for each account, and one bank account class that sends the information to one of the other 3 account classes. What I really need is more of a guide on what to do for these two classes.

Inheritance in C# or any other OO language is for the purpose of implementing common behavior among a set of closely related classes. There are lots of canonical examples: many cars are very different but they can all be started, driven, and stopped; there are many kinds of shapes but they can all be drawn; etc.

Sometimes you start with the most general idea ("Aha, I know what behavior all mammals have in common!"). More often you start with a set of things that seem to be related and try to figure out what a common ancestor would look like.

You have four account types, and your assignment has given you a head start by making it clear that there _are_ somethings that all accounts have in common, and that those things will go into a BankAccount class. We call these base classes, or supertypes.

So you're going to have a BankAccount base class, and in it you will have all the things that _every_account shares. They all have an account number, a holder of record, a date of last transaction, whatever. It's actually an extremely rich problem domain in real life.

Derived from that you will have specialized classes, called subclasses or derived classes, that will implement the things unique to each account type. What you should strive for is to have the main loop reference only BankAccounts, and not have to deal with the subclasses at all. The way to do this is through virtual methods, or "polymorphism." That's the "sends the information to one of the other 3 account classes" part of your post. If you aren't sure what that is we can talk about it more.

 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
Trying to help students with homework is always such a bear - you never know how far along they are, so it's hard to know what you should bring up, and what you shouldn't.

Have you covered abstract classes and methods yet?



At any rate, the most useful advice when dealing with these types of concept is to remember this simple rule: Anything that exists in coding exists to allow coders to be as lazy as humanly possible.
If a piece of code is elegant, what that actually means is the guy who wrote it was so clever he was very, very, very lazy, and managed to solve the problem with as little effort as possible.

How does this help you? Simple - look through your subclasses, find common threads - behaviors and data - and put them in the place where you'll have to write it the fewest times.



EDIT:

Oh, yeah, fair warning - put everything at as high a level as possible, because if your professor is worth his salt, you'll be using this code as a base for your next couple of assignments. And I personally assure you, the next assignment you get will require you to make changes to behaviors that belong in the superclass, so if you don't put them there, you're going to be doing 4x the work, as you'll have to change the code in all 4 subclasses.
 

tjmudder

Member
Dec 16, 2006
77
0
0
thanks for the help! I really appreciate it. I ended up getting help from a friend on how to do this program, but your posts helped explain inheritance to me better.
 

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
I'm not going to look at the problem, but I had this discussion with a programming coworker of mine (he's relatively new).

The best way to think of inheritance is the "is-a" word.

You inherit when something is-a something.

Take for example you have a class:

Person

And have other 3 classes.

Athlete
Nerd
Car

Is a Car a person. No. Car would not inherit a person.
Is an Athlete a person. Yes. Athlete can inherit a person.

Person is the baseclass, and Athelete/Nerd is the abstract class.

Why do you want to do this?

You can define functions, properties, etc in the person class which applies globally. Strength, Intelligence, Weight. Things that apply to a person and don't care if its Nerd or Athlete. You'd throw that in the Person Class, and any abstract class (nerd/athlete) using the baseclass Person would automatically have that functionality.

Plus you can now add specific things about Athlete. Favorite Sport, Number of Golf Clubs owned, etc.

Now if you create a function in another piece of code called "EatFood"... It could define its parameter to take the class "Person" rather than create a function that takes Athelete or Nerd as a parameter. If you pass the Athelete or Nerd variable into the EatFood function which expects Person, it will work, because Person is part of both of those other classes. This is the time saver.

But you can't make the Athlete eat his Golf Clubs in the EatFood function because EatFood knows nothing about Athlete, only the person. (Realistically EatFood would be a function of the Person Class, but just for the point of explaining it I made EatFood something else entirely)...

I hope that made sense.
 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
Out of curiosity, how do you guys feel about mutator functions in base classes? The typical example is the circle-ellipse-scaleY problem, but in the example above, an athlete could theoretically call the base method eat so many times he's no longer an athlete.