ztp4

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.4 kB
1
Indexable
Never
//BIBLIOTEKA.CS

class Biblioteka : IObserver
{
    private List<Utwor> songs;
    private double totalCost;
    public double costPerPlay;

    public Biblioteka()
    {
        this.songs = new List<Utwor>();
        this.totalCost = 0;
        this.costPerPlay = 0.1;
    }

    public void AddSong(Utwor song)
    {
        songs.Add(song);
        song.RegisterObserver(this);
    }

    public void RemoveSong(Utwor song)
    {
        songs.Remove(song);
        song.RemoveObserver(this);
    }

    public Utwor GetSong(int index)
    {
        return songs[index];
    }

    public void Update(IObservable observable)
    {
        if (observable is Utwor)
        {
            totalCost += costPerPlay;
            Console.WriteLine("Aktualna cena za odtworzenia: " + totalCost);
        }
    }
}


//CENNIK.CS

class PriceList : IObserver
{
    private double costPerPlay;

    public PriceList()
    {
        this.costPerPlay = 0.1;
    }

    public double CostPerPlay { get => costPerPlay; set => costPerPlay = value; }

    public void Update(IObservable observable)
    {
        if (observable is Biblioteka)
        {
            ((Biblioteka)observable).costPerPlay = costPerPlay;
        }
    }
}


//UTWOR.CS

using System;
using System.Collections.Generic;

// Interfejs dla obiektów obserwowanych
interface IObservable
{
    void RegisterObserver(IObserver observer);
    void RemoveObserver(IObserver observer);
    void NotifyObservers();
}

// Interfejs dla obiektów obserwujących
interface IObserver
{
    void Update(IObservable observable);
}

class Utwor : IObservable
{
    private string title;
    private string artist;
    private double duration;
    private double play_cost;
    private List<IObserver> observers;

    public Utwor(string title, string artist, double duration, double play_cost)
    {
        this.title = title;
        this.artist = artist;
        this.duration = duration;
        this.play_cost = play_cost;
        this.observers = new List<IObserver>();
    }

    public string Title { get => title; set => title = value; }
    public string Artist { get => artist; set => artist = value; }
    public double Duration { get => duration; set => duration = value; }

    public double Play_Cost { get => play_cost; set => Play_Cost = value; }

    public void Play()
    {
        Console.WriteLine("Odtwarzam utwór: " + Title + " wykonawcy " + Artist + " trwający " + Duration + " sekund. Odtworzenie kosztowało: " + Play_Cost + "zł.");
        NotifyObservers();
    }

    public void RegisterObserver(IObserver observer)
    {
        observers.Add(observer);
    }

    public void RemoveObserver(IObserver observer)
    {
        observers.Remove(observer);
    }

    public void NotifyObservers()
    {
        foreach (IObserver observer in observers)
            observer.Update(this);
    }
}


//PROGRAM.CS

using System;

class Program
{
    static void Main(string[] args)
    {
        Utwor w1 = new Utwor("Tytul", "Artysta", 3.5, 1);
        Utwor w2 = new Utwor("Drugi", "yyyy", 7.00, 2);
        Utwor w3 = new Utwor("Trzeci", "vvvv", 2.00, 3);
        Utwor w4 = new Utwor("Czwarty", "zzzz", 11.00, 4);

        Biblioteka biblioteka = new Biblioteka();
        biblioteka.AddSong(w1);
        biblioteka.AddSong(w4);
        //biblioteka.removeSong(w1);
        w1.Play();
        w2.Play();
        w3.Play();
        w4.Play();
    }
}