rage / java-programming

https://java-programming.mooc.fi
497 stars 209 forks source link

New potential defect in Part04_31.SportStatistics #452

Open robertzofei opened 6 months ago

robertzofei commented 6 months ago

With the file (in which team "superman" clearly wins ( 31 > 30) ) : bestTeam,superteam,32,34 batman,superman,30,31 batman,robin,1,0 cats,dogs,3,0

the input of my program: data.csv superman

the output of my program is: Games: 1 Wins: 1 Losses: 0

This is what the checker says:

FAIL: SportStatisticTest winsAndLosses3 With the file: bestTeam,superteam,32,34 batman,superman,30,31 batman,robin,1,0 cats,dogs,3,0 When searcing for superman the output should have the line " Wins: 0". the output was: File: Team: Games: 0

With the file: bestTeam,superteam,32,34 batman,superman,30,31 batman,robin,1,0 cats,dogs,3,0

When searcing for superman the output should have the line " Wins: 0". the output was: File: Team: Games: 0

Team.class file: ---------------------------------------------------------------------------------------------------- public class Team {

private String name;
private int wins;
private int losses;
private int gamesPlayed;

public Team(String name) {
    this.name = name;
    this.wins = 0;
    this.losses = 0;
    this.gamesPlayed = 0;
}

public void win() {
    ++wins;
}

public void lose() {
    ++losses;
}

public void playGame() {
    ++gamesPlayed;
}

public int getGamesPlayed() {
    return this.gamesPlayed;
}

public int getWins() {
    return this.wins;
}

public int getLosses() {
    return this.losses;
}

public String getName() {
    return this.name;
}

@Override
public String toString() {
    return "Games: " + this.gamesPlayed + "\n" + "Wins: " + this.wins + "\n"
            + "Losses: " + this.losses;
}

}

SportStatistics.java file: ----------------------------------------------------------------------------------------------------

import java.nio.file.Paths; import java.util.ArrayList; import java.util.Scanner;

public class SportStatistics {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    ArrayList<Team> teamsList = new ArrayList<>();
    System.out.println("File:");
    String file = scan.nextLine();

    Boolean homeTeamAlreadyExists = false;
    Boolean visitingTeamAlreadyExists = false;

    try (Scanner scanf = new Scanner(Paths.get(file))) {

        while (scanf.hasNextLine()) {
            String line = scanf.nextLine();
            String[] parts = line.split(",");

            String homeTeam = parts[0];
            String visitingTeam = parts[1];

            homeTeamAlreadyExists = false;
            visitingTeamAlreadyExists = false;
            for (Team team : teamsList) {

                if (homeTeam.equals(team.getName())) {
                    homeTeamAlreadyExists = true;
                }

                if (visitingTeam.equals(team.getName())) {
                    visitingTeamAlreadyExists = true;
                }
            }

            if (homeTeamAlreadyExists == false) {
                teamsList.add(new Team(homeTeam));
            }

            if (visitingTeamAlreadyExists == false) {
                teamsList.add(new Team(visitingTeam));
            }

            for (Team team : teamsList) {
                // pentru home team | parts[2] = home points | parts[3] = visiting points
                if (homeTeam.equals(team.getName())) {
                    team.playGame();
                    if (Integer.valueOf(parts[2]) > Integer.valueOf(parts[3])) {
                        team.win();
                    } else {
                        team.lose();
                    }
                }
                // pentru visiting team
                if (visitingTeam.equals(team.getName())) {
                    team.playGame();
                    if (Integer.valueOf(parts[2]) < Integer.valueOf(parts[3])) {
                        team.win();
                    }  else {
                        team.lose(); 
                    }
                }
            }

        }
        scanf.close(); 

    } catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
    }

    System.out.println("Team:");
    String input = scan.nextLine();

    Boolean inList = false;

    for (Team team : teamsList) {
        if (input.equals(team.getName())) {
            System.out.println(team);
            inList = true;
        }
    }
    if (!inList) {
        System.out.println("Games: 0");
    }

    scan.close();
}

}

additional error information: Assert.java:88: org.junit.Assert.fail Assert.java:41: org.junit.Assert.assertTrue SportStatisticTest.java:133: SportStatisticTest.winsAndLosses3 NativeMethodAccessorImpl.java:-2: jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 NativeMethodAccessorImpl.java:62: jdk.internal.reflect.NativeMethodAccessorImpl.invoke DelegatingMethodAccessorImpl.java:43: jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke Method.java:566: java.lang.reflect.Method.invoke FrameworkMethod.java:47: org.junit.runners.model.FrameworkMethod$1.runReflectiveCall ReflectiveCallable.java:12: org.junit.internal.runners.model.ReflectiveCallable.run FrameworkMethod.java:44: org.junit.runners.model.FrameworkMethod.invokeExplosively InvokeMethod.java:17: org.junit.internal.runners.statements.InvokeMethod.evaluate MockStdio.java:106: fi.helsinki.cs.tmc.edutestutils.MockStdio$1.evaluate RunRules.java:20: org.junit.rules.RunRules.evaluate ParentRunner.java:271: org.junit.runners.ParentRunner.runLeaf BlockJUnit4ClassRunner.java:70: org.junit.runners.BlockJUnit4ClassRunner.runChild BlockJUnit4ClassRunner.java:50: org.junit.runners.BlockJUnit4ClassRunner.runChild ParentRunner.java:238: org.junit.runners.ParentRunner$3.run ParentRunner.java:63: org.junit.runners.ParentRunner$1.schedule ParentRunner.java:236: org.junit.runners.ParentRunner.runChildren ParentRunner.java:53: org.junit.runners.ParentRunner.access$000 ParentRunner.java:229: org.junit.runners.ParentRunner$2.evaluate ParentRunner.java:309: org.junit.runners.ParentRunner.run TestRunner.java:134: fi.helsinki.cs.tmc.testrunner.TestRunner$TestingRunnable.runTestCase TestRunner.java:89: fi.helsinki.cs.tmc.testrunner.TestRunner$TestingRunnable.doRun TestRunner.java:70: fi.helsinki.cs.tmc.testrunner.TestRunner$TestingRunnable.run Thread.java:829: java.lang.Thread.run