C# 我已经附加了足够的代码,这样你现在就可以计算出程序的jist了。主方法还有4个“开关”部分。老实说,我知道这是惊人的实施,但这是一个任务,我一直在努力,所以我只是想确保我至少可以得到一个工作计划,这样我就可以真正通过。基本上,我已经构建了一个购物车,装载了
C# 我已经附加了足够的代码,这样你现在就可以计算出程序的jist了。主方法还有4个“开关”部分。老实说,我知道这是惊人的实施,但这是一个任务,我一直在努力,所以我只是想确保我至少可以得到一个工作计划,这样我就可以真正通过。基本上,我已经构建了一个购物车,装载了,c#,xml,global-variables,C#,Xml,Global Variables,我已经附加了足够的代码,这样你现在就可以计算出程序的jist了。主方法还有4个“开关”部分。老实说,我知道这是惊人的实施,但这是一个任务,我一直在努力,所以我只是想确保我至少可以得到一个工作计划,这样我就可以真正通过。基本上,我已经构建了一个购物车,装载了基于输入从XML文件中选择的产品。我想做的是把所有的switch“1”语句放到一个新类的方法中,以便创建一个新的“策略”,我将为剩下的部分提供更多信息 static void Main(string[] args) {
我已经附加了足够的代码,这样你现在就可以计算出程序的jist了。主方法还有4个“开关”部分。老实说,我知道这是惊人的实施,但这是一个任务,我一直在努力,所以我只是想确保我至少可以得到一个工作计划,这样我就可以真正通过。基本上,我已经构建了一个购物车,装载了基于输入从XML文件中选择的产品。我想做的是把所有的switch“1”语句放到一个新类的方法中,以便创建一个新的“策略”,我将为剩下的部分提供更多信息
static void Main(string[] args)
{
// Below code loads the XML file and builds Product objects with LINQ
XDocument XDoc = XDocument.Load("inventory.xml");
IEnumerable<Product> products = from q in XDoc.Descendants("product")
select new Product
{
RecordNumber = Convert.ToInt32(q.Element("recordNumber").Value),
Name = q.Element("name").Value,
Stock = Convert.ToInt32(q.Element("stock").Value),
Price = Convert.ToInt32(q.Element("price").Value),
CartQuantity = 0,
CartPrice = 0
};
// Builds one instance of the cart that will be used to hold products
Cart ShoppingCart = new Cart();
Console.WriteLine("WELCOME TO CONSOLE-BASED SHOPPING CART!");
Start:
Console.WriteLine("Following options are available: \n" +
"1. Add an item to cart, \n" +
"2. Remove an item from the cart, \n" +
"3. View the cart \n" +
"4. Checkout and Pay \n" +
"5. Exit \n");
String input = Console.ReadLine();
Context context;
public class Cart : System.Collections.IEnumerable
{
//Cart class - one instance is created to hold all the user's items
//Includes methods to add, remove and check if cart contains an item
// Also includes enumerator
private List<Product> items;
public Cart()
{
items = new List<Product>();
}
public System.Collections.IEnumerator GetEnumerator()
{
for (int i = 0; i < items.Count; i++)
{
yield return items[i];
}
}
public void AddItem(Product prod)
{
items.Add(prod);
}
public void RemoveProduct(Product prod)
{
items.Remove(prod);
}
public int CartCount()
{
return items.Count();
}
public void WriteToConsole()
{
Console.WriteLine("{0,16} {1, 16} {2, 16} {3, 16}","Item No.", "Item", "Amount", "Price");
int TotalCost = 0; //Iterator below allows us to work out total price
foreach (Product x in this)
{
Console.WriteLine("{0,16} {1, 16} {2, 16} {3, 16}", x.RecordNumber, x.Name, x.CartQuantity, x.CartPrice);
TotalCost = TotalCost + x.CartPrice;
}
Console.WriteLine("");
Console.WriteLine("Total Price: {0,54}", TotalCost);
}
public void Reset()
{
foreach (Product x in this)
{
this.RemoveProduct(x);
x.CartQuantity = 0;
x.CartPrice = 0;
}
}
public class Product
{
// Product class - objects are built using LINQ in main method
public int RecordNumber { get; set; }
public String Name { get; set; }
public int Price { get; set; }
public int Stock { get; set; }
public int CartQuantity { get; set; }
public int CartPrice { get; set; }
public Product() { }
}