本文介绍了如何使用财产余额向帐户myobj1添加800美元?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;
    }
    }
}





我尝试了什么:



我坚持的部分是// b。问题是,如何使用属性Balance向帐户myObj1添加800美元?



在// b下的Main方法中,我试图添加通过单独尝试这些800美元,myObj1.A2Accounnt(800.0);, myObj1.Balance(800.0);, myObj1.A2AccoontTesst,A2Account.myObj1,但我每次都会收到错误。我的任务不要求GetBalance,所以我不添加它。



我试过自己做,但没有运气。你能帮忙吗?



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?

推荐答案

myObj1.Withdraw(-800);

会这样做,因为负面提款是押金。

但是...

这会让你有两种方式来增加或者提取资金,因为您的Balance属性是 public 。因此,您可以通过添加以下内容来避免交易计数:

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:

myObj1.Balance += 1000;



或删除:


Or removing:

myObj1.Balance += 1000;

钱直接。

既然你的问题谈到使用Balance属性来存钱,你可能想要摆脱Withdraw方法,并使属性记录交易。



请不要将变量称为myObj1等等:clientAccount或custAcc更具可读性。

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.


这篇关于如何使用财产余额向帐户myobj1添加800美元?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 22:18