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