This question already has answers here:
Using assertArrayEquals in unit tests
(4 answers)
Closed 8 years ago.
I am having issues with my assertArrayEquals in java. It is asking me to create a new method but i am using JUnit so I don't really understand how i can fix this issue.
This is my Junit test case
#Test
public void testSortInsert() throws IOException {
Word tester1 [] = input(0,16);
Word tester2 [] = input(1,256);
Word tester3 [] = input(2,256);
Insertion.sortInsert(tester1);
Word insert1 [] = input(0,16);
StopWatch time = new StopWatch();
time.start();
Insertion.sortInsert(insert1);
time.stop();
int insertTime = timer(time);
assertArrayEquals(insert1,tester1);
}
This is my word file which has my Word ADT. It includes a #overide for equals
package sort;
public class Word implements Comparable<Word>{
private String word;
private int score;
public Word(String w, int s)
{
this.word = w;
this.score = s;
}
public Word() {
// TODO Auto-generated constructor stub
}
public int getScore()
{
return this.score;
}
public void setScore(int s)
{
this.score = s;
}
public String getWord()
{
return this.word;
}
public void setWord(Word w)
{
this.word = w.word;
}
#Override
public int compareTo(Word w)
{
if (this.score > w.score){
return 1;
}
if (this.score < w.score){
return -1;
}
return 0;
}
#Override
public boolean equals(Object x)
{
if (this == x) { return true; }
if (x == null) { return false; }
if (getClass() != x.getClass()) { return false; }
Word other = (Word) x;
if (score != other.score) { return false; }
if (word == null)
{
if (other.word != null) { return false; }
else if (!word.equals(other.word)) { return false; }
}
return true;
}
public String toString()
{
//TODO
return "{" + this.word + "," + this.score + "}";
}
}
Import the static methods in the org.junit.Assert class:
import static org.junit.Assert.*;
or use the class name:
Assert.assertArrayEquals(insert1,tester1);
If you're using JUnit 4 (as it looks like you are), you presumably aren't extending an existing test class - so you need to call the static assertArrayEquals with the class name, e.g.
import org.junit.Assert;
...
Assert.assertArrayEquals(...);
Now, that ends up being a bit clunky, so often developers statically import the methods they want, e.g.
import static org.junit.Assert.assertArrayEquals;
...
assertArrayEquals(...);
or just import everthing:
import static org.junit.Assert.*;
...
assertArrayEquals(...);
Related
I created an item named player as follows:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
public class player implements Comparable <player> {
int PlayerId ;
String name ;
double salary;
public player(int PlayerId) {
this.PlayerId = PlayerId;
}
public void setPlayerId(int PlayerId) {
this.PlayerId = PlayerId;
}
public void setName(String name) {
this.name = name;
}
public void setSalary(double salary) {
this.salary = salary;
}
public int getID() {
return PlayerId;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
#Override
public int hashCode() {
int key = 2;
return key=2*key+PlayerId;
}
#Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final player other = (player) obj;
if (this.PlayerId != other.PlayerId) {
return false;
}
return true;
}
#Override
public String toString(){
return hashCode()+" "+getID() +" "+getName()+" "+getSalary();
}
// generic method StoreplayerDetails
public <T> void StoreplayerDetails( HashMap<Integer,T> inputMap ) {
// save elements into text file
PrintWriter pw = null;
try {
pw = new PrintWriter(new FileOutputStream("OutPut.txt"));
for(T element : inputMap.values())
pw.println(element);
pw.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(MainProgram.class.getName()).log(Level.SEVERE, null, ex);
} finally {
pw.close();
}
}
#Override
public int compareTo(player other) {
if(this.salary>other.salary)
return 1;
else
if(this.salary<other.salary)
return -1;
return 0;
}
public interface Update {
public <T> void updateSalaries( HashMap<Integer,player> inputMap);
}
}
create an interface named update in the player class ,create a generic method named updateSalaries in the interface that takes a HashMap as input and returns a Queue of player objects after updating the salaries of players by adding 500 to each one's salary .
in the mainprogram class implement the method updatesalaries as a lamdba expression .in the mainprogram class,print the elements in the returned queue .
I tried it as follows but it did not work out:
#Override
public <T> void updateSalaries(HashMap<Integer, player> map) {
map.replaceAll((k,player.getSalary()) -> player.getSalary()+500;
System.out.println("new map"+map);
}
This is the full code in the main class
import java.util.HashMap;
import player.player.Update;
public class MainProgram implements Update{
public static void main(String[] args) {
HashMap< Integer,player> Keys = new HashMap<>();
player p1 =new player(1);
p1.setName("Ali");
p1.setSalary(5000);
player p2 =new player(2);
p2.setName("Sayed");
p2.setSalary(7000);
player p3 =new player(3);
p3.setName("soha");
p3.setSalary(3000);
Keys.put(1, p1);
Keys.put(2, p2);
Keys.put(3, p3);
// p1.StoreplayerDetails(Keys);
MainProgram m = new MainProgram();
m.updateSalaries(Keys);
}
#Override
public <T> void updateSalaries(HashMap<Integer, player> map) {
map.replaceAll((k,player.getSalary()) -> player.getSalary()+500;
System.out.println("new map"+map);
}
}
Is there any help in solving this?
In your code snippet you have the following line of code:
map.replaceAll((k,player.getSalary()) -> player.getSalary()+500;
Let's take this apart piece by piece:
map.replaceAll This method lets you replace all the values in a map. I believe you want to manipulate the values that are already there, instead.
(k,player.getSalary()) This is where you name the variables that the lambda will dump values into. You aren't supposed to supply numbers here, you are supposed to be receiving numbers. You likely want (k, p), where k will be set to the key (an Integer) and p will be set to the value (a player).
player.getSalary()+500 This returns an int. The replaceAll method requires that you return the value type, which in this case is player.
You forgot to include a close parenthesis at the end.
I believe you want to use this line of code instead, which mitigates all of the above errors:
map.forEach((k, p) -> p.setSalary(p.getSalary() + 500));
class Color implements Comparable<Color>{
private long RValue;
private long GValue;
private long BValue;
public long mix;
public Color() {
this.RValue=0;
this.GValue=0;
this.BValue=0;
this.mix=0;
}
public Color(long c) {
this.RValue=c;
this.GValue=c;
this.BValue=c;
this.mix=c;
}
private void calcMix() {
this.mix=256*256*this.RValue+256*this.GValue+1*this.BValue;
}
public long getRValue() {
return this.RValue;
}
public long getGValue() {
return this.GValue;
}
public long getBValue() {
return this.BValue;
}
public void setRValue(long RValue) {
this.RValue=RValue;
calcMix();
}
public void setGValue(long GValue) {
this.GValue=GValue;
calcMix();
}
public void setBValue(long BValue) {
this.BValue=BValue;
calcMix();
}
public String toString() {
return this.RValue+" "+this.GValue+" "+this.BValue+" "+this.mix;
}
public boolean equals(Object r) {
if(r==this)
return true;
if(r==null)
return false;
if(r.getClass()!=this.getClass())
return false;
Color color=(Color) r;
if(this.RValue!=color.RValue || this.GValue!=color.GValue || this.BValue!=color.BValue)
return false;
return true;
}
public int compareTo(Color c) {
if(this.mix<c.mix)
return -1;
else if(this.mix==c.mix)
return 0;
else
return 1;
}
}
class ColorRectangle extends Color implements Comparable<Color>{
private int iX1,iY1,iX2,iY2;
public ColorRectangle() {
super();
iX1=0;
iY1=0;
iX2=0;
iY2=0;
}
public ColorRectangle(int iX1,int iY1,int iX2,int iY2,long color) {
super(color);
this.iX1=iX1;
this.iY1=iY1;
this.iX2=iX2;
this.iY2=iY2;
}
public int getIX1() {
return iX1;
}
public int getIY1() {
return iY1;
}
public int getIX2() {
return iX2;
}
public int getIY2() {
return iY2;
}
public void setIX1(int iX1) {
this.iX1=iX1;
}
public void setIY1(int iY1) {
this.iY1=iY1;
}
public void setIX2(int iX2) {
this.iX2=iX2;
}
public void setIY2(int iY2) {
this.iY2=iY2;
}
public int calcArea() {
return ((Math.max(this.iX1, this.iX2)-Math.min(this.iX1, this.iX2))*
(Math.max(this.iY1, this.iY2)-Math.min(this.iY1, this.iY2)));
}
public int calcPerimeter() {
return (2*(Math.max(this.iX1, this.iX2)-Math.min(this.iX1, this.iX2))+
2*(Math.max(this.iY1, this.iY2)-Math.min(this.iY1, this.iY2)));
}
public int compareTo(ColorRectangle r) {
if(this.calcArea()<r.calcArea())
return -1;
else if(this.calcArea()==r.calcArea())
return 0;
else
return 1;
}
public String toString() {
return iX1+" "+iY1+" "+iX2+" "+iY2+" "+mix;
}
public boolean equals(ColorRectangle r) {
if(!(r instanceof ColorRectangle))
return false;
ColorRectangle rect=r;
return (this.calcArea()==rect.calcArea() && this.getRValue()==rect.getRValue()
&& this.getGValue()==rect.getGValue() && this.getBValue()==rect.getBValue());
}
public void translateX(int iPoints) {
this.iX1+=iPoints;
this.iX2+=iPoints;
}
public void translateY(int iPoints) {
this.iY1+=iPoints;
this.iY2+=iPoints;
}
public void translateXY(int iPoints) {
this.translateX(iPoints);
this.translateY(iPoints);
}
public boolean isInside(int ptX,int ptY) {
if(ptX>iX1 && ptX<iX2 && ptY>iY1 && ptY<iY2)
return true;
return false;
}
public ColorRectangle unionRect(ColorRectangle r) {
int x1=Math.min(Math.min(this.iX1, this.iX2), Math.min(r.getIX1(),r.getIX2()));
int x2=Math.max(Math.min(this.iX1, this.iX2),Math.max(r.getIX1(), r.getIX2()));
int y1=Math.min(Math.min(this.iY1, this.iY2), Math.min(r.getIY1(),r.getIY2()));
int y2=Math.max(Math.min(this.iY1, this.iY2),Math.max(r.getIY1(), r.getIY2()));
long color=256*256*this.getRValue()+256*this.getGValue()+1*this.getBValue();
return new ColorRectangle(x1,x2,y1,y2,color);
}
public ColorRectangle intersectionRect(ColorRectangle r) {
int x1=Math.min(this.iX1,r.iX2);
int x2=Math.max(this.iX1,r.iX2);
int y1=Math.min(this.iY1, r.iY2);
int y2=Math.max(this.iY1, r.iY2);
long color=256*256*this.getRValue()+256*this.getGValue()+1*this.getBValue();
return new ColorRectangle(x1,x2,y1,y2,color);
}
}
class RectangleCollection{
private SortedSet recSet;
public RectangleCollection(String fileName) throws IOException{
try {
RandomAccessFile file=new RandomAccessFile(fileName,"r");
String line="";
String[] info=new String[5];
recSet=new TreeSet<ColorRectangle>();
while((line=file.readLine()) != null && line.length()>0 ) {
info=line.split(" ");
recSet.add(new ColorRectangle(Integer.parseInt(info[0]),
Integer.parseInt(info[1]),Integer.parseInt(info[2]),Integer.parseInt(info[3]),
Long.parseLong(info[4])));
}
file.close();
}
catch(FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
public void print() {
Iterator<RectangleCollection>iterator=recSet.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next()+" ");
}
}
}
This is my main function:
public static void main(String[] args)throws IOException {
RectangleCollection recCol=new RectangleCollection("rects.txt");
recCol.print();
}
So i have this code in java and the class RectangleCollection.
My task is to create interface SortedSet of type TreeSet which will be for storing ColorRectangle objects. ColorRectangle class has 5 data members: x1,x2,y1,y2,color.
I have to make explicit constructor with name -> filename from which i will read ColorRectangle objects which i will add in TreeSet.
and this is my texfile rects.txt:
-10 -10 6 10 255
-1 -1 10 6 255
-2 -2 10 6 255
*-3 -1 10 6 255
-1 -1 10 6 255
Qhen i debug it, the print function shows me only the first line from the textfile. Can you tell me how to fix it?
I'm trying to sort an ArrayList containing TurnChoices (I will show the code of this class). So I created a class that implements Comparator https://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html
compare method is supposed to return -1,0,1 as the first argument is less than, equal to, or greater than the second, right? I am trying to understand why my ArrayList has the lower TurnChoice on the first index. I could replace 1 by -1 and -1 by 1 but this wouldn't respect what the documentation says and I want to understand what is wrong.
TurnChoiceComparater class:
package pkmnVerDemacia.battle.turn;
import java.util.Comparator;
import pkmnVerDemacia.battle.turn.item.UseItemChoice;
import pkmnVerDemacia.battle.turn.swap.SwapChampionChoice;
import pkmnVerDemacia.model.champion.Champion;
import pkmnVerDemacia.model.champion.stat.STAT;
public class TurnChoiceComparater implements Comparator<TurnChoice>
{
#Override
public int compare(TurnChoice tc1, TurnChoice tc2)
{
if (tc1.getPriority() > tc2.getPriority())
return 1;
else if (tc1.getPriority() == tc2.getPriority()) {
return compareChampSpeed(tc1.getChampionUser(), tc2.getChampionUser());
}
else
return this.getRandomSort();
}
private int compareChampSpeed(Champion c1, Champion c2) {
if (c1.getStat(STAT.SPEED) > c2.getStat(STAT.SPEED)) {
return 1;
}
else if (c1.getStat(STAT.SPEED) < c2.getStat(STAT.SPEED)) {
System.out.println(c2.getName()+" is faster than "+c1.getName()+" so I return -1");
System.out.println("first TurnChoice has Champion: "+c1.getName());
System.out.println("second TurnChoice has Champion: "+c2.getName());
return -1;
}
else
return this.getRandomSort();
}
private int getRandomSort() {
double random = Math.random();
if (random > 0.5)
return 1;
else
return -1;
}
}
Here TurnChoice class
package pkmnVerDemacia.battle.turn;
import pkmnVerDemacia.battle.BATTLE_PARTY;
import pkmnVerDemacia.battle.Battle;
import pkmnVerDemacia.battle.event.BattleEvent;
import pkmnVerDemacia.battle.event.BattleEventQueuer;
import pkmnVerDemacia.model.champion.Champion;
public abstract class TurnChoice
{
protected BattleEventQueuer beQueuer;
protected BATTLE_PARTY userParty;
protected int priority;
public TurnChoice(BattleEventQueuer beQueuer, BATTLE_PARTY userParty, int priority) {
this.beQueuer = beQueuer;
this.userParty = userParty;
this.priority = priority;
}
public int getPriority()
{
return this.priority;
}
public BATTLE_PARTY getUserParty() {
return this.userParty;
}
public BattleEventQueuer getBEQueuer() {
return this.beQueuer;
}
public Champion getChampionUser() {
return this.beQueuer.getChampion(this.userParty);
}
public abstract boolean begin();
}
And here is where I test the code:
public static void sortTurnChoices(ArrayList<TurnChoice> choices)
{
Collections.sort(choices, new TurnChoiceComparater());
for (TurnChoice choice : choices)
System.out.println(choice.getChampionUser().getName());
}
Console result here:
As you can see Vi is faster than Braum so why Braum is at the first index of the ArrayList?
I've created this method and I'm unsure why it says there's a missing return statement. do I need to change the print to a return? (it's the method at the very bottom) I'm a bit of a Java beginner so any help will be appreciated!
public class Book {
private String title;
private String author;
private int copies;
private boolean borrowed;
public Book( String inAuthor, String inTitle, int inNumberOfCopies ) {
this.author = inAuthor;
this.title = inAuthor;
this.copies = inNumberOfCopies;
}
public void borrowed() {
borrowed = true;
}
public void rented() {
borrowed = true;
}
public void returned() {
borrowed = false;
}
public boolean isBorrowed() {
return borrowed;
}
public String getAuthor() {
return this.author;
}
public static String getTitle() {
return getTitle();
}
public int getTotalCopies() {
return this.copies;
}
public int getAvailableCopies() {
}
public void withdrawCopy() {
int found = 0;
for (Book b : Library.getListOfBooks()) {
if (b.getTitle().equals(title)) {
if (found == 0) {
found = 1;
}
if (!b.isBorrowed()) {
b.borrowed=true;
found = 2;
break;
}
if (found == 0) {
System.out.println("Sorry, this book is not in our catalog.");
} else if (found == 1) {
System.out.println("Sorry, this book is already borrowed.");
} else if (found == 2) {
System.out.println("You successfully borrowed " + title);
}
}
}
}
public String returnCopy() {
boolean found = false;
for (Book book : Library.getListOfBooks()) {
if (getTitle().equals(title) && book.isBorrowed()) {
book.returned();
found = true;
}
if (found) {
System.out.println("you successfully returned " + title);
}
}
}
}
public String returnCopy()
String after public means that this method will return a String.
Your public String returnCopy() is currently not returning anything.
If you don't want to return anything, you can use void like this:
public void returnCopy(){
// code
}
Same issue with public int getAvailableCopies(), this is supposed to return an int but you are not returning anything.
Be careful:
this method:
public static String getTitle() {
return getTitle();
}
is a recursive method without a base condition. This will cause an error and force your application to crash.
You've defined the method as returning a String but you don't return a value anywhere in the method body. Simplest fix is probably to change the return type to void...
public void returnCopy() {...
}
All the above answer are pointing to the same issue, you have defined methods that are breaking the contract about what they return..
In you code you have as well something like this:
public int getAvailableCopies() {
}
so you are telling the compiler, you have a method with the name getAvailableCopies, it takes no params and return an integer.
BUT if you don't return anything, then you are contradicting your own method, your own contract, this is an enough reason for a compiler to complain...
Conclusion:
keep in mind the information that defines the method.
So I'm having this problem with adding an element to an ArrayList
I have a class Media with 3 fields and another class Mediatheque with 1 field(which is an ArrayList).
Let's say I have:
A Mediatheque media = new Mediatheque
An equals(Media m) method in class Media < (important method)
I need to write a method add(Media m) which:
If the media.contenu does contain an element equals to the Media m I want to add, I must NOT add it and increase the nbEx field of the element contained in media.contenu
-Else I can add it using the add method provided by the ArrayList ( This doesn't seem too hard)
So I tried to write a contains(Media) method which uses the equals(Media m) method I wrote for the Media class and then use the contains method in the add method.
My question is that how am I supposed to write the add method? < (The Question)
I must write this using ArrayList, it is a school assignment
Sorry about the long code and the bad English, I'm a complete noob.
Here is my Media class:
package Ex1;
public class Media {
private final String support; // Format: Book, CD, DVD,etc...
private final String titre; // Title
private int nbEx; // Number of copy
public Media(String titre, String support){
this.titre = titre;
this.support = support;
this.nbEx = 1;
}
public Media (){
titre = "";
support = "";
nbEx = 0;
}
public boolean equals(Media m){
boolean equality = false;
if (m instanceof Media){
equality = (this.titre.equals(m.titre) && this.support.equals(m.support));
}
return equality;
}
public Media(Media m){
this.titre = m.titre;
this.support = m.support;
}
}
And here is my Mediatheque class:
import java.util.ArrayList;
import static java.lang.System.out;
public class Mediatheque {
ArrayList<Media> contenu;
public Mediatheque(){
this.contenu = new ArrayList<Media>();
}
public Mediatheque(Mediatheque m){
this.contenu = m.contenu;
}
public boolean contains(Media m){
int i = 0;
boolean contain = this.contenu.get(i).equals(m);
for(i = 0; i<this.contenu.size(); i++){
if(contain)
break;
}
return contain;
}
public int indexOf(Media m){
boolean retVal = this.contenu.get(i).equals(m);
for(Media i : contenu){
if(contain)
break;
}
return i;
}
public void add(Media m){
if(this.contains(m)){
this.contenu.get(this.contenu.indexOf(m)).setNbEx(this.contenu.get(this.contenu.indexOf(m)).getNbEx()+m.getNbEx());
}else{
this.contenu.add(m);
}
}
My question is that how am I supposed to write the add method?
Sorry about the long code and the bad English, I'm a complete noob.
Thank you!
As stated by #NeplatnyUdaj in the comment of your question, the use of a Map would greatly improve your code. Instead of recording the number of medias inside the Media object, use a HashMap<Media, Integer> to store your data in this way:
new HashMap<Media, Integer> map = new HashMap<Media,Integer>();
if ( map.containsKey(key) ) {
map.put(key, (map.get(key) + 1));
} else {
map.put(key, 1);
}
Where key is the media. (m in your code)
When one overrides the equals() method, one is also supposed to override the hashCode() method. The equals() method takes an Object parameter. Here's how your Media class should look like:
// Media.java
public class Media
{
private final String support;
private final String title;
public Media(String title, String support)
{
this.title = title;
this.support = support;
}
public Media(Media media)
{
this(media.title, media.support);
}
#Override
public int hashCode()
{
return 31 * title.hashCode() + support.hashCode();
}
#Override
public boolean equals(Object object)
{
if (object instanceof Media)
{
Media media = (Media) object;
return media.title.equals(title) &&
media.support.equals(support);
}
return false;
}
}
Then use a HashMap to map the media with its number of copies. Here's how that's done:
// MediaMap.java
import java.util.HashMap;
import java.util.Map;
public class MediaMap
{
// Media to its Number of Copies mapping.
private Map<Media, Integer> mediaMap;
public MediaMap()
{
mediaMap = new HashMap<>();
}
public void add(Media media)
{
mediaMap.put(media, mediaMap.getOrDefault(media, 0) + 1);
}
public void removeOneMedia(Media media)
{
if (mediaMap.containsKey(media))
{
mediaMap.put(media, mediaMap.get(media) - 1);
}
}
// And so on...
}
Without overriding the hashCode() method in the Media class, the hash based collections won't work as expected.
You can also have a look at MultiSet data structure, and use that instead.
If you are to use ArrayList then here's how its done:
// Media.java
public class Media
{
private final String support;
private final String title;
private int numberOfCopies;
public Media(Media media)
{
this(media.title, media.support, media.numberOfCopies);
}
public Media(String title, String support, int numberOfCopies)
{
this.title = title;
this.support = support;
this.numberOfCopies = numberOfCopies;
}
#Override
public int hashCode()
{
return 31 * title.hashCode() + support.hashCode();
}
#Override
public boolean equals(Object object)
{
if (object instanceof Media)
{
Media media = (Media) object;
return media.title.equals(title) &&
media.support.equals(support);
}
return false;
}
public int getNumberOfCopies()
{
return numberOfCopies;
}
public void setNumberOfCopies(int numberOfCopies)
{
this.numberOfCopies = numberOfCopies;
}
}
And here's a MediaList class which uses ArrayList:
// MediaList.java
import java.util.ArrayList;
public class MediaList
{
private ArrayList<Media> mediaList;
public MediaList()
{
mediaList = new ArrayList<>();
}
public void add(Media media)
{
set(media, +1);
}
public void remove(Media media)
{
set(media, -1);
}
private void set(Media media, int change)
{
if (change == 0)
{
return;
}
int indexOfMedia = mediaList.indexOf(media);
if (indexOfMedia != -1)
{
Media m = mediaList.get(indexOfMedia);
m.setNumberOfCopies(m.getNumberOfCopies() + change);
if (change < 0 && m.getNumberOfCopies() <= 0)
{
mediaList.remove(media);
}
}
else if (change > 0)
{
mediaList.add(media);
}
}
// And so on...
}
I have refactored your classes a little bit. I also implemented an add method. I assumed that you want to add media to the mediatheque if it is not already in the list. If it is in the list you want to add the nbex to the nbex that the item in the list has, right?
As the others I would advise you to use a HashMap() for counting if you don't need the number for your media objects.
Media.class
public class Media {
private final String support; // Format: Book, CD, DVD,etc...
private final String titre; // Title
private int nbEx; // Number of copy
public Media(String titre, String support){
this.titre = titre;
this.support = support;
this.nbEx = 1;
}
public Media(Media m){
this(m.titre, m.support);
}
public Media (){
this("", "");
nbEx = 0;
}
public boolean equals(Media m){
if (m instanceof Media){
return (this.titre.equals(m.titre) && this.support.equals(m.support));
}
return false;
}
}
Mediatheque.class
public class Mediatheque {
ArrayList<Media> contenu;
public Mediatheque(){
this.contenu = new ArrayList<Media>();
}
public Mediatheque(Mediatheque m){
this.contenu = m.contenu;
}
public boolean contains(Media m){
for(Media media: this.contenu) {
if(media.equals(m) {
return true;
}
}
return false;
}
public int indexOf(Media m){
if(this.contenu.contains(m) {
return this.contenu.indexOf(m);
}
return -1;
}
public void add(Media m){
if(this.contains(m)) {
Media media = this.contenu.get(this.contenu.indexOf(m));
media.setNbex(media.getNbex() + m.getNbex());
} else {
this.contenu.add(m);
}
}
}
Hope this helps.