Suppose that you are developing a class, Account, from which a customer can
withdraw money. The Account class needs to keep track of the number of
withdrawals that have occurred in each customer’s account. The partial
specification for Account class is given below. You are required to complete the
code for the Account and Tester classes. Comment all of your code and make sure
to put your name as a comment at the top of your program.

class Account
{
//variables
accountNumber;
accountBalance;
static numberOfWithdrawal;
Account(int account, int amount)
{
//constructor class
}
Withdraw(int account, int amount)
{
//withdraw the amount from the balance
}
DisplayAccountBalace()
{
//display the current account balance to console
}
Static DisplayNumberOfWithdrawal()
{
//display the number of withdrawals to console
}
}
class Tester
{
Main()
{
/*create three Account objects and withdraw money twice from
each account. Then display the account number and current
balance for each account to the console. */
/*display the total number of withdrawals from the three accounts
to console.*/
}

This is what i have so far. I had to miss class one day and it screwed me. Please Help!

Dani AI

Generated

The thread shows two useful directions already: is right that every field and method needs a type in C#, and correctly points out the static counter should be incremented on each successful withdrawal. ’s cookie-cutter image is a helpful way to think about classes (the template) versus objects (each cookie made from it).

Recommended design points for this assignment:

  • Keep account fields private and choose types deliberately: use int for an account number and decimal for money to avoid floating-point rounding.
  • A static integer tracks total successful withdrawals across all instances. Increment that only after a withdrawal passes validation.
  • The constructor initializes number and starting balance. Withdraw should validate positive amounts and sufficient funds; throw or return an error on invalid attempts.
  • Provide instance method(s) to display an account’s balance and a static method to display the total withdrawal count.

Example implementation (concise, easy to compile) that follows those rules:

using System;

class Account
{
    private int _accountNumber;
    private decimal _balance;
    private static int s_withdrawalCount;

    public Account(int accountNumber, decimal initialBalance)
    {
        _accountNumber = accountNumber;
        _balance = initialBalance;
    }

    public void Withdraw(decimal amount)
    {
        if (amount <= 0) throw new ArgumentException("Amount must be positive.");
        if (amount > _balance) throw new InvalidOperationException("Insufficient funds.");
        _balance -= amount;
        s_withdrawalCount++;
    }

    public void DisplayBalance()
    {
        Console.WriteLine("Account {0}: balance {1:C}", _accountNumber, _balance);
    }

    public static void DisplayTotalWithdrawals()
    {
        Console.WriteLine("Total withdrawals: " + s_withdrawalCount);
    }
}

class Tester
{
    static void Main()
    {
        var a1 = new Account(101, 500m);
        var a2 = new Account(102, 400m);
        var a3 = new Account(103, 300m);

        a1.Withdraw(50m); a1.Withdraw(25m);
        a2.Withdraw(100m); a2.Withdraw(50m);
        a3.Withdraw(75m); a3.Withdraw(25m);

        a1.DisplayBalance(); a2.DisplayBalance(); a3.DisplayBalance();
        Account.DisplayTotalWithdrawals();
    }
}

Notes and pitfalls: validate inputs, keep money as decimal, and remember the constructor has no return type. For multi-threaded scenarios use System.Threading.Interlocked.Increment for the static counter. The minimal example above addresses the missing types that pointed out and implements the counter behavior mentioned by .

Recommended Answers

All 18 Replies

That is what you have so far? It really looks like you are quoting directly off an assignment.

None of your functions or variables have been typed. That would be a good start.

each withdraw increment the numberOfWithdrawal

That is what I typed. I couldn't go any further because I really don't understand objects. If someone could explain it, or give me an example within the project to get me started, I would greatly appreciate it.

imagine a cookie cutter. That is a class. Now a cookie is an object of the class that is cookie cutter!

i.e. a cookie cutter can be used >1 time to make numerous objects

First Withdraw shoud be public and some methods too

public Withdraw(int account, int amount)
{
Account.numberOfWithdrawal++;
..... // rest of code
..... // rest of code
}

I see you hardly need to read in OOP before programming with C#, you won't do true projects with C# before fully understanding the concept of OOP.

i.e. a cookie cutter can be used >1 time to make numerous objects

ok that sorta makes sense

but i am still lost....

I am the c# class right now, and i can't learn in here cuz my teacher is from china and i can't understand him so i have to teach myself(or get help here)

That is what I typed. I couldn't go any further because I really don't understand objects. If someone could explain it, or give me an example within the project to get me started, I would greatly appreciate it.

That's just it though. NOTHING is typed! Let's take a step back and look at the code.

Assume it's C++, as from looking at your past postings that is another language you know. If you wrote all those functions and variables in C++, would they compile? No. Why? Because the compiler has no idea what any of them are typed (this isn't PHP, the compiler MUST know what each variable type is and must know if the functions are returning a variable or not).

I say it looks like you copied code because you have the function parameters typed (which means you somehow knew how to type those) and you even know about the concept of 'static' variables and function, yet you didn't type the basic variables they teach you almost in day one of any programming course?


So again, before you even try to deal with the higher level concept of Objects, look at the code and create all the types for the functions and variables. The typing of these is done the same way whether or not you use objects in the program. That's why you should just ignore the objects part first and deal with the basics that you should understand. The ONLY 'function' in all of that code that does not get typed is the Constructor for Class Account.

so i can type all the functions within the program without objects?

I didn't know that.

so I can go on an type the function that records the number of withdrawals before even looking at objects?

would anyone recommend C# for Dummies?

I will get flamed for this but yes I would lol

everyone has to learn somewhere I used C# for dummies and found it helpful - its easy to follow and has good examples.The stuff in there is obviously not that advanced but like I said, everyone has to start somewhere

I recommend Inside C# and How to program

but all in all MSDN is the best for ever, I didn't learn C# from a book, everything I look at MSDN and some methods documentation and some trying...

or there is SAMS teach yourself c# in 21 days thats quite good

guys you have been great help....i am gonna try and write that program now, and i'll post it later, and get your feedback

sure thing good luck

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.