Skip to content

Instantly share code, notes, and snippets.

@connlark
Last active December 9, 2015 15:25
Show Gist options
  • Save connlark/5f1e38eca9e99c512186 to your computer and use it in GitHub Desktop.
Save connlark/5f1e38eca9e99c512186 to your computer and use it in GitHub Desktop.
That rack tho ;)
/**
* Created by Connor on 12/8/15.
*/
public class Rack {
private Vial[][] rack;
public Rack(){
rack = new Vial[3][8]; //instantiates the rack when object is created
}
public Vial[][] getRack(){ //for the consolidate method
return rack;
}
@Override //compile-time flag to catch little mistakes like tostring() instead of toString()
public String toString() {//my toString() is messy af
String str = "";
boolean added = false;
for (int i = 0; i < rack.length; i++) {
for (int j = 0; j < rack[0].length; j++) {
if (rack[i][j] != null){
if (added) str += "\n";//this just adds a new line after each time there is another vial to print
str += "Row "+i+" Col "+j+"\n"+ rack[i][j].toString()+"\n";
added = true;
}
}
}
if (str.length() == 0) return "";
return str.substring(0,str.length()-1);//removes the ""\n" at the end so there isnt another newline at the end of the output
}
// Add a vial to the first empty position in the rack.
// Return true if successful, false otherwise.
public boolean add(Vial vial){
for (int i = 0; i < rack.length; i++) {
for (int j = 0; j < rack[0].length; j++) {
if (rack[i][j] == null){
rack[i][j] = vial;
return true;
}
}
}
return false;
}
// Add a vial to the indicated position in the rack if it is empty.
// Return true if successful, false otherwise.
public boolean add(Vial vial, int row, int col){
if (rack[row][col] == null){
rack[row][col] = vial;
return true;
}
else return false;
}
//Calculate the number of vials in the rack
public int count(){
int sum = 0;
for (int i = 0; i < rack.length; i++) {
for (int j = 0; j < rack[0].length; j++) {
if (rack[i][j] != null)
sum++;
}
}
return sum;
}
// Get the total quantity of a particular potion in all of
// the vials the rack
public double getQuantity(String potionName){
int sum = 0;
for (int i = 0; i < rack.length; i++) {
for (int j = 0; j < rack[0].length; j++) {
if(rack[i][j] != null){
sum += rack[i][j].getQuantity(potionName);
}
}
}
return sum;
}
// Get the quantity (weight in grams) of the entire rack
public double getQuantity(){
int sum = 0;
for (int i = 0; i < rack.length; i++) {
for (int j = 0; j < rack[0].length; j++) {
if (rack[i][j] != null)
sum += rack[i][j].getQuantity();
}
}
return sum;
}
// remove and return the vial at the specified location
// return null if no vial was found
public Vial remove(int row, int col){
if (rack[row][col] != null){
Vial temp = rack[row][col];
rack[row][col] = null;
return temp;
}
else return null;
}
// shift all the vials towards spot 0,0 in the array. Do
// not allow any open spots between vials. Returns nothing.
// Try and do this without creating a new array.
public void consolidate() {
Rack foo = new Rack(); //so much easier than not creating a new array... and its just as efficient (if not more)
for (int i = 0; i < rack.length; i++) {
for (int j = 0; j < rack[0].length; j++) {
if (rack[i][j] != null) foo.add(rack[i][j]);
}
}
this.rack = foo.getRack(); //points this.rack to foo's rack
}
public void printVisual(){ //for fun
for (int i = 0; i < rack.length; i++) {
for (int j = 0; j < rack[0].length; j++) {
if (rack[i][j] == null){
System.out.print("[ ]");
}
else System.out.print("[*]");
}
System.out.println();
}
}
public static void main(String[] args) { //just to visualize the consolidate method
Rack rack1 = new Rack();
int col;
int row;
for (int i = (int) (Math.random()*20); i < 24; i++) {
col = (int) (Math.random()*8);
row = (int) (Math.random()*3);
rack1.add(new Vial(),row,col);
}//creates a completely random rack every time ran
rack1.printVisual();
System.out.println("consolidated:");
rack1.consolidate();
rack1.printVisual();
}
}
import java.util.ArrayList;
class Vial {
private ArrayList<Potion> vial;
public final double EPSILON =1E-14;
public Vial(){
vial = new ArrayList<Potion>(); //instantiates the vial ArrayList when object is created
}
@Override //compile-time flag to catch little mistakes like tostring() instead of toString()
public String toString() { //another messy af toString()
String output = "";
if (vial.size() == 0 || vial == null) return "";
for (int i = 0; i < vial.size() - 1; i++) {//adds everything with a newline except the last
output += vial.get(i).toString() + "\n";
}
output += vial.get(vial.size()-1).toString(); //so that there isn't a newline at the end of the output
return output;
}
public ArrayList<Potion> getVial(){
return vial;
}
public double getQuantity() {
double sum = 0;
for (int i = 0; i < vial.size(); i++) {
sum += vial.get(i).getQuantity();
}
return sum;
}
public double getQuantity(String name) {
double sum = 0;
for (int i = 0; i < vial.size(); i++) {
if (vial.get(i).getDescription().equals(name)){
sum += vial.get(i).getQuantity();
}
}
return sum;
}
public boolean addPotion(Potion foo) {
if (this.getQuantity() + foo.getQuantity() - 100 < EPSILON) {
for (int i = 0; i < vial.size(); i++) {
if (vial.get(i).getDescription().equals(foo.getDescription())){
vial.get(i).add(foo.getQuantity());
return true;
}
}
vial.add(foo);
return true;
} else return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment