Quick Java Question

Soldato
Joined
11 Apr 2003
Posts
4,208
Location
Notts
Hi all, im creating a system that manages stock, and I need to be able to do the following:

When customer rents X, data = number;
When customer returns X, data = number;

However I cannot work out how to overide the propertise I have alreaey stored in the object!

I am storing my objects in a vector and calling them at a later date.

I currently have it so, if they rent a car, the number stored is 1, if a bike its 2, etc, and I use if statements to work out what vehicle they have rented later in the program.

However I need to set it to 3, when they return the vehicle, to say that they are currently renting nothing, but trying to do this has no effect and I am just continualy told that the vehicle that they have returned is still rented by them....

I have tried to alter it like this but it seems to not work:
Code:
        CustomerRecord customerRecord = (CustomerRecord) customer.get(selectNo);       
        if(customerRecord.getRented() == 1) {
            System.out.println("The Customer Is Renting A Car, They Rented It For: " + customerRecord.getDuration() + " Hours"); 
            customerRecord.getRented() = 4;
 
Last edited:
You need to use a different method in the customer record class, e.g:

Code:
public void setValue(int x)
{
     value = x;
}

Where x is the new value and 'value' is the variable.

On the whole it looks like quite a hackish way to do it though, you could do a much better job using objects.

For this you could make an interface called 'Rentable' and include the method signatures of all the methods you'd need in an object which is rentable, for example:

Code:
public String getType();
public boolean isRented();
public void rent();
punlic void return();

And so on, then you can implement this interface in your Bike, Car and other classes, then store them in a Vector/ArrayList that takes objects of type Rentable, e.g:

Code:
ArrayList<Rentable> items = new ArrayList<Rentable>();

You could then have a Vector in your customer record class that holds a list of currently rented items and use further methods to rent/return items and so on. An example method to rent an item might be:

Code:
public boolean rentItem(Rentable item)
{
      if(item.isRented())
      {
            return false;
      }
      else
      {
           items.add(item);
           item.rent();
           return true;
      }
}

public void returnItem(Rentable item)
{
      items.remove(item);
      item.return();
}

Using this way of storing the items you can then see what type of vehicle a customer is currently renting by using the getType() method in the item class making the application a lot more expandable.
 
Last edited:
Thanks for the reply, I will look into this more, but the java version I am using does not support some of the features, and as the computers it will be running on wont have java 2 I cant use them. I also cannot get it to overwrite the data, and store it back into the vector, I simply want it to set a value to 4, so I can throw a "This transaction has been delt with" error should the customer be called twice
 
Surprised the computers are running such an old version of Java, but if they are then there's obviously unfortunately nothing you can do about it :(

Inside the class you want to alter the number for you need to put the method I put above:

Code:
public void setValue(int x)
{
     value = x;
}

You can then use it like:

Code:
vector.get(i).setValue(n);

Where:

vector = name of vector the object is in
i = index of the object inside the vector
n = the number you want to update the variable inside the object to

Also, why do you have to cast the customer record? Are you holding any other object type in the vector?
 
To add the object to vector:

CustomerRecord customerRecord = new CustomerRecord(n, a, t, r, totalTime);
customer.add(customerRecord);

To call it:

CustomerRecord customerRecord = (CustomerRecord) customer.get(selectNo);

Then I need to change the detail and thats that
 
You can simplify that a bit by doing:

Code:
customer.add(new CustomerRecord(n, a, t, r, totalTime));
customer.get(selectNo).doStuff();

Where doStuff() is whatever method you want to call on it.
 
Phil99 said:
Surprised the computers are running such an old version of Java, but if they are then there's obviously unfortunately nothing you can do about it :(

Inside the class you want to alter the number for you need to put the method I put above:

Code:
public void setValue(int x)
{
     value = x;
}

You can then use it like:

Code:
vector.get(i).setValue(n);

Where:

vector = name of vector the object is in
i = index of the object inside the vector
n = the number you want to update the variable inside the object to

Also, why do you have to cast the customer record? Are you holding any other object type in the vector?

Will I still be able to set it with e.g customer(0,0,0,0,0) That way? Its the getRented() Bit I need to update, I set it when I create the object, to show what the customer has rented, however need to be able to update it, as atm when I call it it will still say "Car" even if the car has been returned, and I need it to throw an error

CustomerRecord:
Code:
public class CustomerRecord {
   protected String name;
   protected String address;
   protected String telNo;
   protected int rented;
   protected int duration;
   
    public CustomerRecord(String n,String a, String t, int r, int d) {
        name = n;
        address = a;
        telNo = t;   
        rented = r;
        duration = d;
    }
    public String getName() {
        return this.name;
    }
    public String getAddress() {
        return this.address;
    }
    public String getTelNo() {
        return this.telNo;
    }
    public int getRented() {
        return this.rented;
    }
    public int getDuration() {
        return this.duration;
    }
}

Customer:
Code:
public class Customer extends CustomerRecord {

    public Customer(String name, String address, String telNo, int rented, int duration) {
        super(name, address,telNo, rented, duration);
    }
}
 
All the methods you're using at the moment are accessor methods which means they only return a value. You need to use a mutator method which will set the value inside the class when called and given a variable. You may also want to add a toString() method. This is basically a method where you return a String of details of that class which can be printed by simply printing the instance (example below). Your class would become something like this:

Code:
public class CustomerRecord {
   protected String name;
   protected String address;
   protected String telNo;
   protected int rented;
   protected int duration;
   
    public CustomerRecord(String n,String a, String t, int r, int d) {
        name = n;
        address = a;
        telNo = t;   
        rented = r;
        duration = d;
    }
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return this.address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTelNo() {
        return this.telNo;
    }

    public void setTelNo(String telNo) {
        this.telNo = telNo;
    }

    public int getRented() {
        return this.rented;
    }

    public void setRented(int rented) {
        this.rented = rented;
    }

    public int getDuration() {
        return this.duration;
    }

    public void getDuration(int duration) {
        this.duration = duration;
    }

    public String toString()
    {
         String s = "Name: " + name + "\n"
                  + "Address: " + address + "\n"
                  + "Telephone: " + telNum + "\n";
         return s;
    }
}

You could then use these as follows:

Code:
//Create a new CustomerRecord and add it to the list
vector.add(new CustomerRecord("Joe Bloggs","A Street","01234111222","1","3"));

//Print out customer details
System.out.println(vector.get(0));

//Customer 0 changes address
vector.get(0).setAddress("The Other Road");

//Print out revised customer details
System.out.println(vector.get(0));

This would give an output along the lines of:

Code:
Name: Joe Bloggs
Address: A Street
Telephone: 01234111222

Name: Joe Bloggs
Address: The Other Road
Telephone: 01234111222
 
Last edited:
I also can't see why you'd want Customer to be a subclass of CustomerRecord?

Is there a particular reason for this as the Customer class seems a bit pointless as it doesn't do anything extra, or are there future plans to expand the Customer class in some way?
 
Phil99 said:
I also can't see why you'd want Customer to be a subclass of CustomerRecord?

Is there a particular reason for this as the Customer class seems a bit pointless as it doesn't do anything extra, or are there future plans to expand the Customer class in some way?
Actualy was thinking the same myself, I got a bit eger on the inheritance and hey hey... Anyway, been trying to adapt what you said into my programing, but when I put anything like

public int setRented(int rented) {
this.rented = rented;
}
It returns the error "missing return statement", will making it:
public int setRented(int rented) {
return this.rented = rented;
}
Do the same job?

*EDIT* Nope it returns the error "cannot resolve symbol - method setRented(int)"
 
Last edited:
oops, bit of a stupid error by me there, the methods I added should have a return type of void as they don't return anything :o

Code:
public void setRented(int rented) {
this.rented = rented;
}

Would do the job :)
 
Phil99 said:
oops, bit of a stupid error by me there, the methods I added should have a return type of void as they don't return anything :o

Code:
public void setRented(int rented) {
this.rented = rented;
}

Would do the job :)
Ah that makes sense, and also explains why my earlier atempts to create a void class would not work! Just tried it however (Just set up your code as a demo class untill I get it working) and on:

Code:
        vector.get(0).setRented(1);
It returns: "cannot resolve symbol - method setRented(int)"
 
Just noticed another mistake, I shouldn't have put "" around the numbers. A revised version of the example is as follows:

CustomerRecord.java
Code:
public class CustomerRecord {
   protected String name;
   protected String address;
   protected String telNo;
   protected int rented;
   protected int duration;
   
    public CustomerRecord(String n,String a, String t, int r, int d) {
        name = n;
        address = a;
        telNo = t;   
        rented = r;
        duration = d;
    }
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return this.address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTelNo() {
        return this.telNo;
    }

    public void setTelNo(String telNo) {
        this.telNo = telNo;
    }

    public int getRented() {
        return this.rented;
    }

    public void setRented(int rented) {
        this.rented = rented;
    }

    public int getDuration() {
        return this.duration;
    }

    public void getDuration(int duration) {
        this.duration = duration;
    }

    public String toString()
    {
         String s = "Name: " + name + "\n"
                  + "Address: " + address + "\n"
                  + "Telephone: " + telNo + "\n";
         return s;
    }
}

Main.java
Code:
import java.util.Vector;

public class Main
{
	public static void main(String args[])
	{
		Vector vector = new Vector();

		//Create a new CustomerRecord and add it to the list
		vector.add(new CustomerRecord("Joe Bloggs","A Street","01234111222",1,3));

		CustomerRecord cust = (CustomerRecord) vector.get(0);

		//Print out customer details
		System.out.println(cust);

		//Customer 0 changes address
		cust.setAddress("The Other Road");

		//Print out revised customer details
		System.out.println(cust);
	}
}

That compiles and runs fine on mine and I think I've taken out the things you couldn't use with your older version of Java.

I think I can see why you were casting the object as type (CustomerRecord) now instead of just using it directly from the vector...iirc as you're using an older version of Java you can't use Generics so the compiler throws up an error when trying to call the method as it doesn't realise what type of object it is, so doesn't know what methods it can or can't call.
 
Let me guess your not using parameterized typed collections?

((CustomerRecord)vector.get(0)).setRented(1); is what you want instead.

Edit: too slow, or use the code above if you want a clearer way.
 
Last edited:
Phil99 said:
Just noticed another mistake, I shouldn't have put "" around the numbers. A revised version of the example is as follows:

CustomerRecord.java
Code:
public class CustomerRecord {
   protected String name;
   protected String address;
   protected String telNo;
   protected int rented;
   protected int duration;
   
    public CustomerRecord(String n,String a, String t, int r, int d) {
        name = n;
        address = a;
        telNo = t;   
        rented = r;
        duration = d;
    }
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return this.address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTelNo() {
        return this.telNo;
    }

    public void setTelNo(String telNo) {
        this.telNo = telNo;
    }

    public int getRented() {
        return this.rented;
    }

    public void setRented(int rented) {
        this.rented = rented;
    }

    public int getDuration() {
        return this.duration;
    }

    public void getDuration(int duration) {
        this.duration = duration;
    }

    public String toString()
    {
         String s = "Name: " + name + "\n"
                  + "Address: " + address + "\n"
                  + "Telephone: " + telNo + "\n";
         return s;
    }
}

Main.java
Code:
import java.util.Vector;

public class Main
{
	public static void main(String args[])
	{
		Vector vector = new Vector();

		//Create a new CustomerRecord and add it to the list
		vector.add(new CustomerRecord("Joe Bloggs","A Street","01234111222",1,3));

		CustomerRecord cust = (CustomerRecord) vector.get(0);

		//Print out customer details
		System.out.println(cust);

		//Customer 0 changes address
		cust.setAddress("The Other Road");

		//Print out revised customer details
		System.out.println(cust);
	}
}

That compiles and runs fine on mine and I think I've taken out the things you couldn't use with your older version of Java.

I think I can see why you were casting the object as type (CustomerRecord) now instead of just using it directly from the vector...iirc as you're using an older version of Java you can't use Generics so the compiler throws up an error when trying to call the method as it doesn't realise what type of object it is, so doesn't know what methods it can or can't call.

Thanks, Had already got so far with that, but was missing a bit, and I then could not get it to work in my main, as for some reason I had managed to capitilise every "customer" object, so had to go through and fix it, but now its working! If you try to return a vehicle, and it is already returned then an error is thrown :).

Now I have this working, I will work on reusing a customers details for a new transaction, which should not be to hard.

Thank you for all the help you have given to me, not sure where I would be now if you had not :)
 
Can anyone see an error in this please? Once I enter the selection of what I want to do it drops straight down, e.g if I enter 1 I get:

Code:
Press 1# To Update The Customers Name

Press 2# To Update The Customers Address

Press 3# To Update The CustomersTelephone Number

Press 4# To Return To The Main Menu



1
Please Input The Customers Updated Name

Customer Updated Name: 



Press 1# To Update The Customers Name

