在账户余额上提取一定金额的款项

时间:2015-12-11 15:38:35

标签: c# visual-studio button textbox display

我正在尝试创建简单的if语句,其中程序将检查帐户的余额以及是否低于提款金额。如果是这样,用户将能够提取高达透支金额的金额。如果金额不大于透支金额,则在透支文本框中显示剩余的透支金额。然后在文本框中显示余额。但是,我遇到的问题是当用户退出时,文本框中的透支不会改变。当客户提取超过余额时,应从透支金额中扣除他们超过的金额。剩余的透支应该显示在透支文本框中。例如,如果一个人的余额为0并且允许透支50并试图透支20,那么30仍应保留并显示在透支文本框中并且-20应显示在余额文本框中。 (如果这是有道理的):D 这是我到目前为止的代码

 private void withdraw_Click(object sender, EventArgs e)
    {
        Ballance = double.Parse(balance.Text);
        Withdrawtxt = double.Parse(txt_withdraw.Text);
        Overdraftadd = double.Parse(overdraft.Text);

                    //this checks if the user can even make a withdraw. This checks if the withdraw amount is bigger than ballance + Overdraft

        if (Ballance + Overdraftadd >= Withdrawtxt)
        {
            //if the user can withdraw but its more than the ballance then ballance is equal to ballance + the overdraft: If not then the ballance is equal to ballance - withdraw.
            classify = (Ballance < Withdrawtxt) ? Ballance = (Ballance + Overdraftadd) - Withdrawtxt : Ballance = Ballance - Withdrawtxt;
            //this is then displayed in the textbox
            balance.Text = "" + Ballance;
            // here i want to make it so that the overdraft is changed if the user has used some of it. E.G user withdraws but has to use 20 out of 50 overdraft.
            // if ? true false statement
            classify = (Ballance < Withdrawtxt) ? Overdraftadd =  : ;
            //display overdraft in this box.
            overdraft.Text = "" + Overdraftadd;
        }


    }

1 个答案:

答案 0 :(得分:1)

我真的不明白你在透支盒子里想要什么,但这里有一些代码可以满足你的其他需求。在此示例中,如果帐户透支,我会使用透支作为您要收取的金额。如果这不是你想要的,请澄清。

private void button_Click(object sender, RoutedEventArgs e)
        {
            double balance = double.Parse(Balance.Text);
            double withdraw = double.Parse(Withdraw.Text);
            double overdraft = double.Parse(Overdraft.Text);

            if(balance < withdraw)
            {
                balance -= (overdraft + withdraw);
                Balance.Text = balance.ToString();

            }
            else if(balance > withdraw)
            {
                balance -= withdraw;
                Balance.Text = balance.ToString();
            }
        }

按下按钮时,更新的余额将显示在余额文本框中。

编辑:

private void button_Click(object sender, RoutedEventArgs e)
        {
            double balance = double.Parse(Balance.Text);
            double withdraw = double.Parse(Withdraw.Text);
            double overdraft = double.Parse(Overdraft.Text);

            if (balance < withdraw && (balance - withdraw) >= -(overdraft))
            {
                balance -= withdraw;
                overdraft = overdraft + balance;
                Balance.Text = balance.ToString();
                Overdraft.Text = overdraft.ToString();
            }
        }