package wf.doyle.blockbuster.item;
import java.lang.reflect.Field;
import java.text.DecimalFormat;
import wf.doyle.blockbuster.App;
import wf.doyle.blockbuster.file.FileReader;
import wf.doyle.blockbuster.util.EnumLineType;
public abstract class LibraryItem {
private boolean onLoan = false;
private int timesBorrowed = 0;
private String itemCode;
private int cost;
private String title;
public String getItemCode()
{
return this.itemCode;
}
public abstract EnumLineType getType();
public String getName()
{
return this.title;
}
public String getCost()
{
return new DecimalFormat("'£'0.00").format((float)this.cost / 100);
}
public boolean getLoan()
{
return this.onLoan;
}
public int getTimesBorrowed()
{
return this.timesBorrowed;
}
public void takeItem()
{
if(this.getLoan()) return;
this.onLoan = true;
this.timesBorrowed++;
}
public void returnItem()
{
if(!this.getLoan()) return;
this.onLoan = false;
}
@Override
public String toString()
{
String[] contains = FileReader.containing.get(this.getClass().getName());
String string = "";
for(String key : contains)
{
Class<? extends LibraryItem> child = FileReader.classes.get(this.getType());
Field f = FileReader.getField(key.trim(), child);
f.setAccessible(true);
try
{
string = string + f.get(this) + ", ";
}
catch(IllegalArgumentException | IllegalAccessException e)
{
App.LOGGER.error("Could not get value of reflection field", e);
}
}
return string.substring(0, string.lastIndexOf(","));
}
public boolean toggleItem()
{
if(this.getLoan()) {
this.returnItem();
return false;
} else {
this.takeItem();
return true;
}
}
}