Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

source code problem

Status
Not open for further replies.

badbhoy

Programmer
Apr 11, 2002
12
GB
i have this dataRecorder class that has my getters and setters and i need to produce a class that makes it interactive and saves the details entered to a txt file, and then make another record after the first details entered
the class datarecorder is as follows aswell i have to link it to GUI with the textfield details being saved to the file aswell they are all linked


import java.util.*;
import java.io.*;
import java.text.*;

public class DataRecorder

{

static BufferedReader keyboard = new
BufferedReader (new InputStreamReader(System.in));
static PrintWriter screen = new PrintWriter(System.out,true);

private String BadgeNo;
private String CarRego;
private String CarType;
private String CarColour;
private String Located;

static private int objCount = 0;



public DataRecorder()
{
incrementCount();



BadgeNo = "No Badge number entered";
CarRego = "No Car Registration entered";
CarType = "No Manufacturer entered";
CarColour = "No Colour entered";
Located = "No location entered";

}// end of consructor

public DataRecorder(String aBadgeNo, String aCarRego,String aCarType, String aCarColour, String aLocated)
{

incrementCount();

BadgeNo = aBadgeNo;
CarRego = aCarRego;
CarType = aCarType;
CarColour = aCarColour;
Located = aLocated;

}//end of constructor

// start of getters

private static void incrementCount()
{
objCount = objCount + 1;

}//end of incrementCount

public static int getObjCount()
{
return objCount;

}



public String getBadgeNo()
{
return BadgeNo;
}// end of getBadgeNo

public String getCarRego()
{
return CarRego;
}// end of getcarrego

public String getCarType()
{
return CarType;
}// end of getcartype

public String getCarColour()
{
return CarColour;
}// end of getcarcolour


public String getLocated()
{
return Located;
}// end of getLocated


//setters

public void setBadgeNo(String astring)
{
BadgeNo = astring;
}// end of setCarColour


public void setCarRego(String astring)
{
CarRego = astring;
}//end of setCarrego

public void setCarType(String astring)
{
CarType = astring;
}// end of setcartype

public void setCarColour(String astring)
{
CarColour = astring;
}// end of setCarColour


public void setLocated(String astring)
{
Located = astring;
}// end of setCarColour



public String getTime()
{
String astring;

GregorianCalendar now = new GregorianCalendar();



astring = now.get(Calendar.HOUR_OF_DAY)
+ ":" + now.get(Calendar.MINUTE);

if(now.get(Calendar.MINUTE) < 10)
astring = now.get(Calendar.HOUR_OF_DAY)
+ &quot;:&quot; + &quot;0&quot; +now.get(Calendar.MINUTE);

return astring;

}


public String getDate()
{

String astring;

GregorianCalendar now = new GregorianCalendar();

astring = now.get(Calendar.DAY_OF_MONTH) + &quot;/&quot; + now.get(Calendar.MONTH)
+ &quot;/&quot; + now.get(Calendar.YEAR);

return astring;

}


public String getFine()
{
int finehour = 19;
int fine = 0;
int starttime = 0;
int endtime = 30;

GregorianCalendar presenttime = new GregorianCalendar();


if(finehour == presenttime.get(Calendar.HOUR_OF_DAY) && presenttime.get(Calendar.MINUTE) <= endtime
&& presenttime.get(Calendar.MINUTE) >= starttime)
{
fine = 30;
}
else
{
fine = 20;
}

String astring = String.valueOf(fine);

return astring;

}// end of method to calculate fine


public String toString()
{
String tempString = &quot;&quot;;

tempString = tempString + getTime() + &quot;\n&quot; + getDate() + &quot;\n&quot; +
&quot;Badge Number&quot; + getBadgeNo() + &quot;\n&quot; + &quot;Car Registration is &quot; + getCarRego() + &quot;\n&quot; + &quot;Car Manufacturer &quot; + getCarType()+ &quot;\n&quot;
+ &quot;Car Colour is &quot; + getCarColour() + &quot;\n&quot; + &quot;Car Location &quot; + getLocated() + &quot;\n&quot; + &quot;Fine : \u0153&quot; + getFine() + &quot;\n&quot;;

return tempString;

}//end of toString





}
 
Hmm... there are a few things you could do to make this a lot cleaner and easier for outsiders to read. Bear with me while I nitpick, would you?

Names are not always evident. Why objCount? that means nothing. getFine?? Triple the code size and come back six months (or six weeks) later, and you might be scratching your head. So please, make names meaningful and use naming conventions.

Calling a method to increment it seems odd, since you could just do it in one line:

whateverTypeObject++;

//comments are great to finish off long methods... but they only take up visual space when the method is only 3 lines long.

You could move out your Date functions in a DateUtil class, rather than have seemingly unrelated stuff in an object.

Now, to deal with your current predicament. Have you considered object serialization? It's a good solution for short-term storage. We used that on a regular basis where I work... otherwise you might have to develop your own system of storing the data and retrieving it, which sounds like too much work to me.

Another good solution would be plugging the data in a database. It will allow you more flexibility and make it easier for you to grow your application.

 
I want application for applet to servlet,servlet to database communication. And also in the resverse (database to servlet and from sevlet to applet).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top