Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
using System;

namespace  BankAccountManagemnetSystem
{
    public class BankAccount
    {
        private decimal balance;
        private int amount;

        public BankAccount(decimal initialBalance)
        {
            balance = initialBalance;
        }

        public void Deposit(decimal amouont)
        {

            if (amount > 0)
            {
                balance += amount;
            }
            else
            {
                throw new ArgumentException("Deposit amount must be positive.");
            }
        }

        public void withdraw(decimal amount)
        {
            if (amount <= balance && amount > 0)
            {
                balance -= amount;
            }
            else
            {
                throw new InvalidOperationException("Not enough balance or amount is negative.");
            }
        }

        public decimal GetBalance()
        {
            return balance;
        }

    }
}


What I have tried:

using System;

namespace  BankAccountManagemnetSystem
{
    public class BankAccount
    {
        private decimal balance;
        private int amount;

        public BankAccount(decimal initialBalance)
        {
            balance = initialBalance;
        }

        public void Deposit(decimal amouont)
        {

            if (amount > 0)
            {
                balance += amount;
            }
            else
            {
                throw new ArgumentException("Deposit amount must be positive.");
            }
        }

        public void withdraw(decimal amount)
        {
            if (amount <= balance && amount > 0)
            {
                balance -= amount;
            }
            else
            {
                throw new InvalidOperationException("Not enough balance or amount is negative.");
            }
        }

        public decimal GetBalance()
        {
            return balance;
        }

    }
}
Posted
Comments
Maciej Los 8-Nov-23 2:44am    
What have you tried till now?

Seems, you have to create an instance of BankAccount class...
Lyrica Sugiyama 8-Nov-23 20:17pm    
I actually have’t tried yet. I'm so sorry.
I'm beginner and I didn't know about solution.
That's why I just put same code there.
PIEBALDconsult 8-Nov-23 10:31am    
I'd use a text editor.

1 solution

That is not a program it is a Class.

Assuming you are just trying to create a Console program it could be as simple as
C#
using System;
					
public class Program
{
	public static void Main()
	{
		// put some code here

	}
}
Again, assuming you want to use that code you posted in some way, the first thing you will neeed to do is create an instance of that class.

Every tutorial, book or class I have seen that teaches a coding language starts with a simple program e.g.
C#
using System;
					
public class Program
{
	public static void Main()
	{
		Console.WriteLine("Hello World");
	}
}
Which suggests that you are just diving in with code borrowed from an internet article. Don't try to learn that way, it's hard enough and you are just making it harder for yourself. Follow a structured tutorial or book and do the exercises. Don't try to take shortcuts if you really want to learn

EDIT:
A simple example (untested)
Something like this
public class Program
{
	public static void Main()
	{
		// create a new Bank Account with zero balance
		BankAccount myBA = new BankAccount(0);
		// deposit some money
		myBA.Deposit(100.0m);
		// Output the balance
		Console.WriteLine("Balance is {0}", myBA.GetBalance());
	}
}
Note you have spelling mistakes in your class code so it shouldn't compile - see amouont

You might also find this useful C# Beginner Tutorials | .NET Academy[^]
 
Share this answer
 
v2
Comments
Maciej Los 8-Nov-23 3:46am    
5ed!
Lyrica Sugiyama 8-Nov-23 20:13pm    
Thank you so much! But I am so sorry.
I knew that I have to put like you taught me. I just didn't know how to put some code there.
That's what I want to ask you.
CHill60 9-Nov-23 3:46am    
I have updated my solution with a simple example and a link to some tutorial resource.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900