MilkTheCowsJob.java

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
Location : formatDollars
Killed by : edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests]/[method:test_log_output_with_commons_and_user_commons()]
replaced return value with "" for edu/ucsb/cs156/happiercows/jobs/MilkTheCowsJob::formatDollars → KILLED

35

1.1
Location : accept
Killed by : edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests]/[method:test_log_output_job_skipped()]
removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED

40

1.1
Location : accept
Killed by : edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests]/[method:test_log_output_job_skipped()]
negated conditional → KILLED

41

1.1
Location : accept
Killed by : edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests]/[method:test_log_output_job_skipped()]
removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED

46

1.1
Location : accept
Killed by : edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests]/[method:test_log_output_with_commons_and_user_commons()]
removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED

51

1.1
Location : accept
Killed by : edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests]/[method:test_log_output_with_commons_and_user_commons()]
removed call to edu/ucsb/cs156/happiercows/jobs/MilkTheCowsJob::milkCows → KILLED

55

1.1
Location : accept
Killed by : edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests]/[method:test_log_output_job_skipped()]
removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED

69

1.1
Location : milkCows
Killed by : edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests]/[method:test_log_output_with_commons_and_user_commons()]
removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED

82

1.1
Location : milkCows
Killed by : edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests]/[method:test_log_output_with_commons_and_user_commons()]
Replaced double addition with subtraction → KILLED

83

1.1
Location : milkCows
Killed by : edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests]/[method:test_milk_cows()]
removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setTotalWealth → KILLED

86

1.1
Location : milkCows
Killed by : edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests]/[method:test_log_output_with_commons_and_user_commons()]
removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED

99

1.1
Location : calculateMilkingProfit
Killed by : edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests]/[method:test_log_output_with_commons_and_user_commons()]
Replaced double division with multiplication → KILLED

2.2
Location : calculateMilkingProfit
Killed by : edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests]/[method:test_log_output_with_commons_and_user_commons()]
Replaced double multiplication with division → KILLED

3.3
Location : calculateMilkingProfit
Killed by : edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests]/[method:test_log_output_with_commons_and_user_commons()]
Replaced double multiplication with division → KILLED

100

1.1
Location : calculateMilkingProfit
Killed by : edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests]/[method:test_log_output_with_commons_and_user_commons()]
replaced double return with 0.0d for edu/ucsb/cs156/happiercows/jobs/MilkTheCowsJob::calculateMilkingProfit → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.3