C#实现简单订单管理程序攻略
订单管理程序是一个基本的软件开发任务,通过它我们可以练习C#类和对象,掌握基本的输入输出、判断和运算等编程技巧。下面将详细讲解如何使用C#实现一个简单的订单管理程序。
步骤一:定义订单类
首先,我们需要定义一个订单类,用于表示一个订单。订单有一个唯一的标识符、一个客户信息、一个订单日期以及一组订单项。定义订单类的代码如下:
public class Order
{
public int Id { get; set; } // 订单号
public Customer Customer { get; set; } // 客户信息
public DateTime OrderDate { get; set; } // 订单日期
public List<OrderItem> Items { get; set; } // 订单项
public Order(int id, Customer customer, DateTime orderDate)
{
Id = id;
Customer = customer;
OrderDate = orderDate;
Items = new List<OrderItem>();
}
public override string ToString()
{
StringBuilder builder = new StringBuilder();
builder.AppendFormat("Order Id: {0}\n", Id);
builder.AppendFormat("Customer: {0}\n", Customer);
builder.AppendFormat("Order Date: {0}\n", OrderDate);
builder.AppendLine("Order Items:");
foreach (OrderItem item in Items)
{
builder.AppendFormat("\t{0}\n", item);
}
return builder.ToString();
}
}
在订单类中,我们定义了四个成员变量:订单号、客户信息、订单日期以及订单项。订单项是一个列表,包含了多个订单商品。我们还实现了ToString方法,用于打印订单的详细信息。
步骤二:定义客户类和订单项类
接下来,我们需要定义客户类和订单项类,用于表示客户信息和订单商品。客户类和订单项类的代码如下:
public class Customer
{
public int Id { get; set; } // 客户ID
public string Name { get; set; } // 客户名称
public Customer(int id, string name)
{
Id = id;
Name = name;
}
public override string ToString()
{
return string.Format("ID: {0}, Name: {1}", Id, Name);
}
}
public class OrderItem
{
public int Id { get; set; } // 商品ID
public string Name { get; set; } // 商品名称
public double Price { get; set; } // 商品单价
public int Quantity { get; set; } // 商品数量
public OrderItem(int id, string name, double price, int quantity)
{
Id = id;
Name = name;
Price = price;
Quantity = quantity;
}
public override string ToString()
{
return string.Format("ID: {0}, Name: {1}, Price: {2:C}, Quantity: {3}", Id, Name, Price, Quantity);
}
}
在客户类和订单项类中,我们分别定义了客户ID、客户名称、商品ID、商品名称、商品单价以及商品数量等属性,并实现了ToString方法,用于打印客户信息和订单商品的详细信息。
步骤三:实现订单管理程序
现在,我们已经定义了订单类、客户类和订单项类,可以开始实现订单管理程序了。我们可以通过控制台程序进行实现,具体流程如下:
- 打印主菜单,包括添加订单、删除订单、修改订单、查询订单和退出程序等选项。
- 根据用户选择的选项,执行相应的操作。
- 若用户选择添加订单,则提示用户输入订单信息,并根据用户输入创建一个订单对象。
- 若用户选择删除订单,则提示用户输入要删除订单的ID,并从订单列表中删除该订单。
- 若用户选择修改订单,则提示用户输入要修改订单的ID,并根据用户输入更新该订单的信息。
- 若用户选择查询订单,则提示用户选择查询方式,可以根据订单号、客户名称或商品名称进行查询,并返回符合条件的所有订单。
- 若用户选择退出程序,则退出程序,否则回到步骤1。
以下是一个简单的控制台程序的实现示例:
class Program
{
static List<Order> orders = new List<Order>();
static void Main(string[] args)
{
bool exit = false;
while (!exit)
{
Console.WriteLine("Order Management System");
Console.WriteLine("-----------------------");
Console.WriteLine("1. Add Order");
Console.WriteLine("2. Remove Order");
Console.WriteLine("3. Modify Order");
Console.WriteLine("4. Query Order");
Console.WriteLine("5. Exit");
Console.Write("Please select an option: ");
string option = Console.ReadLine();
switch (option)
{
case "1":
AddOrder();
break;
case "2":
RemoveOrder();
break;
case "3":
ModifyOrder();
break;
case "4":
QueryOrder();
break;
case "5":
exit = true;
break;
default:
Console.WriteLine("Invalid option, please try again.");
break;
}
}
}
// 添加订单
static void AddOrder()
{
Console.WriteLine("Add Order");
Console.WriteLine("---------");
Console.Write("Please enter Order ID: ");
int id = int.Parse(Console.ReadLine());
Console.Write("Please enter Customer ID: ");
int customerId = int.Parse(Console.ReadLine());
Console.Write("Please enter Customer Name: ");
string customerName = Console.ReadLine();
Console.Write("Please enter Order Date (yyyy-MM-dd): ");
DateTime orderDate = DateTime.Parse(Console.ReadLine());
Order order = new Order(id, new Customer(customerId, customerName), orderDate);
bool exit = false;
while (!exit)
{
Console.WriteLine("Add Order Item");
Console.WriteLine("--------------");
Console.Write("Please enter Item ID: ");
int itemId = int.Parse(Console.ReadLine());
Console.Write("Please enter Item Name: ");
string itemName = Console.ReadLine();
Console.Write("Please enter Item Price: ");
double itemPrice = double.Parse(Console.ReadLine());
Console.Write("Please enter Item Quantity: ");
int itemQuantity = int.Parse(Console.ReadLine());
order.Items.Add(new OrderItem(itemId, itemName, itemPrice, itemQuantity));
Console.Write("Add another Item? (Y/N): ");
string yn = Console.ReadLine();
if (yn.ToLower() == "n")
{
exit = true;
}
}
orders.Add(order);
Console.WriteLine("Order added successfully.");
}
// 删除订单
static void RemoveOrder()
{
Console.WriteLine("Remove Order");
Console.WriteLine("------------");
Console.Write("Please enter Order ID: ");
int id = int.Parse(Console.ReadLine());
Order orderToRemove = orders.SingleOrDefault(o => o.Id == id);
if (orderToRemove != null)
{
orders.Remove(orderToRemove);
Console.WriteLine("Order removed successfully.");
}
else
{
Console.WriteLine("Order not found.");
}
}
// 修改订单
static void ModifyOrder()
{
Console.WriteLine("Modify Order");
Console.WriteLine("------------");
Console.Write("Please enter Order ID to modify: ");
int id = int.Parse(Console.ReadLine());
Order orderToModify = orders.SingleOrDefault(o => o.Id == id);
if (orderToModify != null)
{
Console.Write("Please enter new Customer ID: ");
int customerId = int.Parse(Console.ReadLine());
Console.Write("Please enter new Customer Name: ");
string customerName = Console.ReadLine();
Console.Write("Please enter new Order Date (yyyy-MM-dd): ");
DateTime orderDate = DateTime.Parse(Console.ReadLine());
orderToModify.Customer.Id = customerId;
orderToModify.Customer.Name = customerName;
orderToModify.OrderDate = orderDate;
Console.WriteLine("Order modified successfully.");
}
else
{
Console.WriteLine("Order not found.");
}
}
// 查询订单
static void QueryOrder()
{
Console.WriteLine("Query Order");
Console.WriteLine("-----------");
Console.WriteLine("1. Query by Order ID");
Console.WriteLine("2. Query by Customer Name");
Console.WriteLine("3. Query by Item Name");
Console.Write("Please select a query option: ");
string option = Console.ReadLine();
switch (option)
{
case "1":
Console.Write("Please enter Order ID: ");
int id = int.Parse(Console.ReadLine());
var ordersById = orders.Where(o => o.Id == id);
PrintOrders(ordersById);
break;
case "2":
Console.Write("Please enter Customer Name: ");
string customerName = Console.ReadLine();
var ordersByCustomerName = orders.Where(o => o.Customer.Name == customerName);
PrintOrders(ordersByCustomerName);
break;
case "3":
Console.Write("Please enter Item Name: ");
string itemName = Console.ReadLine();
var ordersByItemName = orders.Where(o => o.Items.Any(item => item.Name == itemName));
PrintOrders(ordersByItemName);
break;
default:
Console.WriteLine("Invalid option, please try again.");
break;
}
}
// 打印订单列表
static void PrintOrders(IEnumerable<Order> orders)
{
if (orders.Count() == 0)
{
Console.WriteLine("No orders found.");
}
else
{
foreach (Order order in orders)
{
Console.WriteLine(order);
}
}
}
}
在上面的代码中,我们定义了一个静态变量orders,用于存储所有的订单。在程序启动后,我们打印了一个简单的菜单,并根据用户选择的选项执行相应的操作。
例如,用户选择添加订单选项后,程序会提示用户输入订单信息,并根据输入创建一个订单对象,然后提示用户继续添加订单商品,直到用户不再添加。最后,将订单对象添加到订单列表中,并提示用户添加成功。
另外,我们还实现了删除订单、修改订单和查询订单等操作,并通过LINQ语句和Lambda表达式对订单列表进行过滤和查询,最后将符合条件的所有订单打印出来。
示例说明
示例一:添加订单
Order Management System
-----------------------
1. Add Order
2. Remove Order
3. Modify Order
4. Query Order
5. Exit
Please select an option: 1
Add Order
---------
Please enter Order ID: 1001
Please enter Customer ID: 1
Please enter Customer Name: Alice
Please enter Order Date (yyyy-MM-dd): 2021-06-10
Add Order Item
--------------
Please enter Item ID: 1
Please enter Item Name: iPhone
Please enter Item Price: 6999
Please enter Item Quantity: 1
Add another Item? (Y/N): y
Please enter Item ID: 2
Please enter Item Name: MacBook Pro
Please enter Item Price: 12999
Please enter Item Quantity: 1
Add another Item? (Y/N): n
Order added successfully.
上面的示例中,我们选择了添加订单选项,并输入了订单信息和两个订单商品,程序最后提示添加成功。
示例二:查询订单
Order Management System
-----------------------
1. Add Order
2. Remove Order
3. Modify Order
4. Query Order
5. Exit
Please select an option: 4
Query Order
-----------
1. Query by Order ID
2. Query by Customer Name
3. Query by Item Name
Please select a query option: 1
Please enter Order ID: 1001
Order Id: 1001
Customer: ID: 1, Name: Alice
Order Date: 6/10/2021 12:00:00 AM
Order Items:
ID: 1, Name: iPhone, Price: ¥6,999.00, Quantity: 1
ID: 2, Name: MacBook Pro, Price: ¥12,999.00, Quantity: 1
上面的示例中,我们选择了查询订单选项,并选择了按订单号查询。程序根据我们输入的订单号查找到该订单,并打印了订单的详细信息。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#实现简单订单管理程序 - Python技术站