MilkTheCowsJob.java

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
            String name = commons.getName();
42 1 1. accept : negated conditional → KILLED
            if(commons.gameInProgress()) {
43
44
                double milkPrice = commons.getMilkPrice();
45 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));
46
47
                Iterable<UserCommons> allUserCommons = userCommonsRepository.findByCommonsId(commons.getId());
48
49
                for (UserCommons userCommons : allUserCommons) {
50 1 1. accept : removed call to edu/ucsb/cs156/happiercows/jobs/MilkTheCowsJob::milkCows → KILLED
                    milkCows(ctx, commons, userCommons, profitRepository, userCommonsRepository);
51
                }
52
            } else {
53 1 1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED
                ctx.log("Commons " + name + " is not currently in progress, cows will not be milked in this commons.");
54
            }
55
        }
56
57 1 1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED
        ctx.log("Cows have been milked!");
58
    }
59
60
    /** This method performs the function of milking the cows for a single userCommons.
61
     *  It is a public method only so it can be exposed to the unit tests
62
     * @param ctx the JobContext
63
     * @param commons the Commons
64
     * @param userCommons the UserCommons
65
     *
66
     */
67
68
    public static void milkCows(JobContext ctx, Commons commons, UserCommons userCommons, ProfitRepository profitRepository, UserCommonsRepository userCommonsRepository) {
69
        User user = userCommons.getUser();
70
71 1 1. milkCows : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED
        ctx.log("User: " + user.getFullName()
72
                + ", numCows: " + userCommons.getNumOfCows()
73
                + ", cowHealth: " + userCommons.getCowHealth()
74
                + ", totalWealth: " + formatDollars(userCommons.getTotalWealth()));
75
76
        double profitAmount = calculateMilkingProfit(commons, userCommons);
77
        Profit profit = Profit.builder()
78
                .userCommons(userCommons)
79
                .amount(profitAmount)
80
                .timestamp(LocalDateTime.now())
81
                .numCows(userCommons.getNumOfCows())
82
                .avgCowHealth(userCommons.getCowHealth())
83
                .build();
84 1 1. milkCows : Replaced double addition with subtraction → KILLED
        double newWeath = userCommons.getTotalWealth() + profitAmount;
85 1 1. milkCows : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setTotalWealth → KILLED
        userCommons.setTotalWealth(newWeath);
86
        userCommonsRepository.save(userCommons);
87
        profit = profitRepository.save(profit);
88 1 1. milkCows : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED
        ctx.log("Profit for user: " + user.getFullName()
89
                + " is: " + formatDollars(profitAmount)
90
                + ", newWealth: " + formatDollars(newWeath));
91
    }
92
93
    /**
94
     * Calculate the profit for a user from milking their cows.
95
     *
96
     * @param userCommons
97
     * @return
98
     */
99
    public static double calculateMilkingProfit(Commons commons, UserCommons userCommons) {
100
        double milkPrice = commons.getMilkPrice();
101 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;
102 1 1. calculateMilkingProfit : replaced double return with 0.0d for edu/ucsb/cs156/happiercows/jobs/MilkTheCowsJob::calculateMilkingProfit → KILLED
        return profit;
103
    }
104
}

Mutations

31

1.1
Location : formatDollars
Killed by : edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobIndTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobIndTests]/[method:test_log_output_with_commons_and_user_commons()]
replaced return value with "" for edu/ucsb/cs156/happiercows/jobs/MilkTheCowsJob::formatDollars → KILLED

36

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_no_commons()]
removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED

42

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

45

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

50

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

53

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

57

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_no_commons()]
removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED

71

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

84

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

85

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

88

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

101

1.1
Location : calculateMilkingProfit
Killed by : edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobIndTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobIndTests]/[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.MilkTheCowsJobIndTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobIndTests]/[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.MilkTheCowsJobIndTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobIndTests]/[method:test_log_output_with_commons_and_user_commons()]
Replaced double multiplication with division → KILLED

102

1.1
Location : calculateMilkingProfit
Killed by : edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobIndTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobIndTests]/[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