From d81aa7fa82a435cffb1d28b9effa5ed7923fdf30 Mon Sep 17 00:00:00 2001 From: rick Date: Sat, 6 Mar 2021 17:53:49 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20premi=C3=A8re=20partie=20en=20Java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jour02/Box.java | 26 ++++++++++++++++ jour02/Jour2.java | 79 +++++++++++++++++++++++++++++++++++++++++++++++ jour02/Main.java | 9 ++++++ 3 files changed, 114 insertions(+) create mode 100644 jour02/Box.java create mode 100644 jour02/Jour2.java create mode 100644 jour02/Main.java diff --git a/jour02/Box.java b/jour02/Box.java new file mode 100644 index 0000000..b90dcc9 --- /dev/null +++ b/jour02/Box.java @@ -0,0 +1,26 @@ +public class Box { + private int l, w, h; + + public Box (int l, int w, int h) { + this.l = l; + this.w = w; + this.h = h; + } + + public int getSurface() { + return 2*(this.l*this.w + this.w*this.h + this.h*this.l); + } + + public int areaSmallSurface() { + int wl = this.w * this.l; + int wh = this.w * this.h; + int lh = this.l * this.h; + + int ret = wl >= wh ? wh : wl; + + if (ret > lh) + ret = lh; + + return ret; + } +} diff --git a/jour02/Jour2.java b/jour02/Jour2.java new file mode 100644 index 0000000..7a757bf --- /dev/null +++ b/jour02/Jour2.java @@ -0,0 +1,79 @@ +import java.util.Scanner; +import java.io.FileInputStream; +import java.io.RandomAccessFile; +import java.util.ArrayList; + +public class Jour2 { + private String filename; + private FileInputStream fis; + private Scanner sc; + private ArrayList listBox; + + public Jour2(String filename) { + this.filename = filename; + this.listBox = new ArrayList(); + } + + public void premPartie() { + int result = 0, x, y, z; + this.loadFile(); + try { + String line; + while (sc.hasNextLine()) { + line = sc.nextLine(); + String[] tokens = line.split("x"); + x = Integer.parseInt(tokens[0]); + y = Integer.parseInt(tokens[1]); + z = Integer.parseInt(tokens[2]); + this.listBox.add(new Box(x, y, z)); + } + + for (int i = 0; i < this.listBox.size(); i++) { + result += this.listBox.get(i).getSurface(); + result += this.listBox.get(i).areaSmallSurface(); + } + } catch (Exception e) { + System.err.println("Erreur !!"); + e.printStackTrace(); + } + + this.unloadFile(); + System.out.println("Il faut " + result + " pieds de papiers cadeaux."); + } + + public void deuxPartie() { + this.loadFile(); + try { + //while (sc.hasNextLine()) { + // + //} + + } catch (Exception e) { + System.err.println("Erreur !!"); + e.printStackTrace(); + } + this.unloadFile(); + } + + private void loadFile() { + try { + this.fis = new FileInputStream(filename); + this.sc = new Scanner(fis); + } catch (Exception e) { + System.err.println("Erreur rencontrée lors de" + + "l’ouverture du fichier."); + e.printStackTrace(); + } + } + + private void unloadFile() { + try { + this.sc.close(); + this.fis.close(); + } catch (Exception e) { + System.err.println("Erreur rencontrée lors de la " + + "fermeture du fichier."); + e.printStackTrace(); + } + } +} diff --git a/jour02/Main.java b/jour02/Main.java new file mode 100644 index 0000000..8793816 --- /dev/null +++ b/jour02/Main.java @@ -0,0 +1,9 @@ +public class Main { + static public void main (String[] args) { + Jour2 j = new Jour2("input"); + System.out.println("Exécution de la première partie…"); + j.premPartie(); + System.out.println("Exécution de la deuxième partie…"); + //j.deuxPartie(); + } +}