Skip to content

Commit b464318

Browse files
authored
Merge pull request nus-cs2103-AY2021S1#68 from yuming7144/branch-description-related-commands
Start converting AB3 to ExpenseBook
2 parents 10ba16d + 279db26 commit b464318

48 files changed

Lines changed: 1905 additions & 733 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package seedu.address;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Path;
5+
import java.util.Optional;
6+
import java.util.logging.Logger;
7+
8+
import javafx.application.Application;
9+
import javafx.stage.Stage;
10+
import seedu.address.commons.core.Config;
11+
import seedu.address.commons.core.LogsCenter;
12+
import seedu.address.commons.core.Version;
13+
import seedu.address.commons.exceptions.DataConversionException;
14+
import seedu.address.commons.util.ConfigUtil;
15+
import seedu.address.commons.util.StringUtil;
16+
import seedu.address.logic.Logic;
17+
import seedu.address.logic.LogicManager;
18+
import seedu.address.model.*;
19+
import seedu.address.storage.*;
20+
import seedu.address.ui.Ui;
21+
import seedu.address.ui.UiManager;
22+
23+
/**
24+
* Runs the application.
25+
*/
26+
public class ExpenseMainApp extends Application {
27+
28+
public static final Version VERSION = new Version(0, 6, 0, true);
29+
30+
private static final Logger logger = LogsCenter.getLogger(ExpenseMainApp.class);
31+
32+
protected Ui ui;
33+
protected Logic logic;
34+
protected Storage storage;
35+
protected Model model;
36+
protected Config config;
37+
38+
@Override
39+
public void init() throws Exception {
40+
logger.info("=============================[ Initializing UniSave ]===========================");
41+
super.init();
42+
43+
AppParameters appParameters = AppParameters.parse(getParameters());
44+
config = initConfig(appParameters.getConfigPath());
45+
46+
UserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(config.getUserPrefsFilePath());
47+
UserPrefs userPrefs = initPrefs(userPrefsStorage);
48+
ExpenseBookStorage expenseBookStorage = new JsonExpenseBookStorage(userPrefs.getAddressBookFilePath());
49+
storage = new StorageManager(expenseBookStorage, userPrefsStorage);
50+
51+
initLogging(config);
52+
53+
model = initModelManager(storage, null);
54+
55+
logic = new LogicManager(model, storage);
56+
57+
ui = new UiManager(logic);
58+
}
59+
60+
/**
61+
* Returns a {@code ModelManager} with the data from {@code storage}'s address book and {@code userPrefs}. <br>
62+
* The data from the sample address book will be used instead if {@code storage}'s address book is not found,
63+
* or an empty address book will be used instead if errors occur when reading {@code storage}'s address book.
64+
*/
65+
private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
66+
Optional<ReadOnlyExpenseBook> addressBookOptional;
67+
ReadOnlyExpenseBook initialData;
68+
initialData = new ExpenseBook();
69+
return new ExpenseModelManager(initialData, userPrefs);
70+
}
71+
72+
private void initLogging(Config config) {
73+
LogsCenter.init(config);
74+
}
75+
76+
/**
77+
* Returns a {@code Config} using the file at {@code configFilePath}. <br>
78+
* The default file path {@code Config#DEFAULT_CONFIG_FILE} will be used instead
79+
* if {@code configFilePath} is null.
80+
*/
81+
protected Config initConfig(Path configFilePath) {
82+
Config initializedConfig;
83+
Path configFilePathUsed;
84+
85+
configFilePathUsed = Config.DEFAULT_CONFIG_FILE;
86+
87+
if (configFilePath != null) {
88+
logger.info("Custom Config file specified " + configFilePath);
89+
configFilePathUsed = configFilePath;
90+
}
91+
92+
logger.info("Using config file : " + configFilePathUsed);
93+
94+
try {
95+
Optional<Config> configOptional = ConfigUtil.readConfig(configFilePathUsed);
96+
initializedConfig = configOptional.orElse(new Config());
97+
} catch (DataConversionException e) {
98+
logger.warning("Config file at " + configFilePathUsed + " is not in the correct format. "
99+
+ "Using default config properties");
100+
initializedConfig = new Config();
101+
}
102+
103+
//Update config file in case it was missing to begin with or there are new/unused fields
104+
try {
105+
ConfigUtil.saveConfig(initializedConfig, configFilePathUsed);
106+
} catch (IOException e) {
107+
logger.warning("Failed to save config file : " + StringUtil.getDetails(e));
108+
}
109+
return initializedConfig;
110+
}
111+
112+
/**
113+
* Returns a {@code UserPrefs} using the file at {@code storage}'s user prefs file path,
114+
* or a new {@code UserPrefs} with default configuration if errors occur when
115+
* reading from the file.
116+
*/
117+
protected UserPrefs initPrefs(UserPrefsStorage storage) {
118+
Path prefsFilePath = storage.getUserPrefsFilePath();
119+
logger.info("Using prefs file : " + prefsFilePath);
120+
121+
UserPrefs initializedPrefs;
122+
try {
123+
Optional<UserPrefs> prefsOptional = storage.readUserPrefs();
124+
initializedPrefs = prefsOptional.orElse(new UserPrefs());
125+
} catch (DataConversionException e) {
126+
logger.warning("UserPrefs file at " + prefsFilePath + " is not in the correct format. "
127+
+ "Using default user prefs");
128+
initializedPrefs = new UserPrefs();
129+
} catch (IOException e) {
130+
logger.warning("Problem while reading from the file. Will be starting with an empty AddressBook");
131+
initializedPrefs = new UserPrefs();
132+
}
133+
134+
//Update prefs file in case it was missing to begin with or there are new/unused fields
135+
try {
136+
storage.saveUserPrefs(initializedPrefs);
137+
} catch (IOException e) {
138+
logger.warning("Failed to save config file : " + StringUtil.getDetails(e));
139+
}
140+
141+
return initializedPrefs;
142+
}
143+
144+
@Override
145+
public void start(Stage primaryStage) {
146+
logger.info("Starting UniSave" + ExpenseMainApp.VERSION);
147+
ui.start(primaryStage);
148+
}
149+
150+
@Override
151+
public void stop() {
152+
logger.info("============================ [ Stopping UniSave ] =============================");
153+
}
154+
}

