This repository has been archived by the owner on Dec 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Волгин Т.А. #17
Open
TimaVolgin
wants to merge
2
commits into
yarkinsv:master
Choose a base branch
from
TimaVolgin:master
base: master
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
Волгин Т.А. #17
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
75 changes: 75 additions & 0 deletions
75
src/main/java/com/moneytransfer/dao/impl/AccountDAOImplHashMap.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,75 @@ | ||
package com.moneytransfer.dao.impl; | ||
|
||
import com.moneytransfer.dao.AccountDAO; | ||
import com.moneytransfer.exception.CustomException; | ||
import com.moneytransfer.model.Account; | ||
import com.moneytransfer.model.UserTransaction; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.*; | ||
|
||
public class AccountDAOImplHashMap implements AccountDAO { | ||
|
||
HashMap<Long, Account> accounts = new HashMap<>(); | ||
long counter = 1; | ||
|
||
@Override | ||
public List<Account> getAllAccounts() throws CustomException { | ||
List<Account> result = new ArrayList<>(); | ||
|
||
for (Account value : accounts.values()) { | ||
result.add(value); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public Account getAccountById(long accountId) throws CustomException { | ||
return accounts.get(accountId); | ||
} | ||
|
||
@Override | ||
public Account getAccountByUser(String user, String currency) throws CustomException { | ||
return getAllAccounts() | ||
.stream() | ||
.filter(account -> account.getUserName().equals(user) && account.getCurrencyCode().equals(currency)) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
|
||
@Override | ||
public long createAccount(Account account) throws CustomException { | ||
accounts.put(counter, account); | ||
return counter++; | ||
} | ||
|
||
@Override | ||
public int deleteAccountById(long accountId) throws CustomException { | ||
if (accounts.containsKey(accountId)) { | ||
accounts.remove(accountId); | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
@Override | ||
public int updateAccountBalance(long accountId, BigDecimal deltaAmount) throws CustomException { | ||
if (accounts.containsKey(accountId)) { | ||
accounts.get(accountId).getBalance().add(deltaAmount); | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
@Override | ||
public int transferAccountBalance(UserTransaction userTransaction) throws CustomException { | ||
long from = userTransaction.getFromAccountId(); | ||
long to = userTransaction.getToAccountId(); | ||
if (accounts.containsKey(from) && accounts.containsKey(to)) { | ||
updateAccountBalance(from, userTransaction.getAmount().negate()); | ||
updateAccountBalance(to, userTransaction.getAmount()); | ||
return 2; | ||
} | ||
return 0; | ||
} | ||
} |
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
58 changes: 58 additions & 0 deletions
58
src/main/java/com/moneytransfer/dao/impl/UserDAOImplHashMap.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,58 @@ | ||
package com.moneytransfer.dao.impl; | ||
|
||
import com.moneytransfer.dao.UserDAO; | ||
import com.moneytransfer.exception.CustomException; | ||
import com.moneytransfer.model.User; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
public class UserDAOImplHashMap implements UserDAO { | ||
|
||
HashMap<Long, User> users = new HashMap<>(); | ||
long counter = 1; | ||
|
||
@Override | ||
public List<User> getAllUsers() throws CustomException { | ||
return new ArrayList<User>(users.values()); | ||
} | ||
|
||
@Override | ||
public User getUserById(long userId) throws CustomException { | ||
return users.get(userId); | ||
} | ||
|
||
@Override | ||
public User getUserByName(String userName) throws CustomException { | ||
return getAllUsers() | ||
.stream() | ||
.filter(account -> account.getUserName().equals(userName)) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
|
||
@Override | ||
public long insertUser(User user) throws CustomException { | ||
users.put(counter, user); | ||
return counter++; | ||
} | ||
|
||
@Override | ||
public int updateUser(Long userId, User user) throws CustomException { | ||
if (users.containsKey(userId)) { | ||
users.put(userId, user); | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
@Override | ||
public int deleteUser(long userId) throws CustomException { | ||
if (users.containsKey(userId)) { | ||
users.remove(userId); | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
} |
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
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
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
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.
Тут можно было еще тогда уж стринг билдеру задать первоначальный размер буфера )