Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Text;
- using System.Xml;
- using System.Xml.Serialization;
- // using DansCSharpLibrary.Serialization;
- namespace StockControl
- {
- class MainClass
- {
- static void Main()
- {
- PrincipalMenu();
- }
- static void PrincipalMenu()
- {
- bool _activated = true;
- do
- {
- Console.WriteLine("1) Add stock. \n2) Add sales. \n3) Check stock." +
- "\n4) Clear stock. \n5) Manage brands. \n6) Exit.");
- var input = Console.ReadKey();
- Console.WriteLine();
- switch (input.Key)
- {
- case ConsoleKey.D1:
- {
- var newBeer = new Beer(1, true);
- break;
- }
- case ConsoleKey.D2:
- {
- break;
- }
- case ConsoleKey.D3:
- {
- var newBeer = new Beer(3, false);
- break;
- }
- case ConsoleKey.D4:
- {
- var newBeer = new Beer(4, true);
- break;
- }
- case ConsoleKey.D5:
- {
- ManageBrands();
- break;
- }
- case ConsoleKey.D6:
- {
- _activated = false;
- Console.WriteLine("Closing application");
- break;
- }
- }
- }
- while (_activated == true);
- }
- public static void ManageBrands()
- {
- bool _activated = true;
- do
- {
- Console.Clear();
- Console.WriteLine("Press 1 to add brand. \nPress 2 to modify brand" +
- "\nPress 3 to delete brand. \nPress 4 to exit to menu.");
- var input = Console.ReadKey();
- switch (input.Key)
- {
- case ConsoleKey.D1:
- {
- Console.Clear();
- var newBeer = new Beer(true);
- break;
- }
- case ConsoleKey.D2:
- {
- Console.Clear();
- var newBeer = new Beer(2, true);
- break;
- }
- case ConsoleKey.D3:
- {
- break;
- }
- case ConsoleKey.D4:
- {
- _activated = false;
- break;
- }
- }
- } while (_activated == true);
- Console.Clear();
- }
- }
- // [XmlAttribute()]
- [XmlRoot()]
- public class Menu
- {
- [XmlElement()]
- public List<Beer> Brands { get; set; }
- }
- [XmlRoot()]
- public class Beer
- {
- public static List<Beer> brands = new List<Beer>();
- [XmlAttribute()]
- public string name { get; set; }
- [XmlAttribute()]
- public int beers { get; set; }
- [XmlAttribute()]
- public int beerCases { get; set; }
- [XmlAttribute()]
- public int beerInCase { get; set; }
- public Beer() : this(false)
- {
- }
- public Beer(bool input)
- {
- if (input)
- {
- Console.WriteLine("Write the name of the new brand:");
- name = Console.ReadLine();
- Console.WriteLine("You have registered the {0} brand.", name);
- CheckData(1);
- Continue();
- }
- }
- public Beer(int _x, bool input)
- : this(input)
- {
- Console.Clear();
- CheckData(2);
- Console.WriteLine("Select a brand:");
- for (int _n = 0; _n < brands.Count; ++_n)
- {
- int _y = _n + 1;
- Beer _actualBrand = brands[_n];
- string nameOfBrnd = _actualBrand.name;
- Console.WriteLine("{0}) {1}", _y, nameOfBrnd);
- }
- switch (_x)
- {
- case 1: //instantiate to add stock
- {
- int _UsInpt = Convert.ToInt32(Console.ReadLine());
- brands[(_UsInpt - 1)].AddCase();
- break;
- }
- case 2: //instantiate to change settings
- {
- int _UsInpt = Convert.ToInt32(Console.ReadLine());
- brands[(_UsInpt - 1)].Settings();
- break;
- }
- case 3: //instantiate to show brand
- {
- int _UsInpt = Convert.ToInt32(Console.ReadLine());
- brands[(_UsInpt - 1)].Show();
- break;
- }
- case 4: //instantiate to clear data/all data
- {
- Console.WriteLine("{0}) Clear all data", (brands.Count + 1));
- int _UsInpt = Convert.ToInt32(Console.ReadLine());
- if (_UsInpt == (brands.Count + 1))
- {
- ClearData(2);
- }
- else
- {
- brands[(_UsInpt - 1)].ClearData(1);
- }
- break;
- }
- }
- }
- // I also tried saving/loading outside the class...
- public static void Load()
- {
- var menu = FileMenu.Load("Data.xml");
- brands = menu.Brands;
- }
- public static void Save()
- {
- var menu = new Menu() {Brands = brands};
- FileMenu.Save("Data.xml", menu);
- }
- // I'm using Daniel Schroeder's (aka deadlydog) serializaton.
- public void CheckData(int x)
- {
- switch (x)
- {
- case 1:
- {
- if (File.Exists("Data.xml"))
- {
- Load();
- brands.Add(this);
- Save();
- }
- else
- {
- brands.Add(this);
- Save();
- }
- break;
- }
- case 2:
- {
- if (File.Exists("Data.xml"))
- {
- Load();
- }
- else
- {
- NoBrand();
- }
- break;
- }
- }
- }
- public void Settings()
- {
- bool activated = true;
- do
- {
- Console.Clear();
- Console.WriteLine("{0}\n1) Change name.\n2) change beers in a case.\n3) Exit settings.", name);
- var input = Console.ReadKey();
- switch (input.Key)
- {
- case ConsoleKey.D1:
- {
- Console.WriteLine("Write the new name:");
- string beername = Console.ReadLine();
- name = beername;
- Save();
- Console.WriteLine("You have succesfully changed the name to {0}.", name);
- Continue();
- break;
- }
- case ConsoleKey.D2:
- {
- CaseContainer(1);
- break;
- }
- case ConsoleKey.D3:
- {
- activated = false;
- break;
- }
- }
- } while (activated == true);
- }
- public void CaseContainer(int x)
- {
- switch(x)
- {
- case 1:
- {
- Console.Clear();
- Console.WriteLine("How many beers a {0} case contains?", name);
- beerInCase = Convert.ToInt32(Console.ReadLine());
- Save();
- break;
- }
- case 2:
- {
- beerInCase = Convert.ToInt32(Console.ReadLine());
- Save();
- Console.WriteLine("{0} case: {1} beers.",name,beerInCase);
- break;
- }
- }
- }
- public void ClearData(int _x)
- {
- switch (_x)
- {
- case 1:
- {
- beers = 0;
- beerCases = 0;
- Save();
- Console.WriteLine("You have cleared all {0} data.",name);
- break;
- }
- case 2:
- {
- Console.WriteLine("Are you sure you want to clear all data?" +
- "\n(This includes all brands)");
- var _Input = Console.ReadKey();
- /*while (_Input.Key != ConsoleKey.D1
- || _Input.Key != ConsoleKey.D2) { }*/
- switch (_Input.Key)
- {
- case ConsoleKey.D1:
- {
- brands.Clear();
- Console.WriteLine("You have succesfully clear all data.");
- Save();
- break;
- }
- case ConsoleKey.D2:
- {
- break;
- }
- }
- break;
- }
- }
- Continue();
- }
- public void NoBrand()
- {
- Console.WriteLine("Sorry, you don't have any brand yet." +
- "\nPress <enter> to add a new brand or <space> to continue.");
- var _Inp = Console.ReadKey();
- //Code below not working
- /*while (Console.ReadKey().Key != ConsoleKey.Enter
- || Console.ReadKey().Key != ConsoleKey.Spacebar) { }*/
- switch (_Inp.Key)
- {
- case ConsoleKey.Enter:
- {
- var newBeer = new Beer(true);
- break;
- }
- case ConsoleKey.Spacebar:
- {
- Console.Clear();
- break;
- }
- }
- }
- public void AddCase()
- {
- if (beerInCase == 0)
- {
- Console.WriteLine("Before adding new cases, specify how many beers fit in a {0} case.",name);
- CaseContainer(2);
- }
- Console.WriteLine("How many cases you want to add?");
- int _cases = Convert.ToInt32(Console.ReadLine());
- beers = beers + _cases * beerInCase;
- double _x = beers / beerInCase;
- beerCases = Convert.ToInt32(Math.Floor(_x));
- Save();
- Console.WriteLine("You succefully added {0} {1} cases.\nYour new stock of {1} is {0} cases.\n",
- _cases, name);
- Continue();
- }
- public void Show()
- {
- Console.WriteLine(name +
- "\nNumber of beers: {0}" +
- "\nNumber of cases: {1}" +
- beers, beerCases);
- Continue();
- }
- public void Continue()
- {
- Console.Write("Press <Space> to continue...");
- while (Console.ReadKey().Key != ConsoleKey.Spacebar) { }
- Console.Clear();
- }
- }
- [XmlRoot()]
- public class FileMenu
- {
- public static readonly Encoding Utf8Encoding = Encoding.UTF8;
- public static readonly XmlSerializer MenuSerializer = new XmlSerializer(typeof(Menu));
- public static Menu Load(string path)
- {
- using (var stream = new StreamReader(path, Utf8Encoding, false))
- {
- using (var reader = new XmlTextReader(stream))
- {
- return MenuSerializer.Deserialize(reader) as Menu;
- }
- }
- }
- public static void Save(string path, Menu instance)
- {
- using (var stream = new StreamWriter(path, false, Utf8Encoding))
- {
- using (var writer = new XmlTextWriter(stream))
- {
- MenuSerializer.Serialize(writer, instance);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement