Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Collections;
- namespace Lab2s2
- {
- public partial class Form1 : Form
- {
- Handler handler = new Handler();
- public Form1()
- {
- InitializeComponent();
- handler.Output(dataGridView1);
- }
- interface SortTrain
- {
- void Sort();
- }
- interface Output
- {
- string Output();
- }
- public class SortByTrain : IComparer
- {
- int IComparer.Compare(object a, object b)
- {
- Ticket a1 = (Ticket)a;
- Ticket b1 = (Ticket)b;
- if (a1.Train > b1.Train) return 1;
- if (a1.Train < b1.Train) return -1;
- return 0;
- }
- }
- public class Ticket : Output
- {
- int number_of_train;
- string destination;
- int number_of_seat;
- int cost;
- public int Train
- {
- get { return number_of_train; }
- set { number_of_train = value; }
- }
- public string Destination
- {
- get { return destination; }
- set { destination = value; }
- }
- public int Seat
- {
- get { return number_of_seat; }
- set { number_of_seat = value; }
- }
- public int Cost
- {
- get { return cost; }
- set { cost = value; }
- }
- public Ticket(int train, string d, int seat, int cost)
- {
- this.number_of_train = train;
- this.destination = d;
- this.number_of_seat = seat;
- this.cost = cost;
- }
- public string Output()
- {
- return number_of_train.ToString() + " - " + destination + " - " + number_of_seat.ToString() + " - " + cost.ToString();
- }
- public string File()
- {
- return number_of_train.ToString() + ", " + destination + ", " + number_of_seat.ToString() + ", " + cost.ToString();
- }
- }
- public class Handler:SortTrain
- {
- ArrayList a;
- public int Count{get{return a.Count;}}
- public Handler() { a = new ArrayList(0); }
- public Handler(int n) { a = new ArrayList(n); }
- public Handler(ArrayList b) { a = b; }
- public void Sort() { a.Sort(new SortByTrain()); }
- public void ShowCost(DataGridView grid, string dest)
- {
- grid.RowCount = 0;
- grid.ColumnCount = 1;
- int j = 0;
- for (int i = 0; i < a.Count; i++)
- {
- if (((Ticket)a[i]).Destination == dest) grid.RowCount++;
- }
- for (int i = 0; i < a.Count; i++)
- {
- if (((Ticket)a[i]).Destination == dest) { grid.Rows[j].Cells[0].Value = ((Ticket)a[i]).Output(); j++; }
- }
- }
- public void DeleteTickets(int train)
- {
- for (int i = 0; i < a.Count; i++)
- {
- if (((Ticket)a[i]).Train == train) { a.RemoveAt(i); i--; }
- }
- }
- public void InsertTicket(Ticket to_insert, int i)
- {
- a.Insert(i, to_insert);
- }
- public void AddTicket(Ticket ticket)
- {
- a.Add(ticket);
- }
- public void Output(DataGridView grid)
- {
- grid.RowCount = a.Count;
- grid.ColumnCount = 1;
- for (int i = 0; i < a.Count; i++)
- {
- grid.Rows[i].Cells[0].Value = ((Ticket)a[i]).Output();
- }
- }
- public string File(int i) { return ((Ticket)a[i]).File(); }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- string[] k = textBox1.Text.Split(new Char[] { ' ', ',', });
- int n = 0;
- for (int i = 0; i < k.Length; i++)
- {
- if (k[i] != "") n++;
- }
- string[] c = new string[n];
- n = 0;
- for (int i = 0; i < k.Length; i++)
- {
- if (k[i] != "")
- {
- c[n] = k[i];
- n++;
- }
- }
- handler.AddTicket(new Ticket(Convert.ToInt32(c[0]), c[1], Convert.ToInt32(c[2]), Convert.ToInt32(c[3])));
- handler.Output(dataGridView1);
- }
- private void button2_Click(object sender, EventArgs e)
- {
- handler.DeleteTickets(Convert.ToInt32(textBox2.Text));
- handler.Output(dataGridView1);
- }
- private void button3_Click(object sender, EventArgs e)
- {
- string[] k = textBox1.Text.Split(new Char[] { ' ', ',', });
- int n = 0;
- for (int i = 0; i < k.Length; i++)
- {
- if (k[i] != "") n++;
- }
- string[] c = new string[n];
- n = 0;
- for (int i = 0; i < k.Length; i++)
- {
- if (k[i] != "")
- {
- c[n] = k[i];
- n++;
- }
- }
- handler.InsertTicket(new Ticket(Convert.ToInt32(c[0]), c[1], Convert.ToInt32(c[2]), Convert.ToInt32(c[3])), Convert.ToInt32(textBox3.Text) - 1);
- handler.Output(dataGridView1);
- }
- private void button4_Click(object sender, EventArgs e)
- {
- System.IO.StreamWriter writer = new System.IO.StreamWriter(@"d:\\tickets.txt");
- for (int i = 0; i < handler.Count; i++)
- {
- writer.Write(handler.File(i) + "\n");
- }
- writer.Close();
- }
- private void button5_Click(object sender, EventArgs e)
- {
- ArrayList lines = new ArrayList();
- System.IO.StreamReader reader = new System.IO.StreamReader(@"d:\\tickets.txt");
- while (!reader.EndOfStream)
- {
- lines.Add(reader.ReadLine());
- }
- reader.Close();
- ArrayList tickets = new ArrayList();
- for (int j = 0; j < lines.Count; j++)
- {
- string[] k = ((string)lines[j]).Split(new Char[] { ' ', ',', '\n',});
- int n = 0;
- for (int i = 0; i < k.Length; i++)
- {
- if (k[i] != "") n++;
- }
- string[] c = new string[n];
- n = 0;
- for (int i = 0; i < k.Length; i++)
- {
- if (k[i] != "")
- {
- c[n] = k[i];
- n++;
- }
- }
- handler.AddTicket(new Ticket(Convert.ToInt32(c[0]), c[1], Convert.ToInt32(c[2]), Convert.ToInt32(c[3])));
- }
- handler.Output(dataGridView1);
- handler.ShowCost(dataGridView1, @"Харьков");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement