Java - Exercises - Media and Media Library

1. Create a Media class containing
(a) Three attributes:
            private final String title;
            private final String support;
            private int nbCopies;
(b) The three appropriate "getters".
(c) A "setter" for nbExers.
(d) A Media(String title, String support) constructor whose arguments match the first two attributes and which sets nbExtes=1.
(e) A void method displays() which displays Poly of TO2 (Book) x2 if support="Book", title="TO2 Poly" and nbCopies=2.
(f) A Boolean method isEqual(Media el) that returns true if this and el have identical media and title.


2. In a Main class, create a main method to test all of this. Particular attention should be paid to the test of estEgalA.

3. Create a Media Library class containing
(a) An ArrayList< Media> content.
(b) A Mediatheque() constructor that initializes the attribute to an empty array.
(c) A void method adds (Media el) that appends el to content if it is not already there; if it is already there, the method must increase the value of its nbExemplars.
(d) A void method displays() that displays all the Media in the collection, one per line.

Solution:

1. Created the Media
a. Media
The keywordfinal indicates that the element of the created instance will not change during execution, for example if a second instance of Media is created, the attributes that are not set final of the first instance will receive the values of the second instance:
Media m1 = new Media("title1","support1");
Media m2 = new Media("title2","support2");
After display:
m1 (title2, support2)
m2 (title2, support2)
Note: Click view plain to get the text and do Copy/Paste.


public class Media {

private final String title;
private final String support;
private int nbCopies;

}
b and c. The three getters and a setter for nbCopies
Right-click --> Source --> Generate Getters and Setters.

generate getters and setters

public int getNbCopies() {
return nbCopies;
}

public void setNbCopies(int nbCopies) {
this.nbCopies = nbCopies;
}

public String getTitre() {
return titre;
}

public String getSupport() {
return support;
}
d. The constructor

public Media(Title String, String support){
this.title = title;
this.support = support;
nbCopies = 1;
}
e. Method display()

void display(){
if(this.support=="Book" & & this.titre=="Poly de TO2" & & this.nbEx(exte)==2)
System.out.print(this.title+ "(" + this.support+ ") x"+this.nbEx(eX));
}
f. Boolean method isEquals(Media el)

boolean isEqualsA(Media el){
if( support==el.support & & title==el.title)
return true;
return false;
}

2. Creation of the main

public class Main {

public static void main(String[] args) {
//creation of three Media
Media instances m = new Media("title1", "support1");
Media m2 = new Media("title2", "support2");
Media m3 = new Media("title3", "support3");

//test equality
boolean isEqualA = m.isEqualsA(m2);
if(isEquals)
System.out.println("Media("+m.getTitle()+", "+m.getSupport()+") is equal to Media("+m2.getTitle()+", "+m2.getSupport()+")" );
else
System.out.println("+m.getTitle()+", "+m.getSupport()+") is not equal to Media("+m2.getTitle()+", "+m2.getSupport()+")" );

/************************************
* Médiathèque class
*/
Médiathèque mt = new Médiathèque();
//add
mt.add(m);
mt.added(m2);
mt.added(m2);
mt.added(m3);
//display
mt.display();
}
}

3. Creation of the Mediatheque
a. L'attirubt Arraylist< Media> content
Arraylist is an array that is not set in size, its advantage is that it is infinite, and contains predefined methods such as get and set, sequential search contains(element), or indexOf(element).
< Media> means that the items that our Arraylist stock are of type Media. This will save us from doing the following conversion: (Media) ourList.get(index).

public class Media Library {

ArrayList content;

}
b. The constructor

Mediatheque(){
this.contenu = new ArrayList();
}
c. The method adds(Media el)

void adds(Media el){
if(!this.content.contains(el))
this.content.add(el);
else{
/* if there already exists
* increase the value of nbExemplar
*/
//index of el in the list
int index = this.content.indexOf(el);
//read the nbCopies attribute and increment
int nbCopies = this.content.get(index).getNbCopies() + 1;
//update the nbCopies
System.out.println(this.content.get(index).getTitle());
this.content.get(index).setNbEx(nbEx)
}
}

d. The method displays()

void displays(){
for(Media media: this.contenu)
System.out.println(media.getTitre()+" "+media.getSupport()+" "+media.getNbCopies());
}

Full code Media.java:

public class Media {

private final String title;
private final String support;
private int nbCopies;

public Media(String title, String support){
this.title = title;
this.support = support;
nbCopies = 1;
}

void display(){
if(this.support=="Book" & & this.titre=="Poly de TO2" & & this.nbEx(exte)==2)
System.out.print(this.title+ "(" + this.support+ ") x"+this.nbEx(eX));
}

boolean isEqualsA(Media el){
if( support==el.support & & title==el.title)
return true;
return false;
}


public int getNbCopies() {
return nbCopies;
}

public void setNbCopies(int nbCopies) {
this.nbCopies = nbCopies;
}

public String getTitre() {
return titre;
}

public String getSupport() {
return support;
}
}

Mediatheque.java

import java.util.ArrayList; 

public class Media Library {

ArrayList content;

Media Library(){
this.contenu = new ArrayList();
}

void adds(Media el){
if(!this.content.contains(el))
this.content.add(el);
else{
/* if there already exists
* increase the value of nbExemplar
*/
//index of el in the list
int index = this.content.indexOf(el);
//read the nbCopies attribute and increment
int nbCopies = this.content.get(index).getNbCopies() + 1;
//update the nbCopies
System.out.println(this.content.get(index).getTitle());
this.content.get(index).setNbEx(nbEx)
}
}

void display(){
for(Media media: this.content)
System.out.println(media.getTitle()+" "+media.getSupport()+" "+media.getNbCopies());
}

}

main.java

public class Main {

public static void main(String[] args) {
//creation of three Media
Media instances m = new Media("title1", "support1");
Media m2 = new Media("title2", "support2");
Media m3 = new Media("title3", "support3");

//test equality
boolean isEqualA = m.isEqualsA(m2);
if(isEquals)
System.out.println("Media("+m.getTitle()+", "+m.getSupport()+") is equal to Media("+m2.getTitle()+", "+m2.getSupport()+")" );
else
System.out.println("+m.getTitle()+", "+m.getSupport()+") is not equal to Media("+m2.getTitle()+", "+m2.getSupport()+")" );

/************************************
* Médiathèque class
*/
Médiathèque mt = new Médiathèque();
//add
mt.add(m);
mt.added(m2);
mt.added(m2);
mt.added(m3);
//display
mt.display();
}
}