aoc-2015/jour02/Jour2.java

59 lines
2.2 KiB
Java
Raw Normal View History

2021-03-06 16:53:49 +00:00
import java.util.Scanner;
import java.io.FileInputStream;
import java.util.ArrayList;
public class Jour2 {
private String filename;
private ArrayList<Box> listBox;
public Jour2(String filename) {
this.filename = filename;
this.listBox = new ArrayList<Box>();
}
2021-03-06 18:02:54 +00:00
/**
* Charge dans lattribut listBox les boites contenues dans le fichier.
*/
private void loadFileArrayList() {
int x, y, z;
2021-03-06 16:53:49 +00:00
try {
2021-03-06 18:02:54 +00:00
FileInputStream fis = new FileInputStream(filename);
Scanner sc = new Scanner(fis);
2021-03-06 16:53:49 +00:00
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));
}
2021-03-06 18:02:54 +00:00
sc.close();
fis.close();
2021-03-06 16:53:49 +00:00
} catch (Exception e) {
System.err.println("Erreur !!");
e.printStackTrace();
}
}
2021-03-06 18:02:54 +00:00
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();
2021-03-06 16:53:49 +00:00
}
2021-03-06 18:02:54 +00:00
System.out.println("Il faut " + result + " pieds de papiers cadeaux.");
2021-03-06 16:53:49 +00:00
}
2021-03-06 18:02:54 +00:00
public void deuxPartie() {
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.");
2021-03-06 16:53:49 +00:00
}
}