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

[6238 byte] By [michaeln31a] at [2007-11-27 4:47:35]
# 1
I think we need to see your code where you use this class. Are you calling the constructor multiple times? Also, just curious why you are creating this class rather than the type-safe ArrayList<>?
petes1234a at 2007-7-12 10:00:21 > top of Java-index,Java Essentials,New To Java...
# 2

> 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

> e clearer if it helps!!

I don't see why you would get total as zero. How do you test the class? How are you using it? Are you sure that you don't have to ObjectLists?

Kaj

kajbja at 2007-7-12 10:00:21 > top of Java-index,Java Essentials,New To Java...
# 3

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));

}

michaeln31a at 2007-7-12 10:00:21 > top of Java-index,Java Essentials,New To Java...
# 4
The subclasses look good. Again, I think your problem is likely in how you use the class / subclasses. I need to see where you call the subclass constructors. Are you calling them multiple times?
petes1234a at 2007-7-12 10:00:21 > top of Java-index,Java Essentials,New To Java...
# 5

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

michaeln31a at 2007-7-12 10:00:21 > top of Java-index,Java Essentials,New To Java...
# 6
every time you run through your menu routine, you create a new tenant and overwrite the old. Get that out of the menu routine and it will work fine.
petes1234a at 2007-7-12 10:00:21 > top of Java-index,Java Essentials,New To Java...
# 7

This is what I mean:

public static void main(String[] args)

{

tenantList = new TenantList(arraySize); // this should go here

menu();

}

public static void menu()

{

//!!!!!!!!!!!!!!!!!!!!!!

// tenantList = new TenantList(arraySize); // and should be removed from here

petes1234a at 2007-7-12 10:00:21 > top of Java-index,Java Essentials,New To Java...
# 8
there are other problems in your code including for loops that don't start at 0 and unusual numbering systems. You may want to look at it more closely, but at least w/ the above changes it should run.
petes1234a at 2007-7-12 10:00:21 > top of Java-index,Java Essentials,New To Java...
# 9
how did i not see that! thanks very much! makes so much sense when you see whats wrong! cheers petes1234!!!Mike
michaeln31a at 2007-7-12 10:00:21 > top of Java-index,Java Essentials,New To Java...
# 10

> there are other problems in your code including for

> loops that don't start at 0 and unusual numbering

> systems. You may want to look at it more closely,

> but at least w/ the above changes it should run.

Yah i know i hate the fact that the loops dont start at 0. Its really annoying but the objectList class is what i've been given an i can't change that. That class, for some reason unknown to me, takes into account that 1 is actually to 0 in an array an adjusts for it but when i code i always take into account that the first space is 0! u can imagine how confused i am with it!

Thanks again petes

michaeln31a at 2007-7-12 10:00:21 > top of Java-index,Java Essentials,New To Java...