Kill mega-sena.sh
[lcapit-junk-code.git] / web / horario-oop / disciplina.php
blob48486f01918f3bfa0295a7608a69e0572272ce7b
1 <?php
2 class matrix {
3 protected $rows;
4 protected $cols;
5 protected $matrix;
7 public function get_rows()
9 return $this->rows;
12 public function get_cols()
14 return $this->cols;
17 protected function mk_matrix()
19 $mat = array();
21 for ($i = 0; $i < $this->rows; $i++)
22 for ($j = 0; $j < $this->cols; $j++)
23 $mat[$i][$j] = false;
25 return $mat;
28 public function check_values($mat2, $value)
30 if (($this->rows != $mat2->rows) ||
31 ($this->cols != $mat2->cols))
32 throw new Exception("Matrizes sao diferentes");
34 for ($i = 0; $i < $this->rows; $i++) {
35 for ($j = 0; $j < $this->cols; $j++) {
36 if ($this->matrix[$i][$j] == $value &&
37 $mat2->matrix[$i][$j] == $value)
38 throw new Exception("Matrizes com o mesmo valor");
43 public function __construct($r, $c)
45 if ($r <= 0 || $c <= 0)
46 throw new Exception("Colunas ou Linhas invalidas");
48 $this->rows = $r;
49 $this->cols = $c;
50 $this->matrix = $this->mk_matrix($r, $c);
54 class disciplina extends matrix {
55 private $nome;
57 private function dia_to_row($d)
59 $dias = array("seg" => 0, "ter" => 1, "qua" => 2,
60 "qui" => 3, "sex" => 4);
62 if (!array_key_exists($d, $dias))
63 throw new Exception("Dia invalido");
65 return $dias[$d];
68 public function get_nome()
70 return $this->nome;
73 public function set_nome($n)
75 $this->nome = $n;
78 private function change_horario($d, $h, $value)
80 $h -= 1;
82 if ($h < 0 || $h > $this->cols)
83 throw new Exception("Hora invalida");
85 if ($value != true && $value != false)
86 throw new Exception("value tem que ser booleano");
88 $cur = &$this->matrix[$this->dia_to_row($d)][$h];
89 if ($cur == $value)
90 throw new Exception("Valor ja alterado");
92 $cur = $value;
95 public function has_horario($dia, $hora)
97 $hora -= 1;
98 return $this->matrix[$this->dia_to_row($dia)][$hora];
101 public function set_horario($d, $h)
103 if ($this->has_horario($d, $h))
104 throw new Exception("Dia e hora ja existem " .
105 $d . " " . $h);
107 $this->change_horario($d, $h, true);
110 public function unset_horario($d, $h)
112 $this->change_horario($d, $h, false);
115 public function __construct($n)
117 $rows = 5;
118 $cols = 6;
119 parent::__construct($rows, $cols);
120 $this->set_nome($n);