Pr_robot_factory/domain/Factory.java

52 lines
1.2 KiB
Java
Raw Normal View History

package domain;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
public class Factory implements Serializable {
private HashMap<Integer, Robot> robots = new HashMap<>();
private int c3poID = 0;
2023-01-06 22:55:33 +01:00
private int r2d2ID = 10000;
public Factory(){
}
//Has to return Collection<Robot>
public Collection<Robot> robotListToCollection(){
return robots.values();
}
//return a String arr with robot attributes
public String[] getRobotList() {
Collection<Robot> collect = robotListToCollection();
String[] list = new String[collect.size()];
int i = 0;
for(Robot r: collect){
list[i++] = r.toString();
}
return list;
}
//creates a new robot
public boolean buildNewRobot(String name, int type){
Robot r ;
if(type == 0) {
r = new R2D2(r2d2ID++, name);
} else if(type == 1) {
r = new C3PO(c3poID++, name);
} else {
return false;
}
robots.put(r.getId(), r);
return true;
}
//returns a specific robot via id
public Robot getRobotOfList(int id){
2023-01-06 22:55:33 +01:00
return robots.get(id);
}
}