Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored by: rd122 ym67 #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
24 changes: 16 additions & 8 deletions src/Bins.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

Expand All @@ -9,10 +10,8 @@
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()
};

private static WorstFitAlgorithm myAlgorithm;


/**
Expand Down Expand Up @@ -49,9 +48,18 @@ public static void main (String args[]) {
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);
}

myAlgorithm = new WorstFitAlgorithm();

//do nothing: Worst Fit Algorithm
myAlgorithm.fitDisksAndPrint(data, "Worst Fit Algorithm",(list)->{

});

//sort in reverse order: Worst Fit Algorithm Decreasing
myAlgorithm.fitDisksAndPrint(data, "Worst Fit Algorithm Decreasing",(list)->{
Collections.sort(list, Collections.reverseOrder());
});

}
}
16 changes: 4 additions & 12 deletions src/WorstFitAlgorithm.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import java.util.Collection;
import java.util.List;
import java.util.PriorityQueue;
import java.util.function.Consumer;

/**
* This class represents worst-fit algorithms for allocating files to disks.
Expand All @@ -10,32 +11,23 @@
*/
public class WorstFitAlgorithm {
public static final String WORST_FIT_METHOD = "worst-fit";
private String myDescription;

/**
* Default constructor
*/
public WorstFitAlgorithm () {
this(WORST_FIT_METHOD);
}

/**
* Create with given descriptor.
*/
public WorstFitAlgorithm (String description) {
myDescription = description;
}

/**
* Allocates given files to the fewest number of disks.
*
* @param data collection of files to be allocated to disks
*/
public void fitDisksAndPrint (List<Integer> data) {
public void fitDisksAndPrint (List<Integer> data, String name, Consumer<List<?>> processor) {
List<Integer> copy = new ArrayList<>(data);
organizeData(copy);
processor.accept(copy);
Collection<Disk> disks = addFiles(copy);
printResults(disks, myDescription);
printResults(disks, name);
}

/**
Expand Down
23 changes: 0 additions & 23 deletions src/WorstFitDecreasingAlgorithm.java

This file was deleted.