Last active
January 25, 2019 06:56
-
-
Save anoited007/37731d38d2960f9fd42b4c2edab48f6a to your computer and use it in GitHub Desktop.
A bagable interface to provide functionality for implementing a bag of M and M.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
public class Bag<MAndM> implements Bagable<MAndM> { | |
private boolean empty; | |
private boolean full; | |
private boolean isOpen; | |
private boolean isClosed; | |
private MAndM[] items; | |
private int counter; | |
public Bag(int capacity) { | |
items = (MAndM[]) new Object[capacity]; | |
empty = true; | |
full = false; | |
isClosed = false; | |
counter = 0; | |
} | |
public Bag(){ | |
items = (MAndM[]) new Object[50]; | |
empty = true; | |
isClosed = false; | |
full = false; | |
counter = 0; | |
} | |
public void setEmpty(boolean empty) { | |
this.empty = empty; | |
} | |
public void setFull(boolean full) { | |
this.full = full; | |
} | |
public boolean isOpen() { | |
return isOpen; | |
} | |
public void setIsOpen(boolean isOpen) { | |
this.isOpen = isOpen; | |
} | |
public MAndM[] getItems() { | |
return items; | |
} | |
public void setItems(MAndM[] items) { | |
this.items = items; | |
} | |
public int getCounter() { | |
return counter; | |
} | |
public void setCounter(int counter) { | |
this.counter = counter; | |
} | |
public boolean getIsClosed() { | |
return isClosed; | |
} | |
public void setIsClosed(boolean closed) { | |
isClosed = closed; | |
} | |
public void open(){ | |
if(getIsClosed()){ | |
setIsOpen(true); | |
setIsClosed(false); | |
System.out.println("The bag is opened"); | |
} | |
else | |
System.out.println("The bag is already opened"); | |
} | |
public void add(MAndM item){ | |
if(counter < items.length -1){ | |
items[counter] = item; | |
counter++; | |
} | |
} | |
public MAndM remove(MAndM item){ | |
MAndM removed = null; | |
if(isEmpty()){ | |
System.out.println("The bag is empty"); | |
} | |
else{ | |
Random random = new Random(); | |
int number = random.nextInt(items.length); | |
removed = items[number]; | |
items[number] = null; //Problem is how to fill it after removing from the array. Not catered for. | |
} | |
return removed; | |
} | |
public MAndM[] empty(){ | |
MAndM[] temp = items; // Setting array to return after clearing array. | |
if(isEmpty()) { | |
System.out.println("The bag is already empty"); | |
} | |
else{ | |
items = null; | |
} | |
return temp; | |
} | |
public boolean isEmpty(){ | |
return empty; | |
} | |
public boolean isFull(){ | |
return full; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Bagable<T> { | |
//Open the bag | |
void open(); | |
//Add item to the bag | |
void add(T t); | |
//Remove item from the bag | |
T remove(T t); | |
//Empty the bag | |
T[] empty(); | |
//Check if bag is empty | |
boolean isEmpty(); | |
//Check if bag is full (since we will use Arrays to store data) | |
boolean isFull(); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MAndM { | |
private String color; | |
public MAndM(String color){ | |
this.color = color; | |
} | |
public MAndM(){ | |
this.color = "red"; | |
} | |
public String getColor() { | |
return color; | |
} | |
public void setColor(String color) { | |
this.color = color; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Feedback
Great start. Here's some feedback to help improve the code for the next iteration.
The opposite of empty is full and the opposite of closed it open. This means we can use two boolean variables to represent the states of the bag. Here's an example to help:
We can actually improve this further and remove the need for a variable to maintain the state of the bag being full or not:
Closed (or open) would still require a single boolean value for representation.
Response to comment at line 97: You need to find a way to add items into the first array element which is null.