Advertisement
Dantenerosas

Untitled

May 13th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Collections;
  10.  
  11. namespace Lab2s2
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         Handler handler = new Handler();
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.             handler.Output(dataGridView1);
  20.         }
  21.  
  22.         interface SortTrain
  23.         {
  24.             void Sort();
  25.         }
  26.  
  27.         interface Output
  28.         {
  29.             string Output();
  30.         }
  31.  
  32.         public class SortByTrain : IComparer
  33.         {
  34.             int IComparer.Compare(object a, object b)
  35.             {
  36.                 Ticket a1 = (Ticket)a;
  37.                 Ticket b1 = (Ticket)b;
  38.                 if (a1.Train > b1.Train) return 1;
  39.                 if (a1.Train < b1.Train) return -1;
  40.                 return 0;
  41.             }
  42.         }
  43.  
  44.         public class Ticket : Output
  45.         {
  46.  
  47.             int number_of_train;
  48.             string destination;
  49.             int number_of_seat;
  50.             int cost;
  51.             public int Train
  52.             {
  53.                 get { return number_of_train; }
  54.                 set { number_of_train = value; }
  55.             }
  56.             public string Destination
  57.             {
  58.                 get { return destination; }
  59.                 set { destination = value; }
  60.             }
  61.             public int Seat
  62.             {
  63.                 get { return number_of_seat; }
  64.                 set { number_of_seat = value; }
  65.             }
  66.             public int Cost
  67.             {
  68.                 get { return cost; }
  69.                 set { cost = value; }
  70.             }
  71.             public Ticket(int train, string d, int seat, int cost)
  72.             {
  73.                 this.number_of_train = train;
  74.                 this.destination = d;
  75.                 this.number_of_seat = seat;
  76.                 this.cost = cost;
  77.             }
  78.             public string Output()
  79.             {
  80.                 return number_of_train.ToString() + " - " + destination + " - " + number_of_seat.ToString() + " - " + cost.ToString();
  81.             }
  82.             public string File()
  83.             {
  84.                 return number_of_train.ToString() + ", " + destination + ", " + number_of_seat.ToString() + ", " + cost.ToString();
  85.             }
  86.         }
  87.  
  88.         public class Handler:SortTrain
  89.         {
  90.             ArrayList a;
  91.             public int Count{get{return a.Count;}}
  92.             public Handler() { a = new ArrayList(0); }
  93.             public Handler(int n) { a = new ArrayList(n); }
  94.             public Handler(ArrayList b) { a = b; }
  95.             public void Sort() { a.Sort(new SortByTrain()); }
  96.             public void ShowCost(DataGridView grid, string dest)
  97.             {
  98.                 grid.RowCount = 0;
  99.                 grid.ColumnCount = 1;
  100.                 int j = 0;
  101.                 for (int i = 0; i < a.Count; i++)
  102.                 {
  103.                     if (((Ticket)a[i]).Destination == dest) grid.RowCount++;
  104.                 }
  105.                 for (int i = 0; i < a.Count; i++)
  106.                 {
  107.                     if (((Ticket)a[i]).Destination == dest) { grid.Rows[j].Cells[0].Value = ((Ticket)a[i]).Output(); j++; }
  108.                 }
  109.             }
  110.             public void DeleteTickets(int train)
  111.             {
  112.                 for (int i = 0; i < a.Count; i++)
  113.                 {
  114.                     if (((Ticket)a[i]).Train == train) { a.RemoveAt(i); i--; }
  115.                 }
  116.             }
  117.             public void InsertTicket(Ticket to_insert, int i)
  118.             {
  119.                 a.Insert(i, to_insert);
  120.             }
  121.             public void AddTicket(Ticket ticket)
  122.             {
  123.                 a.Add(ticket);
  124.             }
  125.             public void Output(DataGridView grid)
  126.             {
  127.                 grid.RowCount = a.Count;
  128.                 grid.ColumnCount = 1;
  129.                 for (int i = 0; i < a.Count; i++)
  130.                 {
  131.                     grid.Rows[i].Cells[0].Value = ((Ticket)a[i]).Output();
  132.                 }
  133.             }
  134.             public string File(int i) { return ((Ticket)a[i]).File(); }
  135.         }
  136.  
  137.         private void button1_Click(object sender, EventArgs e)
  138.         {
  139.             string[] k = textBox1.Text.Split(new Char[] { ' ', ',', });
  140.             int n = 0;
  141.             for (int i = 0; i < k.Length; i++)
  142.             {
  143.                 if (k[i] != "") n++;
  144.             }
  145.             string[] c = new string[n];
  146.             n = 0;
  147.             for (int i = 0; i < k.Length; i++)
  148.             {
  149.                 if (k[i] != "")
  150.                 {
  151.                     c[n] = k[i];
  152.                     n++;
  153.                 }
  154.             }
  155.             handler.AddTicket(new Ticket(Convert.ToInt32(c[0]), c[1], Convert.ToInt32(c[2]), Convert.ToInt32(c[3])));
  156.             handler.Output(dataGridView1);
  157.         }
  158.  
  159.         private void button2_Click(object sender, EventArgs e)
  160.         {
  161.             handler.DeleteTickets(Convert.ToInt32(textBox2.Text));
  162.             handler.Output(dataGridView1);
  163.         }
  164.  
  165.         private void button3_Click(object sender, EventArgs e)
  166.         {
  167.             string[] k = textBox1.Text.Split(new Char[] { ' ', ',', });
  168.             int n = 0;
  169.             for (int i = 0; i < k.Length; i++)
  170.             {
  171.                 if (k[i] != "") n++;
  172.             }
  173.             string[] c = new string[n];
  174.             n = 0;
  175.             for (int i = 0; i < k.Length; i++)
  176.             {
  177.                 if (k[i] != "")
  178.                 {
  179.                     c[n] = k[i];
  180.                     n++;
  181.                 }
  182.             }
  183.             handler.InsertTicket(new Ticket(Convert.ToInt32(c[0]), c[1], Convert.ToInt32(c[2]), Convert.ToInt32(c[3])), Convert.ToInt32(textBox3.Text) - 1);
  184.             handler.Output(dataGridView1);
  185.         }
  186.  
  187.         private void button4_Click(object sender, EventArgs e)
  188.         {
  189.             System.IO.StreamWriter writer = new System.IO.StreamWriter(@"d:\\tickets.txt");
  190.             for (int i = 0; i < handler.Count; i++)
  191.             {
  192.                 writer.Write(handler.File(i) + "\n");
  193.             }
  194.             writer.Close();
  195.         }
  196.  
  197.         private void button5_Click(object sender, EventArgs e)
  198.         {
  199.             ArrayList lines = new ArrayList();
  200.             System.IO.StreamReader reader = new System.IO.StreamReader(@"d:\\tickets.txt");
  201.             while (!reader.EndOfStream)
  202.             {
  203.                 lines.Add(reader.ReadLine());
  204.             }
  205.             reader.Close();
  206.             ArrayList tickets = new ArrayList();
  207.             for (int j = 0; j < lines.Count; j++)
  208.             {
  209.                 string[] k = ((string)lines[j]).Split(new Char[] { ' ', ',', '\n',});
  210.                 int n = 0;
  211.                 for (int i = 0; i < k.Length; i++)
  212.                 {
  213.                     if (k[i] != "") n++;
  214.                 }
  215.                 string[] c = new string[n];
  216.                 n = 0;
  217.                 for (int i = 0; i < k.Length; i++)
  218.                 {
  219.                     if (k[i] != "")
  220.                     {
  221.                         c[n] = k[i];
  222.                         n++;
  223.                     }
  224.                 }
  225.                 handler.AddTicket(new Ticket(Convert.ToInt32(c[0]), c[1], Convert.ToInt32(c[2]), Convert.ToInt32(c[3])));
  226.             }
  227.             handler.Output(dataGridView1);
  228.             handler.ShowCost(dataGridView1, @"Харьков");
  229.         }
  230.     }
  231.  
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement