[wise_saying_app] GitHub ๐๐ป
๐ ๊ธฐ๋ฅ์ค๋ช
๋ฑ๋ก: ๋ช ์ธ๊ณผ ์๊ฐ ์ด๋ฆ์ ๋ฑ๋กํ ์ ์๋ค.
๋ชฉ๋ก: ๋ฑ๋ก๋์๋ ๋ช ์ธ๊ณผ ์๊ฐ ์ด๋ฆ์ ๋ฆฌ์คํธ๋ฅผ ํ์ธํ ์ ์๋ค.
์ญ์ : ์ํ๋ ๋ฒํธ๋ฅผ ์ ๋ ฅํ์ฌ ๋ช ์ธ์ ์ญ์ ํ ์ ์๋ค.
์์ : ์ํ๋ ๋ฒํธ๋ฅผ ์ ๋ ฅํ์ฌ ๋ช ์ธ ๋ด์ฉ๊ณผ, ์๊ฐ ์ด๋ฆ์ ์์ ํ ์ ์๋ค.
์ข ๋ฃ: ๋ช ์ธ ์ฑ์ ์ข ๋ฃํ๋ค.
๐ ๋ง๋ค๊ธฐ ์ ํด์ผํ ๊ฒ!
- ์๋ก์ด Git ๋ฆฌํฌ์งํฐ๋ฆฌ ์์ฑํ๊ธฐ
- ํ๋ก์ ํธ ์์ฑํ๊ธฐ
- gradle → IntelliJ ๋ก ์ธํ
- .gitignore ์ถ๊ฐ
- ๋ฆฌํฌ์งํฐ๋ฆฌ ์ฐ๊ฒฐ
- ๋จ๊ณ ๋ณ ์ปค๋ฐํด์ฃผ๊ธฐ
- git init
- git remote add origin ๊นํ๋ธ์ฃผ์
- git add .
- git commit -m “๋ฉ์ธ์ง”
- git push origin main
<1๋จ๊ณ>
โ๏ธ ๋ชฉํ: ์ข ๋ฃ ๊ธฐ๋ฅ ๊ตฌํํ๊ธฐ
[main.java]
package com.example;
import java.util.*;
public class main { // Applicaiton์ด ์ผํ๋๋ก ํจ
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
new Application(sc).run(); // ๊ธฐ๋ณธ์ ์ผ๋ก Scanner๋ฅผ ๊ฐ์ง๊ณ ์๋ Applicaiton
sc.close();
}
}
[Application.java]
package com.example;
import java.util.*;
import io.micronaut.runtime.Micronaut;
public class Application {
public final Scanner sc;
public Application(Scanner sc){
this.sc = sc;
}
public void run() {
System.out.println("== ๋ช
์ธ ์ฑ ==");
while(true){
System.out.print("๋ช
๋ น) ");
String command = sc.nextLine().trim();
// trim() : ํน์ ์์์ง ๋ชจ๋ฅผ ์ข์ฐ๊ณต๋ฐฑ์ ๊ฑฐ๋ ๋ฒ์ ์ผ๋ก ์ฃผ์ธ์!
if(command.equals("์ข
๋ฃ")) { // ์ข
๋ฃ๊ฐ ์
๋ ฅ๋๋ฉด ํ๋ก๊ทธ๋จ ์ข
๋ฃ
break;
}
}
}
}
<2๋จ๊ณ>
โ๏ธ ๋ชฉํ: ๋ฑ๋ก ๊ธฐ๋ฅ ๊ตฌํํ๊ธฐ
[Application.java]
package com.example;
import java.util.*;
import io.micronaut.runtime.Micronaut;
public class Application {
public final Scanner sc;
public Application(Scanner sc){
this.sc = sc;
}
public void run() {
System.out.println("== ๋ช
์ธ ์ฑ ==");
while(true){
System.out.print("๋ช
๋ น) ");
String command = sc.nextLine().trim();
// trim() : ํน์ ์์์ง ๋ชจ๋ฅผ ์ข์ฐ๊ณต๋ฐฑ ์ ๊ฑฐ๋ ๋ฒ์ ์ผ๋ก ์ฃผ์ธ์!
if(command.equals("์ข
๋ฃ")) { // ์ข
๋ฃ๊ฐ ์
๋ ฅ๋๋ฉด ํ๋ก๊ทธ๋จ ์ข
๋ฃ
break;
}
else if (command.equals("๋ฑ๋ก")){
System.out.print("๋ช
์ธ : "); // #2 ๋ช
์ธ๊ณผ ์๊ฐ ์ด๋ฆ์ ์
๋ ฅ๋ฐ๊ธฐ
String content = sc.nextLine().trim();
System.out.print("์๊ฐ : ");
String authorName = sc.nextLine().trim();
}
}
}
}
<3๋จ๊ณ>
โ๏ธ ๋ชฉํ: ๋ฑ๋ก ์ ์์ฑ๋ ๋ช ์ธ ๋ฒํธ๋ฅผ ๋ ธ์ถ์ํค๊ธฐ
[Application.java]
package com.example;
import java.util.*;
import io.micronaut.runtime.Micronaut;
public class Application {
public final Scanner sc;
public Application(Scanner sc){
this.sc = sc;
}
public void run() {
System.out.println("== ๋ช
์ธ ์ฑ ==");
while(true){
System.out.print("๋ช
๋ น) ");
String command = sc.nextLine().trim();
// trim() : ํน์ ์์์ง ๋ชจ๋ฅผ ์ข์ฐ๊ณต๋ฐฑ ์ ๊ฑฐ๋ ๋ฒ์ ์ผ๋ก ์ฃผ์ธ์!
if(command.equals("์ข
๋ฃ")) { // ์ข
๋ฃ๊ฐ ์
๋ ฅ๋๋ฉด ํ๋ก๊ทธ๋จ ์ข
๋ฃ
break;
}
else if (command.equals("๋ฑ๋ก")){
System.out.print("๋ช
์ธ : "); // #2 ๋ช
์ธ๊ณผ ์๊ฐ ์ด๋ฆ์ ์
๋ ฅ๋ฐ๊ธฐ
String content = sc.nextLine().trim();
System.out.print("์๊ฐ : ");
String authorName = sc.nextLine().trim();
System.out.println("1๋ฒ ๋ช
์ธ์ด ๋ฑ๋ก๋์์ต๋๋ค."); // #3 ๋ฑ๋ก ๋ฌธ๊ตฌ ๋์ฐ๊ธฐ
}
}
}
}
<4๋จ๊ณ>
โ๏ธ ๋ชฉํ: ๋ฑ๋กํ ๋๋ง๋ค ์์ฑ๋๋ ๋ช ์ธ๋ฒํธ๊ฐ ์ฆ๊ฐํ๋๋ก ๊ตฌํํ๊ธฐ
[Application.java]
package com.example;
import java.util.*;
import io.micronaut.runtime.Micronaut;
public class Application {
public final Scanner sc;
public Application(Scanner sc){
this.sc = sc;
}
public void run() {
System.out.println("== ๋ช
์ธ ์ฑ ==");
long lastWiseSayingId = 1; // ์ ์ผ ๊ฒ์ ์๋ ๊ณณ์ ๋ณ์๋ฅผ ์ ์ธํด์ผ์ง ์ฅ๊ธฐ๊ธฐ์ต!
while(true){
System.out.print("๋ช
๋ น) ");
String command = sc.nextLine().trim();
// trim() : ํน์ ์์์ง ๋ชจ๋ฅผ ์ข์ฐ๊ณต๋ฐฑ ์ ๊ฑฐ๋ ๋ฒ์ ์ผ๋ก ์ฃผ์ธ์!
if(command.equals("์ข
๋ฃ")) { // ์ข
๋ฃ๊ฐ ์
๋ ฅ๋๋ฉด ํ๋ก๊ทธ๋จ ์ข
๋ฃ
break;
}
else if (command.equals("๋ฑ๋ก")){
System.out.print("๋ช
์ธ : "); // #2 ๋ช
์ธ๊ณผ ์๊ฐ ์ด๋ฆ์ ์
๋ ฅ๋ฐ๊ธฐ
String content = sc.nextLine().trim();
System.out.print("์๊ฐ : ");
String authorName = sc.nextLine().trim();
System.out.printf("%d๋ฒ ๋ช
์ธ์ด ๋ฑ๋ก๋์์ต๋๋ค.\n",lastWiseSayingId); // #3 ๋ฑ๋ก ๋ฌธ๊ตฌ ๋์ฐ๊ธฐ
lastWiseSayingId++; // #4 ๋ฑ๋กํ ๋๋ง๋ค ๋ฒํธ ์ฆ๊ฐ
}
}
}
}
<5๋จ๊ณ>
โ๏ธ ๋ชฉํ: ๋ชฉ๋ก ๊ธฐ๋ฅ ๊ตฌํํ๊ธฐ
[Wisesaying.java]
package com.example;
public class WiseSaying { // ๋ฑ๋ก๋ ๋ช
์ธ๊ณผ ์๊ฐ์ด๋ฆ์ ์ ์ฅ
private long id; // ๋ฑ๋ก ๋ฒํธ
private String content; // ๋ช
์ธ
private String authorName; // ์๊ฐ ์ด๋ฆ
public WiseSaying(long id, String content, String authorName) {
this.id = id;
this.content = content;
this.authorName = authorName;
}
public long getId() {
return id;
}
public String getContent() {
return content;
}
public String getAuthorName() {
return authorName;
}
}
[Application.java]
package com.example;
import java.util.*;
import io.micronaut.runtime.Micronaut;
public class Application {
public final Scanner sc;
public Application(Scanner sc){
this.sc = sc;
}
public void run() {
System.out.println("== ๋ช
์ธ ์ฑ ==");
long lastWiseSayingId = 0; // ์ ์ผ ๊ฒ์ ์๋ ๊ณณ์ ๋ณ์๋ฅผ ์ ์ธํด์ผ์ง ์ฅ๊ธฐ๊ธฐ์ต!
List<WiseSaying> wiseSayings = new ArrayList<>(); // wiseSayings ๋ผ๋ ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค์ด์ค ํ Arraylist ์์ฑ
while(true){
System.out.print("๋ช
๋ น) ");
String command = sc.nextLine().trim();
// trim() : ํน์ ์์์ง ๋ชจ๋ฅผ ์ข์ฐ๊ณต๋ฐฑ ์ ๊ฑฐ๋ ๋ฒ์ ์ผ๋ก ์ฃผ์ธ์!
if(command.equals("์ข
๋ฃ")) { // ์ข
๋ฃ๊ฐ ์
๋ ฅ๋๋ฉด ํ๋ก๊ทธ๋จ ์ข
๋ฃ
break;
}
else if (command.equals("๋ฑ๋ก")){
long id = lastWiseSayingId + 1; // #4 ๋ฑ๋กํ ๋๋ง๋ค ๋ฒํธ ์ฆ๊ฐ
System.out.print("๋ช
์ธ : "); // #2 ๋ช
์ธ๊ณผ ์๊ฐ ์ด๋ฆ์ ์
๋ ฅ๋ฐ๊ธฐ
String content = sc.nextLine().trim();
System.out.print("์๊ฐ : ");
String authorName = sc.nextLine().trim();
WiseSaying wiseSaying = new WiseSaying(id, content, authorName);
wiseSayings.add(wiseSaying); // ๋ฑ๋ก๋ ๋ช
์ธ๋ค์ WiseSaying ๋ฆฌ์คํธ์ ์ ์ฅ
System.out.printf("%d๋ฒ ๋ช
์ธ์ด ๋ฑ๋ก๋์์ต๋๋ค.\n", id); // #3 ๋ฑ๋ก ๋ฌธ๊ตฌ ๋์ฐ๊ธฐ
lastWiseSayingId = id;
}
else if (command.equals("๋ชฉ๋ก")) { // #5 ๋ฑ๋ก๋ ๋ช
์ธ์ ๋ชฉ๋ก ์ถ๋ ฅํ๊ธฐ
System.out.println("๋ฒํธ / ์๊ฐ / ๋ช
์ธ");
System.out.println("-".repeat(30)); // -๋ฅผ 30๋ฒ ๋ฐ๋ณตํด์ ์ถ๋ ฅ -------... ๋ ๊ฐ์
for (int i = wiseSayings.size() - 1; i >= 0; i--){ // ์
๋ ฅ์ ์ญ์์ผ๋ก ์ถ๋ ฅ (๋ชฉ๋ก์ 1๋ฒ๋ถํฐ)
WiseSaying wiseSaying = wiseSayings.get(i);
// '๋ชฉ๋ก๋ฒํธ ์๊ฐ์ด๋ฆ ๋ช
์ธ' ์ ์ถ๋ ฅ
System.out.printf("%d / %s / %s\n", wiseSaying.getId(), wiseSaying.getAuthorName(), wiseSaying.getContent());
}
}
}
}
}
<6๋จ๊ณ> + ๊ฐ ๋ชจ๋๋ก ๊ธฐ๋ฅ๋ถ๋ฆฌ ๋ฐ ๊ตฌ์กฐ๊ฐ์
โ๏ธ ๋ชฉํ: ์ญ์ ๊ธฐ๋ฅ ๊ตฌํํ๊ธฐ
[main.java]
package com.example;
import java.util.*;
public class main { // Applicaiton์ด ์ผํ๋๋ก ํจ
public static void main(String[] args) {
Container.init(); // ๊ณตํต์ ์ผ๋ก ์ฌ์ฉ๋๋ ๊ฒ๋ค์ ๋ชจ์๋๋ ๊ณต๊ฐ(Container) ์ด๊ธฐํ
new Application().run(); // ๊ธฐ๋ณธ์ ์ผ๋ก Scanner๋ฅผ ๊ฐ์ง๊ณ ์๋ Applicaiton
Container.close();
// ๊ณตํต์ ์ผ๋ก ์ฌ์ฉ๋๋ ๊ฒ๋ค์ ๋ชจ์๋๋ ๊ณต๊ฐ(Container) ์์ํด์
}
}
[SystemController.java]
package com.example.system.controller;
public class SystemController { // #1 ์ข
๋ฃํ๊ธฐ
public void exit() {
System.out.println("๋ช
์ธ์ฑ์ ์ข
๋ฃํฉ๋๋ค.");
}
}
[WiseSayingController.java]
package com.example.wiseSaying.controller;
import com.example.Rq;
import com.example.wiseSaying.entity.WiseSaying;
import com.example.Container;
import java.util.*;
import java.util.stream.IntStream;
public class WiseSayingController {
private long lastWiseSayingId;
private final List<WiseSaying> wiseSayings;
public WiseSayingController() {
lastWiseSayingId = 0;
wiseSayings = new ArrayList<>();
}
public void write() { // #2~4 ๋ฑ๋กํ๊ธฐ
long id = lastWiseSayingId + 1; // ๋ฑ๋กํ ๋๋ง๋ค ๋ฒํธ ์ฆ๊ฐ
System.out.print("๋ช
์ธ : "); // ๋ช
์ธ๊ณผ ์๊ฐ ์ด๋ฆ์ ์
๋ ฅ๋ฐ๊ธฐ
String content = Container.getScanner().nextLine().trim();
System.out.print("์๊ฐ : ");
String authorName = Container.getScanner().nextLine().trim();
WiseSaying wiseSaying = new WiseSaying(id, content, authorName);
wiseSayings.add(wiseSaying); // ๋ฑ๋ก๋ ๋ช
์ธ๋ค์ WiseSaying ๋ฆฌ์คํธ์ ์ ์ฅ
System.out.printf("%d๋ฒ ๋ช
์ธ์ด ๋ฑ๋ก๋์์ต๋๋ค.\n", id); // ๋ฑ๋ก ๋ฌธ๊ตฌ ๋์ฐ๊ธฐ
lastWiseSayingId = id;
}
public void list() { // #5 ๋ฑ๋ก๋ ๋ช
์ธ์ ๋ชฉ๋ก ์ถ๋ ฅํ๊ธฐ๊ธฐ
System.out.println("๋ฒํธ / ์๊ฐ / ๋ช
์ธ");
System.out.println("-".repeat(30)); // -๋ฅผ 30๋ฒ ๋ฐ๋ณตํด์ ์ถ๋ ฅ -------... ๋ ๊ฐ์
for (int i = wiseSayings.size() - 1; i >= 0; i--) { // ์
๋ ฅ์ ์ญ์์ผ๋ก ์ถ๋ ฅ (๋ชฉ๋ก์ 1๋ฒ๋ถํฐ)
WiseSaying wiseSaying = wiseSayings.get(i);
// '๋ชฉ๋ก๋ฒํธ ์๊ฐ์ด๋ฆ ๋ช
์ธ' ์ ์ถ๋ ฅ
System.out.printf("%d / %s / %s\n", wiseSaying.getId(), wiseSaying.getAuthorName(), wiseSaying.getContent());
}
}
public void remove(Rq rq) { // #6 ๋ช
์ธ ์ญ์ ํ๊ธฐ
int id = rq.getIntParam("id", -1); // id ๊ฐ์ ์ ์ํ ํด์ ๋ณด๋ด์ฃผ๊ณ , ๋ง์ฝ ์คํจํ๋ฉด -1์ ๋ณด๋ด์ค!
if (id == -1) {
System.out.println("id(์ ์)๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.");
return;
}
// ์
๋ ฅํ id์ ์ผ์นํ๋ ๋ช
์ธ ๊ฐ์ฒด๋ฅผ ์ฐพ์
WiseSaying wiseSaying = findById(id);
// ์ฐพ์ ๋ช
์ธ ๊ฐ์ฒด๋ฅผ ๋ฆฌ์คํธ์์ ์ ๊ฑฐํจ
wiseSayings.remove(wiseSaying);
System.out.printf("%d๋ฒ ๋ช
์ธ์ด ์ญ์ ๋์์ต๋๋ค.\n", id);
}
private WiseSaying findById(int id) {
for (WiseSaying wiseSaying : wiseSayings) {
if (wiseSaying.getId() == id) {
return wiseSaying;
}
}
return null;
}
}
[WiseSaying.java]
package com.example.wiseSaying.entity;
public class WiseSaying { // ๋ฑ๋ก๋ ๋ช
์ธ๊ณผ ์๊ฐ์ด๋ฆ์ ์ ์ฅ
private long id; // ๋ฑ๋ก ๋ฒํธ
private String content; // ๋ช
์ธ
private String authorName; // ์๊ฐ ์ด๋ฆ
public WiseSaying(long id, String content, String authorName) {
this.id = id;
this.content = content;
this.authorName = authorName;
}
public long getId() {
return id;
}
public String getContent() {
return content;
}
public String getAuthorName() {
return authorName;
}
}
[Application.java]
package com.example;
import com.example.system.controller.SystemController;
import com.example.wiseSaying.controller.WiseSayingController;
import java.util.*;
public class Application {
public void run() {
System.out.println("== ๋ช
์ธ ์ฑ ==");
SystemController systemController = new SystemController(); // ๊ฐ์ฒด ์์ฑ
WiseSayingController wiseSayingController = new WiseSayingController();
while(true){
System.out.print("๋ช
๋ น) "); // ์
๋ ฅ ๋ฐ๊ธฐ
String command = Container.getScanner().nextLine().trim();
Rq rq = new Rq(command);
switch (rq.getActionCode()) {
case "์ข
๋ฃ":
systemController.exit();
return;
case "๋ฑ๋ก":
wiseSayingController.write();
break;
case "๋ชฉ๋ก":
wiseSayingController.list();
break;
case "์ญ์ ":
wiseSayingController.remove(rq);
break;
}
}
}
}
[Rq.java]
package com.example;
import java.util.HashMap;
import java.util.Map;
public class Rq {
private String actionCode;
private Map<String, String> params;
public Rq(String command) {
String[] commandBits = command.split("\\?", 2);
actionCode = commandBits[0];
params = new HashMap<>();
if (commandBits.length == 1) return;
String[] paramsBits = commandBits[1].split("&");
for (String paramStr : paramsBits) {
String[] paramStrBits = paramStr.split("=", 2);
if (paramStrBits.length == 1) continue;
String key = paramStrBits[0];
String value = paramStrBits[1];
params.put(key, value);
}
}
public String getActionCode() {
return actionCode;
}
public String getParam(String name) {
return params.get(name);
}
public int getIntParam(String name, int defaultValue) {
try {
return Integer.parseInt(getParam(name));
} catch (NumberFormatException e) {
}
return defaultValue;
}
}
[Container.java]
package com.example;
import java.util.Scanner;
public class Container {
private static Scanner sc;
public static void init() {
sc = new Scanner(System.in);
}
public static void close() {
sc.close();
}
public static Scanner getScanner() {
return sc;
}
}
<7๋จ๊ณ>
โ๏ธ ๋ชฉํ: ์กด์ฌํ์ง ์๋ ๋ช ์ธ์ ์ญ์ ํ์ ๋ ์์ธ์ฒ๋ฆฌํ๊ธฐ
[WiseSayingController.java]
package com.example.wiseSaying.controller;
import com.example.Rq;
import com.example.wiseSaying.entity.WiseSaying;
import com.example.Container;
import java.util.*;
import java.util.stream.IntStream;
public class WiseSayingController {
private long lastWiseSayingId;
private final List<WiseSaying> wiseSayings;
public WiseSayingController() {
lastWiseSayingId = 0;
wiseSayings = new ArrayList<>();
}
public void write() { // #2~4 ๋ฑ๋กํ๊ธฐ
long id = lastWiseSayingId + 1; // ๋ฑ๋กํ ๋๋ง๋ค ๋ฒํธ ์ฆ๊ฐ
System.out.print("๋ช
์ธ : "); // ๋ช
์ธ๊ณผ ์๊ฐ ์ด๋ฆ์ ์
๋ ฅ๋ฐ๊ธฐ
String content = Container.getScanner().nextLine().trim();
System.out.print("์๊ฐ : ");
String authorName = Container.getScanner().nextLine().trim();
WiseSaying wiseSaying = new WiseSaying(id, content, authorName);
wiseSayings.add(wiseSaying); // ๋ฑ๋ก๋ ๋ช
์ธ๋ค์ WiseSaying ๋ฆฌ์คํธ์ ์ ์ฅ
System.out.printf("%d๋ฒ ๋ช
์ธ์ด ๋ฑ๋ก๋์์ต๋๋ค.\n", id); // ๋ฑ๋ก ๋ฌธ๊ตฌ ๋์ฐ๊ธฐ
lastWiseSayingId = id;
}
public void list() { // #5 ๋ฑ๋ก๋ ๋ช
์ธ์ ๋ชฉ๋ก ์ถ๋ ฅํ๊ธฐ๊ธฐ
System.out.println("๋ฒํธ / ์๊ฐ / ๋ช
์ธ");
System.out.println("-".repeat(30)); // -๋ฅผ 30๋ฒ ๋ฐ๋ณตํด์ ์ถ๋ ฅ -------... ๋ ๊ฐ์
for (int i = wiseSayings.size() - 1; i >= 0; i--) { // ์
๋ ฅ์ ์ญ์์ผ๋ก ์ถ๋ ฅ (๋ชฉ๋ก์ 1๋ฒ๋ถํฐ)
WiseSaying wiseSaying = wiseSayings.get(i);
// '๋ชฉ๋ก๋ฒํธ ์๊ฐ์ด๋ฆ ๋ช
์ธ' ์ ์ถ๋ ฅ
System.out.printf("%d / %s / %s\n", wiseSaying.getId(), wiseSaying.getAuthorName(), wiseSaying.getContent());
}
}
public void remove(Rq rq) { // #6 ๋ช
์ธ ์ญ์ ํ๊ธฐ
int id = rq.getIntParam("id", -1); // id ๊ฐ์ ์ ์ํ ํด์ ๋ณด๋ด์ฃผ๊ณ , ๋ง์ฝ ์คํจํ๋ฉด -1์ ๋ณด๋ด์ค!
if (id == -1) {
System.out.println("id(์ ์)๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.");
return;
}
// ์
๋ ฅํ id์ ์ผ์นํ๋ ๋ช
์ธ ๊ฐ์ฒด๋ฅผ ์ฐพ์
WiseSaying wiseSaying = findById(id);
// ์ฐพ์ ๋ช
์ธ ๊ฐ์ฒด๋ฅผ ๋ฆฌ์คํธ์์ ์ ๊ฑฐํจ
if (wiseSaying == null) { // #7 ์กด์ฌํ์ง ์๋ ๋ช
์ธ์ญ์ ์ ๋ํ ์์ธ์ฒ๋ฆฌ
System.out.printf("%d๋ฒ ๋ช
์ธ์ ์กด์ฌํ์ง ์์ต๋๋ค.\n", id);
return;
}
// ์ฐพ์ ๋ช
์ธ๊ฐ์ฒด๋ฅผ ๋ฆฌ์คํธ์์ ์ ๊ฑฐ
wiseSayings.remove(wiseSaying);
System.out.printf("%d๋ฒ ๋ช
์ธ์ด ์ญ์ ๋์์ต๋๋ค.\n", id);
}
private WiseSaying findById(int id) {
for (WiseSaying wiseSaying : wiseSayings) {
if (wiseSaying.getId() == id) {
return wiseSaying;
}
}
return null;
}
}
<8๋จ๊ณ>
โ๏ธ ๋ชฉํ: ์์ ๊ธฐ๋ฅ ๊ตฌํํ๊ธฐ
[Application.java]
package com.example;
import com.example.system.controller.SystemController;
import com.example.wiseSaying.controller.WiseSayingController;
import java.util.*;
public class Application {
public void run() {
System.out.println("== ๋ช
์ธ ์ฑ ==");
SystemController systemController = new SystemController(); // ๊ฐ์ฒด ์์ฑ
WiseSayingController wiseSayingController = new WiseSayingController();
while(true){
System.out.print("๋ช
๋ น) "); // ์
๋ ฅ ๋ฐ๊ธฐ
String command = Container.getScanner().nextLine().trim();
Rq rq = new Rq(command);
switch (rq.getActionCode()) {
case "์ข
๋ฃ":
systemController.exit();
return;
case "๋ฑ๋ก":
wiseSayingController.write();
break;
case "๋ชฉ๋ก":
wiseSayingController.list();
break;
case "์ญ์ ":
wiseSayingController.remove(rq);
break;
case "์์ ":
wiseSayingController.modify(rq);
break;
}
}
}
}
[WiseSayingController.java]
package com.example.wiseSaying.controller;
import com.example.Rq;
import com.example.wiseSaying.entity.WiseSaying;
import com.example.Container;
import java.util.*;
import java.util.stream.IntStream;
public class WiseSayingController {
private long lastWiseSayingId;
private final List<WiseSaying> wiseSayings;
public WiseSayingController() {
lastWiseSayingId = 0;
wiseSayings = new ArrayList<>();
}
private WiseSaying findById(int id) {
for (WiseSaying wiseSaying : wiseSayings) {
if (wiseSaying.getId() == id) {
return wiseSaying;
}
}
return null;
}
public void write() { // #2~4 ๋ฑ๋กํ๊ธฐ
long id = lastWiseSayingId + 1; // ๋ฑ๋กํ ๋๋ง๋ค ๋ฒํธ ์ฆ๊ฐ
System.out.print("๋ช
์ธ : "); // ๋ช
์ธ๊ณผ ์๊ฐ ์ด๋ฆ์ ์
๋ ฅ๋ฐ๊ธฐ
String content = Container.getScanner().nextLine().trim();
System.out.print("์๊ฐ : ");
String authorName = Container.getScanner().nextLine().trim();
WiseSaying wiseSaying = new WiseSaying(id, content, authorName);
wiseSayings.add(wiseSaying); // ๋ฑ๋ก๋ ๋ช
์ธ๋ค์ WiseSaying ๋ฆฌ์คํธ์ ์ ์ฅ
System.out.printf("%d๋ฒ ๋ช
์ธ์ด ๋ฑ๋ก๋์์ต๋๋ค.\n", id); // ๋ฑ๋ก ๋ฌธ๊ตฌ ๋์ฐ๊ธฐ
lastWiseSayingId = id;
}
public void list() { // #5 ๋ฑ๋ก๋ ๋ช
์ธ์ ๋ชฉ๋ก ์ถ๋ ฅํ๊ธฐ๊ธฐ
System.out.println("๋ฒํธ / ์๊ฐ / ๋ช
์ธ");
System.out.println("-".repeat(30)); // -๋ฅผ 30๋ฒ ๋ฐ๋ณตํด์ ์ถ๋ ฅ -------... ๋ ๊ฐ์
for (int i = wiseSayings.size() - 1; i >= 0; i--) { // ์
๋ ฅ์ ์ญ์์ผ๋ก ์ถ๋ ฅ (๋ชฉ๋ก์ 1๋ฒ๋ถํฐ)
WiseSaying wiseSaying = wiseSayings.get(i);
// '๋ชฉ๋ก๋ฒํธ ์๊ฐ์ด๋ฆ ๋ช
์ธ' ์ ์ถ๋ ฅ
System.out.printf("%d / %s / %s\n", wiseSaying.getId(), wiseSaying.getAuthorName(), wiseSaying.getContent());
}
}
public void remove(Rq rq) { // #6 ๋ช
์ธ ์ญ์ ํ๊ธฐ
int id = rq.getIntParam("id", -1); // id ๊ฐ์ ์ ์ํ ํด์ ๋ณด๋ด์ฃผ๊ณ , ๋ง์ฝ ์คํจํ๋ฉด -1์ ๋ณด๋ด์ค!
if (id == -1) {
System.out.println("id(์ ์)๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.");
return;
}
// ์
๋ ฅํ id์ ์ผ์นํ๋ ๋ช
์ธ ๊ฐ์ฒด๋ฅผ ์ฐพ์
WiseSaying wiseSaying = findById(id);
if (wiseSaying == null) { // #7 ์กด์ฌํ์ง ์๋ ๋ช
์ธ์ญ์ ์ ๋ํ ์์ธ์ฒ๋ฆฌ
System.out.printf("%d๋ฒ ๋ช
์ธ์ ์กด์ฌํ์ง ์์ต๋๋ค.\n", id);
return;
}
// ์ฐพ์ ๋ช
์ธ๊ฐ์ฒด๋ฅผ ๋ฆฌ์คํธ์์ ์ ๊ฑฐ
wiseSayings.remove(wiseSaying);
System.out.printf("%d๋ฒ ๋ช
์ธ์ด ์ญ์ ๋์์ต๋๋ค.\n", id);
}
public void modify(Rq rq) {
int id = rq.getIntParam("id", -1);
if (id == -1) {
System.out.println("id(์ ์)๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.");
return;
}
WiseSaying wiseSaying = findById(id); // ์
๋ ฅํ id์ ์ผ์นํ๋ ๋ช
์ธ ๊ฐ์ฒด๋ฅผ ์ฐพ์
if (wiseSaying == null) { // ์กด์ฌํ์ง ์๋ ๋ช
์ธ์ญ์ ์ ๋ํ ์์ธ์ฒ๋ฆฌ (์์ ํ ๋๋)
System.out.printf("%d๋ฒ ๋ช
์ธ์ ์กด์ฌํ์ง ์์ต๋๋ค.\n", id);
return;
}
// #8 ๋ช
์ธ ์์ ํ๊ธฐ
System.out.printf("๋ช
์ธ(๊ธฐ์กด) : %s\n", wiseSaying.getContent());
System.out.print("๋ช
์ธ : ");
String content = Container.getScanner().nextLine().trim();
System.out.printf("์๊ฐ(๊ธฐ์กด) : %s\n", wiseSaying.getAuthorName());
System.out.print("์๊ฐ : ");
String authorName = Container.getScanner().nextLine().trim();
wiseSaying.setContent(content);
wiseSaying.setAuthorName(authorName);
System.out.printf("%d๋ฒ ๋ช
์ธ์ด ์์ ๋์์ต๋๋ค.\n", id);
}
}
[WiseSaying.java]
package com.example.wiseSaying.entity;
public class WiseSaying { // ๋ฑ๋ก๋ ๋ช
์ธ๊ณผ ์๊ฐ์ด๋ฆ์ ์ ์ฅ
private long id; // ๋ฑ๋ก ๋ฒํธ
private String content; // ๋ช
์ธ
private String authorName; // ์๊ฐ ์ด๋ฆ
public WiseSaying(long id, String content, String authorName) {
this.id = id;
this.content = content;
this.authorName = authorName;
}
public long getId() {
return id;
}
public String getContent() {
return content;
}
public String getAuthorName() {
return authorName;
}
public void setContent(String content) {
this.content = content;
}
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
}
๐ ๊ฒฐ๊ณผ๋ฌผ
๐ ๋ณต์ตํ๊ธฐ
๐ก ๋ฆฌํํ ๋ง
- ์์ค์ฝ๋๋ ์ ์ง๋ณด์๊ฐ ์ฌ์ด์ผ ํ๋ค.
- ํฐ ๋ฉ์ด๋ฆฌ์ ํ๋ก๊ทธ๋จ์ ๋์ค์ ์ ์ง๋ณด์ ํ๊ธฐ ์ด๋ ค์!
- ์ ์ ํ ๋จ์๋ก ๋๋ ์ผ ์ข์
- ์์ ํ๋ก๊ทธ๋จ์ ์กฐํฉ์ผ๋ก ๋ง๋ค๊ธฐ
- ๋๋ ํ๋ก๊ทธ๋จ ๋ผ๋ฆฌ ํต์ ์ ํ ๋ ๊ท์น์ด ์์
- ์์ชฝ์ ์๋ ํต์ ์ฑ๋์ ๊ฐ์๋ ์ ์ ์๋ก ์ข๋ค
- ๋ช
๋ น์ ํ๋ฆ์ ์์ง์ ์ด๋ฉด ์ข๋ค
- ํ์ ํ๊ธฐ ์ฝ๊ธฐ ๋๋ฌธ
๐ก ์ปจํธ๋กค๋ฌ
- Appicaiton์์ ์ฃผ๋ ๋ช ๋ น์ด๋ฅผ ๋ฐ์์, ์ถ๋ ฅํด์ฃผ๋ ์ญํ
- ์ธ๋ถ์์ ๋ค์ด์ค๋ ์ ๋ ฅ์ ๋ด๋นํ๋ ๋ถ๋ถ
๐ก Rq ํด๋์ค
: request ์์ฒญ
- Rq ํด๋์ค๋ ์ ์ฐ๋๊ฑธ๊น? (์ฌ๋ฐ๋ ์์๋ฅผ ํตํด ์์๋ณด์!)
- ๊ฐ์ ์ง๊ณผ ์ ํ ๋ง๋ฆฌ๊ฐ ์๋ค๊ณ ๊ฐ์ ํ์!
- ์๊ณ ๊ธฐ๋ฅผ ๋จน๊ณ ์ถ์ ๋, ์ฐ๋ฆฌ๋ ์๋ฅผ ๋์ถ์ ์์๊ฒ ์ฃผ๊ณ ๋ฐ์์จ ๊ณ ๊ธฐ๋ฅผ ๊ตฌ์๋จน๋๋ค.
- ์ด๋๋ ๋ง์ ์์ ์ด๋ค ์์ ์ด ์๊ธฐ๋ฉด์ ์ก๋ฅ ์๋ฆฌ์ฌ๊ฐ ์๋ค๊ณ ๊ฐ์ ํ์!
- ์ด์ ๋ ์๊ณ ๊ธฐ๊ฐ ๋จน๊ณ ์ถ์ ๋, ์๋ฅผ ์ก๋ฅ ์๋ฆฌ์ฌ์๊ฒ ๊ฑด๋ค์ฃผ๋ฉด ์์์ ์ฉ๋์ ๋ง์ถฐ์ ์๋ฅด๊ณ ๋ณด๊ดํ๊ณ ์๋ฆฌํด์ค๋ค!!!
- ์ด๋, ์ก๋ฅ ์๋ฆฌ์ฌ์ ์ญํ ์ด Rq ํด๋์ค๋ผ๊ณ ๋ณผ ์ ์๋ค.
*Rq ํด๋์ค๋ฅผ ์ ์ฒด์ ์ธ ๋ช ๋ น์ด์ ๋์ ํ์ฌ ๋ผ์ฐํ ์ฝ๋๋ฅผ ๊น๋ํ๊ฒ ์ ์ ์ ์๋ค.
*getIntPharam์ ๋์ ํ์ฌ ์ ํจ์ฑ ์ฒดํฌ ๋ก์ง์ ๊ฐ๋จํ๊ฒ ์ฌ์ฉํ ์ ์๋ค.
“์ฌ์ฉ๋ฒ์ ๊ผญ ์ดํดํ๊ณ ์ฐ์!”