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 {
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) {
this.l = l;
this.w = w;
this.h = h;
}
/**
* Calcule la surface de la boite.
*
* @return la surface de la boite
*/
public int getSurface() {
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() {
// pour faire + jolie
int wl = this.w * this.l;
int wh = this.w * this.h;
int lh = this.l * this.h;
@ -23,4 +41,18 @@ public class Box {
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.io.FileInputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
public class Jour2 {
private String filename;
private FileInputStream fis;
private Scanner sc;
private ArrayList<Box> listBox;
public Jour2(String filename) {
@ -14,10 +11,14 @@ public class Jour2 {
this.listBox = new ArrayList<Box>();
}
public void premPartie() {
int result = 0, x, y, z;
this.loadFile();
/**
* Charge dans lattribut listBox les boites contenues dans le fichier.
*/
private void loadFileArrayList() {
int x, y, z;
try {
FileInputStream fis = new FileInputStream(filename);
Scanner sc = new Scanner(fis);
String line;
while (sc.hasNextLine()) {
line = sc.nextLine();
@ -27,53 +28,31 @@ public class Jour2 {
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();
}
sc.close();
fis.close();
} catch (Exception e) {
System.err.println("Erreur !!");
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.");
}
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"
+ "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();
}
if (this.listBox.isEmpty())
this.loadFileArrayList();
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.");
}
}

View file

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