Java tutorial
/* * Copyright (C) 2015 ProBook 450g2 * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /** * * @author Aissa El Mehdi Mecheoud <wehdi-kun@hotmail.fr> */ package xmlproject; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import org.jdom2.*; import org.jdom2.input.SAXBuilder; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter; public class TraitementNote implements ajoutElement { private Element racine = null; Document document = null; private Element etudiant = null; private Element codeEtudiant = null; //private final Element codeModule = null; private Element xmlNote = null; private Element cooperatifNote = null; private Element interactifNote = null; private Element embarqueNote = null; private Element jmxNote = null; private Element testNote = null; private Element moyenne = null; public TraitementNote() { this.etudiant = new Element("etudiant"); } @Override public void readFile(String file) throws JDOMException, IOException { SAXBuilder sxb = new SAXBuilder(); document = sxb.build(file); this.racine = document.getRootElement(); } /** * * @param file * @throws FileNotFoundException * @throws IOException */ @Override public void saveFile(String file) throws FileNotFoundException, IOException { XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat()); sortie.output(document, new FileOutputStream(file)); this.etudiant.detach(); } public void addNote(String xmlNote, String interactifNote, String cooperatifNote, String embarqueNote, String jmxNote, String testNote, String codeEtudiant, String moyenne) { this.racine.addContent(etudiant); Attribute codeEtudiantId = new Attribute("id", codeEtudiant); Attribute xmlNoteId = new Attribute("id", "x1"); Attribute interactifNoteId = new Attribute("id", "i1"); Attribute cooperatifNoteId = new Attribute("id", "c1"); Attribute embarqueNoteId = new Attribute("id", "e1"); Attribute jmxNoteId = new Attribute("id", "j1"); Attribute testNoteId = new Attribute("id", "t1"); //creation des elements avec leurs contenu this.codeEtudiant = new Element("code-etudiant"); this.codeEtudiant.setText(codeEtudiant); this.codeEtudiant.setAttribute(codeEtudiantId); this.xmlNote = new Element("xml-note"); this.xmlNote.setText(xmlNote); this.xmlNote.setAttribute(xmlNoteId); this.interactifNote = new Element("interactif-note"); this.interactifNote.setText(interactifNote); this.interactifNote.setAttribute(interactifNoteId); this.cooperatifNote = new Element("cooperatif-note"); this.cooperatifNote.setText(cooperatifNote); this.cooperatifNote.setAttribute(cooperatifNoteId); this.embarqueNote = new Element("embraque-note"); this.embarqueNote.setText(embarqueNote); this.embarqueNote.setAttribute(embarqueNoteId); this.jmxNote = new Element("jmx-note"); this.jmxNote.setText(jmxNote); this.jmxNote.setAttribute(jmxNoteId); this.testNote = new Element("test-note"); this.testNote.setText(testNote); this.testNote.setAttribute(testNoteId); this.moyenne = new Element("moyenne"); this.moyenne.setText(moyenne); this.etudiant.addContent(this.moyenne).clone(); this.etudiant.addContent(this.codeEtudiant).clone(); this.etudiant.addContent(this.xmlNote).clone(); this.etudiant.addContent(this.interactifNote).clone(); this.etudiant.addContent(this.cooperatifNote).clone(); this.etudiant.addContent(this.embarqueNote).clone(); this.etudiant.addContent(this.testNote).clone(); this.etudiant.addContent(this.jmxNote).clone(); } /** * */ @Override public void relunch() { this.etudiant.detach(); this.racine.detach(); this.document.detachRootElement(); this.codeEtudiant.detach(); this.embarqueNote.detach(); this.xmlNote.detach(); this.interactifNote.detach(); this.cooperatifNote.detach(); this.jmxNote.detach(); this.testNote.detach(); this.moyenne.detach(); } /*public void relunchMoy(){ this.moyenne.detach(); this.codeEtudiant.detach(); }*/ public java.util.List<String> getNote(String code) throws InterruptedException { java.util.List<Element> listNote = racine.getChildren(); java.util.List<String> tabNotes = new ArrayList(); String code2 = null; String xml = null; String embarque = null; String test = null; String jmx = null; String cooperatif = null; String interactif = null; for (Element courant : listNote) { code2 = courant.getChild("code-etudiant").getText(); System.out.println("aaaaa " + code2 + " " + code); if (code2 == null ? code == null : code2.equals(code)) { xml = courant.getChild("xml-note").getText(); embarque = courant.getChild("embraque-note").getText(); test = courant.getChild("test-note").getText(); cooperatif = courant.getChild("cooperatif-note").getText(); interactif = courant.getChild("interactif-note").getText(); jmx = courant.getChild("jmx-note").getText(); tabNotes.add(xml); tabNotes.add(embarque); tabNotes.add(test); tabNotes.add(cooperatif); tabNotes.add(interactif); tabNotes.add(jmx); System.out.println("Traitemehhhhh : " + xml + " " + jmx); break; } else { System.out.println("Erreur dans le code 22 !"); } } return tabNotes; } public double calculMoyenne(String xml, String test, String interactif, String jmx, String cooperatif, String embarque) { int moy = 0; double moyF; moy = Integer.parseInt(xml) * 4 + Integer.parseInt(test) * 5 + Integer.parseInt(interactif) * 6 + Integer.parseInt(jmx) * 6 + Integer.parseInt(cooperatif) * 4 + Integer.parseInt(embarque) * 5; moyF = (double) moy / 30; moyF = (double) (int) (moyF * 100) / 100; System.out.println(" la moyenne " + moyF); return moyF; } public boolean isColor(double moy) { return moy < 10; } }