set Challenge

(출처 : Udemy Programming masterclass for software developers)

-Main

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
package com.timbuchalka;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Main {
private static Map<String, HeavenlyBody> solarSystem = new HashMap<>();
private static Set<HeavenlyBody> planets = new HashSet<>();

public static void main(String[] args) {
HeavenlyBody temp = new HeavenlyBody("Mercury", 88);
solarSystem.put(temp.getName(), temp);
planets.add(temp);

temp = new HeavenlyBody("Venus", 225);
solarSystem.put(temp.getName(), temp);
planets.add(temp);

temp = new HeavenlyBody("Earth", 365);
solarSystem.put(temp.getName(), temp);
planets.add(temp);

HeavenlyBody tempMoon = new HeavenlyBody("Moon", 27);
solarSystem.put(tempMoon.getName(), tempMoon);
temp.addMoon(tempMoon);

temp = new HeavenlyBody("Mars", 687);
solarSystem.put(temp.getName(), temp);
planets.add(temp);

tempMoon = new HeavenlyBody("Deimos", 1.3);
solarSystem.put(tempMoon.getName(), tempMoon);
temp.addMoon(tempMoon); // temp is still Mars

tempMoon = new HeavenlyBody("Phobos", 0.3);
solarSystem.put(tempMoon.getName(), tempMoon);
temp.addMoon(tempMoon); // temp is still Mars

temp = new HeavenlyBody("Jupiter", 4332);
solarSystem.put(temp.getName(), temp);
planets.add(temp);

tempMoon = new HeavenlyBody("Io", 1.8);
solarSystem.put(tempMoon.getName(), tempMoon);
temp.addMoon(tempMoon); // temp is still Jupiter

tempMoon = new HeavenlyBody("Europa", 3.5);
solarSystem.put(tempMoon.getName(), tempMoon);
temp.addMoon(tempMoon); // temp is still Jupiter

tempMoon = new HeavenlyBody("Ganymede", 7.1);
solarSystem.put(tempMoon.getName(), tempMoon);
temp.addMoon(tempMoon); // temp is still Jupiter

tempMoon = new HeavenlyBody("Callisto", 16.7);
solarSystem.put(tempMoon.getName(), tempMoon);
temp.addMoon(tempMoon); // temp is still Jupiter

temp = new HeavenlyBody("Saturn", 10759);
solarSystem.put(temp.getName(), temp);
planets.add(temp);

temp = new HeavenlyBody("Uranus", 30660);
solarSystem.put(temp.getName(), temp);
planets.add(temp);

temp = new HeavenlyBody("Neptune", 165);
solarSystem.put(temp.getName(), temp);
planets.add(temp);

temp = new HeavenlyBody("Pluto", 248);
solarSystem.put(temp.getName(), temp);
planets.add(temp);

System.out.println("Planets");
for(HeavenlyBody planet : planets) {
System.out.println("\t" + planet.getName());
}

HeavenlyBody body = solarSystem.get("Mars");
System.out.println("Moons of " + body.getName());
for(HeavenlyBody jupiterMoon: body.getSatellites()) {
System.out.println("\t" + jupiterMoon.getName());
}

Set<HeavenlyBody> moons = new HashSet<>();
for(HeavenlyBody planet : planets) {
moons.addAll(planet.getSatellites());
}

System.out.println("All Moons");
for(HeavenlyBody moon : moons) {
System.out.println("\t" + moon.getName());
}

HeavenlyBody pluto = new HeavenlyBody("Pluto", 842);
planets.add(pluto);

for(HeavenlyBody planet : planets) {
System.out.println(planet.getName() + ": " + planet.getOrbitalPeriod());
}

Object o = new Object();
o.equals(o);
"pluto".equals("");
}
}

  • HeavenlyBody
    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
    package com.chung;

    import java.util.HashSet;
    import java.util.Set;


    public final class HeavenlyBody {
    private final String name;
    private final double orbitalPeriod;
    private final Set<HeavenlyBody> satellites;

    public HeavenlyBody(String name, double orbitalPeriod) {
    this.name = name;
    this.orbitalPeriod = orbitalPeriod;
    this.satellites = new HashSet<>();

    }

    public String getName() {
    return name;
    }

    public double getOrbitalPeriod() {
    return orbitalPeriod;
    }

    public boolean addMoon(HeavenlyBody moon) {

    return this.satellites.add(moon);
    }

    public Set<HeavenlyBody> getSatellites() {
    return new HashSet<>(this.satellites);
    }


    @Override
    public boolean equals(Object obj) {
    if(this == obj) {
    return true;
    }

    System.out.println("obj.getClass() is " + obj.getClass());
    System.out.println("this.getClass() is " + this.getClass());
    if ((obj == null) || (obj.getClass() != this.getClass())) {
    return false;
    }

    String objName = ((HeavenlyBody) obj).getName();
    return this.name.equals(objName);
    }

    @Override
    public int hashCode() {
    System.out.println("hashcode called");
    return this.name.hashCode() + 57;
    }
    }