所以我需要创建这个银行服务。除了在提款或存款等交易后尝试使用客户的更新余额外,我拥有一切。例如,客户以 1000 美元开始,客户 1 存入 300 美元。更新后的余额应该是 1300 美元,但是一旦我进行另一笔交易,它就会回到默认的 1000 美元,而不是新的 1300 美元。客户在列表中。
[ServiceContract(SessionMode=SessionMode.Allowed)]
public interface IBankService
{
[OperationContract]
string GetCustDetails(int act);
[OperationContract]
string WithdrawMoney(int act, double amt);
[OperationContract]
string DepositMoney(int act,double amt);
[OperationContract]
string ViewBalance(int act);
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class Customer
{
private int accountNumber; private double viewBalance, depositMoney;
private string customerName, customerAddress;
public Customer(int act, string str_name, string adrs, double bal)
{
accountNumber = act;
customerName = str_name;
customerAddress = adrs;
viewBalance = bal;
}
[DataMember(Name = "Name")]
public string CustomerName
{
get { return customerName; }
set { customerName = value; }
}
[DataMember(Name = "Address")]
public string CustomerAddress
{
get { return customerAddress; }
set { customerAddress = value; }
}
[DataMember(Name = "Account")]
public int AccountNumber
{
get { return accountNumber; }
set { accountNumber = value; }
}
[DataMember(Name = "Balance", EmitDefaultValue = true)]
public double ViewBalance
{
get { return viewBalance; }
set
{
if (value <= 0.0)
viewBalance = 0.0;
else
viewBalance = value;
}
}
[DataMember(Name = "Credit")]
public double DepositMoney
{
get { return depositMoney; }
set { depositMoney = value; }
}
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
class BankService : IBankService
{
List<Customer> custList;
static double balance1 = 1000;
static double balance2 = 1000;
static double balance3 = 1000;
static double balance4 = 1000;
static double balance5 = 1000;
public BankService()
{
custList = new List<Customer>();
custList.Add(new Customer(100, "Jack", "404 Bay Avenue", balance1));
custList.Add(new Customer(200, "Jeff", "255 Wade Avenue",balance2));
custList.Add(new Customer(300, "Lou", "984 Leslie Road", balance3));
custList.Add(new Customer(400, "Johnson","1080 Queen Street", balance4));
custList.Add(new Customer(500, "Alex","777 Jane Street", balance5));
}
public string GetCustDetails(int act)
{
foreach (Customer cust in custList)
{
if (cust.AccountNumber == act)
{
return string.Format("Account Number: " + cust.AccountNumber + "\n" +
"Name: " + cust.CustomerName + "\n" +
"Address: " + cust.CustomerAddress + "\n" +
"Balance: $" + cust.ViewBalance);
}
} // end foreach
return string.Format("{0}", "Customer does not exists!");
}
//public string DepositMoney(int act, double amt)
//{
// string balance = null;
// foreach (Customer cust in custList)
// {
// if (cust.AccountNumber == act)
// {
// bal = bal + Dep;
// }
// }
// return balance;
//}
public string DepositMoney(int act, double amt)
{
foreach (Customer cust in custList)
{
if (cust.AccountNumber == act)
{
cust.ViewBalance = cust.ViewBalance + amt;
return string.Format("Account Number : " + cust.AccountNumber + "\n" +
"Name : " + cust.CustomerName + "\n" +
"Address : " + cust.CustomerAddress + "\n" +
"Balance :$ " + cust.ViewBalance);
}
}
return string.Format("{0}", "Customer does not exists!");
}
//public double WithdrawMoney(double widraw)
//{
// return bal = bal - widraw;
//}
// public double ViewBalance(int act)
//{
// return bal;
//}
public string WithdrawMoney(int act, double amt)
{
foreach (Customer cust in custList)
{
if (cust.AccountNumber == act)
{
cust.ViewBalance = cust.ViewBalance - amt;
return string.Format("Account Number : " + cust.AccountNumber + "\n" +
"Name : " + cust.CustomerName + "\n" +
"Address : " + cust.CustomerAddress + "\n" +
"Balance :$ " + cust.ViewBalance);
}
}
return string.Format("{0}", "Customer does not exists!");
}
public string ViewBalance(int act)
{
foreach (Customer cust in custList)
{
if (cust.AccountNumber == act)
{
return string.Format("Balance : $" + cust.ViewBalance);
}
}
return string.Format("{0}", "Customer does not exists!");
}
}
每个客户都有自己的已分配帐号。