src/main/java/seedu/address/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
*/
2121
public class Main {
2222
public static void main(String[] args) {
23-
Application.launch(MainApp.class, args);
23+
Application.launch(ExpenseMainApp.class, args);
2424
}
2525
}

src/main/java/seedu/address/MainApp.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
import seedu.address.model.ReadOnlyUserPrefs;
2323
import seedu.address.model.UserPrefs;
2424
import seedu.address.model.util.SampleDataUtil;
25-
import seedu.address.storage.AddressBookStorage;
26-
import seedu.address.storage.JsonAddressBookStorage;
25+
import seedu.address.storage.JsonExpenseBookStorage;
2726
import seedu.address.storage.JsonUserPrefsStorage;
2827
import seedu.address.storage.Storage;
2928
import seedu.address.storage.StorageManager;
@@ -56,8 +55,8 @@ public void init() throws Exception {
5655

5756
UserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(config.getUserPrefsFilePath());
5857
UserPrefs userPrefs = initPrefs(userPrefsStorage);
59-
AddressBookStorage addressBookStorage = new JsonAddressBookStorage(userPrefs.getAddressBookFilePath());
60-
storage = new StorageManager(addressBookStorage, userPrefsStorage);
58+
// AddressBookStorage addressBookStorage = new JsonExpenseBookStorage(userPrefs.getAddressBookFilePath());
59+
// storage = new StorageManager(addressBookStorage, userPrefsStorage);
6160

6261
initLogging(config);
6362

@@ -76,21 +75,22 @@ public void init() throws Exception {
7675
private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
7776
Optional<ReadOnlyAddressBook> addressBookOptional;
7877
ReadOnlyAddressBook initialData;
79-
try {
80-
addressBookOptional = storage.readAddressBook();
81-
if (!addressBookOptional.isPresent()) {
82-
logger.info("Data file not found. Will be starting with a sample AddressBook");
83-
}
84-
initialData = addressBookOptional.orElseGet(SampleDataUtil::getSampleAddressBook);
85-
} catch (DataConversionException e) {
86-
logger.warning("Data file not in the correct format. Will be starting with an empty AddressBook");
87-
initialData = new AddressBook();
88-
} catch (IOException e) {
89-
logger.warning("Problem while reading from the file. Will be starting with an empty AddressBook");
90-
initialData = new AddressBook();
91-
}
92-
93-
return new ModelManager(initialData, userPrefs);
78+
// try {
79+
//// addressBookOptional = storage.readAddressBook();
80+
//// if (!addressBookOptional.isPresent()) {
81+
//// logger.info("Data file not found. Will be starting with a sample AddressBook");
82+
//// }
83+
//// initialData = addressBookOptional.orElseGet(SampleDataUtil::getSampleAddressBook);
84+
// } catch (DataConversionException e) {
85+
// logger.warning("Data file not in the correct format. Will be starting with an empty AddressBook");
86+
// initialData = new AddressBook();
87+
// } catch (IOException e) {
88+
// logger.warning("Problem while reading from the file. Will be starting with an empty AddressBook");
89+
// initialData = new AddressBook();
90+
// }
91+
return null;
92+
93+
// return new ModelManager(initialData, userPrefs);
9494
}
9595

9696
private void initLogging(Config config) {

src/main/java/seedu/address/commons/core/Messages.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
* Container for user visible messages.
55
*/
66
public class Messages {
7+
public static final String MESSAGE_INVALID_EXPENSE_DISPLAYED_INDEX = "The expense index provided is invalid";
8+
9+
710

811
public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command";
912
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";

src/main/java/seedu/address/logic/Logic.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,21 @@
88
import seedu.address.logic.commands.exceptions.CommandException;
99
import seedu.address.logic.parser.exceptions.ParseException;
1010
import seedu.address.model.ReadOnlyAddressBook;
11+
import seedu.address.model.person.Expense;
1112
import seedu.address.model.person.Person;
1213

1314
/**
1415
* API of the Logic components
1516
*/
1617
public interface Logic {
18+
19+
/** Returns an unmodifiable view of the filtered list of persons */
20+
ObservableList<Expense> getFilteredExpenseList();
21+
22+
23+
24+
25+
1726
/**
1827
* Executes the command and returns the result.
1928
* @param commandText The command as entered by the user.

src/main/java/seedu/address/logic/LogicManager.java

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010
import seedu.address.logic.commands.Command;
1111
import seedu.address.logic.commands.CommandResult;
1212
import seedu.address.logic.commands.exceptions.CommandException;
13-
import seedu.address.logic.parser.AddressBookParser;
13+
import seedu.address.logic.parser.ExpenseBookParser;
1414
import seedu.address.logic.parser.exceptions.ParseException;
15+
import seedu.address.model.AddressBook;
1516
import seedu.address.model.Model;
1617
import seedu.address.model.ReadOnlyAddressBook;
18+
import seedu.address.model.ReadOnlyExpenseBook;
19+
import seedu.address.model.person.Expense;
1720
import seedu.address.model.person.Person;
1821
import seedu.address.storage.Storage;
1922

@@ -26,44 +29,55 @@ public class LogicManager implements Logic {
2629

2730
private final Model model;
2831
private final Storage storage;
29-
private final AddressBookParser addressBookParser;
32+
private final ExpenseBookParser expenseBookParser;
3033

3134
/**
3235
* Constructs a {@code LogicManager} with the given {@code Model} and {@code Storage}.
3336
*/
3437
public LogicManager(Model model, Storage storage) {
3538
this.model = model;
3639
this.storage = storage;
37-
addressBookParser = new AddressBookParser();
40+
expenseBookParser = new ExpenseBookParser();
41+
}
42+
43+
44+
@Override
45+
public ObservableList<Expense> getFilteredExpenseList() {
46+
return model.getFilteredExpenseList();
3847
}
3948

49+
@Override
50+
public ObservableList<Person> getFilteredPersonList() {
51+
return null;
52+
}
53+
54+
4055
@Override
4156
public CommandResult execute(String commandText) throws CommandException, ParseException {
4257
logger.info("----------------[USER COMMAND][" + commandText + "]");
4358

4459
CommandResult commandResult;
45-
Command command = addressBookParser.parseCommand(commandText);
60+
Command command = expenseBookParser.parseCommand(commandText);
4661
commandResult = command.execute(model);
4762

48-
try {
49-
storage.saveAddressBook(model.getAddressBook());
50-
} catch (IOException ioe) {
51-
throw new CommandException(FILE_OPS_ERROR_MESSAGE + ioe, ioe);
52-
}
63+
// try {
64+
// storage.saveAddressBook(model.getAddressBook());
65+
// } catch (IOException ioe) {
66+
// throw new CommandException(FILE_OPS_ERROR_MESSAGE + ioe, ioe);
67+
// }
5368

5469
return commandResult;
5570
}
5671

57-
@Override
58-
public ReadOnlyAddressBook getAddressBook() {
59-
return model.getAddressBook();
72+
public ReadOnlyExpenseBook getExpenseBook() {
73+
return model.getExpenseBook();
6074
}
6175

62-
@Override
63-
public ObservableList<Person> getFilteredPersonList() {
64-
return model.getFilteredPersonList();
76+
public ReadOnlyAddressBook getAddressBook() {
77+
return new AddressBook();
6578
}
6679

80+
6781
@Override
6882
public Path getAddressBookFilePath() {
6983
return model.getAddressBookFilePath();

src/main/java/seedu/address/logic/commands/AddCommand.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ public AddCommand(Person person) {
5050
public CommandResult execute(Model model) throws CommandException {
5151
requireNonNull(model);
5252

53-
if (model.hasPerson(toAdd)) {
54-
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
55-
}
56-
57-
model.addPerson(toAdd);
53+
// if (model.hasPerson(toAdd)) {
54+
// throw new CommandException(MESSAGE_DUPLICATE_PERSON);
55+
// }
56+
//
57+
// model.addPerson(toAdd);
5858
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
5959
}
6060

0 commit comments

Comments
 (0)