Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.IO;
- using System.Xml.Serialization;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Json;
- namespace lab3
- {
- [DataContract]
- public abstract class MusInstrument
- {
- abstract public bool playNote(char note);
- }
- [DataContract]
- public class StringInstrument : MusInstrument
- {
- protected double tune;
- protected static double tunningCoef;
- protected static double TunningCoef
- {
- get { return tunningCoef; }
- set
- {
- if (value <= 0 || value > 3)
- {
- tunningCoef = 3;
- }
- }
- }
- protected virtual double Tune
- {
- get { return tune; }
- set
- {
- if (value >= 0.95)
- {
- tune = 0.9;
- }
- else if (value <= 0.10)
- {
- tune = 0.15;
- }
- else
- {
- tune = value;
- }
- }
- }
- public virtual double updateTune()
- {
- tune = 0.75;
- return tune;
- }
- protected virtual double entropyTunning()
- {
- Random rnd = new Random();
- if (rnd.Next(0, 10) < 6)
- {
- Tune -= rnd.Next(0, 100) / Math.Pow(10, TunningCoef + 3);
- //Console.WriteLine(ind);
- }
- else
- {
- Tune += rnd.Next(0, 100) / Math.Pow(10, TunningCoef + 3) / 3;
- //Console.WriteLine(ind);
- }
- return tune;
- }
- //CONSTRUCTORS
- //One static is inherited from musInstrument
- static StringInstrument()
- {
- TunningCoef = 1;
- Console.WriteLine("Im static constr StringInstr!");
- }
- public StringInstrument()
- {
- Tune = 0.7;
- }
- public StringInstrument(bool isTuned)
- {
- if (isTuned)
- {
- tune = 0.9;
- }
- else
- {
- tune = 0.3;
- }
- }
- // override methods
- public override bool playNote(char note)
- {
- entropyTunning();
- if (tune <= 0.5)
- {
- Console.WriteLine("failed to play [" + note + "], you'd better tune the instrument");
- return false;
- }
- else
- {
- Console.WriteLine("String instrument Played [" + note + "], curr instrument condition: {" + Math.Round(Math.Round(tune, 2) * 100) + "%}");
- return true;
- }
- }
- }
- interface IElectroGuitar
- {
- private bool playPowerChord(string chord)
- {
- return true;
- }
- }
- interface IClassicGuitar
- {
- private bool playClassicChord(string chord)
- {
- return true;
- }
- bool playGamma(string firstNote)
- {
- Console.WriteLine("Classic guitar Gamma ");
- return true;
- }
- }
- interface IUkulele
- {
- bool playGamma(string firstNote)
- {
- Console.WriteLine("Ukulele Gamm ");
- return true;
- }
- }
- class Ukulele : Guitar, IUkulele, IClassicGuitar
- {
- private const string classicChordNotes = "ABCDEFGabcdefg";
- private const string chordsSymbols = "#+7mM";
- public Ukulele()
- {
- tune = 0.85;
- availableNotes = "ABC";
- stringAmount = 4;
- }
- bool IUkulele.playGamma(string firstNote)
- {
- if (availableNotes.Contains(firstNote))
- {
- if (tune <= 0.5 && tune >= 0.3)
- {
- Console.WriteLine("Ukulele pleyed the gamm");
- }
- else
- {
- throw new TunningException("Low Tune", 0.3);
- }
- return true;
- }
- else
- {
- return false;
- }
- }
- private bool playClassicChord(string chord)
- {
- entropyTunning();
- foreach (char ch in chord)
- {
- if (!classicChordNotes.Contains(ch) && !chordsSymbols.Contains(ch))
- {
- Console.WriteLine($"Wrong CHord format or classic guitar couldnt play it[{chord}]");
- return false;
- }
- }
- if (tune <= 0.5 && tune >= 0.3)
- {
- Console.WriteLine($"Played classic hord{chord} disgustfully");
- }
- else if (tune >= 0.5)
- {
- Console.WriteLine($"Played classic hord{chord}, what a beautiful chord!");
- }
- else
- {
- throw new TunningException("Low Tune", 0.3);
- }
- return true;
- }
- public bool playChord(string chord)
- {
- try
- {
- return playClassicChord(chord);
- }
- catch (TunningException err)
- {
- throw err;
- }
- }
- }
- class ClassicGuitar : Guitar, IClassicGuitar
- {
- private const string classicChordNotes = "ABCDEFGabcdefg";
- private const string chordsSymbols = "#+7mM";
- public ClassicGuitar()
- {
- tune = 0.8;
- Console.WriteLine("I'm classic Guitar constr!");
- }
- private bool playClassicChord(string chord)
- {
- entropyTunning();
- foreach (char ch in chord)
- {
- if (!classicChordNotes.Contains(ch) && !chordsSymbols.Contains(ch))
- {
- Console.WriteLine($"Wrong CHord format or classic guitar couldnt play it[{chord}]");
- return false;
- }
- }
- if (tune <= 0.5 && tune >= 0.3)
- {
- Console.WriteLine($"Played classic hord{chord} disgustfully");
- }
- else if (tune >= 0.5)
- {
- Console.WriteLine($"Played classic hord{chord}, what a beautiful chord!");
- }
- else
- {
- throw new TunningException("Low Tune", 0.3);
- }
- return true;
- }
- public bool playChord(string chord)
- {
- try
- {
- return playClassicChord(chord);
- }
- catch (TunningException err)
- {
- throw err;
- }
- }
- }
- class ElectroGuitar : Guitar, IElectroGuitar
- {
- const string powerChordNotes = "ABCDEabcde";
- const string chordsSymbols = "#+b";
- public ElectroGuitar()
- {
- tune = 0.8;
- Console.WriteLine("I'm electroGuitar constr!");
- }
- private bool playPowerChord(string chord)
- {
- entropyTunning();
- foreach (char ch in chord)
- {
- if (!powerChordNotes.Contains(ch) && !chordsSymbols.Contains(ch))
- {
- Console.WriteLine($"Wrong CHord format or overdriven guitar couldnt play it[{chord}]");
- return false;
- }
- }
- if (Tune >= 0.25)
- {
- Console.WriteLine($"Played power hord{chord}, what a beautiful chord!");
- }
- else
- {
- throw new TunningException("low tunning", 0.25);
- }
- return true;
- }
- public bool playChord(string chord)
- {
- try
- {
- return playPowerChord(chord);
- }
- catch (TunningException err)
- {
- throw err;
- }
- }
- }
- class ElectroAcusticGuitar : Guitar, IClassicGuitar, IElectroGuitar
- {
- private const string classicChordNotes = "ABCDEFGabcdefg";
- private const string chordsSymbols = "#+7mM";
- const string powerChordNotes = "ABCDEabcde";
- private double _currGain;
- private double currGain
- {
- get { return _currGain; }
- set
- {
- if (value >= 0.9)
- {
- _currGain = 0.9;
- }
- else if (value <= 0.3)
- {
- _currGain = 0.3;
- }
- }
- }
- private delegate double OnGainChangeDelegate(bool isPositive);
- private event OnGainChangeDelegate onGainChange;
- private event Action<bool> onGainChangeAction;
- private event Func<bool, double> onGainChangeFunc;
- protected double entropyTunning(bool isPositive)
- {
- if (isPositive)
- {
- Tune += 0.1;
- }
- else
- {
- Tune -= 0.1;
- }
- return base.entropyTunning();
- }
- private double changeGain(bool isPositive)
- {
- if (isPositive)
- {
- currGain += 0.2;
- }
- else
- {
- currGain -= 0.2;
- }
- return Tune;
- }
- public ElectroAcusticGuitar()
- {
- //ANONYMUS
- //gainChangeDelegate onGainChangeFunc = delegate(bool isPositive)
- //{
- // if (isPositive)
- // {
- // currGain += 0.2;
- // }
- // else
- // {
- // currGain -= 0.2;
- // }
- // return Tune;
- //};
- //LAMBDA
- OnGainChangeDelegate gainChangeFunc = isPositive =>
- {
- if (isPositive)
- {
- currGain += 0.2;
- }
- else
- {
- currGain -= 0.2;
- }
- return Tune;
- };
- Action<bool> gainChangeAction = isPositive =>
- {
- if (isPositive)
- {
- currGain += 0.2;
- }
- else
- {
- currGain -= 0.2;
- }
- };
- Func<bool, double> gainChangeFuncDel = isPositive =>
- {
- if (isPositive)
- {
- currGain += 0.2;
- }
- else
- {
- currGain -= 0.2;
- }
- return Tune;
- };
- onGainChangeAction += gainChangeAction;
- onGainChangeFunc += gainChangeFuncDel;
- onGainChange += gainChangeFunc;
- onGainChange += entropyTunning;
- currGain = 0.5;
- tune = 0.85;
- Console.WriteLine("Created Electro-Acustic guitar");
- }
- public void switchGain(bool isPositive)
- {
- onGainChange?.Invoke(isPositive);
- }
- public bool playChord(string chord)
- {
- try
- {
- if (currGain >= 0.7)
- {
- return playPowerChord(chord);
- }
- else
- {
- return playClassicChord(chord);
- }
- }
- catch (TunningException err)
- {
- throw err;
- }
- }
- private bool playClassicChord(string chord)
- {
- entropyTunning();
- foreach (char ch in chord)
- {
- if (!classicChordNotes.Contains(ch) && !chordsSymbols.Contains(ch))
- {
- Console.WriteLine($"Wrong CHord format or classic guitar couldnt play it[{chord}]");
- return false;
- }
- }
- if (tune <= 0.5 && tune >= 0.3)
- {
- Console.WriteLine($"Played classic hord{chord} disgustfully");
- }
- else if (tune >= 0.5)
- {
- Console.WriteLine($"Played classic hord{chord}, what a beautiful chord!");
- }
- else
- {
- throw new TunningException("Low Tune", 0.3);
- }
- return true;
- }
- private bool playPowerChord(string chord)
- {
- entropyTunning();
- foreach (char ch in chord)
- {
- if (!powerChordNotes.Contains(ch) && !chordsSymbols.Contains(ch))
- {
- Console.WriteLine($"Wrong CHord format or overdriven guitar couldnt play it[{chord}]");
- return false;
- }
- }
- if (Tune >= 0.25)
- {
- Console.WriteLine($"Played power hord{chord}, what a beautiful chord!");
- }
- else
- {
- throw new TunningException("Low Tune", 0.25);
- }
- return true;
- }
- }
- class TunningException : Exception
- {
- public double MinTune { get; }
- public TunningException(string message, double val)
- : base(message)
- {
- MinTune = val;
- }
- }
- class Drum : MusInstrument
- {
- private const string availableNotes = "CDE";
- static Drum()
- {
- Console.WriteLine("Drum static constr");
- }
- public Drum()
- {
- Console.WriteLine("Im a Drum standart constr");
- }
- public override bool playNote(char note)
- {
- if (!availableNotes.ToLower().Contains(Char.ToLower(note)))
- {
- Console.WriteLine("Drum cant play [" + note + "]");
- return false;
- }
- Console.WriteLine("Drum instrument Played [" + note + "]");
- return true;
- }
- }
- class ViolinAssemblyEnumerator : IEnumerator
- {
- List<Violin> assem = new List<Violin>();
- int position = -1;
- public ViolinAssemblyEnumerator(List<Violin> first, List<Violin> second, List<Violin> third)
- {
- assem.AddRange(first);
- assem.AddRange(second);
- assem.AddRange(third);
- }
- public object Current
- {
- get
- {
- if (position == -1 || position >= assem.Count)
- throw new InvalidOperationException();
- return assem[position];
- }
- }
- public bool MoveNext()
- {
- if (position < assem.Count - 1)
- {
- position++;
- return true;
- }
- else
- return false;
- }
- public void Reset()
- {
- position = -1;
- }
- }
- public class ViolinAssembly : IEnumerable
- {
- public int violinsAmount = 5;
- private List<Violin> first = new List<Violin>();
- private List<Violin> second = new List<Violin>();
- private List<Violin> third = new List<Violin>();
- public Violin this[string index, int index2]
- {
- get
- {
- if (index2 >= violinsAmount) index2 = violinsAmount - 1;
- if (index == "first")
- {
- return first[index2];
- }
- if (index == "second")
- {
- return second[index2];
- }
- if (index == "third")
- {
- return second[index2];
- }
- Console.WriteLine("Wrong index");
- return null;
- }
- }
- public List<Violin> this[string index]
- {
- get
- {
- if (index == "first")
- {
- return first;
- }
- if (index == "second")
- {
- return second;
- }
- if (index == "third")
- {
- return second;
- }
- Console.WriteLine("Wrong index");
- return null;
- }
- }
- public bool addFirst(Violin violin)
- {
- if (first.Count >= violinsAmount)
- {
- return false;
- }
- first.Add(violin);
- return true;
- }
- public bool addSecond(Violin violin)
- {
- if (second.Count >= violinsAmount)
- {
- return false;
- }
- second.Add(violin);
- return true;
- }
- public bool addThird(Violin violin)
- {
- if (third.Count >= violinsAmount)
- {
- return false;
- }
- third.Add(violin);
- return true;
- }
- public IEnumerator GetEnumerator()
- {
- return new ViolinAssemblyEnumerator(first, second, third);
- }
- }
- [Serializable]
- public class Violin : StringInstrument, IComparable
- {
- //[NonSerialized]
- public readonly int stringAmount = 4;
- public int CompareTo(object o)
- {
- Violin v = o as Violin;
- if (this.Tune > v.Tune)
- {
- return 1;
- }
- else if (this.Tune == v.Tune)
- {
- return 0;
- }
- else
- {
- return -1;
- }
- }
- // Two Static constr from basic class
- //paramethrized from basic class
- public Violin(bool isTuned) : base(isTuned) { }
- public Violin() : base()
- {
- }
- //override methods
- public override bool playNote(char note)
- {
- // %tune * 10
- entropyTunning();
- if (tune <= 0.7)
- {
- Console.WriteLine("failed to play [" + note + "], you'd better tune the instrument");
- return false;
- }
- else
- {
- Console.WriteLine("Violin played [" + note + "], curr instrument condition: {" + Math.Round(Math.Round(tune, 2) * 100) + "%}");
- return true;
- }
- }
- public override double updateTune()
- {
- tune = 0.85;
- return tune;
- }
- //private constructor
- private Violin(double tune)
- {
- Console.WriteLine("I'm private constructor of violin");
- this.Tune = tune;
- }
- public Violin getDuetPair(double tune)
- {
- if (this.Tune <= tune)
- {
- return new Violin(tune);
- }
- return new Violin(this.Tune);
- }
- private Violin(bool isElite, int price)
- {
- if (isElite)
- {
- Console.WriteLine("Sold elite violin for " + price);
- Tune = 0.95;
- }
- else
- {
- Console.WriteLine("Sold bad violin for " + price);
- Tune = 0.35;
- }
- }
- static public Violin getEliteViolin(int price)
- {
- bool isElite = price >= 10000;
- return new Violin(isElite, price);
- }
- }
- public static class ViolinAssemblyExt
- {
- public static bool ifCanInsertViolinsNumber(this ViolinAssembly va, int number)
- {
- int counter = 0;
- foreach (var el in va)
- {
- counter++;
- }
- if (number >= va.violinsAmount * 3 - counter) return false;
- return true;
- }
- }
- [Serializable]
- public class duet<T, U> where T : StringInstrument, new()
- where U : StringInstrument, new()
- {
- public T instrument1;
- public U instrument2;
- public duet()
- {
- instrument1 = new T();
- instrument2 = new U();
- }
- public duet(T t, U u)
- {
- instrument1 = t;
- instrument2 = u;
- }
- bool playNote(char note)
- {
- if (instrument1.playNote(note) && instrument2.playNote(note))
- {
- return true;
- }
- return false;
- }
- }
- [Serializable]
- [DataContract]
- public class Guitar : StringInstrument
- {
- [DataMember]
- public string availableNotes;
- [DataMember]
- public int stringAmount;
- XmlSerializer xml = new XmlSerializer(typeof(Guitar));
- DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(Guitar));
- public Guitar() : base()
- {
- stringAmount = 6;
- availableNotes = "ABCDEF";
- }
- public void writeToFileJson(string filename)
- {
- //string json = JsonSerializer.Serialize<Guitar>(this);
- using (FileStream fs = new FileStream($"{filename}.json", FileMode.OpenOrCreate))
- {
- fs.SetLength(0);
- json.WriteObject(fs, this);
- fs.Close();
- }
- }
- public void readFromFileJson(string filename)
- {
- using (FileStream fs = new FileStream($"{filename}.json", FileMode.OpenOrCreate))
- {
- Guitar newGuitar = (Guitar)json.ReadObject(fs);
- this.stringAmount = newGuitar.stringAmount;
- this.availableNotes = newGuitar.availableNotes;
- }
- }
- public bool writeToFileXml(string filename)
- {
- using (var fStream = new FileStream($"{filename}.xml", FileMode.Create, FileAccess.Write, FileShare.None))
- {
- xml.Serialize(fStream, this);
- }
- return true;
- }
- public bool readFromFileXml(string filename)
- {
- using (FileStream fs = new FileStream($"{filename}.xml", FileMode.OpenOrCreate))
- {
- Guitar newGuitar = (Guitar)xml.Deserialize(fs);
- this.stringAmount = newGuitar.stringAmount;
- this.availableNotes = newGuitar.availableNotes;
- Console.WriteLine($"{stringAmount} + {availableNotes}");
- //this. = newPerson;
- return true;
- }
- }
- public override bool playNote(char note)
- {
- // %tune * 10
- entropyTunning();
- if (!availableNotes.ToLower().Contains(Char.ToLower(note)))
- {
- Console.WriteLine("Guitar cant play [" + note + "], curr instrument condition: {" + Math.Round(Math.Round(tune, 2) * 100) + "%}");
- return false;
- }
- if (tune <= 0.3)
- {
- Console.WriteLine("failed to play [" + note + "], you'd better tune the instrument");
- return false;
- }
- else
- {
- Console.WriteLine("Guitar played [" + note + "], curr instrument condition: {" + Math.Round(Math.Round(tune, 2) * 100) + "%}");
- return true;
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- var test = new ViolinAssembly();
- test.addFirst(new Violin(true));
- test.addSecond(new Violin(true));
- test.addThird(new Violin(true));
- foreach (Violin elem in test)
- {
- Console.WriteLine(elem);
- }
- test["first"][0].playNote('B');
- test["first", 0].playNote('C');
- Console.WriteLine(test.ifCanInsertViolinsNumber(5));
- Console.WriteLine(test.ifCanInsertViolinsNumber(20));
- var test2 = new Guitar();
- var test3 = new Guitar();
- var guitarArr = new Guitar[3];
- guitarArr[0] = test2;
- guitarArr[1] = test3;
- Array.Sort(guitarArr);
- //XmlSerializer xml = new XmlSerializer(typeof(Guitar[]));
- //using (var fStream = new FileStream("vovalox.xml", FileMode.Create, FileAccess.Write, FileShare.None))
- //{
- // xml.Serialize(fStream, guitarArr);
- //}
- //test2.readFromFileXml("guitar");
- ////Console.WriteLine(test2.availableNotes);
- //await test2.writeToFileJson("./guit1");
- //await test2.readFromFileJson("./guit1");
- //using (FileStream fs = new FileStream("user.json", FileMode.OpenOrCreate))
- //{
- // await JsonSerializer.SerializeAsync<Guitar>(fs, test2);
- // Console.WriteLine("Data has been saved to file");
- //}
- //using (FileStream fs = new FileStream("user.json", FileMode.OpenOrCreate))
- //{
- // Guitar restoredPerson = await JsonSerializer.DeserializeAsync<Guitar>(fs);
- //}
- test2.writeToFileJson("rema");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement