Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace zad1 {
  5.     //Polimorfizm, wywołując metodę Pole() automatycznie policzy dla dobrej figury
  6.     abstract class Figura {
  7.         public abstract int Pole();
  8.     }
  9.  
  10.     class Kwadrat : Figura {
  11.         int a;
  12.         public Kwadrat(int a) {
  13.             this.a = a;
  14.         }
  15.         public override int Pole() {
  16.             return a * a;
  17.         }
  18.     }
  19.  
  20.     class Prostokat : Figura {
  21.         int a, b;
  22.         public Prostokat(int a, int b) {
  23.             this.a = a;
  24.             this.b = b;
  25.         }
  26.         public override int Pole() {
  27.             return a * b;
  28.         }
  29.     }
  30.  
  31.     //Low coupling - mała zależność między MovieFan, MovieStore, Movie
  32.  
  33.     class MovieFan {
  34.         public void PickAMovie(string movieTitle, MovieStore ms) {
  35.             List<string> titles = ms.GetAvailableTitles();
  36.             if (titles.Contains(movieTitle)) {
  37.                 Console.WriteLine("We got this!");
  38.             } else {
  39.                 Console.WriteLine("Oops");
  40.             }
  41.  
  42.         }
  43.     }
  44.  
  45.     class MovieStore {
  46.         List<Movie> availableMovies;
  47.         public MovieStore() {
  48.             availableMovies = new List<Movie>();
  49.         }
  50.         public List<string> GetAvailableTitles() {
  51.             List<string> res = new List<string>();
  52.             foreach (var movie in availableMovies) {
  53.                 res.Add(movie.title);
  54.             }
  55.             return res;
  56.         }
  57.     }
  58.  
  59.     class Movie {
  60.         public string title, genre;
  61.         int yearOfProduction;
  62.         public Movie(string title, string genre, int yearOfProduction) {
  63.             this.title = title;
  64.             this.genre = genre;
  65.             this.yearOfProduction = yearOfProduction;
  66.         }
  67.     }
  68.     class User {
  69.         string username;
  70.         List<Movie> movies;
  71.         public User(string username) {
  72.             this.username = username;
  73.             movies = new List<Movie>();
  74.         }
  75.         //Użytkownik ma swoje ulubione filmy, w dodatku "agreguje" je, więc to on tworzy obiekty Movie
  76.         public void AddMovieToFavourites(string title, string genre, int yearOfProduction) {
  77.             Movie movie = new Movie(title, genre, yearOfProduction);
  78.             movies.Add(movie);
  79.         }
  80.         //Użytkownik ma listę filmów, więc ma wiedzę ile ich jest            
  81.         public int GetNumOfFavoouriteMovies() {
  82.             return movies.Count;
  83.         }
  84.  
  85.         public void PrintFavourites() {
  86.             foreach (var movie in movies) {
  87.                 Console.WriteLine($"{movie.title}, {movie.genre}");
  88.             }
  89.         }
  90.     }
  91.  
  92.     class Program {
  93.         static void Main(string[] args) {
  94.             User user = new User("igor");
  95.             user.AddMovieToFavourites("Mulan", "animowany", 1998);
  96.             user.PrintFavourites();
  97.  
  98.             Figura kwadrat = new Kwadrat(10);
  99.             Console.WriteLine(kwadrat.Pole());
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement