Skip to content

Instantly share code, notes, and snippets.

@marinhoarthur
Last active August 29, 2015 14:02
Show Gist options
  • Save marinhoarthur/0448a63867654f830f8d to your computer and use it in GitHub Desktop.
Save marinhoarthur/0448a63867654f830f8d to your computer and use it in GitHub Desktop.
Simulation of airport/supermarket carts organization system behaviour.
public class Chain{
private List<Cart> carts = new ArrayList<Cart>();
private void attach(Cart c)
{
if(getSize() >= 2) // If chain has no more than 2 carts , then leave both carts edge field set to true
{
getLastCart().setEdge(false);
}
carts.add(c);
getLastCart().setEdge(true);
}
public void attachEnd(int n)
{
for(int i = 0 ; i < n; i++)
{
if(getSize() >=2)
{
getLastCart().setEdge(false);
}
carts.add(new Cart());
getLastCart().setEdge(true);
}
}
public void attachStart(int n)
{
for(int i = 0 ; i < n; i++)
{
if(getSize() >= 2)
{
getFirstCart().setEdge(false);
}
carts.add(0 ,new Cart());
getFirstCart().setEdge(true);
}
}
private Cart getFirstCart()
{
if(carts.isEmpty())
{
throw new EmptyChainException();
}
else
{
return carts.get(0);
}
}
private Cart getLastCart()
{
if(carts.isEmpty())
{
throw new EmptyChainException();
}
else
{
return carts.get(carts.size()-1);
}
}
public int getSize()
{
return carts.size();
}
public void removeFirst()
{
if(carts.isEmpty())
{
throw new EmptyChainException();
}
else
{
getFirstCart().setEdge(false);
carts.remove(0);
getFirstCart().setEdge(true);
}
}
public void removeLast()
{
if(carts.isEmpty())
{
throw new EmptyChainException();
}
else
{
getLastCart().setEdge(false);
carts.remove(carts.size()-1);
getLastCart().setEdge(true);
}
}
public void removeEdges()
{
if(carts.isEmpty())
{
throw new EmptyChainException();
}
else if(carts.size() == 1)
{
throw new MonoCartChainException();
}
else
{
getFirstCart().setEdge(false);
getLastCart().setEdge(false);
carts.remove(0);
carts.remove(carts.size()-1);
getFirstCart().setEdge(true);
getLastCart().setEdge(true);
}
}
public List<Chain> slice(int slices)
{
if(carts.isEmpty())
{
throw new EmptyChainException();
}
if(slices == 1 || slices == 0)
{
List<Chain> lc = new ArrayList<Chain>();
lc.add(this);
return lc;
}
int begin = 0;
int step = carts.size()/slices;
int diff = carts.size() - (step*slices); //The remainder of the division. If diff > 0 then the FIRST Chain will contain the remainders carts
int end = step+diff;
List<Chain> lc = new ArrayList<Chain>();
for(int i = 0 ; i < slices; i++)
{
lc.add(new Chain());
}
for(int i = 0 ; i < slices; i++)
{
for(; begin < end; begin++)
{
lc.get(i).attach(carts.get(begin));
}
end+=step;
}
return lc;
}
private class Cart
{
private boolean edge;
public boolean isEdge()
{
return edge;
}
public void setEdge(boolean edge)
{
this.edge = edge;
}
}
}
class EmptyChainException extends RuntimeException{
private static final long serialVersionUID = 1L;
private static final String msg = "Chain is empty.";
EmptyChainException()
{
super(msg);
}
}
class MonoCartChainException extends RuntimeException{
private static final long serialVersionUID = 2L;
private static final String msg = "Chain contains only one Cart.";
public MonoCartChainException()
{
super(msg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment