Question: Please help complete below java code. I have already have Tuples, Schema classes and other interfaces. package heapdb; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public

Please help complete below java code. I have already have Tuples, Schema classes and other interfaces. package heapdb;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Table implements ITable {
private List tuples;
private Schema schema;
public Table(Schema schema){
this.schema = schema;
tuples = new ArrayList<>();
}
@Override
public Schema getSchema(){
return schema;
}
@Override
public int size(){
return tuples.size();
}
@Override
public void close(){
// do nothing
}
@Override
public boolean insert(Tuple rec){
if (! rec.getSchema().equals(schema)){
throw new IllegalStateException("Error: tuple schema does not match table schema.");
}
// if schema has no key, then just add the tuple.
// if schema has key, see if key already exists in table
// TODO
throw new UnsupportedOperationException();
}
@Override
public boolean delete(Object key){
if (schema.getKey()== null){
throw new IllegalStateException("Error: table does not have a primary key. Can not delete.");
}
// TODO
throw new UnsupportedOperationException();
}
@Override
public Tuple lookup(Object key){
if (schema.getKey()== null){
throw new IllegalStateException("Error: table does not have a primary key. Can not lookup by key.");
}
// TODO
throw new UnsupportedOperationException();
}
@Override
public ITable lookup(String colname, Object value){
if (schema.getColumnIndex(colname)<0){
throw new IllegalStateException("Error: table does not contain column "+colname);
}
Table result = new Table(this.getSchema());
// find all tuples that satisfy the predicate colname=value
// and insert the tuples to result table.
// return the result
// TODO
throw new UnsupportedOperationException();
}
@Override
public Iterator iterator(){
return tuples.iterator();
}
public String toString(){
if (tuples.isEmpty()){
return "Empty Table";
} else {
StringBuilder sb = new StringBuilder();
for (Tuple t : this){
sb.append(t.toString());
sb.append("
");
}
return sb.toString();
}
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!