MilkTheCowsJob.java

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

32

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_game_and_farmer()]
replaced return value with "" for edu/ucsb/cs156/happiercows/jobs/MilkTheCowsJob::formatDollars → KILLED

37

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_game()]
removed call to edu/ucsb/cs156/jobs/services/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_skips_game_when_game_not_in_progress()]
negated conditional → KILLED

47

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

56

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

70

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_game_and_farmer()]
removed call to edu/ucsb/cs156/jobs/services/JobContext::log → 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_log_output_with_game_and_farmer()]
Replaced double addition with subtraction → KILLED

84

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/Farmer::setTotalWealth → KILLED

87

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_game_and_farmer()]
removed call to edu/ucsb/cs156/jobs/services/JobContext::log → 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_game_and_farmer()]
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_game_and_farmer()]
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_game_and_farmer()]
Replaced double multiplication with division → KILLED

101

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_game_and_farmer()]
replaced double return with 0.0d for edu/ucsb/cs156/happiercows/jobs/MilkTheCowsJob::calculateMilkingProfit → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1