Skip to content
Snippets Groups Projects
Commit b13954d7 authored by Ethan Robert's avatar Ethan Robert
Browse files

tpQU-05::exo-machine

parent f3957213
No related branches found
No related tags found
No related merge requests found
package tpQU.tp05;
import java.util.ArrayList;
import java.util.Collection;
public class Element {
protected ArrayList<Element> components;
protected String label;
public Element(String label) {
this.label = label;
this.components = new ArrayList<>();
}
public void addComponent(Element e) {
this.components.add(e);
}
public Element removeComponent(int i) {
return this.components.remove(i);
}
public boolean removeComponent(Element e) {
return this.components.remove((Object) e);
}
public String toString() {
return toStringHelper(0);
}
private String toStringHelper(int indentLevel) {
String indent = "\t".repeat(indentLevel);
String result = indent + this.label;
// Si c'est une pièce, on ajoute son numéro
if (this instanceof Piece) {
result += "(N." + ((Piece) this).getReference() + ")\n";
} else {
result += ":\n";
// Sinon on parcourt ses composants
for (Element elt : components) {
result += elt.toStringHelper(indentLevel + 1);
}
}
return result;
}
}
package tpQU.tp05;
public class Machine extends Element {
public Machine(String label) {
super(label);
}
}
......@@ -23,7 +23,11 @@ public class NumberFactory {
}
public static Number number(String n) throws NumberFormatException {
try {
return NumberFactory.number(Integer.parseInt(n));
} catch (NumberFormatException e) {
return NumberFactory.number(Double.parseDouble(n));
}
}
}
package tpQU.tp05;
public class Piece extends Element {
private int reference;
public Piece (String label, int reference) {
super(label);
this.reference = reference;
}
public int getReference() {
return this.reference;
}
}
package tpQU.tp05;
public class UseStock {
public static void main (String[] args) {
Machine alice = new Machine("Poste d'Alice");
alice.addComponent(new Piece("Clavier", 1934));
alice.addComponent(new Piece("Écran", 1340));
alice.addComponent(new Piece("Souris", 2805));
Element uc = new Element("Unité centrale");
uc.addComponent(new Piece("Carte mère", 1569));
uc.addComponent(new Piece("Extension Mémoire", 289));
uc.addComponent(new Piece("Disque SCSI", 299));
Element gpu = new Element("Carte graphique");
gpu.addComponent(new Piece("Extension mémoire", 2879));
gpu.addComponent(new Piece("Processeur graphique", 7289));
uc.addComponent(gpu);
alice.addComponent(uc);
System.out.println(alice);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment