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

Refactoring #25

Open
wants to merge 3 commits 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
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# lab_bins
First example code for refactoring
-Refactored the main method into multiple smaller methods
-Changed the structure of the Bins class to better represent data belonging to the class (ie. 'private int')
-Clear method and variable names

Making a change!!!

Even more changes :)
Net Ids:

krb29
snp20
158 changes: 109 additions & 49 deletions src/Bins.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,68 @@
*/
public class Bins {
public static final String DATA_FILE = "example.txt";

private PriorityQueue<Disk> pq;
private int total;
private int numberOfDisks;
private List data;

/**
* Create an empty Bin.
*/
public Bins () {
total = 0;
numberOfDisks = 0;
pq = new PriorityQueue<Disk>();
}

/**
* Given a file, returns a list of the data.
*/
public List getDataFromFile(String filePath){
Scanner input = new Scanner(Bins.class.getClassLoader().getResourceAsStream(filePath));
return readData(input);
}

/**
* Returns the previously written Disk.
*/
public Disk lastUsedDisk(){
return pq.peek();
}

/**
* Given a disk, adds it to the Bin.
*/
public void addDisk(Disk d){
pq.add(d);
}

/**
* Updates an existing disk that has space remaining with the given size.
*/
public void updateExistingDiskInBin(Disk d, int size){
pq.poll();
d.add(size);
addDisk(d);
}

/**
* Create a new disk to hold additional data.
*/
public void addNewDiskToBin(int size){
Disk d2 = new Disk(numberOfDisks);
d2.add(size);
addDisk(d2);
numberOfDisks++;
}

/**
* Adds an empty Disk to the Bin.
*/
public void addEmptyDiskToBin(){
addNewDiskToBin(0);
}

/**
* Reads list of integer data from the given input.
*
Expand All @@ -25,68 +86,67 @@ public List<Integer> readData (Scanner input) {
}
return results;
}

/**
* The main program.
* Adds the data from the file into the Bin as Disks.
*/
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);

PriorityQueue<Disk> pq = new PriorityQueue<Disk>();
pq.add(new Disk(0));

int diskId = 1;
int total = 0;
public void addDataToBin(List<Integer> data){
//Add disks with the info from the file
for (Integer size : data) {
Disk d = pq.peek();
Disk d = lastUsedDisk();
if (d.freeSpace() > size) {
pq.poll();
d.add(size);
pq.add(d);
updateExistingDiskInBin(d, size);
} else {
Disk d2 = new Disk(diskId);
diskId++;
d2.add(size);
pq.add(d2);
addNewDiskToBin(size);
}
total += size;
updateTotal(size);
}

System.out.println("total size = " + total / 1000000.0 + "GB");
}

/**
* Updates the total size of the Bin.
*/
public void updateTotal(int size){
total += size;
}

/**
* Prints the statistics of the Bin.
*/
public void printStats(){
System.out.println("total size = " + total / 1000000.0 + "GB");
System.out.println();
System.out.println("worst-fit method");
System.out.println("number of pq used: " + pq.size());
while (!pq.isEmpty()) {
}

/**
* Prints the disks within the Bin.
*/
public void emptyAndPrintDisks(){
while (!pq.isEmpty()) {
System.out.println(pq.poll());
}
System.out.println();
}

/**
* The main program.
*/
public static void main (String args[]) {
Bins b = new Bins();
List<Integer> data = b.getDataFromFile(DATA_FILE);

Collections.sort(data, Collections.reverseOrder());
pq.add(new Disk(0));

diskId = 1;
for (Integer size : data) {
Disk d = pq.peek();
if (d.freeSpace() >= size) {
pq.poll();
d.add(size);
pq.add(d);
} else {
Disk d2 = new Disk(diskId);
diskId++;
d2.add(size);
pq.add(d2);
}
}
b.addEmptyDiskToBin(); //Adds an empty disk to the bin
b.addDataToBin(data);
b.printStats();
b.emptyAndPrintDisks();

System.out.println();
System.out.println("worst-fit decreasing method");
System.out.println("number of pq used: " + pq.size());
while (!pq.isEmpty()) {
System.out.println(pq.poll());
}
System.out.println();
Collections.sort(data, Collections.reverseOrder());

b.addEmptyDiskToBin(); //Adds an empty disk to the bin
b.addDataToBin(data);
b.printStats();
b.emptyAndPrintDisks();
}
}
6 changes: 1 addition & 5 deletions src/Disk.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ public String toString () {
@Override
public boolean equals (Object other) {
if (other != null && other instanceof Disk) {
if (myId == ((Disk) other).myId) {
return true;
} else {
return false;
}
return (myId == ((Disk) other).myId);
} else {
return false;
}
Expand Down