This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
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.
- Loading branch information
Showing
10 changed files
with
284 additions
and
14 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
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,51 @@ | ||
package ch01.springbook.user; | ||
|
||
import java.util.List; | ||
|
||
import ch01.springbook.user.dao.UserDao; | ||
import ch01.springbook.user.domain.Level; | ||
import ch01.springbook.user.domain.User; | ||
|
||
public class UserService { | ||
|
||
UserDao userDao; | ||
|
||
public void setUserDao(UserDao userDao) { | ||
this.userDao = userDao; | ||
} | ||
|
||
public void upgradeLevels() { | ||
|
||
List<User> userList = userDao.getAll(); | ||
|
||
for (User user : userList) { | ||
|
||
Boolean changed = null; | ||
|
||
if (user.getLevel() == Level.BASIC && user.getLogin() >= 50) { | ||
user.setLevel(Level.SILVER); | ||
changed = true; | ||
|
||
} else if (user.getLevel() == Level.SILVER && user.getRecommend() >= 30) { | ||
user.setLevel(Level.GOLD); | ||
changed = true; | ||
} else if (user.getLevel() == Level.GOLD) { | ||
changed = false; | ||
} else { | ||
changed = false; | ||
} | ||
|
||
if (changed) { | ||
userDao.update(user); | ||
} | ||
} | ||
} | ||
|
||
public void add(User user) { | ||
if (user.getLevel() == null) { | ||
user.setLevel(Level.BASIC); | ||
} | ||
|
||
userDao.add(user); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,13 +1,15 @@ | ||
package ch01.springbook.user.dao; | ||
|
||
import java.util.List; | ||
|
||
import ch01.springbook.user.domain.User; | ||
|
||
import java.util.List; | ||
|
||
public interface UserDao { | ||
|
||
void add(User user); | ||
User get(String id); | ||
List<User> getAll(); | ||
void deleteAll(); | ||
int getCount(); | ||
void update(User user); | ||
} |
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
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,27 @@ | ||
package ch01.springbook.user.domain; | ||
|
||
public enum Level { | ||
|
||
BASIC(1), | ||
SILVER(2), | ||
GOLD(3); | ||
|
||
private final int value; | ||
|
||
private Level(int value) { | ||
this.value = value; | ||
} | ||
|
||
public int intValue() { | ||
return value; | ||
} | ||
|
||
public static Level valueOf(int value) { | ||
switch(value) { | ||
case 1: return BASIC; | ||
case 2: return SILVER; | ||
case 3: return GOLD; | ||
default: throw new AssertionError("Unknown value: " + value); | ||
} | ||
} | ||
} |
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
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
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
Oops, something went wrong.