有负值的问题! [英] Having problems with negative values!

查看:70
本文介绍了有负值的问题!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我创建了一个类,它使用以下三个值进行操作:

Ok, I created a class which manipulates with three values like following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Account
{
    public class Account
    {
        private double amount;
        private double drawing;
        private double deposit;
    
        public Account()
        {
        }

        public double Amount
/*This shows the amount or state of your account*/
        {
            get { return amount; }
            set { amount = value;}
        }
        
        public double Drawing
/*This presents the drawing amount form account*/
        {
            get { return drawing; }
            set { drawing = value; }
        }

        public double Deposit
/*This presents the deposit amount in account*/
        {
            get { return deposit; }
            set { deposit = value; }
        }        

    }
}





如何解决代码问题如果金额小于绘图,则允许从帐户中提取。

我可以在表单应用程序中修复它,但是如果涉及这些类,我不知道如何调用类方法以及如何修复这个在课堂上。



提前感谢您的回复。



How to resolve the code to not allow drawing from account if the amount is less than drawing.
I can fix that in a form application, but if there are involved the classes I don't know how to call class methods and how to fix this inside the class.

Thank you in advance for your reply.

推荐答案

绘图和存款应该是方法而不是属性。

一个帐户基本上持有一定数量的钱;借鉴或存入账户的方法;应该允许或不允许绘制它取决于可用的数量。
Drawing and Deposit should be methods instead of properties.
An account basically holds a given amount of money; drawing from it or depositing to it are methods on the account; drawing from it should be allowed or not depending on the available amount.


这不是很容易描述,因为你显示的代码不包含减少余额的代码,但它允许直接从账户值减去。

我可能会做的是使属性设置者私人(或可能 protected )而不是public来防止这种情况,而是创建一个Withdraw方法 - 它可以检查并抛出异常或返回一个状态代码,上面写着不足的资金:

That's not really simple to describe, because the code you show doesn't include an code to reduce the balances, but it does allow direct subtraction from the account values.
What I would probably do is make the property setters private (or possibly protected) instead of public to prevent that, and create a Withdraw method instead - it can check and either throw an exception or return a status code which says "insufficent funds":
/// <summary>
/// This shows the amount or state of your account
/// </summary>
public double Amount
    {
    get { return amount; }
    private set { amount = value; }
    }
/// <summary>
/// This presents the drawing amount form account
/// </summary>
public double Drawing
    {
    get { return drawing; }
    private set { drawing = value; }
    }
/// <summary>
/// This presents the deposit amount in account
/// </summary>
public double Deposit
    {
    get { return deposit; }
    private set { deposit = value; }
    }
/// <summary>
/// Take the money from the account
/// </summary>
/// <param name="withdrawl"></param>
/// <returns></returns>
public bool Withdraw(double withdrawl)
    {
    if (Amount < withdrawl) return false;
    Amount -= withdrawl;
    return true;
    }


这篇关于有负值的问题!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