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