Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assignment2_Nonyelu
{
    public class A2AccountTest
    {
        public static void Main(string[] args)
        {
            //a
            A2Account myObj1 = new A2Account();
            Console.WriteLine("The balance = {0:C}", myObj1.Balance);
            A2Account myObj2 = new A2Account("Mary Johns", 5000.0);
            Console.WriteLine("The balance = {0:C}", myObj2.Balance);

            //b
            myObj1();

            //c
            double a = myObj1.Withdraw(1000.0);
            if(a < 0)
                Console.WriteLine("Insufficient funds");
            Console.WriteLine("The balance = {0:C}", myObj1.Balance);

            a = myObj2.Withdraw(1000.0);
            if (a < 0)
                Console.WriteLine("Insufficient funds");
            Console.WriteLine("The balance = {0:C}", myObj2.Balance);

            //d
            Console.WriteLine("Account Owner: Mary Johns: Balance = nnnnn");
        }
    }
    public class A2Account
    {
        private string owner; //Q1
        private double balance;
        private int transaction;


    //Q2
    public A2Account()
    {
        owner = "Mary Johns";
        balance = 0;
    }
    public A2Account(string s, double d)
    {
        owner = s;
        balance = d;
    }
    
    //Q3
    public double Balance
    {
            get
            {
                return balance;
            }
            set
            {
                balance = value;
            }
    }
    //Q4
    public double Withdraw(double a)
    {
        if (balance >= a)
        {
            transaction++;
            balance -= a;
            return balance;
        }
        else
        {
            return -1;
        }
            //double amount = a;
            //return balance = 0;
    }
    //Q5
    public string ToString()
    {
            return "Account Owner: Mary Johns: Balance = nnnnn" + balance;
    }
    }
}


What I have tried:

The part I'm stuck on is //b. The question is, How do I add 800 dollars to the account myObj1 using the property Balance?

In the Main method under //b, I've tried to add 800 dollars by trying these separately, myObj1.A2Accounnt(800.0);, myObj1.Balance(800.0);, myObj1.A2AccoontTesst, A2Account.myObj1, but I get an error every time. My assignment doesn't ask for a GetBalance, so I'm not adding it.

I've tried to do it by myself, but no luck. Can you please help?
Posted
Comments
PIEBALDconsult 8-Feb-16 23:16pm    
How about Withdraw(-800) ?

Very poorly-written code.
Yetco 8-Feb-16 23:29pm    
Thank you and that worked, but I'm trying to add 800 dollars. Why would withdraw work? I'm also new to coding so, the code will look crazy.
BillWoodruff 8-Feb-16 23:57pm    
This is probably homework, and it's clear from your code you are confused about very basic aspects of C#. You need to get a good book about C# and put in the hard work to get oriented. Look up ".Net Book Zero" on the web and download the free book by Charles Petzold.
Yetco 9-Feb-16 0:11am    
Yea, C# is difficult to me, but I'm trying. Thank you for telling me about Net Book Zero. I hope it will explain things better.

1 solution

As Piebald says:
C#
myObj1.Withdraw(-800);
will do it, because a negative withdrawal is a deposit.
But...
That leave you with two ways to add or withdraw money, because your Balance property is public. Hence, you can avoid your transaction count just by adding:
C#
myObj1.Balance += 1000;

Or removing:
C#
myObj1.Balance += 1000;
the money directly.
Since your question talks about using the Balance property to deposit money, you probably want to get rid of the Withdraw method, and make the property record the transaction.

And please, don't call your variables "myObj1" and so forth: clientAccount or custAcc is a lot more readable.
 
Share this answer
 

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