Skip to content

Instantly share code, notes, and snippets.

@lduboeuf
Created September 12, 2017 09:29
Show Gist options
  • Save lduboeuf/f8d6fecab8ab714aece57ca7241a15bf to your computer and use it in GitHub Desktop.
Save lduboeuf/f8d6fecab8ab714aece57ca7241a15bf to your computer and use it in GitHub Desktop.
Gereville v1
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.ldub.gereville;
/**
*
* @author lionel
*/
public class Gereville {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Pays p = new Pays(1, "FRANCE");
/**
* instanciation de 3 objets Ville de 3 manière différente.
*
*/
//sans constructeur ou constructeur vide
Ville v1 = new Ville();
v1.setPays(p);
v1.setNom("Toulouse");
v1.setNbHabitants(500000);
Ville v2 = new Ville("PARIS",p,1000000);
Ville v3 = new Ville("LYON", p);
v3.setNbHabitants(1000000);
//ajout des villes dans le pays
p.getVilles().add(v1);
p.getVilles().add(v2);
//on peut simplifier en rajoutant une méthode "raccourci" dans Pays
p.addVille(v3);
//parcours d'une liste de ville du pays
for (Ville v : p.getVilles()) {
v.affDesc();
}
//
v1.affDesc();
v2.affDesc();
v3.affDesc();
// obtenir le nom du pays via une instance de Ville
String nomPays = v3.getPays().getNom();
System.out.println("nomPays:" + nomPays);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.ldub.gereville;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author lionel
*/
public class Pays {
private int id;
private String nom;
private List<Ville> villes = new ArrayList<>();
public Pays(int id, String nom) {
this.id = id;
this.nom = nom;
}
public void addVille(Ville v){
this.villes.add(v);
}
public List<Ville> getVilles() {
return villes;
}
public void setVilles(List<Ville> villes) {
this.villes = villes;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom.toUpperCase();
}
@Override
public String toString() {
return nom;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.ldub.gereville;
/**
*
* @author lionel
*/
public class Ville {
private String nom;
private Pays pays;
private int nbHabitants;
public Ville(){
}
public Ville(String nom, Pays pays, int nbHabitants) {
this.nom = nom;
this.pays = pays;
this.nbHabitants = nbHabitants;
}
public Ville(String nom, Pays pays) {
this.nom = nom;
this.pays = pays;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public Pays getPays() {
return pays;
}
public void setPays(Pays pays) {
this.pays = pays;
}
public int getNbHabitants() {
return nbHabitants;
}
public void setNbHabitants(int nbHabitants) {
this.nbHabitants = nbHabitants;
}
public void affDesc(){
System.out.println(this.nom + " est une ville de " + this.pays + " de catégorie " + this.categorie());
}
private char categorie(){
char categorie = '?';
if (nbHabitants <= 0){
categorie = '?';
} else if (nbHabitants> 0 && nbHabitants < 50000){
categorie = 'A';
}else if(nbHabitants>= 50000 && nbHabitants < 200000){
categorie = 'B';
}else if(nbHabitants>= 200000 && nbHabitants < 1000000){
categorie = 'C';
}else{
categorie = 'D';
}
return categorie;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment