Advertisement
Velenc

Reszveny

Sep 13th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. namespace Reszveny
  8. {
  9.    
  10.     public enum Reszvenytipusok
  11.     {
  12.         IBM, TSLA, NFLX, GOOGL, FB, NVDA, MSFT, AAPL, AMZN
  13.     }
  14.     public class Tulajdonos
  15.     {
  16.         public  int db;
  17.         public decimal arfolyam; List<Reszvenytipusok> reszvenylista;
  18.         bool isAdossag;
  19.         private decimal adossag;
  20.         public decimal Adossag
  21.         {
  22.             get
  23.             {
  24.                 return adossag;
  25.             }
  26.             set
  27.             {
  28.                 adossag = value;
  29.             }
  30.         }
  31.         public List<Reszveny> reszvenyek;
  32.         public Tulajdonos(string nev, decimal penz)
  33.         {
  34.             reszvenylista = new List<Reszvenytipusok>();
  35.             reszvenyek = new List<Reszveny>();
  36.             this.nev = nev;
  37.             this.penz = penz;
  38.         }
  39.         private string nev;
  40.         public string Nev
  41.         {
  42.             get
  43.             {
  44.                 return nev;
  45.             }
  46.             set
  47.             {
  48.                 nev = value;
  49.             }
  50.         }
  51.         private decimal penz;
  52.         public decimal Penz
  53.         {
  54.             get
  55.             {
  56.                 return penz;
  57.             }
  58.             set
  59.             {
  60.                 penz = value;
  61.             }
  62.         }
  63.         public string PrintTulajdon()
  64.         {
  65.             string adat = "";
  66.             adat += "Reszveny Tulajdonosa: " + this.Nev + Environment.NewLine;
  67.             adat += "Keszpenz: $" + this.Penz + Environment.NewLine;
  68.             adat += Environment.NewLine;
  69.             if (isAdossag)
  70.             {
  71.                 this.Adossag = -(this.Penz);
  72.                 adat += "Adossag: $" + this.Adossag + Environment.NewLine;
  73.             }
  74.             adat += Environment.NewLine;
  75.             int count = 0;
  76.             foreach (var item in reszvenyek)
  77.             {
  78.                 adat += count + ".";
  79.                 adat += "Nev: " + item.Nev + Environment.NewLine;
  80.                 adat += "Arfolyam: $" + item.Arfolyam + Environment.NewLine;
  81.                 adat += "Darab: " + item.Db + Environment.NewLine;
  82.                 adat += Environment.NewLine;
  83.                 count++;
  84.             }
  85.             if (reszvenyek.Capacity == 0)
  86.             {
  87.                 adat = this.Nev + "tulajdonaban nincs reszveny!";
  88.             }
  89.             return adat;
  90.         }
  91.         public List<string> GetTulajdon()
  92.         {
  93.             List<string> tulajdon = new List<string>();
  94.             tulajdon.Add(this.Nev);
  95.             tulajdon.Add(Environment.NewLine);
  96.             tulajdon.Add(this.Penz.ToString());
  97.             tulajdon.Add(Environment.NewLine);
  98.             if (isAdossag)
  99.             {
  100.                 this.Adossag = -(this.Penz);
  101.                 tulajdon.Add(this.Adossag.ToString());
  102.             }
  103.             else
  104.             {
  105.                 tulajdon.Add("0");
  106.             }
  107.             tulajdon.Add(Environment.NewLine);
  108.             foreach (var item in reszvenyek)
  109.             {
  110.                 tulajdon.Add(item.Nev);
  111.                 tulajdon.Add(Environment.NewLine);
  112.                 tulajdon.Add(item.Arfolyam.ToString());
  113.                 tulajdon.Add(Environment.NewLine);
  114.                 tulajdon.Add(item.Db.ToString());
  115.                 tulajdon.Add(Environment.NewLine);
  116.             }
  117.             if (reszvenyek.Capacity == 0)
  118.             {
  119.                 tulajdon.Add(this.Nev + "tulajdonaban nincs reszveny!");
  120.             }
  121.             return tulajdon;
  122.         }
  123.         public bool isTulajdon()
  124.         {
  125.             bool tulajdon = true;
  126.             if (reszvenyek.Capacity == 0)
  127.             {
  128.                 tulajdon = false;
  129.             }
  130.             return tulajdon;
  131.         }
  132.         public List<Reszvenytipusok> Vetel(Reszveny reszveny)
  133.         {
  134.             reszvenyek.Add(reszveny);
  135.             penz -= reszveny.Arfolyam * reszveny.Db;
  136.             if (penz < 0)
  137.             {
  138.                 isAdossag = true;
  139.                 adossag = penz;
  140.             }
  141.             return reszvenylista;
  142.         }
  143.         public List<Reszvenytipusok> Eladas(int valasz)
  144.         {
  145.             penz += reszvenyek.ElementAt(valasz).Arfolyam * reszvenyek.ElementAt(valasz).Db;
  146.             reszvenyek.RemoveAt(valasz);
  147.             return reszvenylista;
  148.         }
  149.         public void SaveGame(List<string> adat)
  150.         {
  151.             string text = "";
  152.             foreach (var item in adat)
  153.             {
  154.                 text += item;
  155.             }
  156.             File.WriteAllText(@"D:\teszt.txt", text);
  157.         }
  158.         public void LoadGame(List<string> adat)
  159.         {
  160.             string[] lines = new string[] { };
  161.             if (File.Exists(@"D:\teszt.txt"))
  162.             {
  163.                 lines = File.ReadAllLines(@"D:\teszt.txt");
  164.                 Console.WriteLine("elmentett fajl tartalma: ");
  165.                 if (!lines.Any())
  166.                 {
  167.                     Console.WriteLine("Ures");
  168.                 }
  169.                 else
  170.                 {
  171.                     foreach (string line in lines)
  172.                     {
  173.                         Console.WriteLine("\t" + line);
  174.                     }
  175.                 }                
  176.                 int count = 0;
  177.                 foreach (var item in adat)
  178.                 {
  179.                     switch (count)
  180.                     {
  181.                         case 0:
  182.                             Nev = item;
  183.                             break;
  184.                         case 1:
  185.                             Penz = Decimal.Parse(item);
  186.                             break;
  187.                         case 2:
  188.                             Adossag = Decimal.Parse(item);
  189.                             break;
  190.                         case 3:
  191.                             Nev = item;
  192.                             break;
  193.                         case 4:
  194.                             arfolyam = Decimal.Parse(item);
  195.                             break;
  196.                         case 5:
  197.                             db = Int32.Parse(item);
  198.                             break;
  199.                         default:
  200.                             break;
  201.                     }
  202.                     count++;
  203.                 }
  204.             }
  205.             else
  206.             {
  207.                 this.SaveGame(this.GetTulajdon());
  208.             }
  209.         }
  210.         public List<string> LoadData()
  211.         {
  212.             List<string> data = new List<string>();
  213.             if (File.Exists(@"D:\teszt.txt"))
  214.             {
  215.                 using (StreamReader reader = new StreamReader(@"D:\teszt.txt"))
  216.                 {
  217.                     string s = reader.ReadLine();
  218.                     while (s != null)
  219.                     {
  220.                         data.Add(s);
  221.                         s = reader.ReadLine();
  222.                     }
  223.                 }
  224.             }
  225.             else
  226.             {
  227.                 data = null;
  228.             }
  229.             return data;
  230.         }      
  231.         public class Reszveny
  232.         {  
  233.             public List<string> ReszvenyNevek;
  234.             private string nev;
  235.             private decimal arfolyam;
  236.             private decimal vetelArfolyam;
  237.             public int db;
  238.             public Reszvenytipusok tipus;
  239.             public Reszveny(Reszvenytipusok tipus, int db)
  240.             {
  241.                 this.tipus = tipus;
  242.                 this.db =  db;
  243.             }
  244.             public Reszveny()
  245.             {
  246.                 ReszvenyNevek = new List<string>();
  247.             }
  248.             public Reszveny(string nev, decimal arfolyam, int db)
  249.             {
  250.                 this.nev = nev;
  251.                 this.arfolyam = arfolyam;
  252.                 this.db = db;
  253.                 ReszvenyNevek = new List<string>();
  254.             }
  255.             public string Nev
  256.             {
  257.                 get
  258.                 {
  259.                     return nev;
  260.                 }
  261.                 set
  262.                 {
  263.                     nev = value;
  264.                 }
  265.             }
  266.             public int Db
  267.             {
  268.                 get
  269.                 {
  270.                     return db;
  271.                 }
  272.                 set
  273.                 {
  274.                     db = value;
  275.                 }
  276.             }
  277.             public decimal Arfolyam
  278.             {
  279.                 get
  280.                 {
  281.                     return arfolyam;
  282.                 }
  283.                 set
  284.                 {
  285.                     arfolyam = value;
  286.                 }
  287.             }
  288.             public decimal VetelArfolyam
  289.             {
  290.                 get
  291.                 {
  292.                     return vetelArfolyam;
  293.                 }
  294.                 set
  295.                 {
  296.                     vetelArfolyam = value;
  297.                 }
  298.             }
  299.             public decimal ArfolyamValtozas(double szazalek)
  300.             {
  301.                 szazalek /= 100;
  302.                 this.Arfolyam = this.Arfolyam + this.Arfolyam * (decimal)szazalek;
  303.                 return this.Arfolyam;
  304.             }
  305.             public void VisszaAllitas()
  306.             {
  307.                 this.nev = "";
  308.                 this.arfolyam = 0;
  309.                 this.db = 0;
  310.             }
  311.         }
  312.     }
  313.     class Program
  314.     {
  315.         static void Main(string[] args)
  316.         {
  317.             if (!File.Exists(@"D:\teszt.txt"))
  318.             {
  319.                 File.Create(@"D:\teszt.txt");
  320.             }
  321.             List<string> ReszvenyNevek = Enum.GetNames(typeof(Reszvenytipusok)).ToList();
  322.             List<Tulajdonos.Reszveny> reszvenyek = new List<Tulajdonos.Reszveny>();
  323.             Tulajdonos John = new Tulajdonos("John", 1000);
  324.             string tovabb = "";
  325.             string eladvesz = "";
  326.             List<string> lista = new List<string>();
  327.             lista = John.LoadData();
  328.             while (tovabb != "n")
  329.             {
  330.                 John.LoadGame(lista);
  331.                 try
  332.                 {
  333.                     John.PrintTulajdon();
  334.                     Console.WriteLine();
  335.                     Console.WriteLine("Reszvenyt (V)enni vagy (E)ladni szeretne?");
  336.                     eladvesz = Console.ReadLine();
  337.                     if (eladvesz == "e" || eladvesz == "E")
  338.                     {
  339.                         try
  340.                         {
  341.                             Console.Clear();
  342.                             Console.WriteLine("Valassza ki milyen reszvenyt szeretne eladni a tulajdonabol: ");
  343.                             if (John.isTulajdon())
  344.                             {
  345.                                 Console.WriteLine(John.PrintTulajdon());
  346.                                 Console.WriteLine();
  347.                                 int valasz = Convert.ToInt32(Console.ReadLine());
  348.                                 John.Eladas(valasz);
  349.                                 Console.Clear();
  350.                             }
  351.                             else
  352.                             {
  353.                                 Console.Clear();
  354.                                 Console.WriteLine("Nincs tulajdonaban reszveny!");
  355.                                 Console.WriteLine();
  356.                             }
  357.                         }
  358.                         catch (Exception)
  359.                         {
  360.                             Console.Clear();
  361.                             Console.WriteLine("Ervenytelen Utasitas!");
  362.                             Console.WriteLine();
  363.                         }
  364.                     }
  365.                     else
  366.                     {
  367.                         try
  368.                         {
  369.                             Console.Clear();
  370.                             Console.WriteLine("Valassza ki milyen reszvenyt szeretne venni: ");
  371.                             int count = 0;
  372.                             foreach (var item in ReszvenyNevek)
  373.                             {
  374.                                 Console.WriteLine(count + ": " + item);
  375.                                 count++;
  376.                             }
  377.                             int valasz = Convert.ToInt32(Console.ReadLine());
  378.                             Console.WriteLine("Hany darab reszvenyt szeretne vasarolni");
  379.                             int darab = Convert.ToInt32(Console.ReadLine());
  380.                             Tulajdonos.Reszveny reszveny = new Tulajdonos.Reszveny() { Db = darab, Nev = ((Reszvenytipusok)valasz).ToString(), Arfolyam = 25 };
  381.                             John.Vetel(reszveny);
  382.                             foreach (var item in John.reszvenyek)
  383.                             {
  384.                                 Random random = new Random();
  385.                                 double arfolyamvaltozas = random.Next(-15, 15);
  386.                                 reszveny.ArfolyamValtozas(arfolyamvaltozas);
  387.                             }
  388.                             Console.Clear();
  389.                         }
  390.                         catch (Exception)
  391.                         {
  392.                             Console.WriteLine("Ervenytelen Utasitas!");
  393.                         }
  394.                     }
  395.                     Console.WriteLine(John.PrintTulajdon());
  396.                     Console.WriteLine();
  397.                     John.SaveGame(John.GetTulajdon());
  398.                     lista.Add(John.GetTulajdon().ToString());
  399.                     Console.Clear();
  400.                     Console.WriteLine("Szeretnel tovabb kereskedni reszvenyel? (I/N)");
  401.                     Console.WriteLine("Jo hirunk van szamara. Valtoztak a Reszveny arak.");
  402.                     tovabb = Console.ReadLine();
  403.                     Console.Clear();
  404.                     Console.WriteLine(John.PrintTulajdon());
  405.                     Console.WriteLine();
  406.                 }
  407.                 catch (Exception e)
  408.                 {
  409.                     Console.Clear();
  410.                     Console.WriteLine("Ervenytelen Utasitas!");
  411.                     Console.WriteLine(e);
  412.                     Console.WriteLine(John.PrintTulajdon());
  413.                 }
  414.             }
  415.             Console.Clear();
  416.             Console.WriteLine(John.PrintTulajdon());
  417.             Console.ReadLine();
  418.         }
  419.     }
  420. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement