Ajout 2e partie (marche pas)

This commit is contained in:
rick 2021-03-06 19:02:54 +01:00
parent d81aa7fa82
commit 914adeebbc
Signed by: Rick
GPG key ID: 2B593F087240EE99
3 changed files with 57 additions and 46 deletions

View file

@ -1,17 +1,35 @@
public class Box { public class Box {
private int l, w, h; private int l, w, h;
/**
* Constructeur de boite
*
* @param l longueur
* @param w largeur
* @param h hauteur
*/
public Box (int l, int w, int h) { public Box (int l, int w, int h) {
this.l = l; this.l = l;
this.w = w; this.w = w;
this.h = h; this.h = h;
} }
/**
* Calcule la surface de la boite.
*
* @return la surface de la boite
*/
public int getSurface() { public int getSurface() {
return 2*(this.l*this.w + this.w*this.h + this.h*this.l); return 2*(this.l*this.w + this.w*this.h + this.h*this.l);
} }
/**
* Trouve et renvoi la plus petite surface de la boite
*
* @return la plus petite surface de la boite
*/
public int areaSmallSurface() { public int areaSmallSurface() {
// pour faire + jolie
int wl = this.w * this.l; int wl = this.w * this.l;
int wh = this.w * this.h; int wh = this.w * this.h;
int lh = this.l * this.h; int lh = this.l * this.h;
@ -23,4 +41,18 @@ public class Box {
return ret; return ret;
} }
public int calculateRibbon() {
int wl = 2 * (this.w + this.l);
int wh = 2 * (this.w + this.h);
int lh = 2 * (this.h + this.l);
int ret = wl >= wh ? wh : wl;
if (ret > lh)
ret = lh;
ret += this.w * this.l * this.h;
return ret;
}
} }

View file

@ -1,12 +1,9 @@
import java.util.Scanner; import java.util.Scanner;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList; import java.util.ArrayList;
public class Jour2 { public class Jour2 {
private String filename; private String filename;
private FileInputStream fis;
private Scanner sc;
private ArrayList<Box> listBox; private ArrayList<Box> listBox;
public Jour2(String filename) { public Jour2(String filename) {
@ -14,10 +11,14 @@ public class Jour2 {
this.listBox = new ArrayList<Box>(); this.listBox = new ArrayList<Box>();
} }
public void premPartie() { /**
int result = 0, x, y, z; * Charge dans lattribut listBox les boites contenues dans le fichier.
this.loadFile(); */
private void loadFileArrayList() {
int x, y, z;
try { try {
FileInputStream fis = new FileInputStream(filename);
Scanner sc = new Scanner(fis);
String line; String line;
while (sc.hasNextLine()) { while (sc.hasNextLine()) {
line = sc.nextLine(); line = sc.nextLine();
@ -27,53 +28,31 @@ public class Jour2 {
z = Integer.parseInt(tokens[2]); z = Integer.parseInt(tokens[2]);
this.listBox.add(new Box(x, y, z)); this.listBox.add(new Box(x, y, z));
} }
sc.close();
for (int i = 0; i < this.listBox.size(); i++) { fis.close();
result += this.listBox.get(i).getSurface();
result += this.listBox.get(i).areaSmallSurface();
}
} catch (Exception e) { } catch (Exception e) {
System.err.println("Erreur !!"); System.err.println("Erreur !!");
e.printStackTrace(); e.printStackTrace();
} }
}
this.unloadFile(); public void premPartie() {
if (this.listBox.isEmpty())
this.loadFileArrayList();
int result = 0;
for (int i = 0; i < this.listBox.size(); i++) {
result += this.listBox.get(i).getSurface();
result += this.listBox.get(i).areaSmallSurface();
}
System.out.println("Il faut " + result + " pieds de papiers cadeaux."); System.out.println("Il faut " + result + " pieds de papiers cadeaux.");
} }
public void deuxPartie() { public void deuxPartie() {
this.loadFile(); if (this.listBox.isEmpty())
try { this.loadFileArrayList();
//while (sc.hasNextLine()) { int result = 0;
// for (int i = 0; i < this.listBox.size(); i++)
//} result += this.listBox.get(i).calculateRibbon();
System.out.println("Il faut " + result + " pieds de rubans.");
} 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"
+ "louverture 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();
}
} }
} }

View file

@ -4,6 +4,6 @@ public class Main {
System.out.println("Exécution de la première partie…"); System.out.println("Exécution de la première partie…");
j.premPartie(); j.premPartie();
System.out.println("Exécution de la deuxième partie…"); System.out.println("Exécution de la deuxième partie…");
//j.deuxPartie(); j.deuxPartie();
} }
} }