[英]C# multiple records against the branch object
将客户记录添加到它访问的(一些杂货店的)分支中。 如果客户访问多个分支,则将其添加到具有不同ID的多个分支中。 现在我的问题是,无论访问哪个分支以及访问多少个分支,我如何才能只拥有一份客户记录。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console47
{
class Customer
{
public Guid CustomerID { get; set; }
public string CustomerName { get; set; }
public string CustomerAddress { get; set; }
public string CustomerEmail { get; set; }
public string CustomerTelNo { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console47
{
class Branch
{
public Guid BranchID { get; set; }
public string BranchName { get; set; }
public string BranchAddress { get; set; }
public string BranchTelNo { get; set; }
public List<Customer> customers = new List<Customer>();
public void AddCustomer()
{
var customer = new Customer();
Console.Write("\nEnter No of customers you want to Add: ");
var input = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\nPlease remember to Enter input in the following order:\n");
for (int i = 0; i < input; i++)
{
Console.WriteLine("1-Customer Name: 2-Customer Address: 3-Customer Telno.: 4-Customer Email: 5-:Branch Name:\n");
customers.Add(new Customer()
{
CustomerID = Guid.NewGuid(),
CustomerName = Console.ReadLine(),
CustomerAddress = Console.ReadLine(),
CustomerTelNo = Console.ReadLine(),
CustomerEmail = Console.ReadLine(),
});
}
Console.WriteLine("***************************");
}
public void SearchCustomer()
{
Console.Write("\nEnter the name of the customer to find out which branch it is related to:");
var Cname = Console.ReadLine();
foreach (var Customeritem in customers)
{
if (Cname == Customeritem.CustomerName )
{
Console.WriteLine("\nCustomer ID:{0}\nCustomer Name:{1}\nCustomer Address:{2}\nCustomer Tel. No:{3}\nCustomer Email:{4}\n", Customeritem.CustomerID, Customeritem.CustomerName,Customeritem.CustomerAddress,Customeritem.CustomerTelNo,Customeritem.CustomerEmail);
Console.Write("\nCustomer found");
}
else if (Cname != Customeritem.CustomerName)
{
Console.Write("Customer Not found");
}
}
}
public void DeleteCustomer()
{
Console.Write("To delete customer, enter name of the customer:");
var Cname = Console.ReadLine();
for (int i = 0; i < customers.Count; i++)
{
if (customers[i].CustomerName == Cname)
{
customers.RemoveAt(i);
Console.Write("Customer found and deleted");
break;
}
else if (Cname != customers[i].CustomerName)
{
Console.Write("Customer Not found");
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console47
{
class Program
{
static void Main(string[] args)
{
/***********Add branches************/
List<Branch> branches = new List<Branch>();
var branch = new Branch();
Console.WriteLine("************************");
Console.WriteLine("Total Branches of Lidel:");
Console.WriteLine("************************\n");
var branch1 = new Branch()
{
BranchID = Guid.NewGuid(),
BranchName = "lidel1",
BranchAddress = "ejbyvej 27",
BranchTelNo = "55223366",
};
branches.Add(branch1);
var branch2 = new Branch()
{
BranchID = Guid.NewGuid(),
BranchName = "lidel2",
BranchAddress = "kirkevej 45",
BranchTelNo = "77885544",
};
branches.Add(branch2);
var branch3 = new Branch()
{
BranchID = Guid.NewGuid(),
BranchName = "lidel3",
BranchAddress = "kirkevej 12",
BranchTelNo = "553366611",
};
branches.Add(branch3);
for (int i = 0; i < branches.Count; i++)
{
Console.WriteLine("\nBranch ID:{0}\nBranch Name:{1}\nBranch Address:{2}\nBranch Telno.{3}\n", branches[i].BranchID, branches[i].BranchName, branches[i].BranchAddress, branches[i].BranchTelNo);
Console.WriteLine("************************");
}
/***********Add, delete, search customer in specific branch************/
int choise;
Console.WriteLine("\nPlease choose from the following option:\n");
UserChoise:
Console.WriteLine("\n1: Add Customers:\n2: Search Customer:\n3: Delete Customer:\n4: Display total no of customers in All Lidel Branches:\n5: Press enter to exit:\n");
Console.Write("Your input is: ");
choise = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("***************************");
switch (choise)
{
case 1:
{
Console.Write("Enter the Branch name where you want to add the customer: ");
var BranchInput = Console.ReadLine();
if (BranchInput == branch1.BranchName)
{
branch1.AddCustomer();
}
else if (BranchInput == branch2.BranchName)
{
branch2.AddCustomer();
}
else if (BranchInput == branch3.BranchName)
{
branch3.AddCustomer();
}
else if (BranchInput != branch.BranchName)
{
Console.WriteLine("\nBranch doest not exist, Please enter again.\n");
}
goto UserChoise;
}
case 2:
{
foreach (var bitem in branches)
{
bitem.SearchCustomer();
Console.Write(" in branch " +bitem.BranchName+".");
}
Console.WriteLine("***************************");
goto UserChoise;
}
case 3:
{
foreach (var item in branches)
{
item.DeleteCustomer();
Console.Write(" in branch " + item.BranchName+ ".\n");
}
Console.WriteLine("***************************");
goto UserChoise;
}
case 4:
{
//Console.WriteLine("***************************");
foreach (Branch bitem in branches)
{
foreach (Customer citem in bitem.customers)
{
Console.WriteLine("\nCustomer ID:{0}\nCustomer Name:{1}\nCustomer Address:{2}\nCustomer Telno.:{3}\nCustomer Email:{4}\n", citem.CustomerID, citem.CustomerName, citem.CustomerAddress, citem.CustomerTelNo, citem.CustomerEmail);
Console.WriteLine("***************************");
}
}
Console.WriteLine("***************************");
goto UserChoise;
}
case 5:
{
Console.Write("Enter the Branch name to find its customers: ");
var BranchInput = Console.ReadLine();
foreach (Branch item in branches)
{
if (BranchInput == item.BranchName)
{
foreach (Customer citem in item.customers)
{
{
Console.WriteLine("\nCustomer ID:{0}\nCustomer Name:{1}\nCustomer Address:{2}\nCustomer Telno.:{3}\nCustomer Email:{4}\n", citem.CustomerID, citem.CustomerName, citem.CustomerAddress, citem.CustomerTelNo, citem.CustomerEmail);
Console.WriteLine("***************************");
}
}
}
}
goto UserChoise;
}
}
}
}
}
您如何知道客户是否已经存在? 通过电子邮件? 按名字? 让我们考虑客户是由CustomerName标识的(就像您在“ SearchCustomer”方法中搜索一样)。
您具有多对多的上下文(许多客户可以访问许多分支机构,许多客户可以访问许多分支机构)。
因此,如果同一位客户访问许多分支机构,则需要了解此客户。 好。
您将需要记住添加的每个客户。 就像您对分支机构一样。
就像是:
var customer = new List<Customer>()
但是您不能将其放在Main Method中,因为Branch方法无法访问它。 因此,您可以执行以下操作:
- Create a global list: public static List<Customer> Customers = new List<Customer>();
// initialize list for pass ArgumentNullException on LINQ methods
在AddCustomer方法中更改“ for”:
for (int i = 0; i < input; i++)
{
Console.WriteLine("1-Customer Name:\n2-Customer Address:\n3-Customer Telno.:\n4-Customer Email:\n5-:Branch Name:\n");
var customer = new Customer
{
CustomerName = Console.ReadLine()
};
//Search existing customer
var existingCustomer = Customers.FirstOrDefault(c => c.CustomerName == customer.CustomerName);
if (existingCustomer != null)
{ //existing customer encountered
customers.Add(existingCustomer);
}
else
{
customer.CustomerID = Guid.NewGuid(); // generate new ID
customer.CustomerAddress = Console.ReadLine();
customer.CustomerTelNo = Console.ReadLine();
customer.CustomerEmail = Console.ReadLine();
customers.Add(customer);
Customers.Add(customer); // add to global list
}
}
但是,从对象的方法访问全局列表不是一个好主意。 您可以创建执行此过程的扩展方法或服务类。
编辑:
如果您认为可以将同一客户添加到分支机构中而不重复,则需要在分支机构客户列表中找到该客户。 只需更新代码:
var existingCustomer = Customers.FirstOrDefault(c => c.CustomerName == customer.CustomerName);
if (existingCustomer != null) //existing customer encountered ?
{
// check if customer already added in the branch
if(customers.Any(c => c.CustomerName == existingCustomer.CustomerName)){
// do something
}
else {
customers.Add(existingCustomer);
}
}
唯一的客户可以访问多个分支机构。
多个客户可以访问一个唯一的分支。
这定义了多对多关系。
在数据库中,这将导致这种表结构:
顾客
id | someDatas
---+----------
1 | ...
2 | ...
3 | ...
分支机构
id | someDatas
---+----------
1 | ...
2 | ...
3 | ...
customerbranch
idcustomer | idbranch
-----------+----------
1 | 1
1 | 2
2 | 3
3 | 1
3 | 2
3 | 3
其中idcustomer
和idbranch
是表customers
和branches
外键
由于您没有使用数据库,因此可以通过以下方式建立Customer
类和Branch
类之间的关系:
class Customer
{
public Guid CustomerID { get; set; }
public string CustomerName { get; set; }
public string CustomerAddress { get; set; }
public string CustomerEmail { get; set; }
public string CustomerTelNo { get; set; }
public var Branches = new List<Branch>();
}
class Branch
{
public Guid BranchID { get; set; }
public string BranchName { get; set; }
public string BranchAddress { get; set; }
public string BranchTelNo { get; set; }
public var Customers = new List<Customer>();
}
然后,在Main
方法中,我将为客户和分支使用2个列表。
static void Main(string[] args)
{
var branches = new List<Branch>();
var customers = new List<Customer>();
/* You can still of course hardcode some branches ... */
branches.Add(new Branch
{
BranchID = Guid.NewGuid(),
BranchName = "lidel1",
BranchAddress = "ejbyvej 27",
BranchTelNo = "55223366"
});
branches.Add(new Branch
{
BranchID = Guid.NewGuid(),
BranchName = "lidel2",
BranchAddress = "kirkevej 45",
BranchTelNo = "77885544"
});
branches.Add(new Branch
{
BranchID = Guid.NewGuid(),
BranchName = "lidel3",
BranchAddress = "kirkevej 12",
BranchTelNo = "553366611"
});
/* ... and some customers */
customers.Add(new Branch
{
CustomerID = Guid.NewGuid(),
CustomerName = "John Smith",
CustomerAddress = "somewhere",
CustomerTelNo = "0123456789",
CustomerEmail = "john.smith@example.com"
});
customers.Add(new Branch
{
CustomerID = Guid.NewGuid(),
CustomerName = "Jane Doe",
CustomerAddress = "Elsewhere",
CustomerTelNo = "9876543210",
CustomerEmail = "jane.doe@example.com"
});
customers.Add(new Branch
{
CustomerID = Guid.NewGuid(),
CustomerName = "Foo Bar",
CustomerAddress = "anywhere",
CustomerTelNo = "4242424242",
CustomerEmail = "foo.bar@example.com"
});
}
现在有一些分支机构和一些客户,但是它们之间还没有关系。
您可以在branche类中构建一个方法来添加现有客户。
class Branch
{
//Some properties...
//The customer already exists and is used as parameter
public void AddCustomer(Customer customer)
{
this.Customers.Add(customer); //the reference to the customer is added to the list.
/*
* Now, since we're in a Mant-To-Many relation,
* we have to add the current branch to the newly added customer
*/
customer.Branches.Add(this);
}
}
我们可以对Customer类执行相同的操作,但这不是必需的:
class Customer
{
//Some properties...
//The branch already exists and is used as parameter
public void AddBranch(Branch branch)
{
this.Branches.Add(branch); //the reference to the branch is added to the list.
/*
* Now, since we're in a Mant-To-Many relation,
* we have to add the current customer to the newly added branch
*/
branch.Customers.Add(this);
}
}
当然,当您在客户对象中添加了一个分支,并且在该分支中添加了当前客户时,请不要执行branch.AddCustomers(this);
否则,将存在一个无限循环,因为一个方法将调用另一个方法。
现在,让我们将第二个客户Jane Doe添加到第三个的lidel3分支中。
static void Main(string[] args)
{
// Some code already written ...
branches[2].AddCustomer(customers[1]);
}
我们可以向客户John Smith添加分支Lidel2的方法相同
static void Main(string[] args)
{
// Some code already written ...
customers[0].AddBranch(branches[1]);
}
现在,写代码的左边就是让用户添加新客户,新分支并在它们之间建立关系,这是您的工作。
额外:
为了避免goto
,我将使用以下逻辑:
int choice = 1; //default arbitrary value to make sure the while condition is true
while (choice >= 1 && choice <= 4) //condition is true if user input is 1 2 3 or 4
{
Console.WriteLine("\n1: Add Customers:\n2: Search Customer:\n3: Delete Customer:\n4: Display total no of customers in All Lidel Branches:\n5: Press enter to exit:\n");
Console.Write("Your input is: ");
choice = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("***************************");
switch (choice)
{
case 1:
//some code to handle the case 1
break; //use break to leave the switch case instead of goto
case 2:
//some code to handle the case 2
break;
//And so on ...
}
} //When while condition is false, the loop is exited
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.