Setting an object to blank? | Newbie | | Join Date: Aug 2008
Posts: 11
| | |
Hi it's me again, I've pretty much finished a program I'm making, which does the following:
The program is for a garage, and it does the following functions:
Adds a new car
Display cars
Amend price of a car
Delete car ('sell' car)
Find a certain car
When the program is loaded, it attempts to read existing car data from a text file, which is then converted into an object array. Then when manipulation is finished with the details, it's all completely written back to the text file (Overwriting it, not appending to it).
Almost everything is working perfect, except that I can't get the 'delete' function working correctly. I have 5 fields in my object array, these are Registration Number, Manufacturer, Model (all 3 are strings), and year and price (which are ints). I've researched this and actually deleting from an array is nigh on impossible, but I realised that when data is written back to the text file straight after a car is sold, it skips all blank lines, so if I convert a car position's fields to blank, it'll do what I want it do...
Problem is, I need to somehow set the int to be 'blank'...not 0, just like a space. Is this possible? Thanks
|  | Moderator | | Join Date: Nov 2006 Location: Upstate NY - US
Posts: 2,268
| | | re: Setting an object to blank?
Heiya!
Can you post the delete function?
It sounds like you writing to a text file in two instances. Why not simply write to it once?
Meaning that, if data are already being written to a text file, why not do your manipulation upon submitting to that text file firsthand. You would not need to overwrite...
Still, please post what you have working, you;re sure to get sound support here.
In a bit!
| | Newbie | | Join Date: Aug 2008
Posts: 11
| | | re: Setting an object to blank?
Thanks for the reply :)
Maybe I should explain about my program first, sorry last night I didn't really explain much.
The first thing that happens when the program is run, is the car details text file is loaded and convert into an object array. The coding used to do this is (the text file is opened with a BufferedReader previously, but I thought copying that code would be a bit pointless) -
activeCars = 0;
-
aRegNo = Text.readLine ( inputCarFile );
-
-
while ( aRegNo.equals ( "****" ) == false )
-
{
-
-
aManufacturer = Text.readLine ( inputCarFile );
-
aModel = Text.readLine ( inputCarFile );
-
aYear = Text.readInt ( inputCarFile );
-
aPrice = Text.readInt ( inputCarFile );
-
-
carDetails[activeCars] = new Car( aRegNo, aManufacturer, aModel, aYear, aPrice );
-
-
activeCars++;
-
System.out.println("");
-
-
aRegNo = Text.readString ( inputCarFile );
-
-
}
-
-
inputCarFile.close();
-
Then when adding a car to the database, something like this is used to create the object instance: - carDetails[activeCars] = new Car( tempRegNo, tempManufacturer, tempModel, tempYear, tempPrice );
-
activeCars++;
And this is what I'm currently using to sell a car from the database (line 11 is not valid, that's just me testing around on how to set all the fields to blank) -
correct = Text.readChar("Here are the details of the car to be sold: \n" +
-
"Manufacturer: " + carDetails[search].getManufacturer() + "\n" +
-
"Model: " + carDetails[search].getModel() + "\n" +
-
"Registration No: " + carDetails[search].getRegNo() + "\n" +
-
"Year: " + carDetails[search].getYear() + "\n" +
-
"Price: £" + carDetails[search].getPrice() + "\n" +
-
"\n Are these details correct? If so, press Y or y");
-
-
if (correct == 'Y' || correct == 'y')
-
{
-
carDetails[search] = "";
-
-
activeCars--;
-
Text.showMessage("The car has now been sold.");
-
-
}
-
After each manipulation, the saveCarDetails method is called: -
for ( int printcount = 0; printcount < activeCars; printcount++ )
-
{
-
-
outputFile.println ( carDetails[printcount].getRegNo() );
-
outputFile.println ( carDetails[printcount].getManufacturer() );
-
outputFile.println ( carDetails[printcount].getModel() );
-
outputFile.println ( carDetails[printcount].getYear() );
-
outputFile.println ( carDetails[printcount].getPrice() );
-
outputFile.println ("");
-
}
-
outputFile.println ("****");
-
The car constructor is defined in a file called Car.java, and looks like this: -
public class Car
-
{
-
// attributes
-
-
private String manufacturer;
-
private String model;
-
private String regNo;
-
private int year;
-
private int price;
-
-
// constructor
-
-
public Car(String regNo, String manufacturer, String model, int year, int price)
-
{
-
this.regNo = regNo;
-
this.manufacturer = manufacturer;
-
this.model = model;
-
this.year = year;
-
this.price = price;
-
}
-
The following is mutator methods for cars, which I can call to. -
public void setManufacturer(String manufacturer)
-
{
-
this.manufacturer = manufacturer;
-
}
-
-
public void setModel(String model)
-
{
-
this.model = model;
-
}
-
-
public void setRegNo(String regNo)
-
{
-
this.regNo = regNo;
-
}
-
-
public void setYear(int year)
-
{
-
this.year = year;
-
}
-
-
public void setPrice(int price)
-
{
-
this.price = price;
-
}
-
-
public String getManufacturer()
-
{
-
return this.manufacturer;
-
}
-
-
// accessor methods
-
-
public String getModel()
-
{
-
return this.model;
-
}
-
-
public String getRegNo()
-
{
-
return this.regNo;
-
}
-
-
public int getYear()
-
{
-
return this.year;
-
}
-
-
public int getPrice()
-
{
-
return this.price;
-
}
-
-
-
public String toString()
-
{
-
return " \nManufacturer : " + manufacturer +
-
"\nModel : " + model +
-
"\nRegistration number : " + regNo +
-
"\nYear : " + year +
-
"\nPrice : " + price;
-
}
-
Hopefully that makes sense...:) thanks again
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: Setting an object to blank?
You have an array of Cars; when you sell a Car you want to get rid of it; why not
simply set the reference in the array to null? That way you'll end up with an array
of Cars and nulls so you have to check for that null value every time you loop
over that array (the null represents a sold car).
There are better ways to do it (e.g. use an ArrayList instead) but the above
approach works as well but you have to change all your loops a bit.
Or: add a boolean field 'sold' to your Car class and set it to true when the car is
sold; you have to anticipate for that in your loops as well.
kind regards,
Jos
| | Newbie | | Join Date: Aug 2008
Posts: 11
| | | re: Setting an object to blank?
Ahhh, thanks, that seems to work great :) I've made it set the sold cars fields to 0, then had my save car method skip over any lines with just 0.
Thanks again!
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: Setting an object to blank? Quote:
Originally Posted by vitaminz Ahhh, thanks, that seems to work great :) I've made it set the sold cars fields to 0, then had my save car method skip over any lines with just 0.
Thanks again! Yes, that is the Fortran/Pascal/Cobol way of doing it (all those sily 9999 values).
Have a look at the ArrayList class; it acts like an array but you can remove
entries from it, i.e. there is no need to put sentinel values in that entry.
kind regards,
Jos
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,414 network members.
|