Skip to content

Commit

Permalink
Merge pull request #83 from GDSC-MYONGJI/kimjeongho
Browse files Browse the repository at this point in the history
8주차 - 김정호
  • Loading branch information
rladuswl authored Feb 18, 2023
2 parents fd9e2fd + 3c47622 commit 8188e1c
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
30 changes: 30 additions & 0 deletions 김정호/src/test/java/com/hou27/chap08/LoginService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.hou27.chap08;

public class LoginService {

private String authKey = "some_key";
private CustomerRepository customerRepository;

public LoginService(CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}

public LoginResult login(String id, String password) {
int resp = 0;
boolean authorized = AuthUtil.authorize(authKey); // 정적 메서드를 사용
if (authorized) {
resp = AuthUtil.authenticate(id, password); // 정적 메서드를 사용
} else {
resp = -1;
}

if (resp == -1) {
return LoginResult.badAuthKey();
} else if (resp == 1) {
return LoginResult.success();
} else {
return LoginResult.fail();
}
}

}
32 changes: 32 additions & 0 deletions 김정호/src/test/java/com/hou27/chap08/PaySync.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.hou27.chap08;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Collectors;

public class PaySync {
private PayInfoDao payInfoDao;
private String filePath = "tmp/paydata/payinfo.csv";

// 생성자를 통해 의존성을 주입받는다.
public PaySync(PayInfoDao payInfoDao) {
this.payInfoDao = payInfoDao;
}

// 경로를 설정 가능하게 메서드 생성
public void setFilePath(String filePath) {
this.filePath = filePath;
}

public void sync() throws IOException {
Path path = Path.of(filePath);
List<PayInfo> payInfos = Files.lines(path)
.map(line -> {
String[] data = line.split(",");
return new PayInfo(data[0], Integer.parseInt(data[1]));
}).collect(Collectors.toList());

payInfos.forEach(pi -> payInfoDao.insert(pi));
}
}
9 changes: 9 additions & 0 deletions 김정호/src/test/java/com/hou27/chap08/Times.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.hou27.chap08;

import java.time.LocalDate;

public class Times {
public LocalDate today() {
return LocalDate.now();
}
}
62 changes: 62 additions & 0 deletions 김정호/src/test/java/com/hou27/chap08/UserPointCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.hou27.chap08;

import java.time.LocalDate;

public class UserPointCalculator {

private SubscriptionDao subscriptionDao;
private ProductDao productDao;
private Times times;

public UserPointCalculator(
SubscriptionDao subscriptionDao,
ProductDao productDao,
Times times
) {
this.subscriptionDao = subscriptionDao;
this.productDao = productDao;
this.times = times;
}

public int calculatePoint(User u) {
Subscription s = subscriptionDao.selectByUser(u);
if(s == null) {
throw new NoSubscriptionException();
}

Product p = productDao.selectById(s.getProductId());
// LocalDate now = LocalDate.now(); // 현재 날짜를 구한다.
LocalDate now = times.today(); // 현재 날짜를 구한다.
PointRule rule = new PointRule();
int point = rule.calculate(s, p, now);
// int point = 0;
// if(s.isFinished(now)) { // 현재 시간에 따라 결과가 달라진다.
// point += p.getDefaultPoint();
// } else {
// point += p.getDefaultPoint() + 10;
// }
//
// if(s.getGrade() == GOLD) {
// point += 100;
// }

return point;
}
}

class PointRule {
public int calculate(Subscription s, Product p, LocalDate now) {
int point = 0;
if(s.isFinished(now)) {
point += p.getDefaultPoint();
} else {
point += p.getDefaultPoint() + 10;
}

if(s.getGrade() == GOLD) {
point += 100;
}

return point;
}
}

0 comments on commit 8188e1c

Please sign in to comment.