-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDog.java
More file actions
121 lines (114 loc) · 4.3 KB
/
Dog.java
File metadata and controls
121 lines (114 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.company;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Dog extends Cat {
//konstruktory
public Dog(String animal, String name, int age, int x, int y,String breed, int id, int ownerId ) {
super(animal, name, age, x, y, breed, id, ownerId);
}
public Dog(String animal, String name, int age, int x, int y,String breed, int ownerId ){
super(animal, name, age, x, y, breed, ownerId);
}
///////////// metody na listach obiektów typu Dog /////////////////
protected static List<Dog> dogs = new ArrayList<>();
private static boolean isIdUnique1(int id) {
for (Dog d : Dog.dogs) {
if (d.id == id) return false;
}
return true;
}
private static int generateId1() {
int max = 0;
for (Dog d : Dog.dogs) {
if (max < d.id) max = d.id;
}
return max+1;
}
public static void addDog(Dog d){
int id = d.getId();
if (id == 0) {
d.setId(Dog.generateId1());
} else if (!Dog.isIdUnique1(id)) {
// throw
}
Dog.dogs.add(d);
}
public static void removeByIdDog(int id) {
for (Dog cd : Dog.dogs) {
if (cd.id == id) {
Dog.dogs.remove(cd);
return;
}
}
// throw
}
public static void infoDog() {
for (Dog cd : dogs) {
cd.info();
}
}
private void setId(int id){
this.id=id;
}
public static void move(int id){
for (Dog c : Dog.dogs) {
if (c.id == id) {
System.out.println("W celu przesunięcia zwierzęcia wybierz odpowiedni numer.\n" +
"1. W górę.\n" +
"2. W dół.\n" +
"3. W lewo.\n" +
"4. W prawo.");
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
switch (number) {
case 1:
if ((c.y - 1) >= 0 ) {
c.y = c.y - 1;
System.out.println("Aktualna pozycja to X: "+ c.x+" Y: "+c.y );
} else {
System.out.println("Nie można przesunąć zwierzaka.\n" +
"Będzie wykraczać poza granice ogrodu.");
}
break;
case 2:
if ((c.y + 1) <= 14) { //14
c.y = c.y + 1;
System.out.println("Aktualna pozycja to X: "+ c.x+" Y: "+c.y );
} else {
System.out.println("Nie można przesunąć zwierzaka.\n" +
"Będzie wykraczać poza granice ogrodu.");
}
break;
case 3:
if ((c.x - 1) >= 0) {
c.x = c.x - 1;
System.out.println("Aktualna pozycja to X: "+ c.x+" Y: "+c.y );
} else {
System.out.println("Nie można przesunąć zwierzaka.\n" +
"Będzie wykraczać poza granice ogrodu.");
}
break;
case 4:
if ((c.x + 1) <= 14) { //14
c.x = c.x + 1;
System.out.println("Aktualna pozycja to X: "+ c.x+" Y: "+c.y );
} else {
System.out.println("Nie można przesunąć zwierzaka.\n" +
"Będzie wykraczać poza granice ogrodu.");
}
break;
default:
System.out.println("Niepoprawna opcja.");
break;
}
}
}
}
// metoda 'postarzająca' każdy obiekt typu Dog o rok
public static void Year(){
for (Dog d : dogs) {
d.setAge(d.age+1);
}
}
}