Press 2# To Update The Customers Address

Press 3# To Update The CustomersTelephone Number

Press 4# To Return To The Main Menu

The only way to input is by going "1 New Name" but this is not wht I want I want: "1"
"please enter bla bla"
"new name"

Code:
 public void editCustomer() {
        int menu;
        int selectNo = -1;
        String set;
        boolean loopBreak = false;
        boolean loop = false;
 
        while(!loopBreak) {
            System.out.println("Please Enter The Customer's Customer Number");
            selectNo = TextIO.getInt();
            if(selectNo >customerCount) {
                System.out.println("Im Sorry This Customer Does Not Exist, Please Try Again!");
                loopBreak = false;
            }
            else if(selectNo <0) {
                System.out.println("Im Sorry This Customer Does Not Exist, Please Try Again!");
                loopBreak = false;
            }
            else {
                loopBreak = true;
            }
        }
        
        Customer customer = (Customer) customerR.get(selectNo); 
        System.out.println("Customer Name: " + customer.getName());
        System.out.println("Customer Addrress: " + customer.getAddress());
        System.out.println("Customer Telephone Number: " + customer.getTelNo());
        
        while(!loop) {
            System.out.println("");
            System.out.println("Press 1# To Update The Customers Name");
            System.out.println("Press 2# To Update The Customers Address");
            System.out.println("Press 3# To Update The CustomersTelephone Number"); 
            System.out.println("Press 4# To Return To The Main Menu");
            System.out.println("");
            menu = TextIO.getInt();
            if(menu == 1) {
               	System.out.println("Please Input The Customers Updated Name");
                set = TextIO.getlnString();
                customer.setName(set);
                System.out.println("Customer Updated Name: " + customer.getName());
            }
            else if(menu == 2) {        
                System.out.println("Please Input The Customers Updated Address");
                set = TextIO.getlnString();
                customer.setAddress(set);
                System.out.println("Customer Updated Addrress: " + customer.getAddress());
            }
            else if(menu == 3) {
                System.out.println("Please Input The Customers Updated Telephone Number");
                set = TextIO.getlnString();
                customer.setTelNo(set);
                System.out.println("Customer Updated Telephone Number: " + customer.getTelNo());
            }
            else if(menu == 4) {
                loop = true;
            }
            else {
                loop = false;
            }
        }        
    }

The only way I can get it to work is by doing this:

Code:
        while(!loop) {
            System.out.println("");
            System.out.println("Press 1# To Update The Customers Name");
            System.out.println("Press 2# To Update The Customers Address");
            System.out.println("Press 3# To Update The CustomersTelephone Number"); 
            System.out.println("Press 4# To Return To The Main Menu");
            System.out.println("");
            menu = TextIO.getInt();
            if(menu == 1) {
               	System.out.println("Please Input The Customers Updated Name");
                set = TextIO.getlnString();
                customer.setName(TextIO.getlnString());
                System.out.println("Customer Updated Name: " + customer.getName());
            }
            else if(menu == 2) {        
                System.out.println("Please Input The Customers Updated Address");
                set = TextIO.getlnString();
                customer.setAddress(TextIO.getlnString());
                System.out.println("Customer Updated Addrress: " + customer.getAddress());
            }
            else if(menu == 3) {
                System.out.println("Please Input The Customers Updated Telephone Number");
                set = TextIO.getlnString();
                customer.setTelNo(TextIO.getlnString());
                System.out.println("Customer Updated Telephone Number: " + customer.getTelNo());
            }
            else if(menu == 4) {
                loop = true;
            }
            else {
                loop = false;
            }
        }

However if I have

set = TextIO.getlnString();
customer.setName(set);

or
customer.setName(TextIO.getlnString());

It falls straight through the loop
 
Last edited:
Is it possible to either post the project here or email it so I can have a play around with it to see what's up?

Can be a bit hard sometimes when you can't run the program to see exactly what's going wrong.
 
Last edited:
Phil99 said:
Is it possible to either post the project here or email it so I can have a play around with it to see what's up?

Can be a bit hard sometimes when you can't run the program to see exactly what's going wrong.
Sure, will rar it up and send it you, I warn you its messy, and atm I have not got any commenting (going to be adding that tomorow) will that be ok? Or shall I send it once I get the commenting done? It will still be messy, but thats because im still learning Java, hopefully with time I can minimize my code use etc :)
 
Back
Top Bottom