Advertisement
Guest User

Tips

a guest
Nov 22nd, 2019
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace tipstips
  13. {
  14. public partial class Form1 : Form
  15. {
  16. BokLoader bokLoader = new BokLoader();
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21. private void button1_Click(object sender, EventArgs e)
  22. {
  23. Bok slumpBok = bokLoader.SlumpBok();
  24. }
  25. private void label1_Click(object sender, EventArgs e)
  26. {
  27. slumpBok.Text =
  28. }
  29. }
  30. public class BokLoader
  31. {
  32. List<Bok> böcker = new List<Bok>();
  33. public BokLoader()
  34. {
  35. List<string> itemSaver = new List<string>();
  36. if (File.Exists("texter.txt"))
  37. {
  38. StreamReader reader = new StreamReader("texter.txt", Encoding.Default, false);
  39.  
  40. string item = "";
  41. while ((item = reader.ReadLine()) != null)
  42. {
  43. itemSaver.Add(item);
  44. }
  45. foreach (string a in itemSaver)
  46. {
  47. string[] vektor = a.Split(new string[] { "###" }, StringSplitOptions.None);
  48. string titel = vektor[0];
  49. string författare = vektor[1];
  50. string typ = vektor[2];
  51. bool tillgänglig = Convert.ToBoolean(vektor[3]);
  52.  
  53. Bok bok;
  54. switch (typ)
  55. {
  56. case "Roman":
  57. bok = new Roman(titel, författare, tillgänglig);
  58. break;
  59.  
  60. case "Tidskrift":
  61. bok = new Tidskrift(titel, författare, tillgänglig);
  62. break;
  63.  
  64. default:
  65. bok = new Novellsamling(titel, författare, tillgänglig);
  66. break;
  67. }
  68. böcker.Add(bok);
  69. }
  70. }
  71. }
  72. public Bok SlumpBok()
  73. {
  74. Random slump = new Random();
  75. int antal = böcker.Count();
  76. int slumpatIndex = slump.Next(0, antal);
  77.  
  78. Bok slumpatBok = böcker[slumpatIndex];
  79.  
  80. return slumpatBok;
  81. }
  82. }
  83. public class Bok
  84. {
  85. public static int BokVal;
  86. //egenskaper
  87. public string titel;
  88. public string författare;
  89. public string typ;
  90. public bool tillgänglig;
  91. // konstruktor
  92. public Bok(string titel, string författare, string typ, bool tillgänglig)
  93. {
  94. this.titel = titel;
  95. this.författare = författare;
  96. this.typ = typ;
  97. this.tillgänglig = tillgänglig;
  98. }
  99. public override string ToString()
  100. {
  101. if (tillgänglig)
  102. {
  103. return titel + " " + författare + " " + typ + " " + "tillgänglig";
  104. }
  105. else
  106. {
  107. return titel + " " + författare + " " + typ + " " + "otillgänglig";
  108. }
  109. }
  110. }
  111. public class Roman : Bok
  112. {
  113. public Roman(string titel, string författare, bool tillgänglig) : base(titel, författare, "Roman", tillgänglig){}
  114. }
  115. public class Tidskrift : Bok
  116. {
  117. public Tidskrift(string titel, string författare, bool tillgänglig) : base(titel, författare, "Tidskrift", tillgänglig){}
  118. }
  119. public class Novellsamling : Bok
  120. {
  121. public Novellsamling(string titel, string författare, bool tillgänglig) : base(titel, författare, "Novellsamling", tillgänglig){}
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement