Advertisement
Seal_of_approval

P13ex4

Dec 4th, 2015
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.34 KB | None | 0 0
  1. Program.cs
  2.  
  3. using System;
  4. using System.IO;
  5. using System.Text;
  6. using System.Collections.Generic;
  7.  
  8. namespace test
  9. {
  10.     class Program
  11.     {      
  12.         static void Main (string[] args)
  13.         {
  14.             using (StreamReader fileIn = new StreamReader ("/Users/masha/Projects/test/test/test.txt"))
  15.             {
  16.                 int n = int.Parse (fileIn.ReadLine ());
  17.                 Transport[] list = new Transport[n];
  18.                 for (int i = 0; i < n; i++)
  19.                 {
  20.                     string[] s = fileIn.ReadLine ().Split (' ');
  21.                     if (Convert.ToChar (s [0]) == 'c') {
  22.                         list [i] = new Car (s [1], int.Parse (s [2]), int.Parse (s [3]), int.Parse (s [4]));
  23.                     } else if (Convert.ToChar (s [0]) == 'm') {
  24.                         list [i] = new Motorcycle (s [1], int.Parse (s [2]), int.Parse (s [3]), int.Parse (s [4]), s[5]);
  25.                     } else {
  26.                         list [i] = new Truck (s [1], int.Parse (s [2]), int.Parse (s [3]), int.Parse (s [4]), s[5]);
  27.                     }
  28.  
  29.                 }
  30.  
  31.                 for (int i = 0; i < list.Length; i++)
  32.                     list [i].Show ();
  33.  
  34.                 Console.WriteLine ();
  35.  
  36.                 Array.Sort (list);
  37.  
  38.                 for (int i = 0; i < list.Length; i++)
  39.                     list [i].Show ();
  40.  
  41.                 Console.WriteLine();
  42.  
  43.                 for (int i = 0; i < list.Length; i++)
  44.                 {
  45.                     if (list [i].Capacity() == 20)
  46.                         list [i].Show();
  47.                 }
  48.             }
  49.         }
  50.     }
  51. }
  52. __________________________________________________________
  53.  
  54. Transport.cs
  55.  
  56. using System;
  57.  
  58. namespace test
  59. {
  60.     abstract public class Transport: IComparable
  61.     {
  62.         abstract public void Show();
  63.         abstract public int Capacity();
  64.         abstract public bool Find(int a);
  65.         abstract public int CompareTo(object obj);
  66.         protected string model;
  67.         protected int number;
  68.         protected int speed;
  69.         protected int capacity;
  70.  
  71.         public string Model
  72.         {
  73.             get
  74.             {
  75.                 return model;
  76.             }
  77.             set
  78.             {
  79.                 model = value;
  80.             }
  81.         }
  82.  
  83.         public int Number
  84.         {
  85.             get
  86.             {
  87.                 return number;
  88.             }
  89.             set
  90.             {
  91.                 if (value > 0)
  92.                     number = value;
  93.                 else
  94.                 {
  95.                     number = 0;
  96.                     Console.WriteLine ("Incorrect number");
  97.                 }
  98.             }
  99.         }
  100.  
  101.         public int Speed
  102.         {
  103.             get
  104.             {
  105.                 return speed;
  106.             }
  107.             set
  108.             {
  109.                 if (value > 0)
  110.                     speed = value;
  111.                 else
  112.                 {
  113.                     speed = 0;
  114.                     Console.WriteLine ("Incorrect speed");
  115.                 }
  116.             }
  117.         }
  118.  
  119.         public int Cap
  120.         {
  121.             get
  122.             {
  123.                 return capacity;
  124.             }
  125.             set
  126.             {
  127.                 if (value >= 0)
  128.                     capacity = value;
  129.                 else
  130.                 {
  131.                     capacity = 0;
  132.                     Console.WriteLine ("Incorrect capacity");
  133.                 }
  134.             }
  135.         }
  136.     }
  137. }
  138. ____________________________________________________________________________
  139.  
  140. Car.cs
  141.  
  142. using System;
  143.  
  144. namespace test
  145. {
  146.     public class Car: Transport
  147.     {
  148.         //конструкторы
  149.         public Car ()
  150.         {
  151.         }
  152.  
  153.         public Car(string model, int number, int speed, int capacity)
  154.         {
  155.             this.model = model;
  156.             this.number = number;
  157.             this.speed = speed;
  158.             this.capacity = capacity;
  159.         }
  160.  
  161.         //методы
  162.         public override void Show()
  163.         {
  164.             Console.WriteLine ("Car: Model - {0}, Num {1}, with speed = {2}km/h and capacity = {3} ",
  165.                                 model, number, speed, capacity);
  166.         }
  167.  
  168.         public override int Capacity()
  169.         {
  170.             return capacity;
  171.         }
  172.  
  173.         public override bool Find (int a)
  174.         {
  175.             if (a == this.capacity)
  176.                 return true;
  177.             else
  178.                 return false;
  179.         }
  180.  
  181.         public override int CompareTo (object obj)
  182.         {
  183.             Transport item = (Transport) obj;
  184.             if (this.Capacity () == item.Capacity ())
  185.             {
  186.                 return 0;
  187.             }
  188.             else
  189.             {
  190.                 if (this.Capacity () > item.Capacity ())
  191.                 {
  192.                     return 1;
  193.                 }
  194.                 else
  195.                 {
  196.                     return -1;
  197.                 }
  198.             }
  199.         }
  200.     }
  201. }
  202.  
  203. _________________________________________________________________________________
  204.  
  205. Motorcycle.cs
  206.  
  207. using System;
  208.  
  209. namespace test
  210. {
  211.     public class Motorcycle: Car
  212.     {
  213.         protected string sidecar;
  214.  
  215.         public Motorcycle ()
  216.         {
  217.         }
  218.  
  219.         public Motorcycle (string model, int number, int speed, int capacity, string sidecar)
  220.         {
  221.             this.model = model;
  222.             this.number = number;
  223.             this.speed = speed;
  224.             this.capacity = capacity;
  225.             this.sidecar = sidecar;
  226.         }
  227.  
  228.         public override void Show()
  229.         {
  230.             Console.WriteLine ("Moto: Model - {0}, Sidecar - {4}, Num {1}, with speed = {2}km/h and capacity = {3} ",
  231.                 model, number, speed, Capacity(), sidecar);
  232.         }
  233.  
  234.         public override int Capacity()
  235.         {
  236.             if (sidecar != "sidecar")
  237.                 return 0;
  238.             else
  239.                 return capacity;
  240.         }
  241.  
  242.         public string Sidecar
  243.         {
  244.             get
  245.             {
  246.                 return sidecar;
  247.             }
  248.             set
  249.             {
  250.                 sidecar = value;
  251.             }
  252.         }
  253.  
  254.         public override bool Find (int a)
  255.         {
  256.             if (a == this.capacity)
  257.                 return true;
  258.             else
  259.                 return false;
  260.         }
  261.  
  262.         public override int CompareTo (object obj)
  263.         {
  264.             Car item = (Car) obj;
  265.             if (this.Capacity () == item.Capacity ())
  266.             {
  267.                 return 0;
  268.             }
  269.             else
  270.             {
  271.                 if (this.Capacity () > item.Capacity ())
  272.                 {
  273.                     return 1;
  274.                 }
  275.                 else
  276.                 {
  277.                     return -1;
  278.                 }
  279.             }
  280.         }
  281.     }
  282. }
  283. _______________________________________________________________________
  284. Truck.cs
  285.  
  286. using System;
  287.  
  288. namespace test
  289. {
  290.     public class Truck: Car
  291.     {
  292.         protected string trailer;
  293.  
  294.         public Truck ()
  295.         {
  296.         }
  297.  
  298.         public Truck (string model, int number, int speed, int capacity, string trailer)
  299.         {
  300.             this.model = model;
  301.             this.number = number;
  302.             this.speed = speed;
  303.             this.capacity = capacity;
  304.             this.trailer = trailer;
  305.         }
  306.            
  307.         public override void Show()
  308.         {
  309.             Console.WriteLine ("Truck: Model - {0}, Trailer - {4}, Num {1}, with speed = {2}km/h and capacity = {3} ",
  310.                 model, number, speed, Capacity(), trailer);
  311.         }
  312.  
  313.         public override int Capacity()
  314.         {
  315.             if (trailer == "trailer")
  316.                 return 2*capacity;
  317.             else
  318.                 return capacity;
  319.         }
  320.  
  321.         public string Trailer
  322.         {
  323.             get
  324.             {
  325.                 return trailer;
  326.             }
  327.             set
  328.             {
  329.                 trailer = value;
  330.             }
  331.         }
  332.  
  333.         public override bool Find (int a)
  334.         {
  335.             if (a == this.capacity)
  336.                 return true;
  337.             else
  338.                 return false;
  339.         }
  340.  
  341.         public override int CompareTo (object obj)
  342.         {
  343.             Car item = (Car) obj;
  344.             if (this.Capacity () == item.Capacity ())
  345.             {
  346.                 return 0;
  347.             }
  348.             else
  349.             {
  350.                 if (this.Capacity () > item.Capacity ())
  351.                 {
  352.                     return 1;
  353.                 }
  354.                 else
  355.                 {
  356.                     return -1;
  357.                 }
  358.             }
  359.         }
  360.     }
  361. }
  362. ________________________________________________________________________
  363. test.txt
  364.  
  365. 6
  366. c model1 123 200 10
  367. c model2 234 200 10
  368. t model3 345 150 10 trailer
  369. m model4 456 300 10 sidecar
  370. t model5 567 150 10 no_trailer
  371. m model6 678 300 10 no_sidecar
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement