Ti posto tutto il codice, proprio non riesco a capire il perchè non inserisce.
codice:
public class Ludoteca {
private Map<String, Item> item = null;
private Map<String, Ambient> ambientService = null;
private String name = null;
public Ludoteca(String name) {
if (name == null || name.equals("")) {
throw new IllegalArgumentException("Name ludoteca invalid");
}
this.name = name;
this.item = Collections.synchronizedMap(new HashMap<String, Item>());
this.ambientService = Collections.synchronizedMap(new HashMap<String, Ambient>());
}
public String getName(){
return this.name;
}
public Item addItem(Item item){
if (item!=null){
return this.item.put(item.getId(), item);
}
else return null;
}
public Item removeItem(Item item){
if(item!=null){
if(item.isAvailable() && item.isLent()){
return this.item.remove(item.getId());
}
else return null;
}
else return null;
}
}
codice:
public class TestLudoteca {
public static void main(String[] args){
Ludoteca l=new Ludoteca("Synclab");
System.out.println("Ludoteca:"+l.getName());
Item it=new Item("Gioco1","Toy_1");
Item i=l.addItem(it);
if(i.equals(it)) System.out.println("Inserimento avvenuto");
else System.out.println("Inserimento non avvenuto");
i=l.removeItem(it);
if(i.equals(it)) System.out.println("Cancellazione avvenuta");
else System.out.println("Cancellazione non avvenuta");
}
}
codice:
public class Item {
private String id = null;
private int ageMin = 0;
private int ageMax = 0;
private boolean lent = true;
private String activity = null;
private boolean available = true;
private String name = null;
/**
* @param name
* @param id
*/
public Item(String name, String id) {
if (name == null || name.equals("")) {
throw new IllegalArgumentException("Name invalid");
}
this.name = name;
if (id == null || id.equals("")) {
throw new IllegalArgumentException("Lastname invalid");
}
this.id = id;
}
/**
* @param name
* @param id
* @param agemin
* @param agemax
*/
public Item(String name, String id, int agemin, int agemax) {
this(name, id);
if (agemin < 0) {
throw new IllegalArgumentException("Minimum age is not valid");
}
this.ageMin = agemin;
if (agemax < 0 || agemax < this.ageMin) {
throw new IllegalArgumentException("Maximum age invalid");
}
this.ageMax = agemax;
}
/**
* @return
*/
public String getId() {
return this.id;
}
/**
* @return
*/
public int getAgeMin() {
return this.ageMin;
}
/**
* @return
*/
public int getAgeMax() {
return this.ageMax;
}
/**
* @param age
* @return
*/
public boolean recommended(int age) {
return this.ageMin <= age && age <= this.ageMax;
}
/**
* @param date
* @return
*/
public boolean reccommended(Date date) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
long year = ((new GregorianCalendar().getTimeInMillis() - gc.getTimeInMillis()) / 1000);
year = year / 86400;
year = year / 365;
return this.ageMin <= year && year <= this.ageMax;
}
/**
* @return
*/
public String getActivity() {
return this.activity;
}
/**
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* @return
*/
public String getName() {
return this.name;
}
/**
* @param id
*/
public void setId(String id) {
this.id = id;
}
/**
* @param agemin
*/
public void setAgeMin(int agemin) {
this.ageMin = agemin;
}
/**
* @param agemax
*/
public void setAgeMax(int agemax) {
this.ageMax = agemax;
}
/**
* @param activity
*/
public void setActivity(String activity) {
this.activity = activity;
}
/**
* @param lent
*/
public void setLent(boolean lent) {
this.lent = lent;
}
/**
* @param lent
*/
public void setAvailable(boolean available) {
this.available = available;
}
/**
* @return
*/
public boolean isLent() {
return this.lent;
}
/**
* @return
*/
public boolean isAvailable() {
return this.available;
}
}
codice:
public class Ambient implements AmbientService {
private String name = null;
private String id = null;
private Map<String, Spaces> spaces = null;
public Ambient(String name, String id) {
if (name == null || name.equals("")) {
throw new IllegalArgumentException("Name invalid");
}
this.name = name;
if (id == null || id.equals("")) {
throw new IllegalArgumentException("Id invalid");
}
this.id = id;
this.spaces = Collections.synchronizedMap(new HashMap<String, Spaces>());
}
public void available(Spaces space) {
if(space!=null) {
Spaces sp=this.spaces.get(space.getId());
sp.setAvailable(true);
this.spaces.put(sp.getId(), space);
}
}
public String getId() {
return this.id;
}
public void setId(String id) {
if (id != null && !id.equals("")) {
this.id = id;
}
}
public String getName() {
return this.name;
}
public void setName(String name) {
if (name != null && !name.equals("")) {
this.name = name;
}
}
public Map getSpaces(){
return this.spaces;
}
/**
* @param spaces
* @return
*/
public Spaces addSpace(Spaces spaces) {
return this.spaces.put(spaces.getId(), spaces);
}
/**
* @param spaces
*/
public void removeSpace(Spaces spaces) {
this.spaces.remove(spaces.getId());
}
public Spaces isFree() {
//Verifico che almeno uno spazio nell'ambiente sia libero
Iterator<String> i = this.spaces.keySet().iterator();
Spaces s = null;
while (i.hasNext()) {
s = this.spaces.get(i.next());
if (s != null && s.isAvailable()) {
return s;
}
}
return null;
}
public boolean isAvailable(){
Iterator<String> i = this.spaces.keySet().iterator();
Spaces s = null;
while (i.hasNext()) {
s = this.spaces.get(i.next());
if (!s.isAvailable()) {
return false;
}
}
return true;
}
public Spaces take(String a) {
Iterator<String> i = this.spaces.keySet().iterator();
Spaces sp = null;
while (i.hasNext()) {
sp = this.spaces.get(i.next());
if (sp != null) {
if (sp.isAvailable() && sp.getActivity().equals(a)) {
sp.setAvailable(false);
this.spaces.remove(sp.getId());
this.spaces.put(sp.getId(), sp);
return sp;
}
}
}
return null;
}
public void issue(Spaces spaces) {
spaces.setAvailable(true);
this.spaces.put(spaces.getId(), spaces);
}
public Set getActivity() {
HashSet<String> activity = new HashSet<String>();
//scorro la mappa degli spazi per reperire le attività possibili
Iterator<String> i = this.spaces.keySet().iterator();
Spaces s = null;
while (i.hasNext()) {
s = this.spaces.get(i.next());
if (s != null) {
activity.add(s.getActivity());
}
}
return activity;
}
}