Changes between Version 4 and Version 5 of WikiStart/Development


Ignore:
Timestamp:
03/24/25 22:10:43 (2 weeks ago)
Author:
203206
Comment:

mk2

Legend:

Unmodified
Added
Removed
Modified
  • WikiStart/Development

    v4 v5  
    88
    99[[Image(upgrade2.png, width=900, height=700)]]
     10
     11== Функции во Java Spring services
     12
     13{{{
     14
     15
     16package com.example.bankapp.service;
     17
     18import com.example.bankapp.model.Account;
     19import com.example.bankapp.model.Transaction;
     20import com.example.bankapp.repository.AccountRepository;
     21import com.example.bankapp.repository.TransactionRepository;
     22import org.springframework.beans.factory.annotation.Autowired;
     23import org.springframework.security.core.GrantedAuthority;
     24import org.springframework.security.core.authority.SimpleGrantedAuthority;
     25import org.springframework.security.core.userdetails.UserDetails;
     26import org.springframework.security.core.userdetails.UserDetailsService;
     27import org.springframework.security.core.userdetails.UsernameNotFoundException;
     28import org.springframework.security.crypto.password.PasswordEncoder;
     29import org.springframework.stereotype.Service;
     30
     31import java.math.BigDecimal;
     32import java.time.LocalDateTime;
     33import java.util.Arrays;
     34import java.util.Collection;
     35import java.util.List;
     36
     37@Service
     38public class AccountService implements UserDetailsService {
     39
     40    @Autowired
     41    PasswordEncoder passwordEncoder;
     42
     43    @Autowired
     44    private AccountRepository accountRepository;
     45
     46    @Autowired
     47    private TransactionRepository transactionRepository;
     48
     49
     50    public Account findAccountByUsername(String username) {
     51        return accountRepository.findByUsername(username).orElseThrow(() -> new RuntimeException("Account not found"));
     52    }
     53
     54    public Account registerAccount(String username,String email, String password) {
     55        if (accountRepository.findByUsername(username).isPresent()) {
     56            throw new RuntimeException("Username already exists");
     57        }
     58
     59        Account account = new Account();
     60        account.setUsername(username);
     61        account.setEmail(email);
     62        account.setPassword(passwordEncoder.encode(password)); // Encrypt password
     63        account.setBalance(BigDecimal.ZERO); // Initial balance set to 0
     64        return accountRepository.save(account);
     65    }
     66
     67
     68    public void deposit(Account account, BigDecimal amount) {
     69        account.setBalance(account.getBalance().add(amount));
     70        accountRepository.save(account);
     71
     72        Transaction transaction = new Transaction(
     73                amount,
     74                "(MKD)",
     75                "Deposit",
     76                LocalDateTime.now(),
     77                account
     78        );
     79        transactionRepository.save(transaction);
     80    }
     81
     82    public void withdraw(Account account, BigDecimal amount) {
     83        if (account.getBalance().compareTo(amount) < 0) {
     84            throw new RuntimeException("Insufficient funds");
     85        }
     86        account.setBalance(account.getBalance().subtract(amount));
     87        accountRepository.save(account);
     88
     89        Transaction transaction = new Transaction(
     90                amount,
     91                "(MKD)",
     92                "Withdrawal",
     93                LocalDateTime.now(),
     94                account
     95        );
     96        transactionRepository.save(transaction);
     97    }
     98
     99    public List<Transaction> getTransactionHistory(Account account) {
     100        return transactionRepository.findByAccountId(account.getId());
     101    }
     102
     103    @Override
     104    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
     105
     106        Account account = findAccountByUsername(username);
     107        if (account == null) {
     108            throw new UsernameNotFoundException("Username or Password not found");
     109        }
     110        return new Account(
     111                account.getUsername(),
     112                account.getEmail(),
     113                account.getPassword(),
     114                account.getBalance(),
     115                account.getTransactions(),
     116                authorities());
     117    }
     118
     119    public Collection<? extends GrantedAuthority> authorities() {
     120        return Arrays.asList(new SimpleGrantedAuthority("USER"));
     121    }
     122
     123
     124    public void transferAmount(Account fromAccount, String toUsername, BigDecimal amount, String currency) {
     125
     126        if(fromAccount.getUsername().compareTo(toUsername) == 0){
     127            throw new RuntimeException("No transaction allowed to yourself");
     128        }
     129
     130        Account toAccount = accountRepository.findByUsername(toUsername)
     131                .orElseThrow(() -> new RuntimeException("Recipient account not found"));
     132
     133
     134        if(currency.compareTo("(MKD)")==0){
     135            if (fromAccount.getBalance().compareTo(amount) < 0) {
     136                throw new RuntimeException("Insufficient funds");
     137            }
     138            else{
     139                // Deduct from sender's account
     140                fromAccount.setBalance(fromAccount.getBalance().subtract(amount));
     141                accountRepository.save(fromAccount);
     142
     143                // Add to recipient's account
     144                toAccount.setBalance(toAccount.getBalance().add(amount));
     145                accountRepository.save(toAccount);
     146
     147                // Create transaction records for both accounts
     148                Transaction debitTransaction = new Transaction(
     149                        amount,
     150                        currency,
     151                        "(MKD) Transfer Out to " + toAccount.getUsername(),
     152                        LocalDateTime.now(),
     153                        fromAccount
     154                );
     155                transactionRepository.save(debitTransaction);
     156
     157                Transaction creditTransaction = new Transaction(
     158                        amount,
     159                        currency,
     160                        "(MKD) Transfer In from " + fromAccount.getUsername(),
     161                        LocalDateTime.now(),
     162                        toAccount
     163                );
     164                transactionRepository.save(creditTransaction);
     165            }
     166        }
     167
     168        if(currency.compareTo("(EUR)")==0){
     169            if (fromAccount.getBalance().compareTo(amount.multiply(BigDecimal.valueOf(60.00))) < 0) {
     170                throw new RuntimeException("Insufficient funds");
     171            }
     172            else{
     173                    // Deduct from sender's account
     174                    fromAccount.setBalance(fromAccount.getBalance().subtract(amount.multiply(BigDecimal.valueOf(60.00))));
     175                    accountRepository.save(fromAccount);
     176
     177                    // Add to recipient's account
     178                    toAccount.setBalance(toAccount.getBalance().add(amount.multiply(BigDecimal.valueOf(60.00))));
     179                    accountRepository.save(toAccount);
     180
     181                    // Create transaction records for both accounts
     182                    Transaction debitTransaction = new Transaction(
     183                            amount,
     184                            currency,
     185                            "(EUR) Transfer Out to " + toAccount.getUsername(),
     186                            LocalDateTime.now(),
     187                            fromAccount
     188                    );
     189
     190                    transactionRepository.save(debitTransaction);
     191
     192                    Transaction creditTransaction = new Transaction(
     193                        amount,
     194                        currency,
     195                        "(EUR) Transfer In from " + fromAccount.getUsername(),
     196                        LocalDateTime.now(),
     197                        toAccount
     198                    );
     199                    transactionRepository.save(creditTransaction);
     200            }
     201        }
     202
     203        if(currency.compareTo("(USD)")==0){
     204            if (fromAccount.getBalance().compareTo(amount.multiply(BigDecimal.valueOf(60.00))) < 0) {
     205                throw new RuntimeException("Insufficient funds");
     206            }
     207            else{
     208                // Deduct from sender's account
     209                fromAccount.setBalance(fromAccount.getBalance().subtract(amount.multiply(BigDecimal.valueOf(60.00))));
     210                accountRepository.save(fromAccount);
     211
     212                // Add to recipient's account
     213                toAccount.setBalance(toAccount.getBalance().add(amount.multiply(BigDecimal.valueOf(60.00))));
     214                accountRepository.save(toAccount);
     215
     216                // Create transaction records for both accounts
     217                Transaction debitTransaction = new Transaction(
     218                        amount,
     219                        currency,
     220                  "(USD) Transfer Out to " + toAccount.getUsername(),
     221                        LocalDateTime.now(),
     222                         fromAccount
     223                );
     224                transactionRepository.save(debitTransaction);
     225
     226                Transaction creditTransaction = new Transaction(
     227                        amount,
     228                        currency,
     229                        "(USD) Transfer In from " + fromAccount.getUsername(),
     230                        LocalDateTime.now(),
     231                        toAccount
     232                );
     233                transactionRepository.save(creditTransaction);
     234            }
     235        }
     236
     237        if(currency.compareTo("(GBP)")==0){
     238            if (fromAccount.getBalance().compareTo(amount.multiply(BigDecimal.valueOf(70.00))) < 0) {
     239                throw new RuntimeException("Insufficient funds");
     240            }
     241            else{
     242                    // Deduct from sender's account
     243                    fromAccount.setBalance(fromAccount.getBalance().subtract(amount.multiply(BigDecimal.valueOf(70.00))));
     244                    accountRepository.save(fromAccount);
     245
     246                    // Add to recipient's account
     247                    toAccount.setBalance(toAccount.getBalance().add(amount.multiply(BigDecimal.valueOf(70.00))));
     248                    accountRepository.save(toAccount);
     249
     250                    // Create transaction records for both accounts
     251                    Transaction debitTransaction = new Transaction(
     252                            amount,
     253                            currency,
     254                            "(GBP) Transfer Out to " + toAccount.getUsername(),
     255                            LocalDateTime.now(),
     256                            fromAccount
     257                    );
     258                    transactionRepository.save(debitTransaction);
     259
     260                    Transaction creditTransaction = new Transaction(
     261                            amount,
     262                            currency,
     263                            "(GBP) Transfer In from " + fromAccount.getUsername(),
     264                            LocalDateTime.now(),
     265                            toAccount
     266                    );
     267                    transactionRepository.save(creditTransaction);
     268            }
     269        }
     270
     271    }
     272
     273}
     274
     275}}}