๐2022-08-11๐
dISample project
file new maven project > create a sample project ์ฒดํฌ > Group id : com.gyuone
common
DuplicateMemberException.java
package com.gyuone.common;
public class DuplicateMemberException extends RuntimeException{
public DuplicateMemberException(String message) {
super(message);
}
}
MemberNotFoundException.java
package com.gyuone.common;
public class MemberNotFoundException extends RuntimeException{
}
WrongIdPasswordException.java
package com.gyuone.common;
public class WrongIdPasswordException extends RuntimeException{
}
config
AppConfig1.java
package com.gyuone.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.gyuone.dao.MemberDao;
import com.gyuone.service.MemberPrinter;
@Configuration
public class AppConf1 {
@Bean
public MemberDao memberDao() {
return new MemberDao();
}
@Bean
public MemberPrinter memberPrinter() {
return new MemberPrinter();
}
}
AppConfig2.java
package com.gyuone.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.gyuone.dao.MemberDao;
import com.gyuone.service.ChangePasswordService;
import com.gyuone.service.MemberInfoPrinter;
import com.gyuone.service.MemberListPrinter;
import com.gyuone.service.MemberPrinter;
import com.gyuone.service.MemberRegisterService;
import com.gyuone.service.VersionPrinter;
@Configuration
public class AppConf2 {
@Autowired
private MemberDao memberDao;
@Autowired
private MemberPrinter memberPrinter;
@Bean
public MemberRegisterService memberRegSvc() { // MemberRegisterService์ ์๋ณ์๋ memberRegSvc
return new MemberRegisterService(memberDao);
}
@Bean
public ChangePasswordService changePwdSvc() {
ChangePasswordService pwdSvc = new ChangePasswordService();
pwdSvc.setMemberDao(memberDao);
return pwdSvc;
}
@Bean
public MemberListPrinter listPrinter() {
return new MemberListPrinter(memberDao, memberPrinter);
}
@Bean
public MemberInfoPrinter infoPrinter() {
MemberInfoPrinter infoPrinter = new MemberInfoPrinter();
infoPrinter.setMemberDao(memberDao);
infoPrinter.setPrinter(memberPrinter);
return infoPrinter;
}
@Bean
public VersionPrinter versionPrinter() {
VersionPrinter versionPrinter = new VersionPrinter();
versionPrinter.setMajorVersion(5);
versionPrinter.setMinorVersion(0);
return versionPrinter;
}
}
AppConfImport.java
package com.gyuone.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import com.gyuone.dao.MemberDao;
import com.gyuone.service.MemberPrinter;
@Configuration
@Import({AppConf2.class})
public class AppConfImport {
@Bean
public MemberDao memberDao() {
return new MemberDao();
}
@Bean
public MemberPrinter memberPrinter() {
return new MemberPrinter();
}
}
AppCtx.java
package com.gyuone.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.gyuone.dao.MemberDao;
import com.gyuone.service.ChangePasswordService;
import com.gyuone.service.MemberInfoPrinter;
import com.gyuone.service.MemberListPrinter;
import com.gyuone.service.MemberPrinter;
import com.gyuone.service.MemberRegisterService;
import com.gyuone.service.VersionPrinter;
@Configuration
public class AppCtx {
@Bean
public MemberDao memberDao() {
return new MemberDao();
}
@Bean
public MemberRegisterService memberRegSvc() { // MemberRegisterService์ ์๋ณ์๋ memberRegSvc
return new MemberRegisterService(memberDao());
}
@Bean
public ChangePasswordService changePwdSvc() {
ChangePasswordService pwdSvc = new ChangePasswordService();
pwdSvc.setMemberDao(memberDao());
return pwdSvc;
}
@Bean
public MemberPrinter memberPrinter() {
return new MemberPrinter();
}
@Bean
public MemberListPrinter listPrinter() {
return new MemberListPrinter(memberDao(), memberPrinter());
}
@Bean
public MemberInfoPrinter infoPrinter() {
MemberInfoPrinter infoPrinter = new MemberInfoPrinter();
infoPrinter.setMemberDao(memberDao());
infoPrinter.setPrinter(memberPrinter());
return infoPrinter;
}
@Bean
public VersionPrinter versionPrinter() {
VersionPrinter versionPrinter = new VersionPrinter();
versionPrinter.setMajorVersion(5);
versionPrinter.setMinorVersion(0);
return versionPrinter;
}
}
dao
MemberDao.java
package com.gyuone.dao;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import com.gyuone.model.Member;
public class MemberDao {
private static long nextId = 0;
private Map<String, Member> map = new HashMap<>();
public Member selectByEmail(String email) {
return map.get(email);
}
public void insert(Member member) {
member.setId(++nextId);
map.put(member.getEmail(), member);
}
public void update(Member member) {
map.put(member.getEmail(), member);
}
public Collection<Member> selectAll(){
return map.values();
}
}
main
MainForAssembler.java
package com.gyuone.main;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.DuplicateFormatFlagsException;
import com.gyuone.common.MemberNotFoundException;
import com.gyuone.common.WrongIdPasswordException;
import com.gyuone.model.RegisterRequest;
import com.gyuone.service.Assembler;
import com.gyuone.service.ChangePasswordService;
import com.gyuone.service.MemberRegisterService;
public class MainForAssembler {
private static Assembler assembler = new Assembler();
public static void main(String[] args) throws Exception{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while(true) {
System.out.print("๋ช
๋ น์ด ์
๋ ฅ : ");
String command = reader.readLine();
if(command.equalsIgnoreCase("exit")) {
System.out.println("ํ๋ก๊ทธ๋จ ์ข
๋ฃ");
break;
}
if(command.startsWith("new ")) {
processNewCommand(command.split(" "));
continue;
}else if(command.startsWith("change ")) {
processChangeCommand(command.split(" "));
continue;
}
printHelp();
}
}
private static void printHelp() {
System.out.println();
System.out.println("์๋ชป๋ ๋ช
๋ น์
๋๋ค. ์๋ ๋ช
๋ น์ด ์ฌ์ฉ๋ฒ์ ํ์ธํ์ธ์.");
System.out.println("๋ช
๋ น์ด ์ฌ์ฉ๋ฒ:");
System.out.println("new ์ด๋ฉ์ผ ์ด๋ฆ ์ํธ ์ํธํ์ธ");
System.out.println("change ์ด๋ฉ์ผ ํ์ฌ๋น๋ฒ ๋ณ๊ฒฝ๋น๋ฒ");
System.out.println();
}
private static void processNewCommand(String[] arg) {
if(arg.length != 5) {
printHelp();
return;
}
MemberRegisterService regSvc = assembler.getMemberRegisterService();
RegisterRequest req = new RegisterRequest();
req.setEmail(arg[1]);
req.setName(arg[2]);
req.setPassword(arg[3]);
req.setConfirmPassword(arg[4]);
if(!req.isPasswordEqualToConfirmPassword()) {
System.out.println("์ํธ ํ์ธ์ด ๋ง์ง ์์ต๋๋ค.");
System.out.println();
return;
}
try {
regSvc.register(req);
System.out.println("๋ฑ๋ก ์๋ฃ");
System.out.println();
} catch (DuplicateFormatFlagsException e) {
// TODO: handle exception
System.out.println("์ด๋ฏธ ์กด์ฌํ๋ ์ด๋ฉ์ผ์
๋๋ค.");
System.out.println();
}
}
private static void processChangeCommand(String[] arg) {
if(arg.length != 4) {
printHelp();
return;
}
ChangePasswordService changePwdSvc = assembler.getChangePasswordService();
try {
changePwdSvc.changePassword(arg[1], arg[2], arg[3]);
System.out.println("์ํธ ๋ณ๊ฒฝ ์๋ฃ");
} catch (MemberNotFoundException e) {
// TODO: handle exception
System.out.println("์กด์ฌํ์ง ์๋ ์ด๋ฉ์ผ์
๋๋ค.");
System.out.println();
} catch (WrongIdPasswordException e) {
System.out.println("์ด๋ฉ์ผ๊ณผ ์ํธ ๋ถ์ผ์น");
System.out.println();
}
}
}
MainForImport.java
package com.gyuone.main;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.DuplicateFormatFlagsException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.gyuone.common.DuplicateMemberException;
import com.gyuone.common.MemberNotFoundException;
import com.gyuone.common.WrongIdPasswordException;
import com.gyuone.config.AppConfImport;
import com.gyuone.config.AppCtx;
import com.gyuone.model.RegisterRequest;
import com.gyuone.service.ChangePasswordService;
import com.gyuone.service.MemberInfoPrinter;
import com.gyuone.service.MemberListPrinter;
import com.gyuone.service.MemberRegisterService;
import com.gyuone.service.VersionPrinter;
public class MainForImport {
private static ApplicationContext ctx = null;
public static void main(String[] args) throws Exception{
ctx = new AnnotationConfigApplicationContext(AppConfImport.class);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while(true) {
System.out.print("๋ช
๋ น์ด ์
๋ ฅ: ");
String command = reader.readLine();
if(command.equalsIgnoreCase("exit")) {
System.out.println("ํ๋ก๊ทธ๋จ ์ข
๋ฃ");
break;
}
if(command.startsWith("new ")) {
processNewCommand(command.split(" "));
continue;
}else if(command.startsWith("change ")) {
processChangeCommand(command.split(" "));
continue;
}else if(command.startsWith("list")){
processListCommand();
continue;
}else if(command.startsWith("info")) {
processInfoCommand(command.split(" "));
continue;
}else if(command.startsWith("version")) {
processVersionCommand();
continue;
}
printHelp();
}
}
private static void printHelp() {
System.out.println();
System.out.println("์๋ชป๋ ๋ช
๋ น์
๋๋ค. ์๋ ๋ช
๋ น์ด ์ฌ์ฉ๋ฒ์ ํ์ธํ์ธ์.");
System.out.println("๋ช
๋ น์ด ์ฌ์ฉ๋ฒ:");
System.out.println("new ์ด๋ฉ์ผ ์ด๋ฆ ์ํธ ์ํธํ์ธ");
System.out.println("change ์ด๋ฉ์ผ ํ์ฌ๋น๋ฒ ๋ณ๊ฒฝ๋น๋ฒ");
System.out.println();
}
private static void processNewCommand(String[] arg) {
if(arg.length != 5) {
printHelp();
return;
}
MemberRegisterService regSvc = ctx.getBean("memberRegSvc", MemberRegisterService.class);
RegisterRequest req = new RegisterRequest();
req.setEmail(arg[1]);
req.setName(arg[2]);
req.setPassword(arg[3]);
req.setConfirmPassword(arg[4]);
if(!req.isPasswordEqualToConfirmPassword()) {
System.out.println("์ํธ ํ์ธ์ด ๋ง์ง ์์ต๋๋ค.");
System.out.println();
return;
}
try {
regSvc.register(req);
System.out.println("๋ฑ๋ก ์๋ฃ");
System.out.println();
} catch (DuplicateMemberException e) {
// TODO: handle exception
System.out.println("์ด๋ฏธ ์กด์ฌํ๋ ์ด๋ฉ์ผ์
๋๋ค.");
System.out.println();
}
}
private static void processChangeCommand(String[] arg) {
if(arg.length != 4) {
printHelp();
return;
}
ChangePasswordService changePwdSvc = ctx.getBean("changePwdSvc", ChangePasswordService.class);
try {
changePwdSvc.changePassword(arg[1], arg[2], arg[3]);
System.out.println("์ํธ ๋ณ๊ฒฝ ์๋ฃ");
System.out.println();
} catch (MemberNotFoundException e) {
// TODO: handle exception
System.out.println("์กด์ฌํ์ง ์๋ ์ด๋ฉ์ผ์
๋๋ค.");
System.out.println();
} catch (WrongIdPasswordException e) {
System.out.println("์ด๋ฉ์ผ๊ณผ ์ํธ ๋ถ์ผ์นํฉ๋๋ค.");
System.out.println();
}
}
private static void processListCommand() {
MemberListPrinter listPrinter = ctx.getBean("listPrinter", MemberListPrinter.class); // "listPrinter", MemberListPrinter.class๊ฐ ์๋ณ์, ํ์
listPrinter.printAll();
}
private static void processInfoCommand(String[] arg) {
if(arg.length != 2) {
printHelp();
return;
}
MemberInfoPrinter infoPrinter = ctx.getBean("infoPrinter", MemberInfoPrinter.class);
infoPrinter.printMemberInfo(arg[1]);
}
private static void processVersionCommand() {
VersionPrinter versionPrinter = ctx.getBean("versionPrinter", VersionPrinter.class);
versionPrinter.print();
}
}
MainForSpring.java
package com.gyuone.main;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.DuplicateFormatFlagsException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.gyuone.common.DuplicateMemberException;
import com.gyuone.common.MemberNotFoundException;
import com.gyuone.common.WrongIdPasswordException;
import com.gyuone.config.AppCtx;
import com.gyuone.model.RegisterRequest;
import com.gyuone.service.ChangePasswordService;
import com.gyuone.service.MemberInfoPrinter;
import com.gyuone.service.MemberListPrinter;
import com.gyuone.service.MemberRegisterService;
import com.gyuone.service.VersionPrinter;
public class MainForSpring {
private static ApplicationContext ctx = null;
public static void main(String[] args) throws Exception{
ctx = new AnnotationConfigApplicationContext(AppCtx.class);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while(true) {
System.out.print("๋ช
๋ น์ด ์
๋ ฅ: ");
String command = reader.readLine();
if(command.equalsIgnoreCase("exit")) {
System.out.println("ํ๋ก๊ทธ๋จ ์ข
๋ฃ");
break;
}
if(command.startsWith("new ")) {
processNewCommand(command.split(" "));
continue;
}else if(command.startsWith("change ")) {
processChangeCommand(command.split(" "));
continue;
}else if(command.startsWith("list")){
processListCommand();
continue;
}else if(command.startsWith("info")) {
processInfoCommand(command.split(" "));
continue;
}else if(command.startsWith("version")) {
processVersionCommand();
continue;
}
printHelp();
}
}
private static void printHelp() {
System.out.println();
System.out.println("์๋ชป๋ ๋ช
๋ น์
๋๋ค. ์๋ ๋ช
๋ น์ด ์ฌ์ฉ๋ฒ์ ํ์ธํ์ธ์.");
System.out.println("๋ช
๋ น์ด ์ฌ์ฉ๋ฒ:");
System.out.println("new ์ด๋ฉ์ผ ์ด๋ฆ ์ํธ ์ํธํ์ธ");
System.out.println("change ์ด๋ฉ์ผ ํ์ฌ๋น๋ฒ ๋ณ๊ฒฝ๋น๋ฒ");
System.out.println();
}
private static void processNewCommand(String[] arg) {
if(arg.length != 5) {
printHelp();
return;
}
MemberRegisterService regSvc = ctx.getBean("memberRegSvc", MemberRegisterService.class);
RegisterRequest req = new RegisterRequest();
req.setEmail(arg[1]);
req.setName(arg[2]);
req.setPassword(arg[3]);
req.setConfirmPassword(arg[4]);
if(!req.isPasswordEqualToConfirmPassword()) {
System.out.println("์ํธ ํ์ธ์ด ๋ง์ง ์์ต๋๋ค.");
System.out.println();
return;
}
try {
regSvc.register(req);
System.out.println("๋ฑ๋ก ์๋ฃ");
System.out.println();
} catch (DuplicateMemberException e) {
// TODO: handle exception
System.out.println("์ด๋ฏธ ์กด์ฌํ๋ ์ด๋ฉ์ผ์
๋๋ค.");
System.out.println();
}
}
private static void processChangeCommand(String[] arg) {
if(arg.length != 4) {
printHelp();
return;
}
ChangePasswordService changePwdSvc = ctx.getBean("changePwdSvc", ChangePasswordService.class);
try {
changePwdSvc.changePassword(arg[1], arg[2], arg[3]);
System.out.println("์ํธ ๋ณ๊ฒฝ ์๋ฃ");
System.out.println();
} catch (MemberNotFoundException e) {
// TODO: handle exception
System.out.println("์กด์ฌํ์ง ์๋ ์ด๋ฉ์ผ์
๋๋ค.");
System.out.println();
} catch (WrongIdPasswordException e) {
System.out.println("์ด๋ฉ์ผ๊ณผ ์ํธ ๋ถ์ผ์นํฉ๋๋ค.");
System.out.println();
}
}
private static void processListCommand() {
MemberListPrinter listPrinter = ctx.getBean("listPrinter", MemberListPrinter.class); // "listPrinter", MemberListPrinter.class๊ฐ ์๋ณ์, ํ์
listPrinter.printAll();
}
private static void processInfoCommand(String[] arg) {
if(arg.length != 2) {
printHelp();
return;
}
MemberInfoPrinter infoPrinter = ctx.getBean("infoPrinter", MemberInfoPrinter.class);
infoPrinter.printMemberInfo(arg[1]);
}
private static void processVersionCommand() {
VersionPrinter versionPrinter = ctx.getBean("versionPrinter", VersionPrinter.class);
versionPrinter.print();
}
}
MainForSpring2.java
package com.gyuone.main;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.gyuone.common.DuplicateMemberException;
import com.gyuone.common.MemberNotFoundException;
import com.gyuone.common.WrongIdPasswordException;
import com.gyuone.config.AppConf1;
import com.gyuone.config.AppConf2;
import com.gyuone.model.RegisterRequest;
import com.gyuone.service.ChangePasswordService;
import com.gyuone.service.MemberInfoPrinter;
import com.gyuone.service.MemberListPrinter;
import com.gyuone.service.MemberRegisterService;
import com.gyuone.service.VersionPrinter;
public class MainForSpring2 {
private static ApplicationContext ctx = null;
public static void main(String[] args) throws Exception{
ctx = new AnnotationConfigApplicationContext(AppConf1.class, AppConf2.class);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while(true) {
System.out.print("๋ช
๋ น์ด ์
๋ ฅ: ");
String command = reader.readLine();
if(command.equalsIgnoreCase("exit")) {
System.out.println("ํ๋ก๊ทธ๋จ ์ข
๋ฃ");
break;
}
if(command.startsWith("new ")) {
processNewCommand(command.split(" "));
continue;
}else if(command.startsWith("change ")) {
processChangeCommand(command.split(" "));
continue;
}else if(command.startsWith("list")){
processListCommand();
continue;
}else if(command.startsWith("info")) {
processInfoCommand(command.split(" "));
continue;
}else if(command.startsWith("version")) {
processVersionCommand();
continue;
}
printHelp();
}
}
private static void printHelp() {
System.out.println();
System.out.println("์๋ชป๋ ๋ช
๋ น์
๋๋ค. ์๋ ๋ช
๋ น์ด ์ฌ์ฉ๋ฒ์ ํ์ธํ์ธ์.");
System.out.println("๋ช
๋ น์ด ์ฌ์ฉ๋ฒ:");
System.out.println("new ์ด๋ฉ์ผ ์ด๋ฆ ์ํธ ์ํธํ์ธ");
System.out.println("change ์ด๋ฉ์ผ ํ์ฌ๋น๋ฒ ๋ณ๊ฒฝ๋น๋ฒ");
System.out.println();
}
private static void processNewCommand(String[] arg) {
if(arg.length != 5) {
printHelp();
return;
}
MemberRegisterService regSvc = ctx.getBean("memberRegSvc", MemberRegisterService.class);
RegisterRequest req = new RegisterRequest();
req.setEmail(arg[1]);
req.setName(arg[2]);
req.setPassword(arg[3]);
req.setConfirmPassword(arg[4]);
if(!req.isPasswordEqualToConfirmPassword()) {
System.out.println("์ํธ ํ์ธ์ด ๋ง์ง ์์ต๋๋ค.");
System.out.println();
return;
}
try {
regSvc.register(req);
System.out.println("๋ฑ๋ก ์๋ฃ");
System.out.println();
} catch (DuplicateMemberException e) {
// TODO: handle exception
System.out.println("์ด๋ฏธ ์กด์ฌํ๋ ์ด๋ฉ์ผ์
๋๋ค.");
System.out.println();
}
}
private static void processChangeCommand(String[] arg) {
if(arg.length != 4) {
printHelp();
return;
}
ChangePasswordService changePwdSvc = ctx.getBean("changePwdSvc", ChangePasswordService.class);
try {
changePwdSvc.changePassword(arg[1], arg[2], arg[3]);
System.out.println("์ํธ ๋ณ๊ฒฝ ์๋ฃ");
System.out.println();
} catch (MemberNotFoundException e) {
// TODO: handle exception
System.out.println("์กด์ฌํ์ง ์๋ ์ด๋ฉ์ผ์
๋๋ค.");
System.out.println();
} catch (WrongIdPasswordException e) {
System.out.println("์ด๋ฉ์ผ๊ณผ ์ํธ ๋ถ์ผ์นํฉ๋๋ค.");
System.out.println();
}
}
private static void processListCommand() {
MemberListPrinter listPrinter = ctx.getBean("listPrinter", MemberListPrinter.class); // "listPrinter", MemberListPrinter.class๊ฐ ์๋ณ์, ํ์
listPrinter.printAll();
}
private static void processInfoCommand(String[] arg) {
if(arg.length != 2) {
printHelp();
return;
}
MemberInfoPrinter infoPrinter = ctx.getBean("infoPrinter", MemberInfoPrinter.class);
infoPrinter.printMemberInfo(arg[1]);
}
private static void processVersionCommand() {
VersionPrinter versionPrinter = ctx.getBean("versionPrinter", VersionPrinter.class);
versionPrinter.print();
}
}
model
Member.java
package com.gyuone.model;
import java.time.LocalDateTime;
import com.gyuone.common.WrongIdPasswordException;
public class Member {
private long id;
private String email;
private String password;
private String name;
private LocalDateTime registerDateTime;
public Member(String email, String password, String name, LocalDateTime registerDateTime) {
super();
this.email = email;
this.password = password;
this.name = name;
this.registerDateTime = registerDateTime; // ์์ฑ์ ๋ง๋ค์ด์ฃผ๊ธฐ
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getEmail() {
return email;
}
public String getPassword() {
return password;
}
public String getName() {
return name;
}
public LocalDateTime getRegisterDateTime() {
return registerDateTime;
}
public void changePassword(String oldPassword, String newPassword) {
if(!password.equals(oldPassword)) {
throw new WrongIdPasswordException();
}
this.password = newPassword;
}
}
RegisterRequest.java
package com.gyuone.model;
public class RegisterRequest {
private String email;
private String password;
private String confirmPassword;
private String name;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getConfirmPassword() {
return confirmPassword;
}
public void setConfirmPassword(String confirmPassword) {
this.confirmPassword = confirmPassword;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isPasswordEqualToConfirmPassword() {
return password.equals(confirmPassword);
}
}
service
Assembler.java
package com.gyuone.service;
import com.gyuone.dao.MemberDao;
public class Assembler {
private MemberDao memberDao;
private MemberRegisterService regSvc;
private ChangePasswordService pwdSvc;
public Assembler() {
memberDao = new MemberDao();
regSvc = new MemberRegisterService(memberDao);
pwdSvc = new ChangePasswordService();
pwdSvc.setMemberDao(memberDao);
}
public MemberDao getMemberDao() {
return memberDao;
}
public MemberRegisterService getMemberRegisterService() {
return regSvc;
}
public ChangePasswordService getChangePasswordService() {
return pwdSvc;
}
}
ChangePasswordService.java
package com.gyuone.service;
import com.gyuone.common.MemberNotFoundException;
import com.gyuone.dao.MemberDao;
import com.gyuone.model.Member;
public class ChangePasswordService {
private MemberDao memberDao;
public void setMemberDao(MemberDao memberDao) {
this.memberDao = memberDao;
}
public void changePassword(String email, String oldPwd, String newPwd) {
Member member = memberDao.selectByEmail(email);
if(member == null) {
throw new MemberNotFoundException();
}
member.changePassword(oldPwd, newPwd);
memberDao.update(member);
}
}
MemberInfoPrinter.java
package com.gyuone.service;
import com.gyuone.dao.MemberDao;
import com.gyuone.model.Member;
public class MemberInfoPrinter {
private MemberDao memberDao;
private MemberPrinter printer;
public void setMemberDao(MemberDao memberDao) {
this.memberDao = memberDao;
}
public void setPrinter(MemberPrinter printer) {
this.printer = printer;
}
public void printMemberInfo(String email) {
Member member = memberDao.selectByEmail(email);
if (member == null) {
System.out.println("๋ฐ์ดํฐ ์์\n");
return;
}
printer.print(member);
System.out.println();
}
}
MemberListPrinter.java
package com.gyuone.service;
import java.util.Collection;
import com.gyuone.dao.MemberDao;
import com.gyuone.model.Member;
public class MemberListPrinter {
private MemberDao memberDao;
private MemberPrinter printer;
public MemberListPrinter(MemberDao memberDao, MemberPrinter printer) {
this.memberDao = memberDao;
this.printer = printer;
}
public void printMemberInfo(String email) {
Member member = memberDao.selectByEmail(email);
if(member == null) {
System.out.println("๋ฐ์ดํฐ ์์\n");
return;
}
printer.print(member);
System.out.println();
}
public void printAll() {
Collection<Member> members = memberDao.selectAll();
members.forEach(m -> printer.print(m));
}
}
MemberPrinter.java
package com.gyuone.service;
import com.gyuone.model.Member;
public class MemberPrinter {
public void print(Member member) {
System.out.printf("ํ์์ ๋ณด : ์์ด๋=%d, ์ด๋ฉ์ผ=%s, ์ด๋ฆ=%s, ๋ฑ๋ก์ผ=%tF\n", member.getId(),
member.getEmail(), member.getName(), member.getRegisterDateTime());
}
}
MemberRegisterService.java
package com.gyuone.service;
import java.time.LocalDateTime;
import com.gyuone.common.DuplicateMemberException;
import com.gyuone.dao.MemberDao;
import com.gyuone.model.Member;
import com.gyuone.model.RegisterRequest;
public class MemberRegisterService {
// private MemberDao memberDao = new MemberDao(); // ๊ฐํ ๊ฒฐํฉ
private MemberDao memberDao;
public MemberRegisterService(MemberDao memberDao) { // ์์ฑ์๋ฅผ ํตํด์ ์์กด์ ์ฃผ์
.. ์์ฑ์ ์์กด ์ฃผ์
= DI
this.memberDao = memberDao;
}
public long register(RegisterRequest req) {
Member member = memberDao.selectByEmail(req.getEmail());
if(member != null) {
throw new DuplicateMemberException("dup email " + req.getEmail());
}
Member newmember = new Member(req.getEmail(), req.getPassword(), req.getName(), LocalDateTime.now());
memberDao.insert(newmember);
return newmember.getId();
}
}
VersionPrinter.java
package com.gyuone.service;
public class VersionPrinter {
private int majorVersion;
private int minorVersion;
public void print() {
System.out.printf("์ด ํ๋ก๊ทธ๋จ์ ๋ฒ์ ์ %d.%d์
๋๋ค.\n\n", majorVersion, minorVersion);
}
public void setMajorVersion(int majorVersion) {
this.majorVersion = majorVersion;
}
public void setMinorVersion(int minorVersion) {
this.minorVersion = minorVersion;
}
}
'IT > WEB' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[38์ผ์ฐจ] AOP/ aop concert project (0) | 2022.08.12 |
---|---|
[37์ผ์ฐจ] AutoDi (0) | 2022.08.11 |
[37์ผ์ฐจ] Spring Bean (0) | 2022.08.11 |
[36์ผ์ฐจ] ์คํ๋ง (0) | 2022.08.10 |
[36์ผ์ฐจ] Servlet. jsp (0) | 2022.08.10 |
๋๊ธ