Fixed path issue using nio Path
This commit is contained in:
parent
06855b96c5
commit
5a25feafef
@ -56,7 +56,7 @@ public class DoubleDispatch {
|
||||
ArrayList<Trash> bin = new ArrayList<>();
|
||||
TrashBinSet bins = new TrashBinSet();
|
||||
// ParseTrash still works, without changes:
|
||||
ParseTrash.fillBin("DDTrash.dat", bin);
|
||||
ParseTrash.fillBin("doubledispatch", "DDTrash.dat", bin);
|
||||
// Sort from the master bin into the
|
||||
// individually-typed bins:
|
||||
bins.sortIntoBins(bin);
|
||||
|
@ -44,7 +44,7 @@ public class DynaTrash {
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void main(String[] args) {
|
||||
TypeMap<Trash> bin = new TypeMap<>();
|
||||
ParseTrash.fillBin("Trash.dat",
|
||||
ParseTrash.fillBin("recycleap", "Trash.dat",
|
||||
new TypeMapAdapter(bin));
|
||||
Iterator<Class> keys = bin.keys();
|
||||
while(keys.hasNext())
|
||||
|
@ -9,7 +9,7 @@ public class RecycleAP {
|
||||
public static void main(String[] args) {
|
||||
ArrayList<Trash> bin = new ArrayList<>();
|
||||
// Fill up the Trash bin:
|
||||
ParseTrash.fillBin("Trash.dat", bin);
|
||||
ParseTrash.fillBin("recycleap", "Trash.dat", bin);
|
||||
List<Glass> glassBin = new ArrayList<>();
|
||||
List<Paper> paperBin = new ArrayList<>();
|
||||
List<Aluminum> alBin = new ArrayList<>();
|
||||
|
@ -41,7 +41,7 @@ public class RecycleB {
|
||||
static Tbin<Trash> bin = new Tbin<>(Trash.class);
|
||||
public static void main(String[] args) {
|
||||
// Fill up the Trash bin:
|
||||
ParseTrash.fillBin("Trash.dat", bin);
|
||||
ParseTrash.fillBin("recycleap", "Trash.dat", bin);
|
||||
|
||||
TbinList<Trash> trashBins = new TbinList<>();
|
||||
trashBins.add(new Tbin<>(Aluminum.class));
|
||||
|
@ -5,22 +5,25 @@
|
||||
package patterns.trash;
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
import java.nio.file.*;
|
||||
|
||||
public class ParseTrash {
|
||||
public static <T extends Trash> void
|
||||
fillBin(String filename, Fillable<T> bin) {
|
||||
fillBin(String dir, String fname, Fillable<T> bin) {
|
||||
try {
|
||||
try(BufferedReader data = new BufferedReader(
|
||||
new FileReader(filename))) {
|
||||
Path p = FileSystems.getDefault()
|
||||
.getPath("..", dir, fname);
|
||||
try(BufferedReader data = Files.newBufferedReader(p)){
|
||||
String buf;
|
||||
while((buf = data.readLine())!= null) {
|
||||
if(buf.trim().length() == 0)
|
||||
continue; // Skip empty lines
|
||||
if(buf.startsWith("//"))
|
||||
continue; // Skip comments
|
||||
String type = buf.substring(0,
|
||||
buf.indexOf(':')).trim();
|
||||
buf.indexOf(':')).trim();
|
||||
double weight = Double.valueOf(
|
||||
buf.substring(buf.indexOf(':') + 1)
|
||||
.trim());
|
||||
buf.substring(buf.indexOf(':') + 1).trim());
|
||||
bin.addTrash(Trash.factory(
|
||||
new Trash.Info(type, weight)));
|
||||
}
|
||||
@ -34,7 +37,7 @@ public class ParseTrash {
|
||||
}
|
||||
// Special case to handle ArrayList:
|
||||
public static <T extends Trash> void
|
||||
fillBin(String filename, ArrayList<T> bin) {
|
||||
fillBin(filename, new FillableList<>(bin));
|
||||
fillBin(String dir, String fname, ArrayList<T> bin) {
|
||||
fillBin(dir, fname, new FillableList<>(bin));
|
||||
}
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ public class TrashVisitor {
|
||||
public static void main(String[] args) {
|
||||
ArrayList<Trash> bin = new ArrayList<>();
|
||||
// ParseTrash still works, without changes:
|
||||
ParseTrash.fillBin("VTrash.dat", bin);
|
||||
ParseTrash.fillBin("trashvisitor", "VTrash.dat", bin);
|
||||
// You could even iterate through
|
||||
// a list of visitors!
|
||||
PriceVisitor pv = new PriceVisitor();
|
||||
|
Loading…
x
Reference in New Issue
Block a user