Trouble incrementing a variable!!
Hey all,
I've created an objectList class to maintain tenant records.
In theory, at least in my head, my class should work but as
you know theres a big difference between theory and what
actually works. I'm having trouble incrementing the "total"
variable in my class. It increments when i tell it to i.e.
total++, however it reverts back to 0 when i access again.
/**
*
* @author Mike
*/
publicclass ObjectList
{
private Object[] object;
privateint total;
/** Creates a new instance of ObjectList */
public ObjectList(int arraySize)
{
object =new Object[arraySize];
total = 0;
}
publicboolean add(Object newObject)
{
if(!isFull())
{
object[total] = newObject;
Tenant tempTenant = (Tenant)object[total];
System.out.println(tempTenant.getName() +" " + tempTenant.getRoom());
System.out.println("Before Increase: Total = "+ total +" Total = "+ getTotal());
total++;
System.out.println("After Increase: Total = "+ total +" Total = "+ getTotal());
returntrue;
}
else
{
returnfalse;
}
}
publicboolean remove(int position)
{
if(!isEmpty())
{
if(position >= 1 && position <= getTotal())
{
for(int i = position - 1; i < (getTotal() - 2); i++)
{
object[i] = object[i - 1];
}
total--;
returntrue;
}
else
{
returnfalse;
}
}
else
{
returnfalse;
}
}
public Object getObject(int position)
{
if(!isEmpty())
{
if(position >= 1 && position <= getTotal())
{
return object[position - 1];
}
else
{
returnnull;
}
}
else
{
returnnull;
}
}
publicboolean isFull()
{
if(total == object.length)
{
returntrue;
}
else
{
returnfalse;
}
}
publicboolean isEmpty()
{
System.out.println("isEmpty: Total = "+ total +" Total = "+ getTotal());
if(total == 0)
{
returntrue;
}
else
{
returnfalse;
}
}
publicint getTotal()
{
return total;
}
}
Basically what im trying to do is add in an object (all the
system.out's are there so i can see what the value of total
is once i increment it) but when i try to access the object
using getObject() the isEmpty() returns that total = 0. I think
my problem is that im declaring total the wrong way! I can
be clearer if it helps!!
Mike
Yah i know i would prefer to use the ArrayList its for a test so i have to use it.
Its used when another class TenantList and PaymentList extends it. But so
far i havent gotten past the tenantList. I can post all my code if it makes it easier, here's TenantList and PaymentList:
public class TenantList extends ObjectList
{
/** Creates a new instance of TenantList */
public TenantList(int arraySize)
{
super(arraySize);
}
public Tenant getTenant(int position)
{
return (Tenant)getObject(position);
}
public Tenant search(int position)
{
return (Tenant)getObject(position);
}
public class PaymentList extends ObjectList
{
private Payment payment;
/** Creates a new instance of PaymentsList */
public PaymentList(int arraySize)
{
super(arraySize);
}
public boolean addPayment(Payment newPayment)
{
return add(new Payment(newPayment));
}
public Payment getPayment(int position)
{
return (payment = (Payment)getObject(position));
}
Sure thing
/**
*
* @author Mike
*/
public class Main
{
static TenantList tenantList;
private final static int arraySize = 10;
public static void main(String[] args)
{
menu();
}
public static void menu()
{
tenantList = new TenantList(arraySize);
System.out.println("1 : Add");
System.out.println("2 : Print");
System.out.println("0 : Exit");
System.out.print("What would you like to do : ");
int choice = EasyIn.getInt();
switch(choice)
{
case 1 : addTenant();
break;
case 2 : printTenants();
break;
case 0 : System.exit(0);
default : System.out.println("You did not make a correct choice!");
break;
}
menu();
}
public static void addTenant()
{
System.out.print("Please Enter Tenants Name: ");
String name = EasyIn.getString();
System.out.print("Please Enter Tenants Room: ");
int room = EasyIn.getInt();
boolean returnValue = tenantList.add(new Tenant(name, room));
System.out.println(returnValue);
}
public static void deleteTenant()
{
System.out.print("Please Enter Room Number: ");
/*
int returnValue = tenantList.getTenant(EasyIn.getInt());
if(returnValue == -1)
{
System.out.println("Tenant Was Not Found");
}
else
{
tenantList.remove(returnValue);
}
*/
}
public static void printTenants()
{
for(int i = 1; i < arraySize; i ++)
{
Tenant tempTenant = tenantList.getTenant(i);
if(tempTenant == null)
{
continue;
}
System.out.println("Name: " + tempTenant.getName() + " Room: " + tempTenant.getRoom());
}
}
}
public class TenantList extends ObjectList
{
/** Creates a new instance of TenantList */
public TenantList(int arraySize)
{
super(arraySize);
}
public Tenant getTenant(int position)
{
return (Tenant)getObject(position);
}
public Tenant search(int position)
{
return (Tenant)getObject(position);
}
public class PaymentList extends ObjectList
{
private Payment payment;
/** Creates a new instance of PaymentsList */
public PaymentList(int arraySize)
{
super(arraySize);
}
public boolean addPayment(Payment newPayment)
{
return add(new Payment(newPayment));
}
public Payment getPayment(int position)
{
return (payment = (Payment)getObject(position));
}
public class Tenant
{
private String name;
private int room;
PaymentList paymentList;
private final int maxNoOfPayments = 12;
/** Creates a new instance of Tenant */
public Tenant(String name, int room)
{
this.name = name;
this.room = room;
paymentList = new PaymentList(maxNoOfPayments);
}
public String getName()
{
return name;
}
public int getRoom()
{
return room;
}
public boolean makePayment(Payment payment)
{
return paymentList.addPayment(payment);
}
public void getPayment()
{
for(int i = 1; i <= maxNoOfPayments; i++)
{
Payment tempPayment = paymentList.getPayment(i);
if(tempPayment != null)
{
System.out.println("Month: " + tempPayment.getMonth() + "Payment: " + tempPayment.getPayment() );
}
}
}
}
public class Payment
{
private String month;
private double payment;
/** Creates a new instance of Payments */
public Payment(Payment newPayment)
{
this.month = newPayment.month;
this.payment = newPayment.payment;
}
public double getPayment()
{
return payment;
}
public String getMonth()
{
return month;
}
}
public class ObjectList
{
private Object[] object;
private int total;
/** Creates a new instance of ObjectList */
public ObjectList(int arraySize)
{
object = new Object[arraySize];
total = 0;
}
public boolean add(Object newObject)
{
if(!isFull())
{
object[total] = newObject;
Tenant tempTenant = (Tenant)object[total];
System.out.println(tempTenant.getName() + " " + tempTenant.getRoom());
System.out.println("Before Increase: Total = "+ total + " Total = "+ getTotal());
total++;
System.out.println("After Increase: Total = "+ total + " Total = "+ getTotal());
return true;
}
else
{
return false;
}
}
public boolean remove(int position)
{
if(!isEmpty())
{
if(position >= 1 && position <= getTotal())
{
for(int i = position - 1; i < (getTotal() - 2); i++)
{
object[i] = object[i - 1];
}
total--;
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public Object getObject(int position)
{
if(!isEmpty())
{
if(position >= 1 && position <= getTotal())
{
return object[position - 1];
}
else
{
return null;
}
}
else
{
return null;
}
}
public boolean isFull()
{
if(total == object.length)
{
return true;
}
else
{
return false;
}
}
public boolean isEmpty()
{
System.out.println("isEmpty: Total = "+ total + " Total = "+ getTotal());
if(total == 0)
{
return true;
}
else
{
return false;
}
}
public int getTotal()
{
return total;
}
}
Thats everything so if im missing something that could be the problem!!!
Mike