diff --git "a/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/LoginService.java" "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/LoginService.java" new file mode 100644 index 0000000..a2e6be6 --- /dev/null +++ "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/LoginService.java" @@ -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(); + } + } + +} diff --git "a/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/PaySync.java" "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/PaySync.java" new file mode 100644 index 0000000..b3a8ce5 --- /dev/null +++ "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/PaySync.java" @@ -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 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)); + } +} diff --git "a/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/Times.java" "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/Times.java" new file mode 100644 index 0000000..e9694c1 --- /dev/null +++ "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/Times.java" @@ -0,0 +1,9 @@ +package com.hou27.chap08; + +import java.time.LocalDate; + +public class Times { + public LocalDate today() { + return LocalDate.now(); + } +} diff --git "a/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/UserPointCalculator.java" "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/UserPointCalculator.java" new file mode 100644 index 0000000..e1733ce --- /dev/null +++ "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/UserPointCalculator.java" @@ -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; + } +} \ No newline at end of file