Commit b1887593 authored by Anya Helene Bagge's avatar Anya Helene Bagge 🦆

Laget ny stabel med ArrayList

parent b67cc7c5
package inf101.v16.datastructures;
import java.util.ArrayList;
import java.util.List;
public class YourStack implements IStack {
private List<String> data;
public YourStack() {
this.data = new ArrayList<String>();
}
@Override
public void push(String s) {
data.add(s);
}
@Override
public String pop() {
return data.remove(data.size() - 1);
}
@Override
public String top() {
return data.get(data.size() - 1);
}
@Override
public int maxSize() {
return Integer.MAX_VALUE;
}
@Override
public boolean isEmpty() {
return data.isEmpty();
}
@Override
public int size() {
return data.size();
}
@Override
public void clear() {
data.clear();
}
}
package inf101.v16.datastructures;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class YourStackTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void pushPopTest() {
IStack stack = new YourStack();
stack.push("Hei");
assertEquals("Hei", stack.pop());
}
@Test
public void pushPopTest2() {
IStack stack = new YourStack();
stack.push("Hei");
stack.push("Kaffe");
assertEquals("Kaffe", stack.pop());
assertEquals("Hei", stack.pop());
}
@Test
public void clearTest() {
IStack stack = new YourStack();
stack.push("Hei");
stack.push("Kaffe");
stack.clear();
// test
assertTrue(stack.isEmpty());
assertEquals(0, stack.size());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment