Skip to content

Commit

Permalink
[v0] add GeneralsMap class
Browse files Browse the repository at this point in the history
  • Loading branch information
yudong80 committed Aug 16, 2021
1 parent 8cc38da commit fee46eb
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 2 deletions.
29 changes: 28 additions & 1 deletion V0/src/coding/samguk/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,38 @@ public void testCreateSomeGenerals() {
System.out.println(generals);
}

public void testCreate낙양() { //낙양 만들기
public void testCreate낙양() {
General 조조 = new General("조조", (194-40-1), 89, 94, 98, 90, 93, 59);
General 순욱 = new General("순욱", (194-32-1), 37, 96, 85, 85, 62, 60);

Province 낙양 = new Province(11, "낙양", 조조, 조조, 순욱, 782_000, 3_500, 65_000, 40, 57, 0, 60, 56, 730, 40);
System.out.println(낙양);
}

public void test낙양장수들() {
GeneralsMap map = GeneralsMap.getInstance();
General 조조 = map.find("조조");
General 순욱 = map.find("순욱");
Province 낙양 = new Province(11, "낙양", 조조, 조조, 순욱, 782_000, 3_500, 65_000, 40, 57, 0, 60, 56, 730, 40);
낙양.addGeneral(map.find("조조").setArmy(6500));
낙양.addGeneral(map.find("곽가"));
낙양.addGeneral(map.find("하우연").setArmy(2300));
낙양.addGeneral(map.find("하우은"));
낙양.addGeneral(map.find("하우모").setArmy(1500));
낙양.addGeneral(map.find("순욱").setArmy(1600));
낙양.addGeneral(map.find("순상"));
낙양.addGeneral(map.find("순유"));
낙양.addGeneral(map.find("조앙").setArmy(1000));
낙양.addGeneral(map.find("조홍").setArmy(2000));
낙양.addGeneral(map.find("조인").setArmy(2100));
낙양.addGeneral(map.find("조순").setArmy(1800));
낙양.addGeneral(map.find("진교"));
낙양.addGeneral(map.find("정욱"));
낙양.addGeneral(map.find("전위").setArmy(2300));
낙양.addGeneral(map.find("이전").setArmy(1800));
낙양.addGeneral(map.find("유연"));
System.out.println(낙양);
}

public static void main(String[] args) throws Exception {
System.out.println("Hello, Coding Samguk V0!");
Expand All @@ -30,5 +54,8 @@ public static void main(String[] args) throws Exception {

//2. 낙양 만들기
app.testCreate낙양();

//3. 낙양 장수들(17명)
app.test낙양장수들();
}
}
7 changes: 7 additions & 0 deletions V0/src/coding/samguk/General.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public General(String n,

public String getName() { return name; }

public int getArmy() { return army; }

public General setArmy(int man) {
army = man;
return this;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Expand Down
55 changes: 55 additions & 0 deletions V0/src/coding/samguk/GeneralsMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package coding.samguk;

import java.util.HashMap;
import java.util.Map;

public class GeneralsMap {
private Map<String, General> map = new HashMap<>();
private static GeneralsMap _instance;

private GeneralsMap() {
init();
}

public static GeneralsMap getInstance() {
if (_instance == null) {
_instance = new GeneralsMap();
}
return _instance;
}

public General find(String key) {
assert map.containsKey(key);
return map.get(key);
}

private void init() {
General[] arr = {
//이름, 연도, 무력, 지력, 매력, 정치력, 육지, 수지
new General("조조", (194-40-1), 89, 94, 98, 90, 93, 59),
new General("곽가", (194-25-1), 36, 80, 82, 97, 32, 6),
new General("하우연", (194-37-1), 90, 58, 55, 82, 32, 6),
new General("하우은", (194-28-1), 60, 62, 75, 64, 58, 27),
new General("하우모", (194-18-1), 54, 35, 27, 40, 37, 13),
new General("순욱", (194-32-1), 37, 96, 85, 85, 62, 60),
new General("순상", (194-40-1), 45, 70, 76, 77, 39, 16),
new General("순유", (194-38-1), 40, 94, 85, 94, 60, 58),
new General("조앙", (194-20-1), 60, 54, 67, 50, 58, 19),
new General("조홍", (194-26-1), 74, 47, 75, 41, 74, 72),
new General("조인", (194-27-1), 83, 62, 67, 57, 79, 72),
new General("조순", (194-25-1), 60, 51, 43, 42, 54, 20),
new General("진교", (194-45-1), 20, 68, 71, 79, 18, 9),
new General("정욱", (194-54-1), 25, 90, 75, 71, 85, 75),
new General("전위", (194-35-1), 96, 33, 54, 21, 84, 74),
new General("이전", (194-34-1), 72, 47, 53, 36, 73, 70),
new General("유연", (194-29-1), 45, 50, 32, 57, 40, 20),
};

for (General gen : arr) {
String key = gen.getName();
General value = gen;
map.put(key, value);
}
}

}
46 changes: 45 additions & 1 deletion V0/src/coding/samguk/Province.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package coding.samguk;

import java.util.ArrayList;
import java.util.List;

public class Province {
private int no; //번호
private String name; //이름
Expand All @@ -19,6 +22,8 @@ public class Province {
private int commerce; //상업
private int taxRate; //세율

private List<General> generals = new ArrayList<>();

public Province(int num, String nm, General sv, General gv, General ad,
int pop, int gld, int fd, int loy,
int dev, int cul, int wt, int irr, int cmr, int tax) {
Expand All @@ -37,14 +42,53 @@ public Province(int num, String nm, General sv, General gv, General ad,
irrigation = irr;
commerce = cmr;
taxRate = tax;

//addGenerals
addGeneralIfNotNull(sv);
addGeneralIfNotNull(gv);
addGeneralIfNotNull(ad);
}

private void addGeneralIfNotNull(General gen) {
if (gen == null) return;

addGeneral(gen);
}

public void addGeneral(General newGeneral) {
for (General gen : generals) {
if (newGeneral.getName().equals(gen.getName())) {
return; //skip
}
}

generals.add(newGeneral);
}

public int getGenerals() {
return generals.size();
}

public int getSolders() {
int total = 0;
for (General general : generals) {
total += general.getArmy();
}
return total;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(no).append(",").append(name).append(",").append(sovereign.getName()).append("::")
.append(governor.getName()).append(",").append(advisor.getName()).append("::")
.append(population).append(",").append(gold).append(",").append(food);
.append(population).append(",").append(gold).append(",").append(food).append("::")
.append(getSolders()).append("::")
.append("[").append(getGenerals()).append("] ");

for (General gen : generals) {
sb.append(gen.getName()).append(",");
}

return sb.toString();
}
Expand Down

0 comments on commit fee46eb

Please sign in to comment.