-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
96 lines (83 loc) · 3.02 KB
/
Main.java
File metadata and controls
96 lines (83 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import java.io.FileNotFoundException;
import java.nio.file.Paths;
import java.util.Scanner;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import Menu.Menu;
import Menu.MenuBuilder;
import craftingSystem.CraftingSystem;
import exceptions.ItemNotFoundException;
import inventory.Inventory;
import repositories.ItemsRepository;
import services.PrologService;
import services.PrologServiceBuilder;
public class Main {
public static void main(String[] args) {
// Load items repository
String recipesPath = Paths.get("src", "assets", "recipes.json").toString();
ItemsRepository itemsRepository = null;
try {
itemsRepository = ItemsRepository.loadFromJSON(recipesPath);
} catch (FileNotFoundException e) {
System.out.print("> Fatal error! Items repository path was not found.");
System.exit(101);
} catch (JsonIOException | JsonSyntaxException e) {
System.out.print("> Fatal error! Items repository file has an invalid JSON syntax.");
System.exit(102);
} catch (ItemNotFoundException e) {
System.out.print("> Fatal error! Items repository file has an invalid recipes structure.");
System.exit(103);
} catch (Exception e) {
System.out.printf("> Fatal error! An error occurred on try to load items repository from \"%s\" json file.",
recipesPath);
System.exit(104);
}
// Load inventory
String inventoryPath = Paths.get("src", "assets", "inventory.json").toString();
Inventory inventory = null;
try {
inventory = Inventory.loadFromJSON(inventoryPath, itemsRepository);
} catch (FileNotFoundException e) {
System.out.print("> Fatal error! Inventory path was not found.");
System.exit(201);
} catch (JsonIOException | JsonSyntaxException e) {
System.out.print("> Fatal error! Inventory file has an invalid JSON syntax.");
System.exit(202);
} catch (Exception e) {
System.out.printf("> Fatal error! An error occurred on try to load the inventory from \"%s\" json file.",
inventoryPath);
System.exit(203);
}
// Build prolog service
// @formatter:off
PrologService prologService = new PrologServiceBuilder()
.setBaseItemFactName("base_item")
.setIngredientFactName("ingredient")
.setItemInInventoryFactName("have")
.setItemsRepository(itemsRepository)
.setInventory(inventory)
.build();
// @formatter:on
// Build menu
Scanner stdin = new Scanner(System.in);
// @formatter:off
Menu menu = new MenuBuilder()
.setScanner(stdin)
.setInventory(inventory)
.setItemsRepository(itemsRepository)
.setCraftingSystem(new CraftingSystem(inventory))
.setPrologService(prologService)
.build();
// @formatter:on
// Print items within items repository
System.out.println("> Repository items:\n");
System.out.println(itemsRepository.toString(new String[] { "•", "•", "◦" }, 2));
// Print items within inventory
System.out.println("\n> Inventory:\n");
System.out.printf("%s\n\n", inventory.toString("•", 2));
// Initialize menu
menu.init();
stdin.close();
System.out.println("> Program finished.");
}
}