我试图让make current balance方法在调用提款方法后更新其余额,以便它成为任何后续提款的起始余额。我不知道我在做什么wrong.Thanks。
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
public class Customer
{
private double deposit;
private double balance;
static double bankCharge = 0.50;
public Customer(double depo)
{
if (depo > 2000)
{
Console.WriteLine("deposit cannot be morethan 2000");
}
else
{
this.deposit += depo;
}
}
public void setDeposit(double depo)
{
if (depo > 2000)
{
Console.WriteLine(" Sorry, you cannot deposit morethan 2000");
}
else
{
this.deposit+= depo;
}
}
public double currentBalance()
{
this.balance= this.deposit;
//this.balance+=this.balance;
return this.balance;
}
public double Withdrawal()
{
int amount;
bool passed;
do
{
Console.WriteLine("How much do you want to withdraw?");
passed = int.TryParse(Console.ReadLine(), out amount);
if ((!passed) || (amount % 5 != 0))
{
Console.WriteLine("Wrong input,No decimals please,Enter in multiples of 5, Try again");
}
} while (!passed || amount % 5 != 0);
double charges = amount + bankCharge; // amount to be withdrawn + the bank charge
Console.WriteLine("charges are {0}", charges);
// Console.WriteLine("Your current balance is {0}", this.currentBalance());
if (charges > this.currentBalance())
{
Console.WriteLine("Sorry, you do not have enough money to perform this transaction");
}
else
{
Console.WriteLine("Your current balance is {0}", this.balance);
this.balance-= charges; // withdrawal done
Console.WriteLine(" balance after transaction/charges is={0} ",this.balance);
}
return this.balance;
}
}
class Program
{
static void Main()
{
Customer lee = new Customer(500);
lee.setDeposit(2000);
lee.setDeposit(400);
//Console.WriteLine("the withdraw function returns this amount {0}", lee.Withdrawal());
Console.WriteLine(lee.Withdrawal());
Console.WriteLine(lee.currentBalance());
}
}
答案 0 :(得分:0)
代码现在按照我想要的方式工作。我摆脱了currentBalance方法中的实现。我将this.balance设置为存放在构造函数和setDeposit方法中,每次存入时都会更新余额。平衡同样更新每个调用取款方法并取款的时间。这是我最初的挑战;取款后余额没有更新。现在已经确定。您可以关闭。
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
public class Customer
{
private double deposit;
private double balance;
static double bankCharge = 0.50;
public Customer(double depo)
{
if (depo > 2000)
{
Console.WriteLine("deposit cannot be morethan 2000");
}
else
{
this.deposit += depo;
this.balance = this.deposit;
}
}
public void setDeposit(double depo)
{
if (depo > 2000)
{
Console.WriteLine(" Sorry, you cannot deposit morethan 2000");
}
else
{
this.deposit += depo;
this.balance = this.deposit;
}
}
public double currentBalance()
{
return this.balance;
}
public double Withdrawal()
{
int amount;
bool passed;
do
{
Console.WriteLine("How much do you want to withdraw?");
passed = int.TryParse(Console.ReadLine(), out amount);
if ((!passed) || (amount % 5 != 0))
{
Console.WriteLine("Wrong input,No decimals please,Enter in multiples of 5, Try again");
}
} while (!passed || amount % 5 != 0);
double charges = amount + bankCharge;
Console.WriteLine("charges are {0}", charges);
Console.WriteLine("Your current balance is {0}", this.balance);
if (charges > this.balance)
{
Console.WriteLine("Sorry, you do not have enough money to perform this transaction");
}
else
{
this.balance = this.balance - charges;
}
return this.balance;
}
}
class Program
{
static void Main()
{
Customer lee = new Customer(500);
lee.setDeposit(2000);
lee.setDeposit(400);
lee.setDeposit(250);
lee.setDeposit(1850);
// Console.WriteLine( lee.Withdrawal());
//Console.WriteLine(lee.Withdrawal());
Console.WriteLine("Balance before withdawal is {0}", lee.currentBalance());
Console.WriteLine( " Your balance after withdawal is {0}",lee.Withdrawal());
Console.WriteLine("Starting balance for next transaction is {0}",lee.currentBalance());
Console.WriteLine("*******************************************************");
Console.WriteLine("Balance before withdawal is {0}", lee.currentBalance());
Console.WriteLine(" Your balance after withdawal is {0}", lee.Withdrawal());
Console.WriteLine("Starting balance for next transaction is {0}", lee.currentBalance());
}
}