大约两周前,我开始学习C#作为Web开发人员的教育的一部分。我有这个任务来创建一个充当银行的控制台应用程序。到目前为止,我可以创建一个客户,找到一个客户并显示他们的信息,帐户余额,我可以存入资金。
但我还需要能够将资金从一个帐户转移到另一个帐户,删除客户并编辑客户。现在我被困在转移资金上了。我希望你们中的某些人能够提供帮助。
我在此处上传了我的代码:https://pastebin.com/Hqyvjuuc
Program.cs的
class Program
{
static void Main(string[] args)
{
ShowMainMenu();
}
public static void ShowMainMenu()
{
Console.Clear();
Console.WriteLine("Weolcome to the Bank");
Console.WriteLine("-------------------");
Console.WriteLine("1: Create customer");
Console.WriteLine("2: Show Customers");
Console.WriteLine("-----------------");
string command = Console.ReadLine();
if (command == "1")
{
ShowCreateCustomer();
}
else if (command == "2")
{
ShowAllCustomers();
}
}
public static void ShowAllCustomers()
{
Console.Clear();
Console.WriteLine("----- All Customers -----");
foreach (Customer myCustomer in Bank.AllCustomers)
{
Console.WriteLine("ID: " + myCustomer.Id + " Name: " + myCustomer.Name + " Money: " + myCustomer.Money);
}
Console.WriteLine("-----------");
Console.WriteLine("1: Deposit, 2: Transfer, 3: Return to main");
string command = Console.ReadLine();
if (command == "1")
{
//deposit
//first find the customer, then deposit the amount
Console.Write("Enter the id of the customer: ");
string customerId = Console.ReadLine();
Console.Write("Enter the amount to deposit: ");
string amount = Console.ReadLine();
//now do the magic, find the customer then increase the money
int theCustomerId = int.Parse(customerId);
Customer myFoundCustomer = null;
foreach (Customer c in Bank.AllCustomers)
{
if (c.Id == theCustomerId)
{
myFoundCustomer = c;
break;
}
}
//we might have found it
if (myFoundCustomer != null)
{
myFoundCustomer.Money += float.Parse(amount);
}
ShowAllCustomers();
}
else if (command == "2")
{
TransferMoney();
}
else if (command == "3")
{
ShowMainMenu();
}
}
public static void ShowCreateCustomer()
{
Console.Clear();
Console.WriteLine("--- Create new Customer ---");
Console.Write("Please enter the name: ");
string name = Console.ReadLine();
Console.Write("Please enter the id: ");
string id = Console.ReadLine();
Customer customer = new Customer();
customer.Name = name;
customer.Id = int.Parse(id);
//add it to the bank
Bank.AllCustomers.Add(customer);
//create customer etc...
//eventually return to main menu
ShowMainMenu();
}
public static void TransferMoney()
{
Console.Clear();
Console.WriteLine("--- Transfer Money ---");
Console.Write("Please enter your account's ID: ");
string id = Console.ReadLine();
Console.Write("Please enter the Name of the person you would like to tranfer funds to: ");
string name = Console.ReadLine();
Console.Write("Enter the amount of funds you would like to transfer: ");
string amount = Console.ReadLine();
/*
if (id == Customer.Id()) & (name == Customer.Name())
{
//transfer happens here
}
*/
ShowAllCustomers();
}
}
Bank.cs
public class Bank
{
private static List<Customer> customers = new List<Customer>();
public Bank()
{
customers = new List<Customer>();
}
public static List<Customer> AllCustomers
{
get { return customers; }
}
}
Customer.cs
public class Customer
{
public Customer()
{
Money = 50;
}
public int Id { get; set; }
public string Name { get; set; }
public float Money { get; set; }
}
答案 0 :(得分:0)
你可以试试这个:
public static void TransferMoney()
{
Console.Clear();
Console.WriteLine("--- Transfer Money ---");
Console.Write("Please enter your account's ID: ");
string id = Console.ReadLine();
Console.Write("Please enter the Name of the person you would like to tranfer funds to: ");
string name = Console.ReadLine();
Console.Write("Enter the amount of funds you would like to transfer: ");
string amount = Console.ReadLine();
foreach (Customer c in Bank.AllCustomers)
{
if (c.Id() == id)
{
c.setMoney(c.getMoney()-amount)
}
}
foreach (Customer c in Bank.AllCustomers)
{
if (c.getName() == name)
{
c.setMoney(c.getMoney()+amount)
}
}
ShowAllCustomers();
}
}
我希望我帮助过。好运银行任务。
祝你好运, Dimitar Georgiev
答案 1 :(得分:0)
您需要实施以下内容:
public Customer findCustomer(int id) {
for (int i = 0; i < Bank.AllCustomers.Count(); i++) {
if (Bank.AllCustomers.ElementAt(i).Id == id) return Bank.AllCustomers.ElementAt(i);
}
return null; //Not found
}
这样可以简化您的生活,因为当您想要向给定帐户添加/减少金额,或将其删除或编辑时,您需要先找到它。
public void transfer(Customer sender, Customer receiver, float amount) {
if ((sender != null) && (receiver != null)) {
//Existent user
if ((amount > 0) && (sender.Money >= amount)) {
//Valid amount
sender.Money -= amount;
receiver.Money += amount;
}
}
}
public void transfer(int fromID, int toID, float amount) {
Customer sender = findCustomer(fromID);
Customer receiver = findCustomer(fromID);
transfer(sender, receiver, amount);
}
您需要找到要编辑和删除的Customer
。所有这些都有详细记录,如果您能够Customer
找到Id
,则应该很容易实现。