public void Load(bool isNew) { //第一种做法,从文件中读取 this.Schedule.LoadItems(); if (!isNew) { if (!Directory.Exists(Environment.CurrentDirectory + "\\SaveTicket"))//判断当前文件目录下指定的文件夹是否存在 { return; } if (!File.Exists("SaveTicket\\SaveTicket.txt")) { return; } FileStream fs = new FileStream("SaveTicket\\SaveTicket.txt", FileMode.Open); StreamReader sr = new StreamReader(fs, Encoding.Default); string line = sr.ReadLine(); while (line.Trim() != "The End") { Movie m = new Movie( line, sr.ReadLine(), sr.ReadLine(), (MovieType)Enum.Parse(typeof(MovieType), sr.ReadLine()), sr.ReadLine(), double.Parse(sr.ReadLine()) ); ScheduleItem si = new ScheduleItem(m, sr.ReadLine()); Seat s = new Seat(Color.Red, sr.ReadLine()); TicKet t = new TicKet(si, s); this.SoldTickets.Add(t); line = sr.ReadLine(); } sr.Close(); fs.Close(); } else { this.SoldTickets.Clear(); } //第二种,以序列化方式存储,再读取 //if (isNew) //{ // this.Schedule.LoadItems(); //} //else //{ // //从二进制文件读取信息 // if (File.Exists("SaveTicKet.bin")) // { // BinaryFormatter bf = new BinaryFormatter(); // FileStream fs = new FileStream("SaveTicKet.bin", FileMode.Open); // Cinema c = (Cinema)bf.Deserialize(fs); // this.Seats = c.Seats; // this.SoldTickets = c.SoldTickets; // this.Schedule = c.Schedule; // fs.Close(); // } //} }
void l_Click(object sender, EventArgs e) { if (this.lblTime.Text.Length == 0) { MessageBox.Show("请选择一部电影及场次!"); return; } Label l = sender as Label; if (l.BackColor == Color.Red) { MessageBox.Show("对不起,该座位已经售出"); return; } if (MessageBox.Show("是否购买?", "购买提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel) { return; } string type = ""; if (this.rbtnPT.Checked) { type = "TicKet"; } else if (this.rbtnZP.Checked) { type = "FreeTicKet"; } else { type = "StuTicKet"; } ScheduleItem s = null; //判断选中的是哪个节点 if (this.treeView1.SelectedNode.Level == 0) { s = this.treeView1.SelectedNode.Nodes[0].Tag as ScheduleItem;//取当前节点下的第一个子节点的tag } else { s = this.treeView1.SelectedNode.Tag as ScheduleItem; } TicKet t = TicketUtil.CreateTicket(type, s, this.c.Seats[l.Text], this.txtPerson.Text, double.Parse(this.cboZheKou.Text) * 0.1); this.c.SoldTickets.Add(t); this.c.Seats[l.Text].Color = Color.Red; //变颜色 l.BackColor = Color.Red; //显示颜色 t.Print(); //打印 }
//工厂模式 public static TicKet CreateTicket(string type, ScheduleItem item, Seat seat, string customerName, double disCount) //买票 { TicKet t = null; switch (type) { case "TicKet": t = new TicKet(item, seat); break; case "FreeTicKet": t = new FreeTicket(item, seat, customerName); break; case "StuTicKet": t = new StudentTicket(item, seat, disCount); break; } return(t); }