using System.Collections; namespace Lesson1_ArrayList练习题 { class Backpack { ArrayList stuff; int money; public Backpack() { money = 0; stuff = new ArrayList(); } public Backpack(int money) { stuff = new ArrayList(); this.money = money; } public void ShowBackPack() { Console.WriteLine("背包物品有:"); for (int i = 0; i < stuff.Count; i++) { if (i + 1 != stuff.Count) { Console.Write(stuff[i] + "、"); } else { Console.WriteLine(stuff[i]); } } } public void BuyStuff(Object obj,int cost) { if(cost > money) { Console.WriteLine("余额不足"); } else { stuff.Add(obj); money -= cost; Console.WriteLine("成功购买物品:" + obj); } } public void SoldStuff(object obj,int earned) { if (stuff.Contains(obj)) { stuff.Remove(obj); money += earned; Console.WriteLine("成功出售物品:" + obj); } else { Console.WriteLine("没有该物品,无法出售:" + obj); } } public void ShowMoney() { Console.WriteLine("当前余额:" + money); } } class Program { static void Main(string[] args) { Backpack b1= new Backpack(); Backpack b2 = new Backpack(100); b1.ShowBackPack(); b2.ShowBackPack(); b1.BuyStuff("sth1", 50); b2.BuyStuff("sth2", 50); b1.ShowBackPack(); b2.ShowBackPack(); b1.SoldStuff("sth1", 25); b2.SoldStuff("sth2", 25); b1.ShowBackPack(); b2.ShowBackPack(); b1.ShowMoney(); b2.ShowMoney(); } } }