Advertisement
Guest User

StockControl - Fix

a guest
Feb 1st, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.08 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Xml;
  6. using System.Xml.Serialization;
  7.  
  8. // using DansCSharpLibrary.Serialization;
  9.  
  10. namespace StockControl
  11. {
  12.     class MainClass
  13.     {
  14.         static void Main()
  15.         {
  16.             PrincipalMenu();
  17.         }
  18.  
  19.         static void PrincipalMenu()
  20.         {
  21.             bool _activated = true;
  22.             do
  23.             {
  24.                 Console.WriteLine("1) Add stock. \n2) Add sales. \n3) Check stock." +
  25.                                   "\n4) Clear stock. \n5) Manage brands. \n6) Exit.");
  26.  
  27.  
  28.                 var input = Console.ReadKey();
  29.                 Console.WriteLine();
  30.                 switch (input.Key)
  31.                 {
  32.                     case ConsoleKey.D1:
  33.                         {
  34.                             var newBeer = new Beer(1, true);
  35.                             break;
  36.                         }
  37.                     case ConsoleKey.D2:
  38.                         {
  39.                             break;
  40.                         }
  41.                     case ConsoleKey.D3:
  42.                         {
  43.                             var newBeer = new Beer(3, false);
  44.                             break;
  45.                         }
  46.                     case ConsoleKey.D4:
  47.                         {
  48.                             var newBeer = new Beer(4, true);
  49.                             break;
  50.                         }
  51.                     case ConsoleKey.D5:
  52.                         {
  53.                             ManageBrands();
  54.                             break;
  55.                         }
  56.                     case ConsoleKey.D6:
  57.                         {
  58.                             _activated = false;
  59.                             Console.WriteLine("Closing application");
  60.                             break;
  61.                         }
  62.                 }
  63.             }
  64.             while (_activated == true);
  65.         }
  66.  
  67.         public static void ManageBrands()
  68.         {
  69.             bool _activated = true;
  70.  
  71.             do
  72.             {
  73.                 Console.Clear();
  74.                 Console.WriteLine("Press 1 to add brand. \nPress 2 to modify brand" +
  75.                                   "\nPress 3 to delete brand. \nPress 4 to exit to menu.");
  76.                
  77.                 var input = Console.ReadKey();
  78.                 switch (input.Key)
  79.                 {
  80.                     case ConsoleKey.D1:
  81.                         {
  82.                             Console.Clear();
  83.                             var newBeer = new Beer(true);
  84.                             break;
  85.                         }
  86.                     case ConsoleKey.D2:
  87.                         {
  88.                             Console.Clear();
  89.                             var newBeer = new Beer(2, true);
  90.                             break;
  91.                         }
  92.                     case ConsoleKey.D3:
  93.                         {
  94.                             break;
  95.                         }
  96.                     case ConsoleKey.D4:
  97.                         {
  98.                             _activated = false;
  99.                             break;
  100.                         }
  101.                 }
  102.             } while (_activated == true);
  103.             Console.Clear();
  104.         }
  105.     }
  106.  
  107.     // [XmlAttribute()]
  108.  
  109.     [XmlRoot()]
  110.     public class Menu
  111.     {
  112.         [XmlElement()]
  113.         public List<Beer> Brands { get; set; }
  114.     }
  115.  
  116.     [XmlRoot()]
  117.     public class Beer
  118.     {
  119.         public static List<Beer> brands = new List<Beer>();
  120.  
  121.         [XmlAttribute()]
  122.         public string name { get; set; }
  123.  
  124.         [XmlAttribute()]
  125.         public int beers { get; set; }
  126.  
  127.         [XmlAttribute()]
  128.         public int beerCases { get; set; }
  129.  
  130.         [XmlAttribute()]
  131.         public int beerInCase { get; set; }
  132.  
  133.         public Beer() : this(false)
  134.         {
  135.         }
  136.  
  137.         public Beer(bool input)
  138.         {
  139.             if (input)
  140.             {
  141.                 Console.WriteLine("Write the name of the new brand:");
  142.                 name = Console.ReadLine();
  143.                 Console.WriteLine("You have registered the {0} brand.", name);
  144.                 CheckData(1);
  145.                 Continue();
  146.             }
  147.         }
  148.  
  149.         public Beer(int _x, bool input)
  150.             : this(input)
  151.         {
  152.             Console.Clear();
  153.             CheckData(2);
  154.             Console.WriteLine("Select a brand:");
  155.                 for (int _n = 0; _n < brands.Count; ++_n)
  156.                 {
  157.                     int _y = _n + 1;
  158.                     Beer _actualBrand = brands[_n];
  159.                     string nameOfBrnd = _actualBrand.name;
  160.                     Console.WriteLine("{0}) {1}", _y, nameOfBrnd);
  161.                 }
  162.                 switch (_x)
  163.                 {
  164.                     case 1: //instantiate to add stock
  165.                         {
  166.                             int _UsInpt = Convert.ToInt32(Console.ReadLine());
  167.                             brands[(_UsInpt - 1)].AddCase();
  168.                             break;
  169.                         }
  170.                     case 2: //instantiate to change settings
  171.                         {
  172.                             int _UsInpt = Convert.ToInt32(Console.ReadLine());
  173.                             brands[(_UsInpt - 1)].Settings();
  174.                             break;
  175.                         }
  176.                     case 3: //instantiate to show brand
  177.                         {
  178.                             int _UsInpt = Convert.ToInt32(Console.ReadLine());
  179.                             brands[(_UsInpt - 1)].Show();
  180.                             break;
  181.                         }
  182.                     case 4: //instantiate to clear data/all data
  183.                         {
  184.                             Console.WriteLine("{0}) Clear all data", (brands.Count + 1));
  185.                             int _UsInpt = Convert.ToInt32(Console.ReadLine());
  186.                             if (_UsInpt == (brands.Count + 1))
  187.                             {
  188.                                 ClearData(2);
  189.                             }
  190.                             else
  191.                             {
  192.                                 brands[(_UsInpt - 1)].ClearData(1);
  193.                             }
  194.                             break;
  195.                         }
  196.                 }
  197.         }
  198.         // I also tried saving/loading outside the class...
  199.         public static void Load()
  200.         {
  201.             var menu = FileMenu.Load("Data.xml");
  202.             brands = menu.Brands;
  203.         }
  204.         public static void Save()
  205.         {
  206.             var menu = new Menu() {Brands = brands};
  207.             FileMenu.Save("Data.xml", menu);
  208.         }
  209.         // I'm using Daniel Schroeder's (aka deadlydog) serializaton.
  210.         public void CheckData(int x)
  211.         {
  212.             switch (x)
  213.             {
  214.                 case 1:
  215.                     {
  216.                         if (File.Exists("Data.xml"))
  217.                         {
  218.                             Load();
  219.                             brands.Add(this);
  220.                             Save();
  221.                         }
  222.                         else
  223.                         {
  224.                             brands.Add(this);
  225.                             Save();
  226.                         }
  227.  
  228.                         break;
  229.                     }
  230.                 case 2:
  231.                     {
  232.                         if (File.Exists("Data.xml"))
  233.                         {
  234.                             Load();
  235.                         }
  236.                         else
  237.                         {
  238.                             NoBrand();
  239.                         }
  240.                         break;
  241.                     }
  242.             }
  243.         }
  244.         public void Settings()
  245.         {
  246.             bool activated = true;
  247.             do
  248.             {
  249.                 Console.Clear();
  250.                 Console.WriteLine("{0}\n1) Change name.\n2) change beers in a case.\n3) Exit settings.", name);
  251.                 var input = Console.ReadKey();
  252.                 switch (input.Key)
  253.                 {
  254.                     case ConsoleKey.D1:
  255.                         {
  256.                             Console.WriteLine("Write the new name:");
  257.                             string beername = Console.ReadLine();
  258.                             name = beername;
  259.                             Save();
  260.                             Console.WriteLine("You have succesfully changed the name to {0}.", name);
  261.                             Continue();
  262.                             break;
  263.                         }
  264.                     case ConsoleKey.D2:
  265.                         {
  266.                             CaseContainer(1);
  267.                             break;
  268.                         }
  269.                     case ConsoleKey.D3:
  270.                         {
  271.                             activated = false;
  272.                             break;
  273.                         }
  274.                 }
  275.             } while (activated == true);
  276.         }
  277.         public void CaseContainer(int x)
  278.         {
  279.             switch(x)
  280.             {
  281.                 case 1:
  282.                     {
  283.                         Console.Clear();
  284.                         Console.WriteLine("How many beers a {0} case contains?", name);
  285.                         beerInCase = Convert.ToInt32(Console.ReadLine());
  286.                         Save();
  287.                         break;
  288.                     }
  289.                 case 2:
  290.                     {
  291.                         beerInCase = Convert.ToInt32(Console.ReadLine());
  292.                         Save();
  293.                         Console.WriteLine("{0} case: {1} beers.",name,beerInCase);
  294.                         break;
  295.                     }
  296.             }
  297.         }
  298.         public void ClearData(int _x)
  299.         {
  300.             switch (_x)
  301.             {
  302.                 case 1:
  303.                     {
  304.                         beers = 0;
  305.                         beerCases = 0;
  306.                         Save();
  307.                         Console.WriteLine("You have cleared all {0} data.",name);
  308.                         break;
  309.                     }
  310.                 case 2:
  311.                     {
  312.                         Console.WriteLine("Are you sure you want to clear all data?" +
  313.                                           "\n(This includes all brands)");
  314.  
  315.                         var _Input = Console.ReadKey();
  316.                         /*while (_Input.Key != ConsoleKey.D1
  317.                                || _Input.Key != ConsoleKey.D2) { }*/
  318.                         switch (_Input.Key)
  319.                         {
  320.                             case ConsoleKey.D1:
  321.                                 {
  322.                                     brands.Clear();
  323.                                     Console.WriteLine("You have succesfully clear all data.");
  324.                                     Save();
  325.                                     break;
  326.                                 }
  327.                             case ConsoleKey.D2:
  328.                                 {
  329.                                     break;
  330.                                 }
  331.                         }
  332.                         break;
  333.                     }
  334.             }
  335.             Continue();
  336.         }
  337.         public void NoBrand()
  338.         {
  339.             Console.WriteLine("Sorry, you don't have any brand yet." +
  340.                     "\nPress <enter> to add a new brand or <space> to continue.");
  341.             var _Inp = Console.ReadKey();
  342.  
  343.             //Code below not working
  344.             /*while (Console.ReadKey().Key != ConsoleKey.Enter
  345.                 || Console.ReadKey().Key != ConsoleKey.Spacebar) { }*/
  346.             switch (_Inp.Key)
  347.             {
  348.                 case ConsoleKey.Enter:
  349.                     {
  350.                         var newBeer = new Beer(true);
  351.                         break;
  352.                     }
  353.                 case ConsoleKey.Spacebar:
  354.                     {
  355.                         Console.Clear();
  356.                         break;
  357.                     }
  358.             }
  359.  
  360.         }
  361.         public void AddCase()
  362.         {
  363.             if (beerInCase == 0)
  364.             {
  365.                 Console.WriteLine("Before adding new cases, specify how many beers fit in a {0} case.",name);
  366.                 CaseContainer(2);
  367.             }
  368.             Console.WriteLine("How many cases you want to add?");
  369.             int _cases = Convert.ToInt32(Console.ReadLine());
  370.             beers = beers + _cases * beerInCase;
  371.             double _x = beers / beerInCase;
  372.             beerCases = Convert.ToInt32(Math.Floor(_x));
  373.             Save();
  374.             Console.WriteLine("You succefully added {0} {1} cases.\nYour new stock of {1} is {0} cases.\n",
  375.                               _cases, name);
  376.             Continue();
  377.         }
  378.         public void Show()
  379.         {
  380.             Console.WriteLine(name +
  381.                               "\nNumber of beers: {0}" +
  382.                               "\nNumber of cases: {1}" +
  383.                               beers, beerCases);
  384.             Continue();
  385.         }
  386.         public void Continue()
  387.         {
  388.             Console.Write("Press <Space> to continue...");
  389.             while (Console.ReadKey().Key != ConsoleKey.Spacebar) { }
  390.             Console.Clear();
  391.         }
  392.     }
  393.  
  394.     [XmlRoot()]
  395.     public class FileMenu
  396.     {
  397.         public static readonly Encoding Utf8Encoding = Encoding.UTF8;
  398.         public static readonly XmlSerializer MenuSerializer = new XmlSerializer(typeof(Menu));
  399.         public static Menu Load(string path)
  400.         {
  401.             using (var stream = new StreamReader(path, Utf8Encoding, false))
  402.             {
  403.                 using (var reader = new XmlTextReader(stream))
  404.                 {
  405.                     return MenuSerializer.Deserialize(reader) as Menu;
  406.                 }
  407.             }
  408.         }
  409.         public static void Save(string path, Menu instance)
  410.         {
  411.             using (var stream = new StreamWriter(path, false, Utf8Encoding))
  412.             {
  413.                 using (var writer = new XmlTextWriter(stream))
  414.                 {
  415.                     MenuSerializer.Serialize(writer, instance);
  416.                 }
  417.             }
  418.         }
  419.     }
  420. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement