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

Mutations

31

1.1
Location : formatDollars
Killed by : edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobTests]/[method:test_milk_cows()]
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

43

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

48

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

52

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

66

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/services/jobs/JobContext::log → KILLED

79

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()]
Replaced double addition with subtraction → KILLED

80

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

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/services/jobs/JobContext::log → KILLED

96

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

97

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