/// <summary> /// Summary description for Swimmer /// geovindu,Geovin Du,涂聚文 /// 原型模式(Prototype Patterns) /// </summary> public class Swimmer : IComparable { private string name; //name private string lname, frname;//split names private int age; //age private string club; //club initials private float time; //time achieved private bool female; //sex /// <summary> /// /// </summary> public Swimmer() { } /// <summary> /// /// </summary> /// <param name="line"></param> public Swimmer(string line) { StringTokenizer tok = new StringTokenizer(line, ","); splitName(tok); age = Convert.ToInt32(tok.nextToken()); club = tok.nextToken(); time = Convert.ToSingle(tok.nextToken()); string sx = tok.nextToken().ToUpper().Trim(); female = sx.Equals("F"); } /// <summary> /// /// </summary> /// <param name="tok"></param> private void splitName(StringTokenizer tok) { name = tok.nextToken(); int i = name.IndexOf(" "); if (i > 0) { frname = name.Substring(0, i); lname = name.Substring(i + 1).Trim(); } } /// <summary> /// /// </summary> /// <param name="swo"></param> /// <returns></returns> public int CompareTo(object swo) { Swimmer sw = (Swimmer)swo; return lname.CompareTo(sw.getLName()); } /// <summary> /// /// </summary> /// <returns></returns> public bool isFemale() { return female; } /// <summary> /// /// </summary> /// <returns></returns> public int getAge() { return age; } /// <summary> /// /// </summary> /// <returns></returns> public float getTime() { return time; } /// <summary> /// /// </summary> /// <returns></returns> public string getName() { return name; } /// <summary> /// /// </summary> /// <returns></returns> public string getClub() { return club; } /// <summary> /// /// </summary> /// <returns></returns> public string getLName() { return lname; } }
/// <summary> /// A simple file handlng class /// geovindu,Geovin Du,涂聚文 /// 原型模式(Prototype Patterns) /// </summary> public class csFile { private string fileName; StreamReader ts; StreamWriter ws; private bool opened, writeOpened; /// <summary> /// /// </summary> public csFile() { init(); } /// <summary> /// /// </summary> private void init() { opened = false; writeOpened = false; } /// <summary> /// /// </summary> /// <param name="file_name"></param> public csFile(string file_name) { fileName = file_name; init(); } /// <summary> /// /// </summary> /// <param name="file_name"></param> /// <returns></returns> public bool OpenForRead(string file_name) { fileName = file_name; try { ts = new StreamReader(fileName); opened = true; } catch (FileNotFoundException e) { Console.WriteLine(e.Message); return false; } return true; } /// <summary> /// /// </summary> /// <returns></returns> public bool OpenForRead() { return OpenForRead(fileName); } /// <summary> /// /// </summary> /// <returns></returns> public string readLine() { return ts.ReadLine(); } /// <summary> /// /// </summary> /// <param name="s"></param> public void writeLine(string s) { ws.WriteLine(s); } /// <summary> /// /// </summary> public void close() { if (opened) ts.Close(); if (writeOpened) ws.Close(); } /// <summary> /// /// </summary> /// <returns></returns> public bool OpenForWrite() { return OpenForWrite(fileName); } /// <summary> /// /// </summary> /// <param name="file_name"></param> /// <returns></returns> public bool OpenForWrite(string file_name) { try { ws = new StreamWriter(file_name); fileName = file_name; writeOpened = true; return true; } catch (FileNotFoundException) { return false; } } }
/// <summary> /// String Tokenizer class /// geovindu,Geovin Du,涂聚文 /// 原型模式(Prototype Patterns) /// </summary> public class StringTokenizer { private string data, delimiter; private string[] tokens; private int index; /// <summary> /// /// </summary> /// <param name="dataLine"></param> public StringTokenizer(string dataLine) { init(dataLine, " "); } /// <summary> /// /// </summary> /// <param name="dataLine"></param> /// <param name="delim"></param> private void init(String dataLine, string delim) { delimiter = delim; data = dataLine; tokens = data.Split(delimiter.ToCharArray()); index = 0; } /// <summary> /// /// </summary> /// <param name="dataLine"></param> /// <param name="delim"></param> public StringTokenizer(string dataLine, string delim) { init(dataLine, delim); } /// <summary> /// /// </summary> /// <returns></returns> public bool hasMoreElements() { return (index < (tokens.Length)); } /// <summary> /// /// </summary> /// <returns></returns> public string nextToken() { return nextElement(); } /// <summary> /// /// </summary> /// <returns></returns> public string nextElement() { string s = tokens[index++]; while ((s.Length <= 0) && (index < tokens.Length)) s = tokens[index++]; return s; } }
/// <summary> /// Summary description for SwimData. /// geovindu,Geovin Du,涂聚文 /// 原型模式(Prototype Patterns) /// </summary> public class SwimData { protected ArrayList swdata; private int index; /// <summary> /// /// </summary> public SwimData() { swdata = new ArrayList(); } /// <summary> /// /// </summary> /// <param name="swd"></param> public SwimData(ArrayList swd) { swdata = swd; index = 0; } /// <summary> /// /// </summary> /// <returns></returns> public int count() { return swdata.Count; } /// <summary> /// /// </summary> /// <param name="swdat"></param> public virtual void cloneMe(SwimData swdat) { swdata = new ArrayList(); ArrayList swd = swdat.getData(); //copy in swimmer objects for (int i = 0; i < swd.Count; i++) swdata.Add(swd[i]); } /// <summary> /// /// </summary> /// <param name="filename"></param> public SwimData(string filename) { swdata = new ArrayList(); csFile fl = new csFile(filename); fl.OpenForRead(); string s = fl.readLine(); while (s != null) { Swimmer sw = new Swimmer(s); swdata.Add(sw); s = fl.readLine(); } fl.close(); } /// <summary> /// /// </summary> /// <returns></returns> public ArrayList getData() { return swdata; } /// <summary> /// /// </summary> public void moveFirst() { index = 0; } /// <summary> /// /// </summary> /// <returns></returns> public bool hasMoreElements() { return (index < swdata.Count); } /// <summary> /// /// </summary> public virtual void sort() { //sort using IComparable interface of Swimmer swdata.Sort(0, swdata.Count, null); } /// <summary> /// /// </summary> /// <returns></returns> public Swimmer getSwimmer() { if (index < swdata.Count) return (Swimmer)swdata[index++]; else return null; } }
/// <summary> /// Summary description for SexSwimData. /// geovindu,Geovin Du,涂聚文 /// 原型模式(Prototype Patterns) /// </summary> public class AgeSwimData : SwimData { ArrayList swd; public AgeSwimData() { swdata = new ArrayList(); } /// <summary> /// /// </summary> /// <param name="filename"></param> public AgeSwimData(string filename) : base(filename) { } /// <summary> /// /// </summary> /// <param name="ssd"></param> public AgeSwimData(ArrayList ssd) : base(ssd) { } /// <summary> /// /// </summary> /// <param name="swdat"></param> public override void cloneMe(SwimData swdat) { swd = swdat.getData(); } /// <summary> /// /// </summary> public override void sort() { Swimmer[] sws = new Swimmer[swd.Count]; //copy in swimmer objects for (int i = 0; i < swd.Count; i++) { sws[i] = (Swimmer)swd[i]; } //sort into increasing order for (int i = 0; i < sws.Length; i++) { for (int j = i; j < sws.Length; j++) { if (sws[i].getAge() > sws[j].getAge()) { Swimmer sw = sws[i]; sws[i] = sws[j]; sws[j] = sw; } } } int age = sws[0].getAge(); int agecount = 0; int k = 0; swdata = new ArrayList(); bool quit = false; while (k < sws.Length && !quit) { while (sws[k].getAge() == age && !quit) { agecount++; if (k < sws.Length - 1) k++; else quit = true; } //create a new Swimmer with a series of X's for a name //for each new age string name = ""; for (int j = 0; j < agecount; j++) name += "X"; Swimmer sw = new Swimmer(age.ToString() + " " + name + "," + age.ToString() + ",club,0,F"); swdata.Add(sw); agecount = 0; if (quit) age = 0; else age = sws[k].getAge(); } } }
窗体调用:
/// <summary> /// geovindu,Geovin Du,涂聚文 /// 原型模式(Prototype Patterns) /// </summary> public partial class PrototypePatternsForm : Form { private SwimData swdata; /// <summary> /// /// </summary> private void init() { swdata = new AgeSwimData("swimmers.txt"); reload(); } /// <summary> /// /// </summary> private void reload() { lsKids.Items.Clear(); swdata.moveFirst(); while (swdata.hasMoreElements()) { Swimmer sw = swdata.getSwimmer(); lsKids.Items.Add(sw.getName()); } } /// <summary> /// /// </summary> public PrototypePatternsForm() { InitializeComponent(); init(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PrototypePatternsForm_Load(object sender, EventArgs e) { } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btClone_Click(object sender, EventArgs e) { AgeSwimData newSd = new AgeSwimData(); newSd.cloneMe(swdata); newSd.sort(); lsNewKids.Items.Clear(); while (newSd.hasMoreElements()) { Swimmer sw = (Swimmer)newSd.getSwimmer(); lsNewKids.Items.Add(sw.getName()); } } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btBack_Click(object sender, EventArgs e) { reload(); } }
输出:
标签:return,string,Swimmer,private,Patterns,Csharp,swdata,Prototype,public From: https://www.cnblogs.com/geovindu/p/16704761.html