I need to make a game that demonstates that my logger works, theres 3 different
ID: 3661842 • Letter: I
Question
I need to make a game that demonstates that my logger works, theres 3 different classes.GameManager, Game Engine, and Sound.The GameManager class will have 3 methods Initialize(), PlayGame() and ShutDown(). GameEngine will also have 3 classes Initialize(), Draw() and ShutDown().Sound with also have 3 classes Initialize() PlaySound(), andShutDown(). In the main it will call the GameManager and do gamemanger.Play(), gamemanger.Initialize() and gamemanger.shutdown. The GameManager class initializes the Sound and Game Engine classes, which both just log that they have been initialized, and the the GameManger will aslo call GameEngine.Draw() and Sound.PlaySound() a random numbe of times between 2 and 10. Each of those methods will log that they are called and then once all the rounds are complete it will call the Shutdown() for both of the other classes, which will log that they were shut down. I'm writing this in IntelliJ so just simple Java, no Jswing,or any GUI things its all text
And here is my logger if needed
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.IllegalFormatConversionException;
import java.util.MissingFormatArgumentException;
import com.sun.jmx.snmp.Timestamp;
enum MessageLevel{Info, FatalError, Warning, Error,NewLevel }
public class Logger {
static BufferedWriter bw;
static{
String logFileName ="LogFile.log";
File file = new File(logFileName);
FileWriter fw = null;
try {
fw = new FileWriter(file.getAbsoluteFile());
} catch (IOException e) {
e.printStackTrace();
}
bw = new BufferedWriter(fw);
}
public void WriteLog(String msg) {
java.util.Date date = new java.util.Date();
System.out.print(msg);
System.out.print(new Timestamp(date.getTime()));
try {
bw.write(msg);
bw.write(new Timestamp(date.getTime()).toString());
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
System.out.print(new Timestamp(date.getTime()));
}
public String GetMsgLevelString(MessageLevel level) {
switch (level) {
case Info:
return "Info: ";
case FatalError:
return "Fatal Error: ";
case Warning:
return "Warning: ";
case Error:
return "Error: ";
}
return "Programmer ERROR: No message";
}
public void Log(MessageLevel level, String msg) {
Logger log = new Logger();
log.WriteLog((String.format(log.GetMsgLevelString(level), msg)));
}
public void Log(MessageLevel level, String msg, Object... value) {
Logger logger = new Logger();
try {
logger.WriteLog(String.format(msg, value) + GetMsgLevelString(level));
} catch (IllegalFormatConversionException e) {
logger.WriteLog(String.format("Programmer Error: Bad Format ")+e.getMessage());
} catch (MissingFormatArgumentException e) {
logger.WriteLog(String.format("Programmer Error: Missing ")+e.getMessage());
}
}
}
Explanation / Answer
I've designed 3 classes as required by you + main class
1. main class
code start:
public class Main {
public static void main(String[] args) {
GameManager.Initialize();
GameManager.PlayGame();
GameManager.ShutDown();
}
}
code end:
Main class is simple enough, its just calling 3 static methos of gamemanager
2.GameManager class
code start:
import java.util.Random;
public class GameManager {
static Logger log=new Logger();
public static void Initialize(){
GameEngine.Initialize();
Sound.Initialize();
log.Log(MessageLevel.Info,"GameManager has been Initialized");
}
public static void PlayGame(){
Random randomGenerator = new Random();
int randomInt=0;
while(randomInt==0 || randomInt==1)
randomInt= randomGenerator.nextInt(10);
for (int i = 1; i <= randomInt; i++){
GameEngine.Draw();
Sound.PlaySound();
}
}
public static void ShutDown(){
GameEngine.ShutDown();
Sound.ShutDown();
log.Log(MessageLevel.Info,"GameManager has been ShutDown");
}
}
code end:
initialize() methods is calling initialize() of other 2 classes and logging itself.
PlayGame() is first generating random no. till it gets between 2-10 range and then calling Draw() and PlaySound() that random times.
3.GameEngine class
code start:
public class GameEngine {
static Logger log=new Logger();
public static void Initialize(){
log.Log(MessageLevel.Info,"GameEngine has been Initialized");
}
public static void Draw(){
log.Log(MessageLevel.Info,"Draw method of GameEngine has been called");
}
public static void ShutDown(){
log.Log(MessageLevel.Info,"GameEngine has been ShutDown");
}
}
code end:
Simple methods just logging info.
4.Sound class
code start:
public class Sound {
static Logger log=new Logger();
public static void Initialize(){
log.Log(MessageLevel.Info,"Sound has been Initialized");
}
public static void PlaySound(){
log.Log(MessageLevel.Info,"PlaySound method of Sound has been called");
}
public static void ShutDown(){
log.Log(MessageLevel.Info,"Sound has been ShutDown");
}
}
code ends: