Skip to content

Instantly share code, notes, and snippets.

@Malix-off
Last active November 20, 2023 09:09
Show Gist options
  • Save Malix-off/b035744654f5d7d67b52e20d644f3db5 to your computer and use it in GitHub Desktop.
Save Malix-off/b035744654f5d7d67b52e20d644f3db5 to your computer and use it in GitHub Desktop.
L3-Budapest - Java - 5

Instructions

Today we start with a singleton exercise

Let's create a java program that has two classes Cat and Dog

They should have some fields it is up to you

And then create a Logger class which takes log messages and prints them to the screen.

  • The Logger should be a singleton, create it like that.
  • The logger should have logging levels defined in an enum these levels are error, info, debug.
  • The logging level can be set at any time and then the Log messages should only appear if the match the level

Traditional log level use:

  • When set to error only error messages are printed
  • When set to info info and error messages are printed
  • When set to debug debug, info and error messages are printed
public class Cat {
private String name;
private int age;
private String color;
public Cat(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
public void meow() {
System.out.println("Meow!");
}
public void sleep() {
System.out.println("Zzzz...");
}
public void eat(String food) {
System.out.println("Eating " + food);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
public class Dog {
private String name;
private int age;
private String breed;
public Dog(String name, int age, String breed) {
this.name = name;
this.age = age;
this.breed = breed;
}
public void bark() {
System.out.println("Woof woof!");
}
public void eat(String food) {
System.out.println(name + " is eating " + food);
}
public void sleep() {
System.out.println(name + " is sleeping Zzz...");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
}
public class Logger {
private static Logger instance;
private LogLevel logLevel;
private Logger() {
// Initialize log level to default, e.g., LogLevel.INFO
this.logLevel = LogLevel.INFO;
}
public static synchronized Logger getInstance() {
if (instance == null) {
instance = new Logger();
}
return instance;
}
public void setLogLevel(LogLevel logLevel) {
this.logLevel = logLevel;
}
public void log(LogLevel messageLevel, String message) {
if (messageLevel.ordinal() <= logLevel.ordinal()) {
System.out.println("[" + messageLevel + "] " + message);
}
}
}
public enum LogLevel {
ERROR,
INFO,
DEBUG,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment