Bonsoir,
Dans un premier temps les règles du forum stipulent qu'il faut mettre ton code dans une balise [CODE=Java], j'ai mis CODE=Java car ton langage est le Java.
Ensuite concernant ton code, il faut créer une classe Movie à part dans un paquetage que tu peux appeller fr.monprojet.domaine par exemple où monprojet est le nom de ton projet. Il s'agit du guide des bonnes pratiques du Java.
Ensuite, tu crées un paquetage fr.monprojet.application où là tu créer la classe Application contenant ton point d'entrée à l'application, la fameuse méthode
public static void main(String[] args) { ...}
Par ailleurs tous les attributs doivent être mis en visibilité cachée et en lecture seule (donc non modifiable), c'est-à-dire en
private final String title;
Tu devras par ailleurs créer un constructeur paramétré car les attributs sont en visibilité privée, ainsi que des accesseurs pour "accéder" à la valeur des attributs puis les fameuses méthodes display() et getTotalPrice().
Voici un exemple de bonnes pratiques que tu pourras adopter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
package edu.bordeaux.domaine;
public class Movie {
private final String title;
private final int dateSortie;
private final double price;
private final int quantity;
public Movie(final String title, final int dateSortie, final double price, final int quantity) {
this.title = title;
this.dateSortie = dateSortie;
this.price = price;
this.quantity = quantity;
}
public String getTitle() {
return title;
}
public int getDateSortie() {
return dateSortie;
}
public double getPrice() {
return price;
}
public int getQuantity() {
return quantity;
}
public void display() {
// The Imitation Game, sorti en 2014, 2 exemplaires à 14.99 euros, total : 29.98 euros
System.out.println(this.getTitle() + ", sorti en " + this.getDateSortie() + ", " + this.getQuantity() + " exemplaires à "
+ this.getPrice() + " euros, total : " + this.getTotalPrice() + " euros");
}
public double getTotalPrice() {
return this.getPrice() * this.getQuantity();
}
} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
package edu.bordeaux.application;
import edu.bordeaux.domaine.Movie;
import java.time.LocalDate;
import java.time.Month;
public class Application {
public static void main(String[] args) {
LocalDate dateSortie = LocalDate.of(2014, Month.DECEMBER, 25);
Movie movie1 = new Movie("The Imitation Game", dateSortie.getYear(), 14.99, 2);
movie1.display();
}
} |
Partager