89 lines
2.4 KiB
Java
89 lines
2.4 KiB
Java
package tech.nevets.signaturecardgen;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.JsonArray;
|
|
import com.google.gson.JsonElement;
|
|
|
|
import javax.imageio.ImageIO;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.File;
|
|
import java.io.FileReader;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class Location {
|
|
public static final List<Location> LOCATIONS = new ArrayList<>();
|
|
|
|
private final String id;
|
|
private final String name;
|
|
private final String address;
|
|
private final String number;
|
|
private transient BufferedImage background;
|
|
|
|
public Location(String id, String name, String address, String number) {
|
|
this.id = id;
|
|
this.name = name;
|
|
this.address = address;
|
|
this.number = number;
|
|
loadBackground();
|
|
}
|
|
|
|
public String getId() {
|
|
return id;
|
|
}
|
|
|
|
public String getName() {
|
|
return name;
|
|
}
|
|
|
|
public String getAddress() {
|
|
return address;
|
|
}
|
|
|
|
public String getNumber() {
|
|
return number;
|
|
}
|
|
|
|
public BufferedImage getBackground() {
|
|
return background;
|
|
}
|
|
|
|
public void loadBackground() {
|
|
File backgroundFile = new File("./backgrounds/" + id + ".png");
|
|
if (!backgroundFile.exists()) backgroundFile = new File("./backgrounds/default.png");
|
|
try {
|
|
background = ImageIO.read(backgroundFile);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return "{ \"id\": " + id + ", \"name\": " + name + ", \"address\": " + address + ", \"number\": " + number + " }";
|
|
}
|
|
|
|
public static void loadLocations () {
|
|
try {
|
|
Gson gson = new Gson();
|
|
JsonArray jsonFile = gson.fromJson(new FileReader("./locations.json"), JsonArray.class);
|
|
for (JsonElement element : jsonFile.asList()) {
|
|
Location location = gson.fromJson(element, Location.class);
|
|
location.loadBackground();
|
|
LOCATIONS.add(location);
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public static Location getLocation (String locationId) {
|
|
for (Location loc : LOCATIONS) {
|
|
if (loc.getId().equals(locationId)) {
|
|
return loc;
|
|
}
|
|
}
|
|
return new Location("", "", "", "");
|
|
}
|
|
} |