I want every piece of feedback that is possible as I am new to Java. I've coded some C# before but not much. I want to be sure that I am following the guidelines of how to write and name Java programs. Hit me as hard as you can.
I wrote this code for testing other functions I am writing, to track what is going on and when in the program. There are two classes one message class
and one container class
. Currently there are two constants stored in my final class
.
It will later be used for logging in the program as well, so it is nothing temporary.
Constants.java
public final class Constants
{
// log
public static final int LogEntriesDisplay = 6;
public static final int LogEntriesMax = 500;
}
Log.java
import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import java.text.SimpleDateFormat;
/**
* Log () default constructor; initializes a log
*
* void AddEntry (String s) adds an entry at current time containing message s, removes oldest entry if entries.size() >= Constants.LogEntriesMax
* List<LogMessage> GetLogEntries () returns entries from offset to Constants.LogEntriesDisplay, never returns out of bounds
*
* void IncOffset () increases offset by 1, never goes out of bounds
* void IncOffset () decreases offset by 1, never goes out of bounds
* void GoToTop () offset at start, never goes out of bounds
* void GoToBottom () offset at end, never goes out of bounds
*
* @author Emz
* @version 1.0
* @date 2014-11-15
*/
public class Log
{
private List<LogMessage> entries;
private int offset;
// default constructor; initializes a log
public Log ()
{
entries = new ArrayList<LogMessage>();
offset = 0;
}
// adds an entry at current time containing message s, removes oldest entry if entries.size() >= Constants.LogEntriesMax
public void AddEntry (String s)
{
String t = new SimpleDateFormat("HH:mm:ss").format(new Date());
LogMessage lm = new LogMessage(t, s);
if (entries.size() >= Constants.LogEntriesMax)
entries.remove(0);
entries.add(lm);
}
// returns entries from offset to Constants.LogEntriesDisplay, never returns out of bounds
public List<LogMessage> GetLogEntries ()
{
int fromIndex = offset;
int toIndex = (offset + Constants.LogEntriesDisplay > entries.size()) ? entries.size() : offset + Constants.LogEntriesDisplay;
return entries.subList(fromIndex, toIndex);
}
// increases offset by 1, never goes out of bounds
public void IncOffset ()
{
offset = (offset < entries.size() - Constants.LogEntriesDisplay) ? offset+1 : offset;
}
// decreases offset by 1, never goes out of bounds
public void DecOffset ()
{
offset = (offset == 0) ? 0 : offset-1;
}
// offset at start, never goes out of bounds
public void GoToTop ()
{
offset = 0;
}
// offset at end, never goes out of bounds
public void GoToBottom ()
{
offset = entries.size() - Constants.LogEntriesDisplay;
}
}
LogMessage.java
public class LogMessage
{
private String t;
private String m;
public LogMessage (String t, String m)
{
this.t = t;
this.m = m;
}
public String GetTime ()
{
return t;
}
public String GetMessage ()
{
return m;
}
}