【发布时间】:2018-12-05 23:27:21
【问题描述】:
我正在制作一个简单的库存控制台应用程序。
到目前为止,我有以下代码:
using System;
using System.Collections;
class Inventory
{
string name;
double cost;
int onhand;
public Inventory(string n, double c, int h)
{
name = n;
cost = c;
onhand = h;
}
public override string ToString()
{
return
String.Format("{0,-10}Cost: {1,6:C} On hand: {2}", name, cost, onhand);
}
}
public class InventoryList
{
public static void Main()
{
ArrayList inv = new ArrayList();
// Add elements to the list
inv.Add(new Inventory("Pliers", 5.95, 3));
inv.Add(new Inventory("Wrenches", 8.29, 2));
inv.Add(new Inventory("Hammers", 3.50, 4));
inv.Add(new Inventory("Drills", 19.88, 8));
Console.WriteLine("Inventory list:");
foreach (Inventory i in inv)
{
Console.WriteLine(" " + i);
}
我添加了此代码以将新产品添加到列表中。
Console.WriteLine("\n");
Console.WriteLine("Input New Inventory");
Console.WriteLine("Name : ");
string newName = Console.ReadLine();
Console.Write("Cost : ");
double newCost = Double.Parse(Console.ReadLine());
Console.Write("Onhand : ");
int newOnhand = Int32.Parse(Console.ReadLine());
inv.Add(new Inventory(newName, newCost, newOnhand));
Console.WriteLine("\n");
Console.WriteLine("Inventory List:");
foreach (Inventory i in inv)
{
Console.WriteLine("" + i);
}
Console.WriteLine("\n")
}
}
我无法弄清楚如何添加 update() 方法来更改列表中的库存编号。
我的问题是如何在控制台窗口中添加更新方法来更改列表中的库存数量?
在控制台中,我将输入产品名称和新库存编号,然后返回一个包含产品更新库存编号的新列表。
【问题讨论】: