com.epam.ipodromproject.service.MainService.java Source code

Java tutorial

Introduction

Here is the source code for com.epam.ipodromproject.service.MainService.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.epam.ipodromproject.service;

import com.epam.ipodromproject.domain.Bet;
import com.epam.ipodromproject.domain.BetResult;
import com.epam.ipodromproject.domain.BetType;
import com.epam.ipodromproject.domain.Competition;
import com.epam.ipodromproject.domain.CompetitionState;
import com.epam.ipodromproject.domain.Horse;
import com.epam.ipodromproject.domain.HorseInfo;
import com.epam.ipodromproject.domain.User;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Oleh
 */
@Service(value = "mainService")
public class MainService {

    @Autowired
    UserService userService;

    @Autowired
    CompetitionService competitionService;

    @Autowired
    HorseService horseService;

    @Autowired
    BetService betService;

    @Transactional
    public boolean makeBet(User user, Competition competitionToBet, String money, BetType betType, String horseID) {
        if ((!userService.canMakeBet(user.getUsername(), money, competitionToBet))
                || (!horseService.existsHorseInCompetition(competitionToBet, horseID))) {
            return false;
        }
        Date now = new Date();
        Horse horse = horseService.getHorseByID(Long.valueOf(horseID));
        double moneyToBet = Double.parseDouble(money);
        if ((CompetitionState.NOT_REGISTERED.equals(competitionToBet.getState()))
                && (now.compareTo(competitionToBet.getDateOfStart()) <= 0)) {
            Double coefficient = returnCoefficientDueToBetType(
                    competitionToBet.getHorses().get(horse).getCoefficient(), betType);
            Bet betInfo = new Bet(moneyToBet, new Date(), betType, horse, user.getPerson(), competitionToBet,
                    coefficient);
            betService.update(betInfo);
            userService.makeBet(user, moneyToBet);
            return true;
        } else {
            return false;
        }
    }

    private double returnCoefficientDueToBetType(double coefficient, BetType type) {
        return (coefficient - 1) * coefByBetType(type) + 1;
    }

    private double coefByBetType(BetType type) {
        switch (type) {
        case FIRST_PLACE: {
            return 1;
        }
        case ONE_OF_TWO: {
            return 0.7;
        }
        case ONE_OF_THREE: {
            return 0.3;
        }
        }
        return 0;
    }

    @Transactional
    public boolean registerResult(Long competitionID, String[] placesTaken) {
        if (!competitionService.canRegisterResult(competitionID, placesTaken)) {
            return false;
        }
        Competition competition = competitionService.getCompetitionByID(competitionID);
        int i = 0;
        for (HorseInfo horseInfo : competition.getHorses().values()) {
            horseInfo.setPlace(Integer.parseInt(placesTaken[i++]));
        }
        competition.setState(CompetitionState.REGISTERED);
        Map<Horse, HorseInfo> horses = competition.getHorses();
        List<Bet> bets = betService.getBetsByCompetition(competitionID);
        for (Bet bet : bets) {
            User user = userService.getUserByUsername(bet.getPerson().getUser().getUsername());
            if (won(bet.getBetType(), horses.get(bet.getHorse()).getPlace())) {
                user.getPerson()
                        .setMoney(user.getPerson().getMoney() + bet.getBetAmount() * bet.getCoefficientOnHorse());
                bet.setBetResult(BetResult.WON);
            } else {
                bet.setBetResult(BetResult.LOST);
            }
        }
        return true;
    }

    private boolean won(BetType betType, int placeTaken) {
        switch (betType) {
        case FIRST_PLACE: {
            return placeTaken == 1;
        }
        case ONE_OF_TWO: {
            return placeTaken <= 2;
        }
        case ONE_OF_THREE: {
            return placeTaken <= 3;
        }
        }
        return false;
    }

    @Transactional
    public boolean cancelCompetition(Long competitionID) {
        if ((competitionID != null) && (CompetitionState.NOT_REGISTERED
                .equals(competitionService.getCompetitionByID(competitionID).getState()))) {
            competitionService.cancelCompetition(competitionID);
            userService.returnMoneyByCompetition(competitionID);
            betService.cancelBetsByCompetition(competitionID);
            return true;
        } else {
            return false;
        }
    }

    @Transactional
    public boolean cancelBet(Long betID, User user) {
        if (betService.cancelBet(betID, user.getUsername())) {
            userService.cancelBet(user, betService.getBetByID(betID).getBetAmount());
            return true;
        } else {
            return false;
        }
    }

}