Skip to content
Snippets Groups Projects
Commit 116f3ad9 authored by medengessia's avatar medengessia
Browse files

feat(java project): add project of the previous term.

parent 9b92d90d
No related branches found
No related tags found
No related merge requests found
Pipeline #15183 passed with warnings
Showing
with 925 additions and 0 deletions
/target/
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>git_tp_qualite_logicielle</groupId>
<artifactId>git_tp_qualite_logicielle</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<sonar.qualitygate.wait>true</sonar.qualitygate.wait>
</properties>
</project>
\ No newline at end of file
package competition;
import java.util.LinkedList;
import java.util.List;
import competition.competitor.Competitor;
import competition.observer.util.Bookmaker;
import competition.observer.util.Journalist;
import competition.organization.League;
public class BookmakerMain {
public static void main(String[] args) {
//Creation of competitors
List<Competitor> competitors = new LinkedList<>();
competitors.add(new Competitor("Lynx"));
competitors.add(new Competitor("Raven"));
competitors.add(new Competitor("Drift"));
competitors.add(new Competitor("Catalyst"));
competitors.add(new Competitor("Midas"));
competitors.add(new Competitor("Blastoff"));
List<Bookmaker> bookmakers = new LinkedList<>();
bookmakers.add(new Bookmaker("Tintin"));
bookmakers.add(new Bookmaker("Haddock"));
//League Main
Competition competition = new League(competitors);
for(Bookmaker bookmaker : bookmakers) {
competition.register(bookmaker);
}
competition.play();
}
}
package competition;
import competition.match.Match;
import competition.match.util.RandomMatch;
import competition.observer.MatchObserver;
import competition.organization.MapUtil;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import competition.competitor.*;
/**
* abstract class to create a competition
*
* @author Lounes Meddahi and Matthieu Medeng Essia
*
*/
public abstract class Competition {
protected static final StdDisplayer DISPLAYER = new StdDisplayer();
protected Match match;
private final List<Competitor> competitors;
private List<MatchObserver> observers;
/**
* To play a competition, we need a list of competitors
* @param competitors of the competition
*/
public Competition(List<Competitor> competitors) {
this.competitors = competitors;
this.match = new RandomMatch();
this.observers = new LinkedList<>();
}
/**
* This method is to be used to start a competition.
* The consequences is that all competitors from the list given in the constructor will play the competition, and gain points (if they win at least one match)
*/
public void play() {
this.play(competitors);
}
/**
* This method starts a competition with competitors given in param
* @param competitors that will play the competition
*/
protected abstract void play(List<Competitor> competitors);
/**
* This method designates the winner between c1 and c2.
* This method depends on the type of match we used to play a match.
* @param c1 first competitor
* @param c2 second competitor
*/
protected void playMatch(Competitor c1 , Competitor c2) {
if(! c1.equals(c2)) { //To avoid winning against yourself
Competitor winner = this.match.declareWinner(c1, c2);
winner.addPoints(1);
DISPLAYER.displayCompetitionMsg(c1.getName()+" vs "+c2.getName()+" --> "+winner.getName()+" wins!");
if(winner.equals(c1)) {
newMatchDetected(winner,c2);
}
else {
newMatchDetected(winner,c1);
}
}
}
/**
* Method to get the ranking of all competitors
* @return a map with the ranking of all competitors
*/
public Map<Competitor,Integer> ranking(){
Map<Competitor , Integer> Mapcompetitors = new HashMap<Competitor , Integer>();
for (Competitor c : this.competitors) {
Mapcompetitors.put(c, c.getPoints());
}
return MapUtil.sortByDescendingValue(Mapcompetitors);
}
/**
* returns the list of all competitors
* @return the list of all competitors
*/
public List<Competitor> getCompetitors(){
return this.competitors;
}
/**
* returns the match used to play the competition
* @return the match used to play the competition
*/
public Match getMatch() {
return this.match;
}
/**
* returns the tournament winner
* @return the tournament winner
*/
public Competitor displayWinner() {
return ranking().keySet().iterator().next();
}
/**
* this method add a MatchObserver to the observers list
* @param observer to be added
*/
public void register(MatchObserver observer) {
if(! this.observers.contains(observer)) {
this.observers.add(observer);}
}
/**
* this method remove a MatchObserver to the observers list
* @param observer to be removed
*/
public void unregister(MatchObserver observer) {
if(this.observers.contains(observer)) {
this.observers.remove(observer);}
}
/**
* This method spreads the information about the match between two competitors
* @param winner of the match
* @param loser of the match
*/
public void newMatchDetected(Competitor winner,Competitor loser) {
for(MatchObserver observer : this.observers) {
observer.reactToMatch(winner, loser);
}
}
/**
* this function returns the MatchOberservers of the competition
* @return the MatchOberservers of the competition
*/
public List<MatchObserver> getMatchObservers(){
return this.observers;
}
}
package competition;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import competition.competitor.Competitor;
import competition.observer.util.Journalist;
import competition.organization.League;
import competition.organization.masters.Master;
import competition.organization.masters.Strategy;
import competition.organization.masters.strategies.Strategy2;
public class JournalistMain {
public static void main(String[] args) {
//Creation of competitors
List<Competitor> competitors = new LinkedList<>();
competitors.add(new Competitor("Lynx"));
competitors.add(new Competitor("Raven"));
competitors.add(new Competitor("Drift"));
competitors.add(new Competitor("Catalyst"));
competitors.add(new Competitor("Midas"));
competitors.add(new Competitor("Blastoff"));
List<Journalist> journalists = new LinkedList<>();
journalists.add(new Journalist("TF1"));
journalists.add(new Journalist("France2"));
//League Main
Competition competition = new League(competitors);
for(Journalist journalist : journalists) {
competition.register(journalist);
}
competition.play();
}
}
package competition;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import competition.competitor.Competitor;
import competition.organization.League;
/**
* League Main
*
* @author Lounes Meddahi and Matthieu Medeng Essia
*
*/
public class LeagueMain {
public static void main(String[] args) {
//Creation of competitors
List<Competitor> competitors = new LinkedList<>();
competitors.add(new Competitor("Lynx"));
competitors.add(new Competitor("Raven"));
competitors.add(new Competitor("Drift"));
competitors.add(new Competitor("Catalyst"));
competitors.add(new Competitor("Midas"));
competitors.add(new Competitor("Blastoff"));
//League Main
Competition competition = new League(competitors);
competition.play();
Set<Competitor> rank = competition.ranking().keySet();
Iterator<Competitor> cIterator = rank.iterator();
System.out.println("*** Ranking ***");
while(cIterator.hasNext()) {
Competitor c = cIterator.next();
System.out.println(""+c.getName()+" - "+c.getPoints());
}
}
}
package competition;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import competition.competitor.Competitor;
import competition.organization.masters.Master;
import competition.organization.masters.Strategy;
import competition.organization.masters.strategies.Strategy1;
/**
* Master 16 Main
*
* @author Lounes Meddahi and Matthieu Medeng Essia
*
*/
public class MasterStrategy1Main {
public static void main(String[] args) {
//Creation of competitors
List<Competitor> competitors = new LinkedList<>();
competitors.add(new Competitor("Lynx"));
competitors.add(new Competitor("Raven"));
competitors.add(new Competitor("Nami"));
competitors.add(new Competitor("Luffy"));
competitors.add(new Competitor("Midas"));
competitors.add(new Competitor("Blastoff"));
competitors.add(new Competitor("Mihawk"));
competitors.add(new Competitor("Law"));
competitors.add(new Competitor("Kid"));
competitors.add(new Competitor("Ace"));
competitors.add(new Competitor("Dragon"));
competitors.add(new Competitor("Shanks"));
competitors.add(new Competitor("Drift"));
competitors.add(new Competitor("Catalyst"));
competitors.add(new Competitor("Zoro"));
competitors.add(new Competitor("Kaido"));
//Master 16 Main
Strategy strategy = new Strategy1();
Master competition = new Master(strategy, competitors);
competition.play();
}
}
package competition;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import competition.competitor.Competitor;
import competition.organization.masters.Master;
import competition.organization.masters.Strategy;
import competition.organization.masters.strategies.Strategy1;
import competition.organization.masters.strategies.Strategy2;
/**
* Master 24 Main
*
* @author Lounes Meddahi and Matthieu Medeng Essia
*
*/
public class MasterStrategy2Main {
public static void main(String[] args) {
//Creation of competitors
List<Competitor> competitors = new LinkedList<>();
competitors.add(new Competitor("Lynx"));
competitors.add(new Competitor("Raven"));
competitors.add(new Competitor("Nami"));
competitors.add(new Competitor("Luffy"));
competitors.add(new Competitor("Midas"));
competitors.add(new Competitor("Blastoff"));
competitors.add(new Competitor("Mihawk"));
competitors.add(new Competitor("Law"));
competitors.add(new Competitor("Kid"));
competitors.add(new Competitor("Ace"));
competitors.add(new Competitor("Dragon"));
competitors.add(new Competitor("Shanks"));
competitors.add(new Competitor("Drift"));
competitors.add(new Competitor("Catalyst"));
competitors.add(new Competitor("Zoro"));
competitors.add(new Competitor("Kaido"));
competitors.add(new Competitor("Natsu"));
competitors.add(new Competitor("Gray"));
competitors.add(new Competitor("Lee"));
competitors.add(new Competitor("Guy"));
competitors.add(new Competitor("Erza"));
competitors.add(new Competitor("Mira"));
competitors.add(new Competitor("Luxus"));
competitors.add(new Competitor("Gajeel"));
//Master 16 Main
Strategy strategy = new Strategy2();
Master competition = new Master(strategy, competitors);
competition.play();
}
}
package competition;
/**
* This class display Strings
*
* @author Lounes Meddahi and Matthieu Medeng Essia
*
*
*/
public class StdDisplayer {
/**
* print the String given in parameter
* @param msg to be print
*/
public void displayCompetitionMsg(String msg) {
System.out.println(msg);
}
/**
* print the String given in parameter
* @param msg to be print by observers
*/
public void displayObserverMsg(String msg) {
System.out.println(msg);
}
}
package competition;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import competition.competitor.Competitor;
import competition.organization.Tournament;
/**
* Tournament Main
*
* @author Lounes Meddahi and Matthieu Medeng Essia
*
*/
public class TournamentMain {
public static void main(String[] args) {
//Creation of competitors
List<Competitor> competitors = new LinkedList<>();
competitors.add(new Competitor("Lynx"));
competitors.add(new Competitor("Raven"));
competitors.add(new Competitor("Drift"));
competitors.add(new Competitor("Catalyst"));
competitors.add(new Competitor("Midas"));
competitors.add(new Competitor("Blastoff"));
competitors.add(new Competitor("Luffy"));
competitors.add(new Competitor("Ussop"));
//League Main
Competition competition = new Tournament(competitors);
competition.play();
Set<Competitor> rank = competition.ranking().keySet();
Iterator<Competitor> cIterator = rank.iterator();
System.out.println("*** Ranking ***");
while(cIterator.hasNext()) {
Competitor c = cIterator.next();
System.out.println(""+c.getName()+" - "+c.getPoints());
}
}
}
package competition.competitor;
/**
* This class define how to create a player
*
* @author Lounes Meddahi and Matthieu Medeng Essia
*
*/
public class Competitor {
/**
* This attribute is used to define player's name
*/
private final String name;
/**
* This attribute is used to know how many points the player has
*/
private int nbOfPoints;
/**
* To create a player, we use a name, and initialize his number of points to zero
* @param name the name of the player
*/
public Competitor(String name) {
this.name = name;
this.nbOfPoints = 0;
}
/**
* Method to get the player's number of points
* @return player's number of points
*/
public int getPoints() {
return this.nbOfPoints;
}
/**
* Method to get the competitor name
* @return competitor's name
*/
public String getName() {
return this.name;
}
/**
* Method used to add points to the competitor
* @param bonus points to add
*/
public void addPoints(int bonus) {
this.nbOfPoints += bonus;
}
/**
* Method used to compare two players
* @return True if they are both the same (same name)
* False else
*/
public boolean equals(Object o) {
if (o instanceof Competitor) {
Competitor other = (Competitor) o;
return (other.getName()).equals(this.name);
}
else {
return false;
}
}
}
package competition.match;
import competition.competitor.Competitor;
/**
*
* Interface class to implements different type of Match
*
* @author Lounes Meddahi and Matthieu Medeng Essia
*
*/
public interface Match {
/**
* Method that declares the winner at the end of the match
* @param competitor1 first competitor
* @param competitor2 second competitor
* @return the winner of the match
*/
public Competitor declareWinner(Competitor competitor1 , Competitor competitor2);
}
package competition.match.util;
import java.util.Random;
import competition.competitor.Competitor;
import competition.match.Match;
/**
* class to create a match that return a random winner
*
* @author Lounes Meddahi and Matthieu Medeng Essia
*
*/
public class RandomMatch implements Match {
private Random winner;
/**
* Empty constructor
*/
public RandomMatch() {
this.winner = new Random();
}
/**
* returns a random winner between competitor1 and competitor2
*/
@Override
public Competitor declareWinner(Competitor competitor1 , Competitor competitor2) {
int nxt = winner.nextInt(2);
Competitor[] competitors = {competitor1 , competitor2};
return competitors[nxt];
}
}
package competition.observer;
import competition.StdDisplayer;
import competition.competitor.Competitor;
/**
* Class to creat a match observer
*
* @author Lounes Meddahi and Matthieu Medeng Essia
*
*/
public interface MatchObserver {
static final StdDisplayer DISPLAYER = new StdDisplayer();
/**
* abstract method used by the observer to react to the match
* @param winner of the match
* @param loser of the match
*/
public void reactToMatch(Competitor winner , Competitor loser);
}
package competition.observer.util;
import java.util.HashMap;
import java.util.Map;
import competition.competitor.Competitor;
import competition.observer.MatchObserver;
/**
* Class to instantiate a Bookmaker
*
* @author Lounes Meddahi and Matthieu Medeng Essia
*
*/
public class Bookmaker implements MatchObserver{
/**
* Competitors managed by the bookmaker
*/
private Map<Competitor,Double> competitors;
/**
* name of the bookmaker
*/
private String name;
/**
* Bookmaker constructor
* @param name of the bookmaker
*/
public Bookmaker(String name) {
this.name = name;
this.competitors = new HashMap<>();
}
/**
* incerment the odds of the looser only if he is managed by the Mock
* @param loser competitor to be incremented
*/
private void increment(Competitor loser){
if(competitors.containsKey(loser)) {
competitors.replace(loser , competitors.get(loser)*2 );
}
}
/**
* decerment the odds of the winner only if he is managed by the Bookmaker
* @param winner competitor to be incremented
*/
private void decrement(Competitor winner){
if(competitors.containsKey(winner)) {
competitors.replace(winner , competitors.get(winner)/2 );
}
}
/**
* Add a competitor to be managed by the bookmaker
* @param comp to be managed by the bookmaker
*/
public void addCompetitor(Competitor comp) {
if(!competitors.containsKey(comp)) {
this.competitors.put(comp, 1.0);
}
}
@Override
/**
* This method adjusts the odds
*/
public void reactToMatch(Competitor winner, Competitor loser) {
addCompetitor(winner);
addCompetitor(loser);
DISPLAYER.displayObserverMsg(this.getName()+": Les cotes avant le match etaient de "+winner.getName()+" : "+this.competitors.get(winner)+ " et de "+loser.getName()+" : "+this.competitors.get(loser) );
increment(loser);
decrement(winner);
DISPLAYER.displayObserverMsg(this.getName()+": elles sont desormais de "+winner.getName()+" : "+this.competitors.get(winner)+ " et de "+loser.getName()+" : "+this.competitors.get(loser)+" \n");
}
/**
* return competitors managed by the bookmaker
* @return competitors managed by the bookmaker
*/
public Map<Competitor,Double> getCompetitors(){
return this.competitors;
}
/**
* Return the name of the bookmaker
*/
public String getName() {
return this.name;
}
}
package competition.observer.util;
import competition.competitor.Competitor;
import competition.observer.MatchObserver;
/**
* Class to instantiate a Journalist
*
* @author Lounes Meddahi and Matthieu Medeng Essia
*
*/
public class Journalist implements MatchObserver {
private String name;
public Journalist(String name) {
this.name = name;
}
@Override
public void reactToMatch(Competitor winner, Competitor loser) {
DISPLAYER.displayObserverMsg(this.getName()+": "+winner.getName()+" a gagne contre "+loser.getName()+" \n");
}
/**
* return the name of the journalist
* @return the name of the journalist
*/
public String getName() {
return this.name;
}
}
package competition.organization;
import java.util.List;
import competition.Competition;
import competition.competitor.Competitor;
/**
* Class to create a League
*
* @author Lounes Meddahi and Matthieu Medeng Essia
*
*/
public class League extends Competition {
/**
* League constructor
* @param competitors of the league
*/
public League(List<Competitor> competitors) {
super(competitors);
}
/**
* For a League, every competitor plays 2 matches against the same competitor (home-and-back match)
*/
protected void play(List<Competitor> competitors) {
for (Competitor attacker : competitors ) {
for(Competitor defender : competitors) {
this.playMatch(attacker, defender);
}
}
}
}
package competition.organization;
import java.util.ArrayList ;
import java.util.Collections ;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List ;
import java.util.Map;
import java.util.Map.Entry ;
public class MapUtil {
public static <K, V extends Comparable<? super V>> Map<K, V> sortByDescendingValue(Map<K, V> map) {
List<Entry<K, V>> sortedEntries = new ArrayList<Entry<K, V>>(map. entrySet ( ) );
Collections . sort ( sortedEntries , new Comparator<Entry<K, V>>() {
@Override
public int compare(Entry<K, V> e1, Entry<K, V> e2) {
return e2. getValue ( ).compareTo(e1. getValue ( ) );
}
});
Map<K, V> result = new LinkedHashMap<>();
for (Entry<K, V> entry : sortedEntries ) {
result.put( entry.getKey() , entry.getValue() );
}
return result ;
}
}
package competition.organization;
/**
* Exception
*
* @author Lounes Meddahi and Matthieu Medeng Essia
*
*/
public class NotPowerOfTwoNumberOfCompetitorsException extends RuntimeException {
/**
* This exception is raised when we try to play a tournament without a power of two number of competitors
* @param msg to print
*/
public NotPowerOfTwoNumberOfCompetitorsException(String msg) {
super(msg);
}
}
package
competition.organization;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import competition.Competition;
import competition.competitor.Competitor;
/**
* Class to create a tournament
*
* @author Lounes Meddahi and Matthieu Medeng Essia
*
*/
public class Tournament extends Competition {
/**
* Tournament constructor
* @param competitors of the tournament
* @throws NotPowerOfTwoNumberOfCompetitorsException raised if there is not a power of two number of competitors
*/
public Tournament(List<Competitor> competitors) throws NotPowerOfTwoNumberOfCompetitorsException{
super(competitors);
if( ! powerOfTwoBitwise(competitors.size())) {
throw new NotPowerOfTwoNumberOfCompetitorsException("Not even number of competitors");
}
}
/**
* Function to know if a number is a power of 2 or not
* @param n number to check
* @return True if n is a power of two
* False else.
*/
private static boolean powerOfTwoBitwise(int n)
{
return (n & n-1)==0;
}
/**
* This function plays a tournament.
* The tournament is finish when there is one competitor left.
* @param competitors
*
*/
protected void play(List<Competitor> competitors) {
List<List<Competitor>> competitorCouple = new LinkedList<>();
List<Competitor> winners = new LinkedList<>(competitors); //List of competitors still in list
competitorCouple = this.splitCompetitorsForOneRound(competitors);
Boolean finish = false;
while(! finish){
competitorCouple = this.splitCompetitorsForOneRound(winners);
winners = new LinkedList<>();
for (List<Competitor> p : competitorCouple) {
this.playMatch(p.get(0), p.get(1));
if(p.get(0).getPoints()>p.get(1).getPoints()) {
winners.add(p.get(0));
}
else {
winners.add(p.get(1));
}
}
finish = winners.size()==1;
}
}
/**
* This function divides a list of competitors into a list of competitor pairs
* @param comp competitors to be distributed in pairs
* @return a list with pairs of competitors
*/
private List<List<Competitor>> splitCompetitorsForOneRound(List<Competitor> comp) {
int size = comp.size();
List<List<Competitor>> subList = new LinkedList<>();
for(int i = 0; i<size; i+=2) {
subList.add(comp.subList(i, i+2));
}
return subList;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment