ioana_martin98

Untitled

Jun 12th, 2019
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.19 KB | None | 0 0
  1. namespace Hotel
  2. {
  3.     [Serializable]
  4.     public class Camera
  5.     {
  6.         private int nr;
  7.         public enum  TipuriCamera {single, doubla, tripla};
  8.         private DateTime checkout;
  9.         private string tipCamera;
  10.  
  11.         public int Nr { get => nr; set => nr = value; }
  12.         public bool Ocupata
  13.         {
  14.             get
  15.             {
  16.                 if (checkout < DateTime.Now)
  17.                     return false;
  18.                 return true;
  19.             }
  20.         }
  21.         public string TipCamera { get => tipCamera; set => tipCamera = value; }
  22.         public DateTime Checkout { get => checkout; set => checkout = value; }
  23.  
  24.         public Camera(int nr, DateTime checkout, TipuriCamera tipCamera)
  25.         {
  26.             this.nr = nr;
  27.             this.checkout = checkout;
  28.             this.tipCamera = tipCamera.ToString();
  29.         }
  30.  
  31.         public override string ToString()
  32.         {
  33.             return "" + nr + ": " + checkout.ToShortDateString() + " " + tipCamera;
  34.         }
  35.  
  36.         public int getNrCamere()
  37.         {
  38.            
  39.             if (tipCamera == "single")
  40.                 return 1;
  41.             else if (tipCamera == "doubla")
  42.                 return 2;
  43.             return 3;
  44.         }
  45.  
  46.         public static int operator + (Camera c1, Camera c2)
  47.         {
  48.             return  c1.getNrCamere() + c2.getNrCamere();
  49.         }
  50.  
  51.         public static bool operator < (Camera c1, Camera c2)
  52.         {
  53.             if (c1.getNrCamere() < c2.getNrCamere())
  54.                 return true;
  55.             return false;
  56.         }
  57.  
  58.         public static bool operator > (Camera c1, Camera c2)
  59.         {
  60.             if (c1.getNrCamere() > c2.getNrCamere())
  61.                 return true;
  62.             return false;
  63.         }
  64.  
  65.     }
  66. }
  67.  
  68. namespace Hotel
  69. {
  70.     public class Hotel
  71.     {
  72.  
  73.         private List<Camera> camere;
  74.         public List<Camera> Camere { get => camere; set => camere = value; }
  75.  
  76.         public int nrCamereLibere()
  77.         {
  78.             int sum = 0;
  79.             foreach(Camera c in camere)
  80.             {
  81.                 if (c.Ocupata == false)
  82.                     sum++;
  83.             }
  84.             return sum;
  85.         }
  86.  
  87.  
  88.     }
  89. }
  90.  
  91. namespace Hotel
  92. {
  93.     static class Program
  94.     {
  95.         /// <summary>
  96.         /// The main entry point for the application.
  97.         /// </summary>
  98.         [STAThread]
  99.         static void Main()
  100.         {
  101.             Camera c1 = new Camera(1, DateTime.Now, Camera.TipuriCamera.doubla);
  102.             Console.WriteLine(c1);
  103.  
  104.             List<Camera> list = new List<Camera>();
  105.             StreamReader stream = new StreamReader("camere.txt");
  106.             string line;
  107.             while ((line = stream.ReadLine()) != null)
  108.             {
  109.                 string[] sir = line.Split(',');
  110.                 Camera.TipuriCamera tip;
  111.                 if (sir[2] == "dubla")
  112.                 {
  113.                     tip = Camera.TipuriCamera.doubla;
  114.                 }
  115.                 else if (sir[2] == "tripla")
  116.                 {
  117.                     tip = Camera.TipuriCamera.tripla;
  118.                 }
  119.                 else
  120.                     tip = Camera.TipuriCamera.single;
  121.                 Camera c = new Camera(int.Parse(sir[0]), DateTime.ParseExact(sir[1], "dd-MM-yyyy", null), tip);
  122.                 list.Add(c);
  123.             }
  124.  
  125.             foreach (Camera c in list)
  126.             {
  127.                 Console.WriteLine(c);
  128.             }
  129.  
  130.             Application.EnableVisualStyles();
  131.             Application.SetCompatibleTextRenderingDefault(false);
  132.             Application.Run(new Form1(list));
  133.         }
  134.     }
  135. }
  136.  
  137. namespace Hotel
  138. {
  139.     public partial class Form2 : Form
  140.     {
  141.         Camera camera;
  142.         public Form2(Camera c)
  143.         {
  144.             InitializeComponent();
  145.  
  146.             camera = c;
  147.             textBoxNr.Text = c.Nr.ToString();
  148.             if (c.TipCamera == "dubla")
  149.                 comboBoxTip.Text = comboBoxTip.Items[1].ToString();
  150.             else if (c.TipCamera == "single")
  151.                 comboBoxTip.Text = comboBoxTip.Items[0].ToString();
  152.             else
  153.                 comboBoxTip.Text = comboBoxTip.Items[2].ToString();
  154.             textBoxData.Text = c.Checkout.ToShortDateString();
  155.  
  156.  
  157.  
  158.         }
  159.  
  160.         private void btnOk_Click(object sender, EventArgs e)
  161.         {
  162.  
  163.             errorProvider1.SetError(textBoxNr, string.Empty);
  164.  
  165.             try
  166.             {
  167.                 int nr = int.Parse(textBoxNr.Text);
  168.                 if (nr < 0)
  169.                     throw new Exception("negativ");
  170.                 if (nr > 400 || nr < 100)
  171.                     throw new Exception("inexistent");
  172.                 camera.Nr = nr;
  173.  
  174.  
  175.                 Camera.TipuriCamera tip;
  176.                 if (comboBoxTip.SelectedItem.ToString() == "single")
  177.                     tip = Camera.TipuriCamera.single;
  178.                 else if (comboBoxTip.SelectedItem.ToString() == "doubla")
  179.                     tip = Camera.TipuriCamera.doubla;
  180.                 else
  181.                     tip = Camera.TipuriCamera.tripla;
  182.                 DateTime data = DateTime.Parse(textBoxData.Text);
  183.                 camera.Checkout = data;
  184.  
  185.                 camera.TipCamera = tip.ToString();
  186.  
  187.                 Close();
  188.             }
  189.             catch(FormatException)
  190.             {
  191.                 MessageBox.Show("here");
  192.                 errorProvider1.SetError(textBoxNr, "Numarul camerei este doar numeric!");
  193.             }
  194.             catch(Exception ex)
  195.             {
  196.                 if (ex.Message == "negativ")
  197.                 {
  198.                     errorProvider1.SetError(textBoxNr, "Numarul camerei este pozitiv");
  199.                 }
  200.                 else if (ex.Message == "inexistent")
  201.                 {
  202.                     errorProvider1.SetError(textBoxNr, "Aceasta camera nu exista!");
  203.                 }
  204.             }
  205.            
  206.            
  207.         }
  208.     }
  209. }
  210.  
  211. namespace Hotel
  212. {
  213.     public partial class Form1 : Form
  214.     {
  215.         public List<Camera> lstCamere;
  216.         public Form1(List<Camera> list)
  217.         {
  218.             InitializeComponent();
  219.             lstCamere = new List<Camera>();
  220.             lstCamere = list;
  221.  
  222.             listView1.View = View.Details;
  223.             listView1.Columns.Add("Numar camera", 100);
  224.             listView1.Columns.Add("Tip camera", 100);
  225.             listView1.Columns.Add("Checkout", 80);
  226.             listView1.Columns.Add("Ocupata", 80);
  227.  
  228.             listView1.MultiSelect = false;
  229.             listView1.FullRowSelect = true;
  230.  
  231.             populareListView();
  232.  
  233.             listView1.DoubleClick += ListView1_DoubleClick;
  234.             listView1.MouseClick += ListView1_MouseClick;
  235.  
  236.             panel1.Paint += Panel1_Paint;
  237.             panel1.Hide();
  238.             FormClosing += Form1_FormClosing;
  239.         }
  240.  
  241.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  242.         {
  243.  
  244.             Stream stream = new FileStream("camere.dat", FileMode.Create);
  245.             BinaryFormatter formatter = new BinaryFormatter();
  246.             foreach (Camera c in lstCamere)
  247.             {
  248.                 formatter.Serialize(stream, c);
  249.             }
  250.             stream.Close();
  251.  
  252.  
  253.         }
  254.  
  255.         private void Panel1_Paint(object sender, PaintEventArgs e)
  256.         {
  257.             Pen pen = new Pen(Color.Black, 3);
  258.  
  259.             Graphics g = e.Graphics;
  260.             int i = 2;
  261.             foreach (Camera c in lstCamere)
  262.             {
  263.                 int nr = c.getNrCamere();
  264.                 int space = 0;
  265.                 while (nr != 0)
  266.                 {
  267.                     g.DrawLine(pen, new Point(space, 20 * i), new Point(space + 30 , 20 * i));
  268.                     nr--;
  269.                     space += 60;
  270.                 }
  271.                 i++;
  272.             }
  273.             g.Dispose();
  274.         }
  275.  
  276.         private void ListView1_MouseClick(object sender, MouseEventArgs e)
  277.         {
  278.             if (e.Button == MouseButtons.Right)
  279.             {
  280.                 colorDialog1.ShowDialog();
  281.                 listView1.SelectedItems[0].BackColor = colorDialog1.Color;
  282.             }
  283.         }
  284.  
  285.         private void ListView1_DoubleClick(object sender, EventArgs e)
  286.         {
  287.             Camera c = (Camera)(listView1.SelectedItems[0].Tag);
  288.             Form2 form2 = new Form2(c);
  289.             form2.ShowDialog();
  290.  
  291.             populareListView();
  292.  
  293.         }
  294.  
  295.         public void populareListView()
  296.         {
  297.             listView1.Items.Clear();
  298.             foreach(Camera c in lstCamere.OrderBy(c => c.Checkout))
  299.             {
  300.                 ListViewItem rand = new ListViewItem();
  301.                 rand.Text = c.Nr.ToString();
  302.                 rand.SubItems.Add(c.TipCamera.ToString());
  303.                 rand.SubItems.Add(c.Checkout.ToShortDateString());
  304.                 if (c.Ocupata)
  305.                 {
  306.                     rand.SubItems.Add("Da");
  307.                 }
  308.                 else
  309.                 {
  310.                     rand.SubItems.Add("Nu");
  311.                 }
  312.                 rand.Tag = c;
  313.                
  314.                 listView1.Items.Add(rand);
  315.             }
  316.         }
  317.  
  318.         private void button1_Click(object sender, EventArgs e)
  319.         {
  320.             panel1.Show();
  321.         }
  322.  
  323.     }
  324. }
Advertisement
Add Comment
Please, Sign In to add comment