Advertisement
eniodordan

[OOP] LV7 - Zadaci + Analiza

Jan 16th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.10 KB | None | 0 0
  1. // Zadaci
  2. // 1. zadatak
  3. namespace LV7___Zadatak_1
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             List<IPayable> list = new List<IPayable>();
  10.             BankAccount B1 = new BankAccount("Enio", 1000, 0.0);
  11.             BankAccount B2 = new BankAccount("Petar", 2000, 500);
  12.             MobileAccount P1 = new MobileAccount("0992051444", 2.5, 50);
  13.             MobileAccount P2 = new MobileAccount("0976432454", 1.4, 100);
  14.  
  15.             list.Add(B1);
  16.             list.Add(B2);
  17.             list.Add(P1);
  18.             list.Add(P2);
  19.  
  20.             Random rnd = new Random();
  21.             foreach (IPayable account in list)
  22.             {
  23.                 account.SendPayment(rnd.Next(10, 100));
  24.             }
  25.             foreach (IPayable account in list)
  26.             {
  27.                 account.RequestPayment(rnd.Next(1, 100));
  28.             }
  29.             foreach (IPayable account in list)
  30.             {
  31.                 Console.WriteLine("Balance: " + account.GetBalance() + "$");
  32.             }
  33.         }
  34.     }
  35.  
  36.     interface IPayable
  37.     {
  38.         double GetBalance();
  39.         void SendPayment(double payment);
  40.         void RequestPayment(double payment);
  41.     }
  42.  
  43.     public class BankAccount : IPayable
  44.     {
  45.         private string mName;
  46.         private double mLimit;
  47.         private double mBalance;
  48.  
  49.         public BankAccount(string name, double limit, double balance)
  50.         {
  51.             this.mName = name;
  52.             this.mLimit = limit;
  53.             this.mBalance = balance;
  54.         }
  55.  
  56.         public double GetBalance()
  57.         {
  58.             return this.mBalance;
  59.         }
  60.  
  61.         public void SendPayment(double payment)
  62.         {
  63.             this.mBalance += payment;
  64.         }
  65.  
  66.         public void RequestPayment(double payment)
  67.         {
  68.             double balance = this.mBalance;
  69.  
  70.             balance -= payment;
  71.  
  72.             if (balance > mLimit)
  73.             {
  74.                 this.mBalance += payment;
  75.             }
  76.             else
  77.             {
  78.                 Console.WriteLine("Error!");
  79.             }
  80.         }
  81.     }
  82.  
  83.     public class MobileAccount : IPayable
  84.     {
  85.         private string mNumber;
  86.         private double mPrice;
  87.         private double mBalance;
  88.  
  89.         public MobileAccount(string number, double price, double balance)
  90.         {
  91.             this.mNumber = number;
  92.             this.mPrice = price;
  93.             this.mBalance = balance;
  94.         }
  95.  
  96.         public double GetBalance()
  97.         {
  98.             return this.mBalance;
  99.         }
  100.  
  101.         public void SendPayment(double payment)
  102.         {
  103.             this.mBalance += payment;
  104.         }
  105.  
  106.         public void RequestPayment(double payment)
  107.         {
  108.             double balance = this.mBalance;
  109.  
  110.             balance -= payment;
  111.  
  112.             if (balance > 0)
  113.             {
  114.                 this.mBalance += payment;
  115.             }
  116.             else
  117.             {
  118.                 Console.WriteLine("Error!");
  119.             }
  120.         }
  121.     }
  122. }
  123.  
  124. // 2. zadatak
  125. namespace LV7___Zadatak_2
  126. {
  127.     public partial class Form1 : Form
  128.     {
  129.         Graphics g;
  130.         Pen p;
  131.  
  132.         public Form1()
  133.         {
  134.             InitializeComponent();
  135.             g = pbDrawing.CreateGraphics();
  136.         }
  137.  
  138.         private void pbDrawing_Click(object sender, EventArgs e)
  139.         {
  140.  
  141.         }
  142.  
  143.         private void pbDrawing_MouseUp(object sender, MouseEventArgs e)
  144.         {
  145.             if (rbRed.Checked == true)
  146.             {
  147.                 p = new Pen(Color.Red, sbThickness.Value);
  148.             }
  149.             else if (rbGreen.Checked == true)
  150.             {
  151.                 p = new Pen(Color.Green, sbThickness.Value);
  152.             }
  153.             else if (rbBlue.Checked == true)
  154.             {
  155.                 p = new Pen(Color.Blue, sbThickness.Value);
  156.             }
  157.  
  158.             if (rbCircle.Checked == true)
  159.             {
  160.                 Circle c = new Circle();
  161.                 c.draw(g, p, e.X, e.Y);
  162.             }
  163.             else if (rbSquare.Checked == true)
  164.             {
  165.                 Square s = new Square();
  166.                 s.draw(g, p, e.X, e.Y);
  167.             }
  168.             else if (rbTriangle.Checked == true)
  169.             {
  170.                 Triangle t = new Triangle();
  171.                 t.draw(g, p, e.X, e.Y);
  172.             }            
  173.         }
  174.     }
  175.  
  176.     class Circle
  177.     {
  178.         int r;
  179.         public Circle()
  180.         {
  181.             r = 10;
  182.         }
  183.         public void draw(Graphics g, Pen p, int x, int y)
  184.         {
  185.             g.DrawEllipse(p, x, y, r, r);
  186.         }
  187.     }
  188.  
  189.     class Square
  190.     {
  191.         int a, b;
  192.         public Square()
  193.         {
  194.             a = 10;
  195.             b = 10;
  196.         }
  197.         public void draw(Graphics g, Pen p, int x, int y)
  198.         {
  199.             g.DrawRectangle(p, x, y, a, b);
  200.         }
  201.     }
  202.  
  203.     class Triangle
  204.     {
  205.         int a;
  206.         public Triangle()
  207.         {
  208.         }
  209.         public void draw(Graphics g, Pen p, int x, int y)
  210.         {
  211.             Point[] points = { new Point(x, y), new Point(x + 90, y), new Point(x + 40, y + 90) };
  212.             g.DrawPolygon(p, points);
  213.         }
  214.     }
  215. }
  216.  
  217. // Analiza
  218. // 1. Napravite igru križić-kružić (iks-oks) korištenjem znanja stečenih na ovoj
  219. // laboratorijskoj vježbi. Omogućiti pokretanje igre, unos imena dvaju igrača, ispis
  220. // koji igrač je trenutno na potezu, igranje igre s iscrtavanjem križića i kružića na
  221. // odgovarajućim mjestima te ispis dijaloga s porukom o pobjedi, odnosno
  222. // neriješenom rezultatu kao i praćenje ukupnog rezultata.
  223.  
  224. namespace LV7___Analiza
  225. {
  226.     public partial class TicTacToe : Form
  227.     {
  228.         bool turn = true; // true = X turn; flase = O turn
  229.         public TicTacToe()
  230.         {
  231.             InitializeComponent();
  232.         }
  233.  
  234.         private void NewGame()
  235.         {
  236.             label_turn.Text = "Turn: " + textBox_player1.Text;
  237.  
  238.             button1.Text = "";
  239.             button2.Text = "";
  240.             button3.Text = "";
  241.             button4.Text = "";
  242.             button5.Text = "";
  243.             button6.Text = "";
  244.             button7.Text = "";
  245.             button8.Text = "";
  246.             button9.Text = "";
  247.  
  248.             button1.Enabled = true;
  249.             button2.Enabled = true;
  250.             button3.Enabled = true;
  251.             button4.Enabled = true;
  252.             button5.Enabled = true;
  253.             button6.Enabled = true;
  254.             button7.Enabled = true;
  255.             button8.Enabled = true;
  256.             button9.Enabled = true;
  257.         }
  258.  
  259.         private void button_click(object sender, EventArgs e)
  260.         {
  261.             Button b = (Button)sender;
  262.             if (turn)
  263.             {
  264.                 label_turn.Text = "Turn: " + textBox_player2.Text;
  265.                 b.Text = "X";
  266.             }
  267.             else
  268.             {
  269.                 label_turn.Text = "Turn: " + textBox_player1.Text;
  270.                 b.Text = "O";
  271.             }
  272.  
  273.             turn = !turn;
  274.             b.Enabled = false;
  275.  
  276.             if ((button1.Text == "X" && button2.Text == "X" && button3.Text == "X") || (button4.Text == "X" && button5.Text == "X" && button6.Text == "X") || (button7.Text == "X" && button8.Text == "X" && button9.Text == "X"))
  277.             {
  278.                 MessageBox.Show(textBox_player1.Text + " has won!");
  279.                 NewGame();
  280.             }
  281.             else if ((button1.Text == "O" && button2.Text == "O" && button3.Text == "O") || (button4.Text == "O" && button5.Text == "O" && button6.Text == "O") || (button7.Text == "O" && button8.Text == "O" && button9.Text == "O"))
  282.             {
  283.                 MessageBox.Show(textBox_player2.Text + " has won!");
  284.                 NewGame();
  285.             }
  286.             else if ((button1.Text == "X" && button4.Text == "X" && button7.Text == "X") || (button2.Text == "X" && button5.Text == "X" && button8.Text == "X") || (button3.Text == "X" && button6.Text == "X" && button9.Text == "X"))
  287.             {
  288.                 MessageBox.Show(textBox_player1.Text + " has won!");
  289.                 NewGame();
  290.             }
  291.             else if ((button1.Text == "O" && button4.Text == "O" && button7.Text == "O") || (button2.Text == "O" && button5.Text == "O" && button8.Text == "O") || (button3.Text == "O" && button6.Text == "O" && button9.Text == "O"))
  292.             {
  293.                 MessageBox.Show(textBox_player2.Text + " has won!");
  294.                 NewGame();
  295.             }
  296.             else if ((button1.Text == "X" && button5.Text == "X" && button9.Text == "X") || (button3.Text == "X" && button5.Text == "X" && button7.Text == "X"))
  297.             {
  298.                 MessageBox.Show(textBox_player1.Text + " has won!");
  299.                 NewGame();
  300.             }
  301.             else if ((button1.Text == "O" && button5.Text == "O" && button9.Text == "O") || (button3.Text == "O" && button5.Text == "O" && button7.Text == "O"))
  302.             {
  303.                 MessageBox.Show(textBox_player2.Text + " has won!");
  304.                 NewGame();
  305.             }
  306.             else
  307.             {
  308.                 if (!button1.Enabled && !button2.Enabled && !button3.Enabled && !button4.Enabled && !button5.Enabled && !button6.Enabled && !button7.Enabled && !button8.Enabled && !button9.Enabled)
  309.                 {
  310.                     MessageBox.Show("Tie!");
  311.                     NewGame();
  312.                 }
  313.             }
  314.         }
  315.  
  316.         private void button10_Click(object sender, EventArgs e)
  317.         {
  318.             Application.Exit();
  319.         }
  320.  
  321.         private void button_start_Click(object sender, EventArgs e)
  322.         {
  323.             if (textBox_player1.Text.Length == 0 || (textBox_player2.Text.Length == 0))
  324.             {
  325.                 MessageBox.Show("Invalid player name!");
  326.             }
  327.             else
  328.             {
  329.                 NewGame();
  330.             }
  331.         }
  332.  
  333.         private void TicTacToe_Load(object sender, EventArgs e)
  334.         {
  335.             button1.Enabled = false;
  336.             button2.Enabled = false;
  337.             button3.Enabled = false;
  338.             button4.Enabled = false;
  339.             button5.Enabled = false;
  340.             button6.Enabled = false;
  341.             button7.Enabled = false;
  342.             button8.Enabled = false;
  343.             button9.Enabled = false;
  344.         }
  345.     }
  346. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement