| 1 | package edu.ucsb.cs156.happiercows.jobs; | |
| 2 | ||
| 3 | import java.time.LocalDateTime; | |
| 4 | ||
| 5 | import edu.ucsb.cs156.happiercows.entities.Commons; | |
| 6 | import edu.ucsb.cs156.happiercows.entities.Profit; | |
| 7 | import edu.ucsb.cs156.happiercows.entities.User; | |
| 8 | import edu.ucsb.cs156.happiercows.entities.UserCommons; | |
| 9 | import edu.ucsb.cs156.happiercows.repositories.CommonsRepository; | |
| 10 | import edu.ucsb.cs156.happiercows.repositories.ProfitRepository; | |
| 11 | import edu.ucsb.cs156.happiercows.repositories.UserCommonsRepository; | |
| 12 | import edu.ucsb.cs156.happiercows.repositories.UserRepository; | |
| 13 | import edu.ucsb.cs156.happiercows.services.jobs.JobContext; | |
| 14 | import edu.ucsb.cs156.happiercows.services.jobs.JobContextConsumer; | |
| 15 | import lombok.AllArgsConstructor; | |
| 16 | import lombok.Builder; | |
| 17 | import lombok.Getter; | |
| 18 | ||
| 19 | @AllArgsConstructor | |
| 20 | public class MilkTheCowsJob implements JobContextConsumer { | |
| 21 | ||
| 22 | @Getter | |
| 23 | private CommonsRepository commonsRepository; | |
| 24 | @Getter | |
| 25 | private UserCommonsRepository userCommonsRepository; | |
| 26 | @Getter | |
| 27 | private UserRepository userRepository; | |
| 28 | @Getter | |
| 29 | private ProfitRepository profitRepository; | |
| 30 | ||
| 31 | public String formatDollars(double amount) { | |
| 32 |
1
1. formatDollars : replaced return value with "" for edu/ucsb/cs156/happiercows/jobs/MilkTheCowsJob::formatDollars → KILLED |
return String.format("$%.2f", amount); |
| 33 | } | |
| 34 | ||
| 35 | @Override | |
| 36 | public void accept(JobContext ctx) throws Exception { | |
| 37 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log("Starting to milk the cows"); |
| 38 | ||
| 39 | Iterable<Commons> allCommons = commonsRepository.findAll(); | |
| 40 | ||
| 41 | for (Commons commons : allCommons) { | |
| 42 | String name = commons.getName(); | |
| 43 | double milkPrice = commons.getMilkPrice(); | |
| 44 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log("Milking cows for Commons: " + name + ", Milk Price: " + formatDollars(milkPrice)); |
| 45 | ||
| 46 | Iterable<UserCommons> allUserCommons = userCommonsRepository.findByCommonsId(commons.getId()); | |
| 47 | ||
| 48 | for (UserCommons userCommons : allUserCommons) { | |
| 49 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/jobs/MilkTheCowsJob::milkCows → KILLED |
milkCows(ctx, commons, userCommons); |
| 50 | } | |
| 51 | } | |
| 52 | ||
| 53 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log("Cows have been milked!"); |
| 54 | } | |
| 55 | ||
| 56 | /** This method performs the function of milking the cows for a single userCommons. | |
| 57 | * It is a public method only so it can be exposed to the unit tests | |
| 58 | * @param ctx the JobContext | |
| 59 | * @param commons the Commons | |
| 60 | * @param userCommons the UserCommons | |
| 61 | * | |
| 62 | * */ | |
| 63 | ||
| 64 | public void milkCows(JobContext ctx, Commons commons, UserCommons userCommons) { | |
| 65 |
1
1. lambda$milkCows$0 : replaced return value with null for edu/ucsb/cs156/happiercows/jobs/MilkTheCowsJob::lambda$milkCows$0 → KILLED |
User user = userRepository.findById(userCommons.getUserId()).orElseThrow(() -> new RuntimeException( |
| 66 | "Error calling userRepository.findById(" + userCommons.getUserId() + ")")); | |
| 67 |
1
1. milkCows : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log("User: " + user.getFullName() |
| 68 | + ", numCows: " + userCommons.getNumOfCows() | |
| 69 | + ", cowHealth: " + userCommons.getCowHealth() | |
| 70 | + ", totalWealth: " + formatDollars(userCommons.getTotalWealth())); | |
| 71 | ||
| 72 | double profitAmount = calculateMilkingProfit(commons, userCommons); | |
| 73 | Profit profit = Profit.builder() | |
| 74 | .userCommons(userCommons) | |
| 75 | .amount(profitAmount) | |
| 76 | .timestamp(LocalDateTime.now()) | |
| 77 | .numCows(userCommons.getNumOfCows()) | |
| 78 | .avgCowHealth(userCommons.getCowHealth()) | |
| 79 | .build(); | |
| 80 |
1
1. milkCows : Replaced double addition with subtraction → KILLED |
double newWeath = userCommons.getTotalWealth() + profitAmount; |
| 81 |
1
1. milkCows : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setTotalWealth → KILLED |
userCommons.setTotalWealth(newWeath); |
| 82 | userCommonsRepository.save(userCommons); | |
| 83 | profit = profitRepository.save(profit); | |
| 84 |
1
1. milkCows : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log("Profit for user: " + user.getFullName() |
| 85 | + " is: " + formatDollars(profitAmount) | |
| 86 | + ", newWealth: " + formatDollars(newWeath)); | |
| 87 | } | |
| 88 | ||
| 89 | /** | |
| 90 | * Calculate the profit for a user from milking their cows. | |
| 91 | * | |
| 92 | * @param userCommons | |
| 93 | * @return | |
| 94 | */ | |
| 95 | public static double calculateMilkingProfit(Commons commons, UserCommons userCommons) { | |
| 96 | double milkPrice = commons.getMilkPrice(); | |
| 97 |
3
1. calculateMilkingProfit : Replaced double division with multiplication → KILLED 2. calculateMilkingProfit : Replaced double multiplication with division → KILLED 3. calculateMilkingProfit : Replaced double multiplication with division → KILLED |
double profit = userCommons.getNumOfCows() * (userCommons.getCowHealth() / 100.0) * milkPrice; |
| 98 |
1
1. calculateMilkingProfit : replaced double return with 0.0d for edu/ucsb/cs156/happiercows/jobs/MilkTheCowsJob::calculateMilkingProfit → KILLED |
return profit; |
| 99 | } | |
| 100 | } | |
Mutations | ||
| 32 |
1.1 |
|
| 37 |
1.1 |
|
| 44 |
1.1 |
|
| 49 |
1.1 |
|
| 53 |
1.1 |
|
| 65 |
1.1 |
|
| 67 |
1.1 |
|
| 80 |
1.1 |
|
| 81 |
1.1 |
|
| 84 |
1.1 |
|
| 97 |
1.1 2.2 3.3 |
|
| 98 |
1.1 |