Skip to content

Commit 3d70f74

Browse files
committed
#9 - Java
1 parent 5c5e682 commit 3d70f74

File tree

1 file changed

+173
-0
lines changed

1 file changed

+173
-0
lines changed
+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
public class Josegs95 {
5+
public static void main(String[] args) {
6+
//Herencia
7+
Animal animal = new Animal(null);
8+
Perro dog = new Perro();
9+
Gato cat = new Gato();
10+
11+
animal.makeSound(); //Out: null
12+
dog.makeSound(); //Out: Guau
13+
cat.makeSound(); //Out: Miau
14+
15+
//Reto
16+
System.out.println("\n");
17+
retoFinal();
18+
}
19+
20+
public static class Animal{
21+
22+
private String sound;
23+
24+
public Animal(String sound){
25+
this.sound = sound;
26+
}
27+
28+
public void makeSound(){
29+
System.out.println(sound);
30+
}
31+
32+
}
33+
34+
public static class Perro extends Animal{
35+
36+
public Perro(){
37+
super("Guau");
38+
}
39+
}
40+
41+
public static class Gato extends Animal{
42+
43+
public Gato(){
44+
super("Miau");
45+
}
46+
}
47+
48+
public static void retoFinal(){
49+
Manager manager = new Manager("1154257", "Jose");
50+
ProyectManager pm1 = new ProyectManager("9822361", "Rocío");
51+
ProyectManager pm2 = new ProyectManager("5418742", "Guillermo");
52+
Programmer p1 = new Programmer("7739682", "Lucía", "Java");
53+
Programmer p2 = new Programmer("3125055", "Nuria", "Python");
54+
Programmer p3 = new Programmer("4060533", "Isidoro", "C#");
55+
56+
manager.addEmployee(pm1);
57+
manager.addEmployee(pm2);
58+
pm1.addEmployee(p1);
59+
pm1.addEmployee(p2);
60+
pm2.addEmployee(p3);
61+
62+
p1.addEmployee(manager);
63+
p1.programme();
64+
65+
pm2.addEmployee(manager);
66+
pm2.manageProyect();
67+
68+
manager.manageCompany();
69+
70+
System.out.print("Empleados a cargo de " + manager.getName() + ": ");
71+
manager.printEmployeeList();
72+
System.out.println();
73+
74+
System.out.print("Empleados a cargo de " + pm1.getName() + ": ");
75+
pm1.printEmployeeList();
76+
System.out.println();
77+
78+
System.out.print("Empleados a cargo de " + pm2.getName() + ": ");
79+
pm2.printEmployeeList();
80+
System.out.println();
81+
}
82+
83+
public static class Employee {
84+
private final String ID;
85+
private String name;
86+
private List<Employee> employeeList;
87+
88+
public Employee(String id, String name){
89+
this.ID = id;
90+
this.name = name;
91+
employeeList = new ArrayList<>();
92+
}
93+
94+
public void addEmployee(Employee e){
95+
employeeList.add(e);
96+
}
97+
98+
public void printEmployeeList(){
99+
for (Employee e : employeeList){
100+
e.printEmployeeList();
101+
System.out.print(e + " ");
102+
}
103+
}
104+
105+
public String getID() {
106+
return ID;
107+
}
108+
109+
public String getName() {
110+
return name;
111+
}
112+
113+
public void setName(String name) {
114+
this.name = name;
115+
}
116+
117+
@Override
118+
public String toString() {
119+
return "[" + name + ":" + ID + "]";
120+
}
121+
}
122+
123+
public static class Manager extends Employee{
124+
125+
public Manager(String id, String name){
126+
super(id, name);
127+
}
128+
129+
public void manageCompany(){
130+
System.out.println(getName() + " is managing the company...");
131+
}
132+
}
133+
134+
public static class ProyectManager extends Employee{
135+
136+
public ProyectManager(String id, String name){
137+
super(id, name);
138+
}
139+
140+
public void manageProyect(){
141+
System.out.println(getName() + " is managing their project...");
142+
}
143+
144+
@Override
145+
public void addEmployee(Employee e){
146+
if (e instanceof Manager){
147+
System.out.println("The project manager can not have a manager in their charge");
148+
return;
149+
}
150+
151+
super.addEmployee(e);
152+
}
153+
}
154+
155+
public static class Programmer extends Employee{
156+
157+
private String language;
158+
159+
public Programmer(String id, String name, String language){
160+
super(id, name);
161+
this.language = language;
162+
}
163+
164+
public void programme(){
165+
System.out.println(getName() + " is programming with " + language);
166+
}
167+
168+
@Override
169+
public void addEmployee(Employee e) {
170+
System.out.println("The programmer can not have employees in their charge");
171+
}
172+
}
173+
}

0 commit comments

Comments
 (0)