Skip to content

Commit 279db26

Browse files
author
yuming7144
committed
Modify storage“
1 parent 12cfb0e commit 279db26

14 files changed

Lines changed: 221 additions & 230 deletions

src/main/java/seedu/address/ExpenseMainApp.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
import seedu.address.logic.Logic;
1717
import seedu.address.logic.LogicManager;
1818
import seedu.address.model.*;
19-
import seedu.address.storage.Storage;
20-
import seedu.address.storage.UserPrefsStorage;
19+
import seedu.address.storage.*;
2120
import seedu.address.ui.Ui;
2221
import seedu.address.ui.UiManager;
2322

@@ -44,7 +43,10 @@ public void init() throws Exception {
4443
AppParameters appParameters = AppParameters.parse(getParameters());
4544
config = initConfig(appParameters.getConfigPath());
4645

47-
storage = null;
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);
4850

4951
initLogging(config);
5052

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/storage/AddressBookStorage.java renamed to src/main/java/seedu/address/storage/ExpenseBookStorage.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,41 @@
55
import java.util.Optional;
66

77
import seedu.address.commons.exceptions.DataConversionException;
8-
import seedu.address.model.ReadOnlyAddressBook;
8+
import seedu.address.model.ReadOnlyExpenseBook;
99

1010
/**
11-
* Represents a storage for {@link seedu.address.model.AddressBook}.
11+
* Represents a storage for {@link seedu.address.model.ExpenseBook}.
1212
*/
13-
public interface AddressBookStorage {
13+
public interface ExpenseBookStorage {
1414

1515
/**
1616
* Returns the file path of the data file.
1717
*/
18-
Path getAddressBookFilePath();
18+
Path getExpenseBookFilePath();
1919

2020
/**
21-
* Returns AddressBook data as a {@link ReadOnlyAddressBook}.
21+
* Returns ExpenseBook data as a {@link ReadOnlyExpenseBook}.
2222
* Returns {@code Optional.empty()} if storage file is not found.
2323
* @throws DataConversionException if the data in storage is not in the expected format.
2424
* @throws IOException if there was any problem when reading from the storage.
2525
*/
26-
Optional<ReadOnlyAddressBook> readAddressBook() throws DataConversionException, IOException;
26+
Optional<ReadOnlyExpenseBook> readExpenseBook() throws DataConversionException, IOException;
2727

2828
/**
29-
* @see #getAddressBookFilePath()
29+
* @see #getExpenseBookFilePath()
3030
*/
31-
Optional<ReadOnlyAddressBook> readAddressBook(Path filePath) throws DataConversionException, IOException;
31+
Optional<ReadOnlyExpenseBook> readExpenseBook(Path filePath) throws DataConversionException, IOException;
3232

3333
/**
34-
* Saves the given {@link ReadOnlyAddressBook} to the storage.
35-
* @param addressBook cannot be null.
34+
* Saves the given {@link ReadOnlyExpenseBook} to the storage.
35+
* @param expenseBook cannot be null.
3636
* @throws IOException if there was any problem writing to the file.
3737
*/
38-
void saveAddressBook(ReadOnlyAddressBook addressBook) throws IOException;
38+
void saveExpenseBook(ReadOnlyExpenseBook expenseBook) throws IOException;
3939

4040
/**
41-
* @see #saveAddressBook(ReadOnlyAddressBook)
41+
* @see #saveExpenseBook(ReadOnlyExpenseBook)
4242
*/
43-
void saveAddressBook(ReadOnlyAddressBook addressBook, Path filePath) throws IOException;
43+
void saveExpenseBook(ReadOnlyExpenseBook expenseBook, Path filePath) throws IOException;
4444

4545
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package seedu.address.storage;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashSet;
5+
import java.util.List;
6+
import java.util.Set;
7+
import java.util.stream.Collectors;
8+
9+
import com.fasterxml.jackson.annotation.JsonCreator;
10+
import com.fasterxml.jackson.annotation.JsonProperty;
11+
12+
import seedu.address.commons.exceptions.IllegalValueException;
13+
import seedu.address.model.person.*;
14+
import seedu.address.model.tag.Tag;
15+
16+
17+
/**
18+
* Jackson-friendly version of {@link Person}.
19+
*/
20+
class JsonAdaptedExpense {
21+
22+
public static final String MISSING_FIELD_MESSAGE_FORMAT = "Expense's %s field is missing!";
23+
24+
private final String amount;
25+
private final String date;
26+
private final String category;
27+
private final String description;
28+
29+
/**
30+
* Constructs a {@code JsonAdaptedPerson} with the given person details.
31+
*/
32+
@JsonCreator
33+
public JsonAdaptedExpense(@JsonProperty("amount") String amount, @JsonProperty("date") String date,
34+
@JsonProperty("category") String category,
35+
@JsonProperty("description") String description) {
36+
this.amount = amount;
37+
this.date = date;
38+
this.category = category;
39+
this.description = description;
40+
}
41+
42+
/**
43+
* Converts a given {@code Expense} into this class for Jackson use.
44+
*/
45+
public JsonAdaptedExpense(Expense source) {
46+
amount = source.getAmount().value.toString();
47+
date = source.getDate().date;
48+
category = source.getCategory().categoryName;
49+
description = source.getDescription().value;
50+
}
51+
52+
/**
53+
* Converts this Jackson-friendly adapted person object into the model's {@code Person} object.
54+
*
55+
* @throws IllegalValueException if there were any data constraints violated in the adapted person.
56+
*/
57+
public Expense toModelType() throws IllegalValueException {
58+
final List<Tag> personTags = new ArrayList<>();
59+
60+
if (amount == null) {
61+
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Amount.class.getSimpleName()));
62+
}
63+
// if (!Amount.isValidAmount(amount)) {
64+
// throw new IllegalValueException(Name.MESSAGE_CONSTRAINTS);
65+
// }
66+
final Amount modelAmount = new Amount(Double.valueOf(amount));
67+
68+
if (date == null) {
69+
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Date.class.getSimpleName()));
70+
}
71+
// if (!Phone.isValidPhone(phone)) {
72+
// throw new IllegalValueException(Phone.MESSAGE_CONSTRAINTS);
73+
// }
74+
final Date modelDate = new Date(date);
75+
76+
if (category == null) {
77+
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Category.class.getSimpleName()));
78+
}
79+
// if (!Email.isValidEmail(email)) {
80+
// throw new IllegalValueException(Email.MESSAGE_CONSTRAINTS);
81+
// }
82+
final Category modelCategory = new Category(category);
83+
84+
if (description == null) {
85+
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Description.class.getSimpleName()));
86+
}
87+
final Description modelDescription = new Description(description);
88+
return new Expense(modelAmount, modelDate, modelCategory, modelDescription);
89+
}
90+
91+
}

src/main/java/seedu/address/storage/JsonAdaptedPerson.java

Lines changed: 0 additions & 106 deletions
This file was deleted.

0 commit comments

Comments
 (0)