Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace tipstips
- {
- public partial class Form1 : Form
- {
- BokLoader bokLoader = new BokLoader();
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Bok slumpBok = bokLoader.SlumpBok();
- }
- private void label1_Click(object sender, EventArgs e)
- {
- slumpBok.Text =
- }
- }
- public class BokLoader
- {
- List<Bok> böcker = new List<Bok>();
- public BokLoader()
- {
- List<string> itemSaver = new List<string>();
- if (File.Exists("texter.txt"))
- {
- StreamReader reader = new StreamReader("texter.txt", Encoding.Default, false);
- string item = "";
- while ((item = reader.ReadLine()) != null)
- {
- itemSaver.Add(item);
- }
- foreach (string a in itemSaver)
- {
- string[] vektor = a.Split(new string[] { "###" }, StringSplitOptions.None);
- string titel = vektor[0];
- string författare = vektor[1];
- string typ = vektor[2];
- bool tillgänglig = Convert.ToBoolean(vektor[3]);
- Bok bok;
- switch (typ)
- {
- case "Roman":
- bok = new Roman(titel, författare, tillgänglig);
- break;
- case "Tidskrift":
- bok = new Tidskrift(titel, författare, tillgänglig);
- break;
- default:
- bok = new Novellsamling(titel, författare, tillgänglig);
- break;
- }
- böcker.Add(bok);
- }
- }
- }
- public Bok SlumpBok()
- {
- Random slump = new Random();
- int antal = böcker.Count();
- int slumpatIndex = slump.Next(0, antal);
- Bok slumpatBok = böcker[slumpatIndex];
- return slumpatBok;
- }
- }
- public class Bok
- {
- public static int BokVal;
- //egenskaper
- public string titel;
- public string författare;
- public string typ;
- public bool tillgänglig;
- // konstruktor
- public Bok(string titel, string författare, string typ, bool tillgänglig)
- {
- this.titel = titel;
- this.författare = författare;
- this.typ = typ;
- this.tillgänglig = tillgänglig;
- }
- public override string ToString()
- {
- if (tillgänglig)
- {
- return titel + " " + författare + " " + typ + " " + "tillgänglig";
- }
- else
- {
- return titel + " " + författare + " " + typ + " " + "otillgänglig";
- }
- }
- }
- public class Roman : Bok
- {
- public Roman(string titel, string författare, bool tillgänglig) : base(titel, författare, "Roman", tillgänglig){}
- }
- public class Tidskrift : Bok
- {
- public Tidskrift(string titel, string författare, bool tillgänglig) : base(titel, författare, "Tidskrift", tillgänglig){}
- }
- public class Novellsamling : Bok
- {
- public Novellsamling(string titel, string författare, bool tillgänglig) : base(titel, författare, "Novellsamling", tillgänglig){}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement