9

いくつかの追加ツールを追加した場合、カスタマイズされたリストを使用しようとしています。このリストを、私が作成したカスタマイズされたクラスの長いリストに適用したいと考えています。すべてのクラスには ID 番号があり、リスト内の一部のツールは ID を使用します。

これは、使用しようとしているコードの一部です。これが私の問題を理解するのに役立つことを願っています。

namespace Simple_Point_of _Sales_System
{
    public class MyList<T> : List<T>
    {
        internal int SetID()
        {
            return this.Max(n => n.ID) + 1;
        }
        internal T Find(int ID)
        {
            return this.Find(n => n.ID == ID);
        }
        internal T Add(T n)
        {
            Read();
            Add(n);
            Write();
            return n;
        }
        internal void Remove(int ID)
        {
            Read();
            if (this.Exists(t => t.ID == ID)) RemoveAll(t => t.ID == ID);
            else MessageBox.Show(GetType().Name + " " + ID + " does not exist.", "Missing Item", MessageBoxButtons.OK, MessageBoxIcon.Error);
            Write();
        }
        internal void Edit(int ID, T n)
        {
            Read();
            if (this.Exists(t => t.ID == ID)) this[FindIndex(t => t.ID == ID)] = n;
            else MessageBox.Show(GetType().Name + " " + ID + " does not exist.", "Missing Item", MessageBoxButtons.OK, MessageBoxIcon.Error);
            Write();
        }
        internal MyList<T> Read()
        {
            Clear();
            StreamReader sr = new StreamReader(@"../../Files/" + GetType().Name + ".txt");
            while (!sr.EndOfStream)
                Add(new T().Set(sr.ReadLine()));
            sr.Close();
            return this;
        }
        internal void Write()
        {
            StreamWriter sw = new StreamWriter(@"../../Files/" + GetType().Name + ".txt");
            foreach (T n in this)
                sw.WriteLine(n.ToString());
            sw.Close();
        }
    }

    public class Customer
    {
        public int ID;
        public string FirstName;
        public string LastName;
    }

    public class Item
    {
        public int ID { get; set; }
        public string Category { get; set; }
        public string Name { get; set; }
        public double Price { get; set; }
    }

    public class MyClass
    {
        MyList<Customer> Customers = new MyList<Customer>();
        MyList<Item> Items = new MyList<Item>();
    }
}
4

1 に答える 1