-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[이한솔][Week2] 웹 백엔드 과제 #7
Open
onesound6
wants to merge
1
commit into
main
Choose a base branch
from
hansol
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,33 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.edwith.webbe</groupId> | ||
<artifactId>cardmanager</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>8.0.18</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.6.1</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
37 changes: 37 additions & 0 deletions
37
hansol/week2/cardmanager/src/main/java/org/edwith/webbe/cardmanager/CardManager.java
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,37 @@ | ||
package org.edwith.webbe.cardmanager; | ||
|
||
import org.edwith.webbe.cardmanager.dao.BusinessCardManagerDao; | ||
import org.edwith.webbe.cardmanager.dto.BusinessCard; | ||
import org.edwith.webbe.cardmanager.ui.CardManagerUI; | ||
|
||
import java.util.List; | ||
|
||
public class CardManager { | ||
public static void main(String[] args){ | ||
CardManagerUI cardManagerUI = CardManagerUI.getInstance(); | ||
BusinessCardManagerDao businessCardManagerDao = new BusinessCardManagerDao(); | ||
|
||
while_loop: | ||
while(true) { | ||
cardManagerUI.printMainMenu(); | ||
int menuNumber = cardManagerUI.getMenuNumber(); | ||
switch(menuNumber){ | ||
case 1 : | ||
BusinessCard businessCard = cardManagerUI.inputBusinessCard(); | ||
businessCardManagerDao.addBusinessCard(businessCard); | ||
break; | ||
case 2 : | ||
String searchKeyword = cardManagerUI.getSearchKeyword(); | ||
List<BusinessCard> businessCardList = businessCardManagerDao.searchBusinessCard(searchKeyword); | ||
cardManagerUI.printBusinessCards(businessCardList); | ||
break; | ||
case 3 : | ||
cardManagerUI.printExitMessage(); | ||
break while_loop; | ||
default: | ||
cardManagerUI.printErrorMessage(); | ||
break; | ||
} | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...k2/cardmanager/src/main/java/org/edwith/webbe/cardmanager/dao/BusinessCardManagerDao.java
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,73 @@ | ||
package org.edwith.webbe.cardmanager.dao; | ||
|
||
import org.edwith.webbe.cardmanager.dto.BusinessCard; | ||
|
||
import com.mysql.cj.protocol.Resultset; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class BusinessCardManagerDao { | ||
private static String dburl = "jdbc:mysql://localhost:3306/connectdb?serverTimezone=UTC"; | ||
private static String dbUser = "connectuser"; | ||
private static String dbpassword = "connect123!@#"; | ||
|
||
public List<BusinessCard> searchBusinessCard(String keyword){ | ||
List<BusinessCard> list = new ArrayList<>(); | ||
|
||
try { | ||
Class.forName("com.mysql.cj.jdbc.Driver"); | ||
} catch (ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
} | ||
String sql = "SELECT name,phone,companyName FROM BusinessCard WHERE name like ?"; | ||
try (Connection conn = DriverManager.getConnection(dburl, dbUser, dbpassword); | ||
PreparedStatement ps = conn.prepareStatement(sql)){ | ||
|
||
ps.setString(1, "%"+keyword+"%"); | ||
try(ResultSet rs = ps.executeQuery()){ | ||
|
||
while(rs.next()) { | ||
String name = rs.getString(1); | ||
String phone = rs.getString(2); | ||
String companyName = rs.getString(3); | ||
|
||
BusinessCard businessCard = new BusinessCard(name,phone,companyName); | ||
list.add(businessCard); | ||
} | ||
}catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
}catch (Exception ex) { | ||
ex.printStackTrace(); | ||
} | ||
return list; | ||
} | ||
|
||
public BusinessCard addBusinessCard(BusinessCard businessCard){ | ||
int insertCount = 0; | ||
|
||
try { | ||
Class.forName("com.mysql.cj.jdbc.Driver"); | ||
} catch (ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
} //Driver Loading | ||
String sql = "INSERT INTO BusinessCard (name,phone,companyName) VALUES(?,?,?)"; | ||
try (Connection conn = DriverManager.getConnection(dburl, dbUser, dbpassword); | ||
PreparedStatement ps = conn.prepareStatement(sql)){ | ||
ps.setString(1, businessCard.getName()); | ||
ps.setString(2, businessCard.getPhone()); | ||
ps.setString(3, businessCard.getCompanyName()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Date값이 안들어가 있네요~ DB에 date 관련된 타입의 컬럼 추가 하시고, 값한번 넣어보면서 테스트해보세요 |
||
|
||
insertCount = ps.executeUpdate(); | ||
}catch (Exception ex) { | ||
ex.printStackTrace(); | ||
} | ||
return businessCard; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
hansol/week2/cardmanager/src/main/java/org/edwith/webbe/cardmanager/dto/BusinessCard.java
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,59 @@ | ||
package org.edwith.webbe.cardmanager.dto; | ||
|
||
import java.util.Date; | ||
|
||
public class BusinessCard { | ||
private String name; | ||
private String phone; | ||
private String companyName; | ||
private Date createDate; | ||
|
||
public BusinessCard(String name, String phone, String companyName) { | ||
this.name = name; | ||
this.phone = phone; | ||
this.companyName = companyName; | ||
this.createDate = new Date(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 생성자가 생성되는 시점에 Date값이 초기화되는 것을 인지해야합니다. BusinessCard card = new BusinessCard() // 이 시점의 시간이 저장됨 |
||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getPhone() { | ||
return phone; | ||
} | ||
|
||
public void setPhone(String phone) { | ||
this.phone = phone; | ||
} | ||
|
||
public String getCompanyName() { | ||
return companyName; | ||
} | ||
|
||
public void setCompanyName(String companyName) { | ||
this.companyName = companyName; | ||
} | ||
|
||
public Date getCreateDate() { | ||
return createDate; | ||
} | ||
|
||
public void setCreateDate(Date createDate) { | ||
this.createDate = createDate; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "BusinessCard{" + | ||
"name='" + name + '\'' + | ||
", phone='" + phone + '\'' + | ||
", companyName='" + companyName + '\'' + | ||
", createDate=" + createDate + | ||
'}'; | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
hansol/week2/cardmanager/src/main/java/org/edwith/webbe/cardmanager/ui/CardManagerUI.java
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,81 @@ | ||
package org.edwith.webbe.cardmanager.ui; | ||
|
||
import org.edwith.webbe.cardmanager.dto.BusinessCard; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.util.List; | ||
|
||
public class CardManagerUI { | ||
private BufferedReader in; | ||
|
||
private CardManagerUI(){ | ||
in = new BufferedReader(new InputStreamReader(System.in)); | ||
} | ||
|
||
private static CardManagerUI instance = new CardManagerUI(); | ||
|
||
public static CardManagerUI getInstance(){ | ||
return instance; | ||
} | ||
|
||
public void printMainMenu(){ | ||
System.out.println("------------------------"); | ||
System.out.println("1. 명함 입력"); | ||
System.out.println("2. 명함 검색"); | ||
System.out.println("3. 종료"); | ||
System.out.println("------------------------"); | ||
System.out.print("메뉴를 입력하세요 : "); | ||
} | ||
|
||
public int getMenuNumber(){ | ||
try { | ||
int menuNumber = Integer.parseInt(in.readLine()); | ||
return menuNumber; | ||
}catch(Exception ex){ | ||
return -1; // 숫자로 변환 못할 경우 -1을 리턴한다. | ||
} | ||
} | ||
|
||
public BusinessCard inputBusinessCard(){ | ||
try { | ||
System.out.print("이름을 입력하세요 : "); | ||
String name = in.readLine(); | ||
System.out.print("전화번호를 입력하세요. : "); | ||
String phone = in.readLine(); | ||
System.out.print("회사 이름을 입력하세요. : "); | ||
String companyName = in.readLine(); | ||
BusinessCard businessCard = new BusinessCard(name, phone, companyName); | ||
return businessCard; | ||
}catch(Exception ex){ | ||
System.out.println("잘못된 값을 입력했습니다. "); | ||
return null; | ||
} | ||
} | ||
|
||
public String getSearchKeyword(){ | ||
try { | ||
System.out.print("검색할 이름을 입력하세요. (like검색) : "); | ||
String searchKeyword = in.readLine(); | ||
return searchKeyword; | ||
}catch(Exception ex){ | ||
System.out.println("잘못된 값을 입력했습니다. "); | ||
return null; | ||
} | ||
} | ||
|
||
public void printBusinessCards(List<BusinessCard> businessCardList){ | ||
for(BusinessCard businessCard: businessCardList){ | ||
System.out.println(businessCard); | ||
System.out.println("---------------------------------------------------------------"); | ||
} | ||
} | ||
|
||
public void printExitMessage(){ | ||
System.out.println("프로그램을 종료합니다. :-) "); | ||
} | ||
|
||
public void printErrorMessage(){ | ||
System.out.println("잘못된 입력입니다."); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 시점에 date값이 초기화되기 떄문에 사용자에게 보여질때 생성된 시점이 아니라 호출된 시점의 시간이 보여지겠죠. 또 호출될 때마다 시간이 다르게 보일겁니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
그리고 라인 삐뚤빼뚤한거 라인포매팅으로 정리한번 해주세요~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IDE들에서 라인포매팅을 잘 지원해주고 있어요!
https://blog.jetbrains.com/idea/2020/06/code-formatting/