Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 54 additions & 48 deletions src/Bins.java
Original file line number Diff line number Diff line change
@@ -1,57 +1,63 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;


/**
* Runs a number of algorithms that try to fit files onto disks.
*/
public class Bins {
public static final String DATA_FILE = "example.txt";
// all possible algorithms to compare --- add new instances here!
private static WorstFitAlgorithm algortihmsToCompare[] = {
new WorstFitAlgorithm(),
new WorstFitDecreasingAlgorithm()
};


/**
* Reads list of integer data from the given input.
*
* @param input tied to an input source that contains space separated numbers
* @return list of the numbers in the order they were read
*/
public List<Integer> readData (Scanner input) {
List<Integer> results = new ArrayList<Integer>();
while (input.hasNext()) {
results.add(input.nextInt());
}
return results;
}

/**
* Returns sum of all values in given list.
*/
public int getTotal (List<Integer> data) {
int total = 0;
for (int d : data) {
total += d;
}
return total;
}


/**
* The main program.
*/
public static void main (String args[]) {
Bins b = new Bins();
Scanner input = new Scanner(Bins.class.getClassLoader().getResourceAsStream(DATA_FILE));
List<Integer> data = b.readData(input);
System.out.println("total size = " + b.getTotal(data) / 1000000.0 + "GB");

for (WorstFitAlgorithm al : algortihmsToCompare) {
al.fitDisksAndPrint(data);
}
}
public static final String DATA_FILE = "example.txt";
// all possible algorithms to compare --- add new instances here!
/*
* private static WorstFitAlgorithm algortihmsToCompare[] = { new
* WorstFitAlgorithm(), new WorstFitDecreasingAlgorithm() };
*/

/**
* Reads list of integer data from the given input.
*
* @param input
* tied to an input source that contains space separated numbers
* @return list of the numbers in the order they were read
*/
public List<Integer> readData(Scanner input) {
List<Integer> results = new ArrayList<Integer>();
while (input.hasNext()) {
results.add(input.nextInt());
}
return results;
}

/**
* Returns sum of all values in given list.
*/
public int getTotal(List<Integer> data) {
int total = 0;
for (int d : data) {
total += d;
}
return total;
}

/**
* The main program.
*/
public static void main(String args[]) {
Bins b = new Bins();
Scanner input = new Scanner(Bins.class.getClassLoader().getResourceAsStream(DATA_FILE));
List<Integer> data = b.readData(input);
System.out.println("total size = " + b.getTotal(data) / 1000000.0 + "GB");

WorstFitAlgorithm al = new WorstFitAlgorithm();
// worst fit
al.fitDisksAndPrint(data, dataList -> dataList);
// decreasing

al.fitDisksAndPrint(data, dataList -> {
Collections.sort(dataList, Collections.reverseOrder());
return dataList;
});

}
}
17 changes: 17 additions & 0 deletions src/StaticAbstractMultiInheritedGlobalInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import java.util.List;

/**
*
*/

/**
*
* @author Sally Al
*
*/

@FunctionalInterface
public interface StaticAbstractMultiInheritedGlobalInterface {
List<Integer> operate(List<Integer> dataList);

}
8 changes: 5 additions & 3 deletions src/WorstFitAlgorithm.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ public WorstFitAlgorithm (String description) {
*
* @param data collection of files to be allocated to disks
*/
public void fitDisksAndPrint (List<Integer> data) {
List<Integer> copy = new ArrayList<>(data);
organizeData(copy);
public void fitDisksAndPrint (List<Integer> data, StaticAbstractMultiInheritedGlobalInterface i) {
List<Integer> copy = new ArrayList<>(i.operate(data));
// organizeData(copy);
Collection<Disk> disks = addFiles(copy);
printResults(disks, myDescription);


}

/**
Expand Down