Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 25.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.IO;
  5. using System.Xml.Serialization;
  6. using System.Runtime.Serialization;
  7. using System.Runtime.Serialization.Json;
  8.  
  9. namespace lab3
  10. {
  11.     [DataContract]
  12.     public abstract class MusInstrument
  13.     {
  14.  
  15.         abstract public bool playNote(char note);
  16.  
  17.     }
  18.     [DataContract]
  19.     public class StringInstrument : MusInstrument
  20.     {
  21.         protected double tune;
  22.         protected static double tunningCoef;
  23.         protected static double TunningCoef
  24.         {
  25.  
  26.             get { return tunningCoef; }
  27.             set
  28.             {
  29.                 if (value <= 0 || value > 3)
  30.                 {
  31.                     tunningCoef = 3;
  32.                 }
  33.             }
  34.         }
  35.  
  36.         protected virtual double Tune
  37.         {
  38.             get { return tune; }
  39.             set
  40.             {
  41.                 if (value >= 0.95)
  42.                 {
  43.                     tune = 0.9;
  44.                 }
  45.                 else if (value <= 0.10)
  46.                 {
  47.                     tune = 0.15;
  48.                 }
  49.                 else
  50.                 {
  51.                     tune = value;
  52.                 }
  53.             }
  54.         }
  55.         public virtual double updateTune()
  56.         {
  57.             tune = 0.75;
  58.             return tune;
  59.         }
  60.  
  61.         protected virtual double entropyTunning()
  62.         {
  63.             Random rnd = new Random();
  64.             if (rnd.Next(0, 10) < 6)
  65.             {
  66.                 Tune -= rnd.Next(0, 100) / Math.Pow(10, TunningCoef + 3);
  67.                 //Console.WriteLine(ind);
  68.             }
  69.             else
  70.             {
  71.                 Tune += rnd.Next(0, 100) / Math.Pow(10, TunningCoef + 3) / 3;
  72.                 //Console.WriteLine(ind);
  73.             }
  74.  
  75.             return tune;
  76.         }
  77.  
  78.  
  79.         //CONSTRUCTORS
  80.         //One static is inherited from musInstrument
  81.         static StringInstrument()
  82.         {
  83.             TunningCoef = 1;
  84.             Console.WriteLine("Im static constr StringInstr!");
  85.  
  86.         }
  87.  
  88.         public StringInstrument()
  89.         {
  90.             Tune = 0.7;
  91.         }
  92.         public StringInstrument(bool isTuned)
  93.         {
  94.             if (isTuned)
  95.             {
  96.                 tune = 0.9;
  97.             }
  98.             else
  99.             {
  100.                 tune = 0.3;
  101.             }
  102.  
  103.         }
  104.         // override methods
  105.         public override bool playNote(char note)
  106.         {
  107.             entropyTunning();
  108.  
  109.             if (tune <= 0.5)
  110.             {
  111.                 Console.WriteLine("failed to play [" + note + "], you'd better tune the instrument");
  112.                 return false;
  113.             }
  114.             else
  115.             {
  116.                 Console.WriteLine("String instrument Played [" + note + "], curr instrument condition: {" + Math.Round(Math.Round(tune, 2) * 100) + "%}");
  117.                 return true;
  118.             }
  119.         }
  120.  
  121.     }
  122.     interface IElectroGuitar
  123.     {
  124.         private bool playPowerChord(string chord)
  125.         {
  126.             return true;
  127.         }
  128.  
  129.     }
  130.     interface IClassicGuitar
  131.     {
  132.         private bool playClassicChord(string chord)
  133.         {
  134.             return true;
  135.         }
  136.         bool playGamma(string firstNote)
  137.         {
  138.             Console.WriteLine("Classic guitar Gamma ");
  139.             return true;
  140.         }
  141.     }
  142.     interface IUkulele
  143.     {
  144.         bool playGamma(string firstNote)
  145.         {
  146.             Console.WriteLine("Ukulele Gamm ");
  147.             return true;
  148.         }
  149.     }
  150.     class Ukulele : Guitar, IUkulele, IClassicGuitar
  151.     {
  152.         private const string classicChordNotes = "ABCDEFGabcdefg";
  153.         private const string chordsSymbols = "#+7mM";
  154.         public Ukulele()
  155.         {
  156.             tune = 0.85;
  157.             availableNotes = "ABC";
  158.             stringAmount = 4;
  159.  
  160.         }
  161.         bool IUkulele.playGamma(string firstNote)
  162.         {
  163.             if (availableNotes.Contains(firstNote))
  164.             {
  165.                 if (tune <= 0.5 && tune >= 0.3)
  166.                 {
  167.                     Console.WriteLine("Ukulele pleyed the gamm");
  168.                 }
  169.                 else
  170.                 {
  171.                     throw new TunningException("Low Tune", 0.3);
  172.                 }
  173.                 return true;
  174.  
  175.             }
  176.             else
  177.             {
  178.                 return false;
  179.             }
  180.         }
  181.         private bool playClassicChord(string chord)
  182.         {
  183.  
  184.             entropyTunning();
  185.             foreach (char ch in chord)
  186.             {
  187.                 if (!classicChordNotes.Contains(ch) && !chordsSymbols.Contains(ch))
  188.                 {
  189.                     Console.WriteLine($"Wrong CHord format or classic guitar couldnt play it[{chord}]");
  190.                     return false;
  191.                 }
  192.             }
  193.  
  194.             if (tune <= 0.5 && tune >= 0.3)
  195.             {
  196.                 Console.WriteLine($"Played classic hord{chord} disgustfully");
  197.  
  198.             }
  199.             else if (tune >= 0.5)
  200.             {
  201.                 Console.WriteLine($"Played classic hord{chord}, what a beautiful chord!");
  202.             }
  203.             else
  204.             {
  205.                 throw new TunningException("Low Tune", 0.3);
  206.             }
  207.             return true;
  208.  
  209.         }
  210.  
  211.         public bool playChord(string chord)
  212.         {
  213.             try
  214.             {
  215.                 return playClassicChord(chord);
  216.             }
  217.             catch (TunningException err)
  218.             {
  219.                 throw err;
  220.             }
  221.         }
  222.     }
  223.     class ClassicGuitar : Guitar, IClassicGuitar
  224.     {
  225.         private const string classicChordNotes = "ABCDEFGabcdefg";
  226.         private const string chordsSymbols = "#+7mM";
  227.         public ClassicGuitar()
  228.         {
  229.             tune = 0.8;
  230.             Console.WriteLine("I'm classic Guitar constr!");
  231.         }
  232.         private bool playClassicChord(string chord)
  233.         {
  234.  
  235.             entropyTunning();
  236.             foreach (char ch in chord)
  237.             {
  238.                 if (!classicChordNotes.Contains(ch) && !chordsSymbols.Contains(ch))
  239.                 {
  240.                     Console.WriteLine($"Wrong CHord format or classic guitar couldnt play it[{chord}]");
  241.                     return false;
  242.                 }
  243.             }
  244.  
  245.             if (tune <= 0.5 && tune >= 0.3)
  246.             {
  247.                 Console.WriteLine($"Played classic hord{chord} disgustfully");
  248.  
  249.             }
  250.             else if (tune >= 0.5)
  251.             {
  252.                 Console.WriteLine($"Played classic hord{chord}, what a beautiful chord!");
  253.             }
  254.             else
  255.             {
  256.                 throw new TunningException("Low Tune", 0.3);
  257.             }
  258.             return true;
  259.  
  260.         }
  261.  
  262.         public bool playChord(string chord)
  263.         {
  264.             try
  265.             {
  266.                 return playClassicChord(chord);
  267.             }
  268.             catch (TunningException err)
  269.             {
  270.                 throw err;
  271.             }
  272.         }
  273.     }
  274.     class ElectroGuitar : Guitar, IElectroGuitar
  275.     {
  276.         const string powerChordNotes = "ABCDEabcde";
  277.         const string chordsSymbols = "#+b";
  278.         public ElectroGuitar()
  279.         {
  280.             tune = 0.8;
  281.             Console.WriteLine("I'm electroGuitar constr!");
  282.         }
  283.         private bool playPowerChord(string chord)
  284.         {
  285.             entropyTunning();
  286.  
  287.  
  288.             foreach (char ch in chord)
  289.             {
  290.                 if (!powerChordNotes.Contains(ch) && !chordsSymbols.Contains(ch))
  291.                 {
  292.                     Console.WriteLine($"Wrong CHord format or overdriven guitar couldnt play it[{chord}]");
  293.                     return false;
  294.                 }
  295.             }
  296.  
  297.             if (Tune >= 0.25)
  298.             {
  299.                 Console.WriteLine($"Played power hord{chord}, what a beautiful chord!");
  300.             }
  301.             else
  302.             {
  303.                 throw new TunningException("low tunning", 0.25);
  304.             }
  305.             return true;
  306.  
  307.         }
  308.         public bool playChord(string chord)
  309.         {
  310.             try
  311.             {
  312.                 return playPowerChord(chord);
  313.             }
  314.             catch (TunningException err)
  315.             {
  316.                 throw err;
  317.             }
  318.         }
  319.     }
  320.     class ElectroAcusticGuitar : Guitar, IClassicGuitar, IElectroGuitar
  321.     {
  322.  
  323.         private const string classicChordNotes = "ABCDEFGabcdefg";
  324.         private const string chordsSymbols = "#+7mM";
  325.  
  326.         const string powerChordNotes = "ABCDEabcde";
  327.  
  328.         private double _currGain;
  329.         private double currGain
  330.         {
  331.             get { return _currGain; }
  332.             set
  333.             {
  334.                 if (value >= 0.9)
  335.                 {
  336.                     _currGain = 0.9;
  337.                 }
  338.                 else if (value <= 0.3)
  339.                 {
  340.                     _currGain = 0.3;
  341.                 }
  342.             }
  343.         }
  344.         private delegate double OnGainChangeDelegate(bool isPositive);
  345.         private event OnGainChangeDelegate onGainChange;
  346.         private event Action<bool> onGainChangeAction;
  347.         private event Func<bool, double> onGainChangeFunc;
  348.  
  349.         protected double entropyTunning(bool isPositive)
  350.         {
  351.             if (isPositive)
  352.             {
  353.                 Tune += 0.1;
  354.             }
  355.             else
  356.             {
  357.                 Tune -= 0.1;
  358.             }
  359.             return base.entropyTunning();
  360.         }
  361.         private double changeGain(bool isPositive)
  362.         {
  363.             if (isPositive)
  364.             {
  365.                 currGain += 0.2;
  366.             }
  367.             else
  368.             {
  369.                 currGain -= 0.2;
  370.             }
  371.  
  372.             return Tune;
  373.         }
  374.         public ElectroAcusticGuitar()
  375.         {
  376.             //ANONYMUS
  377.             //gainChangeDelegate onGainChangeFunc = delegate(bool isPositive)
  378.             //{
  379.             //    if (isPositive)
  380.             //    {
  381.             //        currGain += 0.2;
  382.             //    }
  383.             //    else
  384.             //    {
  385.             //        currGain -= 0.2;
  386.             //    }
  387.  
  388.             //    return Tune;
  389.             //};
  390.             //LAMBDA
  391.             OnGainChangeDelegate gainChangeFunc = isPositive =>
  392.             {
  393.                 if (isPositive)
  394.                 {
  395.                     currGain += 0.2;
  396.                 }
  397.                 else
  398.                 {
  399.                     currGain -= 0.2;
  400.                 }
  401.  
  402.                 return Tune;
  403.             };
  404.             Action<bool> gainChangeAction = isPositive =>
  405.             {
  406.                 if (isPositive)
  407.                 {
  408.                     currGain += 0.2;
  409.                 }
  410.                 else
  411.                 {
  412.                     currGain -= 0.2;
  413.                 }
  414.             };
  415.             Func<bool, double> gainChangeFuncDel = isPositive =>
  416.              {
  417.                  if (isPositive)
  418.                  {
  419.                      currGain += 0.2;
  420.                  }
  421.                  else
  422.                  {
  423.                      currGain -= 0.2;
  424.                  }
  425.  
  426.                  return Tune;
  427.              };
  428.             onGainChangeAction += gainChangeAction;
  429.             onGainChangeFunc += gainChangeFuncDel;
  430.             onGainChange += gainChangeFunc;
  431.             onGainChange += entropyTunning;
  432.  
  433.             currGain = 0.5;
  434.             tune = 0.85;
  435.             Console.WriteLine("Created Electro-Acustic guitar");
  436.         }
  437.  
  438.         public void switchGain(bool isPositive)
  439.         {
  440.             onGainChange?.Invoke(isPositive);
  441.  
  442.         }
  443.  
  444.         public bool playChord(string chord)
  445.         {
  446.             try
  447.             {
  448.                 if (currGain >= 0.7)
  449.                 {
  450.                     return playPowerChord(chord);
  451.                 }
  452.                 else
  453.                 {
  454.                     return playClassicChord(chord);
  455.                 }
  456.             }
  457.             catch (TunningException err)
  458.             {
  459.                 throw err;
  460.             }
  461.  
  462.         }
  463.         private bool playClassicChord(string chord)
  464.         {
  465.  
  466.             entropyTunning();
  467.             foreach (char ch in chord)
  468.             {
  469.                 if (!classicChordNotes.Contains(ch) && !chordsSymbols.Contains(ch))
  470.                 {
  471.                     Console.WriteLine($"Wrong CHord format or classic guitar couldnt play it[{chord}]");
  472.                     return false;
  473.                 }
  474.             }
  475.  
  476.             if (tune <= 0.5 && tune >= 0.3)
  477.             {
  478.                 Console.WriteLine($"Played classic hord{chord} disgustfully");
  479.  
  480.             }
  481.             else if (tune >= 0.5)
  482.             {
  483.                 Console.WriteLine($"Played classic hord{chord}, what a beautiful chord!");
  484.  
  485.  
  486.             }
  487.             else
  488.             {
  489.                 throw new TunningException("Low Tune", 0.3);
  490.             }
  491.             return true;
  492.  
  493.         }
  494.         private bool playPowerChord(string chord)
  495.         {
  496.             entropyTunning();
  497.  
  498.  
  499.             foreach (char ch in chord)
  500.             {
  501.                 if (!powerChordNotes.Contains(ch) && !chordsSymbols.Contains(ch))
  502.                 {
  503.                     Console.WriteLine($"Wrong CHord format or overdriven guitar couldnt play it[{chord}]");
  504.                     return false;
  505.                 }
  506.             }
  507.  
  508.             if (Tune >= 0.25)
  509.             {
  510.                 Console.WriteLine($"Played power hord{chord}, what a beautiful chord!");
  511.             }
  512.             else
  513.             {
  514.                 throw new TunningException("Low Tune", 0.25);
  515.             }
  516.             return true;
  517.  
  518.         }
  519.  
  520.     }
  521.     class TunningException : Exception
  522.     {
  523.         public double MinTune { get; }
  524.         public TunningException(string message, double val)
  525.             : base(message)
  526.         {
  527.             MinTune = val;
  528.         }
  529.     }
  530.     class Drum : MusInstrument
  531.     {
  532.         private const string availableNotes = "CDE";
  533.  
  534.         static Drum()
  535.         {
  536.             Console.WriteLine("Drum static constr");
  537.         }
  538.         public Drum()
  539.         {
  540.             Console.WriteLine("Im a Drum standart constr");
  541.         }
  542.  
  543.         public override bool playNote(char note)
  544.         {
  545.             if (!availableNotes.ToLower().Contains(Char.ToLower(note)))
  546.             {
  547.                 Console.WriteLine("Drum cant play [" + note + "]");
  548.                 return false;
  549.             }
  550.  
  551.             Console.WriteLine("Drum instrument Played [" + note + "]");
  552.             return true;
  553.         }
  554.  
  555.  
  556.     }
  557.     class ViolinAssemblyEnumerator : IEnumerator
  558.     {
  559.         List<Violin> assem = new List<Violin>();
  560.         int position = -1;
  561.         public ViolinAssemblyEnumerator(List<Violin> first, List<Violin> second, List<Violin> third)
  562.         {
  563.             assem.AddRange(first);
  564.             assem.AddRange(second);
  565.             assem.AddRange(third);
  566.         }
  567.         public object Current
  568.         {
  569.             get
  570.             {
  571.                 if (position == -1 || position >= assem.Count)
  572.                     throw new InvalidOperationException();
  573.                 return assem[position];
  574.             }
  575.         }
  576.  
  577.         public bool MoveNext()
  578.         {
  579.             if (position < assem.Count - 1)
  580.             {
  581.                 position++;
  582.                 return true;
  583.             }
  584.             else
  585.                 return false;
  586.         }
  587.  
  588.         public void Reset()
  589.         {
  590.             position = -1;
  591.         }
  592.     }
  593.     public class ViolinAssembly : IEnumerable
  594.     {
  595.         public int violinsAmount = 5;
  596.  
  597.         private List<Violin> first = new List<Violin>();
  598.         private List<Violin> second = new List<Violin>();
  599.         private List<Violin> third = new List<Violin>();
  600.         public Violin this[string index, int index2]
  601.         {
  602.             get
  603.             {
  604.                 if (index2 >= violinsAmount) index2 = violinsAmount - 1;
  605.  
  606.                 if (index == "first")
  607.                 {
  608.                     return first[index2];
  609.                 }
  610.                 if (index == "second")
  611.                 {
  612.                     return second[index2];
  613.                 }
  614.                 if (index == "third")
  615.                 {
  616.                     return second[index2];
  617.                 }
  618.                 Console.WriteLine("Wrong index");
  619.                 return null;
  620.             }
  621.         }
  622.  
  623.         public List<Violin> this[string index]
  624.         {
  625.             get
  626.             {
  627.                 if (index == "first")
  628.                 {
  629.                     return first;
  630.                 }
  631.                 if (index == "second")
  632.                 {
  633.                     return second;
  634.                 }
  635.                 if (index == "third")
  636.                 {
  637.                     return second;
  638.                 }
  639.                 Console.WriteLine("Wrong index");
  640.                 return null;
  641.  
  642.             }
  643.         }
  644.         public bool addFirst(Violin violin)
  645.         {
  646.             if (first.Count >= violinsAmount)
  647.             {
  648.                 return false;
  649.             }
  650.             first.Add(violin);
  651.             return true;
  652.         }
  653.         public bool addSecond(Violin violin)
  654.         {
  655.             if (second.Count >= violinsAmount)
  656.             {
  657.                 return false;
  658.             }
  659.             second.Add(violin);
  660.             return true;
  661.         }
  662.         public bool addThird(Violin violin)
  663.         {
  664.             if (third.Count >= violinsAmount)
  665.             {
  666.                 return false;
  667.             }
  668.             third.Add(violin);
  669.             return true;
  670.         }
  671.         public IEnumerator GetEnumerator()
  672.         {
  673.             return new ViolinAssemblyEnumerator(first, second, third);
  674.         }
  675.     }
  676.     [Serializable]
  677.     public class Violin : StringInstrument, IComparable
  678.     {
  679.         //[NonSerialized]
  680.         public readonly int stringAmount = 4;
  681.         public int CompareTo(object o)
  682.         {
  683.             Violin v = o as Violin;
  684.             if (this.Tune > v.Tune)
  685.             {
  686.                 return 1;
  687.             }
  688.             else if (this.Tune == v.Tune)
  689.             {
  690.                 return 0;
  691.             }
  692.             else
  693.             {
  694.                 return -1;
  695.             }
  696.  
  697.  
  698.         }
  699.         // Two Static constr from basic class
  700.         //paramethrized from basic class
  701.         public Violin(bool isTuned) : base(isTuned) { }
  702.         public Violin() : base()
  703.         {
  704.         }
  705.         //override methods
  706.         public override bool playNote(char note)
  707.         {
  708.  
  709.             // %tune * 10
  710.             entropyTunning();
  711.  
  712.             if (tune <= 0.7)
  713.             {
  714.                 Console.WriteLine("failed to play [" + note + "], you'd better tune the instrument");
  715.                 return false;
  716.             }
  717.             else
  718.             {
  719.                 Console.WriteLine("Violin played [" + note + "], curr instrument condition: {" + Math.Round(Math.Round(tune, 2) * 100) + "%}");
  720.                 return true;
  721.             }
  722.         }
  723.         public override double updateTune()
  724.         {
  725.             tune = 0.85;
  726.             return tune;
  727.         }
  728.  
  729.         //private constructor
  730.         private Violin(double tune)
  731.         {
  732.             Console.WriteLine("I'm private constructor of violin");
  733.             this.Tune = tune;
  734.         }
  735.         public Violin getDuetPair(double tune)
  736.         {
  737.             if (this.Tune <= tune)
  738.             {
  739.                 return new Violin(tune);
  740.             }
  741.             return new Violin(this.Tune);
  742.  
  743.         }
  744.         private Violin(bool isElite, int price)
  745.         {
  746.             if (isElite)
  747.             {
  748.                 Console.WriteLine("Sold elite violin for " + price);
  749.                 Tune = 0.95;
  750.             }
  751.             else
  752.             {
  753.                 Console.WriteLine("Sold bad violin for " + price);
  754.                 Tune = 0.35;
  755.             }
  756.         }
  757.         static public Violin getEliteViolin(int price)
  758.         {
  759.             bool isElite = price >= 10000;
  760.             return new Violin(isElite, price);
  761.         }
  762.  
  763.     }
  764.     public static class ViolinAssemblyExt
  765.     {
  766.         public static bool ifCanInsertViolinsNumber(this ViolinAssembly va, int number)
  767.         {
  768.             int counter = 0;
  769.             foreach (var el in va)
  770.             {
  771.                 counter++;
  772.             }
  773.             if (number >= va.violinsAmount * 3 - counter) return false;
  774.             return true;
  775.         }
  776.     }
  777.     [Serializable]
  778.     public class duet<T, U> where T : StringInstrument, new()
  779.                             where U : StringInstrument, new()
  780.     {
  781.         public T instrument1;
  782.         public U instrument2;
  783.         public duet()
  784.         {
  785.             instrument1 = new T();
  786.             instrument2 = new U();
  787.         }
  788.         public duet(T t, U u)
  789.         {
  790.             instrument1 = t;
  791.             instrument2 = u;
  792.         }
  793.  
  794.         bool playNote(char note)
  795.         {
  796.             if (instrument1.playNote(note) && instrument2.playNote(note))
  797.             {
  798.                 return true;
  799.             }
  800.             return false;
  801.         }
  802.     }
  803.     [Serializable]
  804.     [DataContract]
  805.     public class Guitar : StringInstrument
  806.     {
  807.         [DataMember]
  808.         public string availableNotes;
  809.         [DataMember]
  810.         public int stringAmount;
  811.         XmlSerializer xml = new XmlSerializer(typeof(Guitar));
  812.         DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(Guitar));
  813.  
  814.         public Guitar() : base()
  815.         {
  816.             stringAmount = 6;
  817.             availableNotes = "ABCDEF";
  818.         }
  819.         public void writeToFileJson(string filename)
  820.         {
  821.             //string json = JsonSerializer.Serialize<Guitar>(this);
  822.             using (FileStream fs = new FileStream($"{filename}.json", FileMode.OpenOrCreate))
  823.             {
  824.                 fs.SetLength(0);
  825.                 json.WriteObject(fs, this);
  826.                 fs.Close();
  827.             }
  828.  
  829.         }
  830.         public void readFromFileJson(string filename)
  831.         {
  832.             using (FileStream fs = new FileStream($"{filename}.json", FileMode.OpenOrCreate))
  833.             {
  834.                 Guitar newGuitar = (Guitar)json.ReadObject(fs);
  835.                 this.stringAmount = newGuitar.stringAmount;
  836.                 this.availableNotes = newGuitar.availableNotes;
  837.             }
  838.         }
  839.  
  840.  
  841.  
  842.         public bool writeToFileXml(string filename)
  843.         {
  844.             using (var fStream = new FileStream($"{filename}.xml", FileMode.Create, FileAccess.Write, FileShare.None))
  845.             {
  846.                 xml.Serialize(fStream, this);
  847.             }
  848.             return true;
  849.         }
  850.  
  851.         public bool readFromFileXml(string filename)
  852.         {
  853.             using (FileStream fs = new FileStream($"{filename}.xml", FileMode.OpenOrCreate))
  854.             {
  855.                 Guitar newGuitar = (Guitar)xml.Deserialize(fs);
  856.                 this.stringAmount = newGuitar.stringAmount;
  857.                 this.availableNotes = newGuitar.availableNotes;
  858.                 Console.WriteLine($"{stringAmount} + {availableNotes}");
  859.                 //this. = newPerson;
  860.                 return true;
  861.             }
  862.         }
  863.         public override bool playNote(char note)
  864.         {
  865.             // %tune * 10
  866.             entropyTunning();
  867.             if (!availableNotes.ToLower().Contains(Char.ToLower(note)))
  868.             {
  869.                 Console.WriteLine("Guitar cant play [" + note + "], curr instrument condition: {" + Math.Round(Math.Round(tune, 2) * 100) + "%}");
  870.                 return false;
  871.             }
  872.             if (tune <= 0.3)
  873.             {
  874.                 Console.WriteLine("failed to play [" + note + "], you'd better tune the instrument");
  875.  
  876.                 return false;
  877.             }
  878.             else
  879.             {
  880.                 Console.WriteLine("Guitar played [" + note + "], curr instrument condition: {" + Math.Round(Math.Round(tune, 2) * 100) + "%}");
  881.                 return true;
  882.             }
  883.         }
  884.  
  885.     }
  886.     class Program
  887.     {
  888.         static void Main(string[] args)
  889.         {
  890.             var test = new ViolinAssembly();
  891.             test.addFirst(new Violin(true));
  892.             test.addSecond(new Violin(true));
  893.             test.addThird(new Violin(true));
  894.             foreach (Violin elem in test)
  895.             {
  896.                 Console.WriteLine(elem);
  897.             }
  898.  
  899.  
  900.             test["first"][0].playNote('B');
  901.             test["first", 0].playNote('C');
  902.  
  903.             Console.WriteLine(test.ifCanInsertViolinsNumber(5));
  904.             Console.WriteLine(test.ifCanInsertViolinsNumber(20));
  905.  
  906.             var test2 = new Guitar();
  907.             var test3 = new Guitar();
  908.             var guitarArr = new Guitar[3];
  909.  
  910.             guitarArr[0] = test2;
  911.             guitarArr[1] = test3;
  912.             Array.Sort(guitarArr);
  913.             //XmlSerializer xml = new XmlSerializer(typeof(Guitar[]));
  914.  
  915.             //using (var fStream = new FileStream("vovalox.xml", FileMode.Create, FileAccess.Write, FileShare.None))
  916.             //{
  917.             //    xml.Serialize(fStream, guitarArr);
  918.             //}
  919.             //test2.readFromFileXml("guitar");
  920.             ////Console.WriteLine(test2.availableNotes);
  921.  
  922.  
  923.             //await test2.writeToFileJson("./guit1");
  924.             //await test2.readFromFileJson("./guit1");
  925.  
  926.  
  927.             //using (FileStream fs = new FileStream("user.json", FileMode.OpenOrCreate))
  928.             //{
  929.             //    await JsonSerializer.SerializeAsync<Guitar>(fs, test2);
  930.             //    Console.WriteLine("Data has been saved to file");
  931.             //}
  932.             //using (FileStream fs = new FileStream("user.json", FileMode.OpenOrCreate))
  933.             //{
  934.             //    Guitar restoredPerson = await JsonSerializer.DeserializeAsync<Guitar>(fs);
  935.             //}
  936.  
  937.             test2.writeToFileJson("rema");
  938.  
  939.         }
  940.     }
  941. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement