-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from NHNAcademy4th-CIA/leejungbum
Leejungbum 코드리뷰 요청합니다.
- Loading branch information
Showing
9 changed files
with
1,118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.nhnacademy.jungbum; | ||
|
||
import org.nhnacademy.jungbum.*; | ||
|
||
/** | ||
* Hello world! | ||
* | ||
*/ | ||
public class App | ||
{ | ||
public static void main( String[] args ) | ||
{ | ||
new Quiz1(); | ||
// new Quiz2(); | ||
// new Quiz3(); | ||
// new Quiz4(); | ||
// new Quiz6(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.nhnacademy.jungbum; | ||
|
||
|
||
import java.util.*; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/*** | ||
* 전화번호부 treemap으로 구현 | ||
*/ | ||
public class Quiz1 { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(Quiz1.class); | ||
|
||
public static void main(String[] args) { | ||
PhoneDirectory phoneBook = new PhoneDirectory(); | ||
logger.info("전화번호부에 추가할 사람을 이름 전화번호 순으로 입력해주세요.(만약 끝내고싶다면 빈칸을 입력해주세요)"); | ||
try { | ||
phoneBook.addNumber(); | ||
} catch (IllegalArgumentException |NoSuchElementException e) { | ||
logger.warn(e.toString()); | ||
} | ||
logger.info("전화번호부에 등록된 모든사람을 조회합니다."); | ||
phoneBook.print(); | ||
} | ||
|
||
} | ||
|
||
/*** | ||
* 전화번호부 | ||
*/ | ||
class PhoneDirectory { | ||
private static final Logger logger = LoggerFactory.getLogger(Quiz1.class); | ||
|
||
private static Scanner scanner = new Scanner(System.in); | ||
|
||
private TreeMap<String, String> PhoneEntry; | ||
|
||
public PhoneDirectory() { | ||
PhoneEntry = new TreeMap<String, String>(); | ||
} | ||
|
||
// public String getNumber( String name ) { | ||
// return PhoneEntry.get(name); | ||
// } | ||
|
||
|
||
/*** | ||
* 전화번호 추가 | ||
* @throws IllegalArgumentException 이름이나 번호가 없는경우 | ||
*/ | ||
public void addNumber() throws IllegalArgumentException { | ||
String value; | ||
StringTokenizer stringTokenizer; | ||
while (!(value = scanner.nextLine()).equals("")) { | ||
stringTokenizer = new StringTokenizer(value); | ||
String name = stringTokenizer.nextToken(); | ||
String number = stringTokenizer.nextToken(); | ||
if (name == null) | ||
throw new IllegalArgumentException("이름이 비어 등록이 불가능합니다."); | ||
if (number == null) | ||
throw new IllegalArgumentException("번호가 비어 등록이 불가능합니다."); | ||
PhoneEntry.put(name, number); | ||
|
||
} | ||
} | ||
|
||
|
||
/*** | ||
* 모든사람 출력 | ||
*/ | ||
public void print() { | ||
for (Map.Entry<String, String> entry : PhoneEntry.entrySet()) | ||
logger.info(entry.getKey() + ": " + entry.getValue()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package org.nhnacademy.jungbum; | ||
|
||
import java.util.Scanner; | ||
import java.util.Set; | ||
import java.util.StringTokenizer; | ||
import java.util.TreeSet; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/*** | ||
* 집합 - 합집합, 교집합, 차집합 만들기 | ||
*/ | ||
public class Quiz2 { | ||
|
||
public Quiz2() { | ||
new Calc(); | ||
} | ||
} | ||
|
||
/*** | ||
* 집합 계산기 | ||
*/ | ||
class Calc { | ||
|
||
private CustomSet a; | ||
private CustomSet b; | ||
private String op; | ||
private final Scanner scanner = new Scanner(System.in); | ||
private Logger logger = LoggerFactory.getLogger(Quiz2.class); | ||
|
||
/*** | ||
* 글자제외하고 추가 | ||
*/ | ||
public Calc() { | ||
String line; | ||
while (!(line = scanner.nextLine()).equals("")) { | ||
logger.info(line); | ||
a = new CustomSet(); | ||
b = new CustomSet(); | ||
int count = 0; | ||
StringTokenizer stringTokenizer = new StringTokenizer(line); | ||
while (stringTokenizer.hasMoreTokens()) { | ||
String numString = stringTokenizer.nextToken(); | ||
System.out.println(numString); | ||
if (numString.equals("[") || numString.equals("]")) { | ||
count++; | ||
continue; | ||
} | ||
if (numString.equals("+") || numString.equals("-") || numString.equals("*")) { | ||
op = numString; | ||
continue; | ||
} | ||
try { | ||
int num = Integer.parseInt(numString.split(",")[0]); | ||
numCheck(num); | ||
if (count == 1 || count == 2) { | ||
System.out.println("1 "+num); | ||
a.getSet().add(num); | ||
} else { | ||
System.out.println("2 "+num); | ||
|
||
b.getSet().add(num); | ||
} | ||
} catch (IllegalArgumentException e) { | ||
logger.warn(e.toString()); | ||
} | ||
} | ||
logger.info("{}",a.getSet()); | ||
logger.info("{}",b.getSet()); | ||
printResult(); | ||
} | ||
} | ||
private void printResult(){ | ||
switch (op) { | ||
case "+": | ||
a.addAll(b); | ||
logger.info("{}", a.getSet()); | ||
break; | ||
case "-": | ||
a.removeAll(b); | ||
logger.info("{}", a.getSet()); | ||
break; | ||
case "*": | ||
a.retrainAll(b); | ||
logger.info("{}", a.getSet()); | ||
break; | ||
} | ||
} | ||
/*** | ||
* 숫자가 아닐때 예외처리 | ||
* @param num 숫자후보 | ||
* @throws IllegalArgumentException 숫자가아닐때 | ||
*/ | ||
public void numCheck(int num) throws IllegalArgumentException { | ||
if (num == -1 || num == -2) | ||
throw new IllegalArgumentException("숫자가 아닌 값이 있습니다."); | ||
} | ||
} | ||
|
||
/*** | ||
* 사용자 정의 커스텀 셋 | ||
*/ | ||
class CustomSet { | ||
private TreeSet<Integer> set; | ||
|
||
public CustomSet() { | ||
set = new TreeSet<>(); | ||
} | ||
|
||
/*** | ||
* 합집합 | ||
* @param set 집합 | ||
*/ | ||
public void addAll(CustomSet set) { | ||
getSet().addAll(set.getSet()); | ||
} | ||
|
||
/*** | ||
* 교집합 | ||
* @param set 집합 | ||
*/ | ||
public void retrainAll(CustomSet set) { | ||
getSet().retainAll(set.getSet()); | ||
} | ||
|
||
/*** | ||
* 차집합 | ||
* @param set 집합 | ||
*/ | ||
public void removeAll(CustomSet set) { | ||
getSet().removeAll(set.getSet()); | ||
} | ||
|
||
/*** | ||
* 집합 읽기 | ||
* @return 집합값 | ||
*/ | ||
public Set<Integer> getSet() { | ||
return set; | ||
} | ||
|
||
} |
Oops, something went wrong.