Skip to content

Commit 21c4ac7

Browse files
authored
Merge pull request mouredev#4716 from AmadorQuispe/aqh
#26 - JAVA
2 parents ec273e2 + eef62bb commit 21c4ac7

File tree

4 files changed

+1004
-0
lines changed

4 files changed

+1004
-0
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
// En GO las clases son diferentes a los demás lenguajes, se usan structs
9+
type Student struct {
10+
id int
11+
name string
12+
}
13+
14+
// Constructor
15+
func NewStudent() Student {
16+
return Student{}
17+
}
18+
19+
// También es posible simular los métodos setter y getter
20+
func (s *Student) SetId(id int) {
21+
s.id = id
22+
}
23+
func (s *Student) SetName(name string) {
24+
s.name = name
25+
}
26+
27+
func (s *Student) GetId() int {
28+
return s.id
29+
}
30+
31+
func (s *Student) GetName() string {
32+
return s.name
33+
}
34+
35+
func (s *Student) ToString() string {
36+
return fmt.Sprintf("{Id: %d, Name: %s}", s.id, s.name)
37+
}
38+
39+
// Pila LIFO último en entrar, primero en salir
40+
type Stack[T any] struct {
41+
elements []T
42+
}
43+
44+
func NewStack[T any]() Stack[T] {
45+
return Stack[T]{}
46+
}
47+
48+
func (s *Stack[T]) Empty() bool {
49+
return len(s.elements) == 0
50+
}
51+
52+
func (s *Stack[T]) Push(item T) T {
53+
s.elements = append(s.elements, item)
54+
return item
55+
}
56+
57+
func (s *Stack[T]) Pop() (T, error) {
58+
var zero T
59+
if s.Empty() {
60+
return zero, errors.New("stack is empty")
61+
}
62+
item := s.elements[len(s.elements)-1]
63+
s.elements = s.elements[:len(s.elements)-1]
64+
return item, nil
65+
}
66+
67+
func (s *Stack[T]) Peek() (T, error) {
68+
var zero T
69+
if s.Empty() {
70+
return zero, errors.New("stack is empty")
71+
}
72+
return s.elements[len(s.elements)-1], nil
73+
}
74+
75+
func (s *Stack[T]) Size() int {
76+
return len(s.elements)
77+
}
78+
79+
func (s *Stack[T]) Print() {
80+
fmt.Println(s.elements)
81+
}
82+
83+
// Colas FIFO primero en entrar, primero en salir
84+
type Queue[T any] struct {
85+
elements []T
86+
}
87+
88+
func (q *Queue[T]) Empty() bool {
89+
return len(q.elements) == 0
90+
}
91+
func NewQueue[T any]() Queue[T] {
92+
return Queue[T]{}
93+
}
94+
95+
func (q *Queue[T]) Add(item T) T {
96+
q.elements = append(q.elements, item)
97+
return item
98+
}
99+
100+
func (q *Queue[T]) Poll() (T, error) {
101+
var item T
102+
if q.Empty() {
103+
return item, errors.New("queue is empty")
104+
}
105+
item = q.elements[0]
106+
q.elements = q.elements[1:]
107+
return item, nil
108+
}
109+
110+
func (q *Queue[T]) Size() int {
111+
return len(q.elements)
112+
}
113+
114+
func (q *Queue[T]) Print() {
115+
fmt.Println(q.elements)
116+
}
117+
118+
func main() {
119+
//CLASES
120+
student := Student{}
121+
student.SetId(100)
122+
student.SetName("Amador")
123+
fmt.Println(student.ToString())
124+
//PILAS
125+
stack := NewStack[int]()
126+
stack.Push(1)
127+
stack.Push(2)
128+
stack.Push(3)
129+
stack.Push(4)
130+
stack.Push(5)
131+
fmt.Println(stack.Size())
132+
stack.Print()
133+
stack.Pop()
134+
stack.Pop()
135+
fmt.Println(stack.Size())
136+
stack.Print()
137+
138+
//COLAS
139+
queue := NewQueue[string]()
140+
queue.Add("Amador")
141+
queue.Add("Alex")
142+
queue.Add("Pedro")
143+
fmt.Println(queue.Size())
144+
queue.Print()
145+
queue.Poll()
146+
fmt.Println(queue.Size())
147+
queue.Print()
148+
149+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package main
2+
3+
import "fmt"
4+
5+
//Go no cuenta con una palabra para declarar herencia en los structs, sin embargo sí tiene algo parecido.
6+
//Para que un struct en go posea todos los campos que declara otro struct, le pasamos este último como un campo anónimo
7+
8+
type Animal struct {
9+
code string
10+
name string
11+
}
12+
13+
type Cat struct {
14+
Animal
15+
}
16+
17+
type Dog struct {
18+
Animal
19+
}
20+
21+
type Sound interface {
22+
MakeSound()
23+
}
24+
25+
func (c *Cat) MakeSound() {
26+
fmt.Printf("El gato %s hace miau miau \n", c.name)
27+
}
28+
29+
func (d *Dog) MakeSound() {
30+
fmt.Printf("El perro %s hace wau wau \n", d.name)
31+
}
32+
33+
//Está función puede recibir cualquier struct que implemente la interfaz Sound
34+
func PrintAnimal(sound Sound) {
35+
sound.MakeSound()
36+
}
37+
38+
//=============================================================
39+
//======================= EXTRA ===============================
40+
//=============================================================
41+
type EmployeeManager interface {
42+
AddEmployee(employee Employee)
43+
PrintEmployees()
44+
}
45+
46+
type Employee struct {
47+
id int
48+
name string
49+
employees []Employee
50+
}
51+
52+
func (e *Employee) AddEmployee(employee Employee) {
53+
e.employees = append(e.employees, employee)
54+
}
55+
56+
func (e *Employee) PrintEmployees() {
57+
for _, employee := range e.employees {
58+
fmt.Printf("id : %d, name:%s\n", employee.id, employee.name)
59+
}
60+
}
61+
62+
type Manager struct {
63+
Employee
64+
}
65+
66+
func (m *Manager) CoordinateProjects() {
67+
fmt.Printf("%s está coordinando todos los proyectos de la empresa.\n", m.name)
68+
}
69+
70+
type ProjectManager struct {
71+
Employee
72+
project string
73+
}
74+
75+
func (pm *ProjectManager) CoordinateProject() {
76+
fmt.Printf("%s está coordinando su proyecto %s \n", pm.name, pm.project)
77+
}
78+
79+
type Programmer struct {
80+
Employee
81+
languages []string
82+
}
83+
84+
func (e *Programmer) AddEmployee(employee Employee) {
85+
fmt.Println("Un programador no puede tener empleados, no se ha agregado")
86+
}
87+
88+
func (e *Programmer) Code() {
89+
fmt.Printf("%s está programando en %v", e.name, e.languages)
90+
}
91+
92+
func main() {
93+
cat1 := Cat{Animal{code: "0001", name: "Thor"}}
94+
PrintAnimal(&cat1)
95+
dog1 := Dog{Animal{code: "0002", name: "Run Run"}}
96+
PrintAnimal(&dog1)
97+
//EXTRA
98+
m := Manager{Employee{id: 100, name: "Amador"}}
99+
m.AddEmployee(Employee{id: 200, name: "Alex"})
100+
p := Programmer{languages: []string{"Java", "Go"}, Employee: Employee{id: 100, name: "Mia"}}
101+
p.AddEmployee(Employee{id: 300, name: "Pedro"})
102+
103+
var employeeManagers []EmployeeManager
104+
employeeManagers = append(employeeManagers, &m, &p)
105+
106+
for _, manager := range employeeManagers {
107+
manager.AddEmployee(Employee{id: 500, name: "New Employee"})
108+
}
109+
m.CoordinateProjects()
110+
m.PrintEmployees()
111+
}

0 commit comments

Comments
 (0)