Skip to content

Commit

Permalink
Integrate AutoExpenditure with Date and Frequency Classes (#105)
Browse files Browse the repository at this point in the history
* Add Frequency class

* Add in basic functions for Date integration
  • Loading branch information
SwampertX authored and Dandford committed Oct 23, 2019
1 parent 8f72e34 commit 780e96d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
26 changes: 12 additions & 14 deletions src/main/java/seedu/address/model/person/AutoExpense.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Set;

import seedu.address.model.tag.Tag;
import seedu.address.model.util.Frequency;

/**
* An expense factory, basically.
Expand All @@ -11,14 +12,11 @@ public class AutoExpense extends Entry {

private static final String ENTRY_TYPE = "AutoExpense";
private final Date lastTime;
private final String freq;
private final Frequency freq;

// should take in desc, freq, amt, tags
// TODO: freq as an enum or a class:
// monthly, weekly, daily, fortnightly
public AutoExpense(Description desc, Amount amount, Set<Tag> tags, String freq) {
super(desc, new Date("stub time"), amount, tags);
this.lastTime = new Date("right now lmao");
public AutoExpense(Description desc, Amount amount, Set<Tag> tags, Frequency freq) {
super(desc, Date.now(), amount, tags);
this.lastTime = this.getDate();
this.freq = freq;
}

Expand All @@ -32,9 +30,7 @@ public String getType() {
* @return
*/
public Date getNextTime() {
// TODO: wishful thinking
// return lastTime + freq;
return lastTime;
return lastTime.plus(freq);
}

/**
Expand All @@ -47,11 +43,11 @@ public Date getLastTime() {
}

/**
* Gets the frequency. TODO: currently a string.
* Gets the frequency.
*
* @return
*/
public String getFrequency() {
public Frequency getFrequency() {
return freq;
}

Expand All @@ -73,15 +69,17 @@ public boolean equals(Object other) {
}

AutoExpense otherAutoExpense = (AutoExpense) other;
return otherAutoExpense.getDesc().equals(getDesc()) && otherAutoExpense.getAmount().equals(getAmount())
return otherAutoExpense.getDesc().equals(getDesc())
&& otherAutoExpense.getAmount().equals(getAmount())
&& otherAutoExpense.getTags().equals(getTags())
&& otherAutoExpense.getFrequency().equals(getFrequency());
}

@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append(ENTRY_TYPE + ": ").append(getDesc()).append(" Amount: ").append(getAmount()).append(" Tags: ");
builder.append(ENTRY_TYPE + ": ").append(getDesc()).append(" Amount: ").append(getAmount())
.append(" Tags: ");
getTags().forEach(builder::append);
builder.append("( every " + freq + ", last updated:" + getLastTime() + ")");
return builder.toString();
Expand Down
26 changes: 18 additions & 8 deletions src/main/java/seedu/address/model/person/Date.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;

import seedu.address.model.util.Frequency;

/**
* Represents a Person's name in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidDescription(String)}
* Represents a Person's name in the address book. Guarantees: immutable; is
* valid as declared in {@link #isValidDescription(String)}
*/
public class Date {

public static final String MESSAGE_CONSTRAINTS =
"Names should only contain alphanumeric characters and spaces, and it should not be blank";

/*
* The first character of the address must not be a whitespace,
* otherwise " " (a blank string) becomes a valid input.
* The first character of the address must not be a whitespace, otherwise " " (a
* blank string) becomes a valid input.
*/
//public static final String VALIDATION_REGEX = "[\\p{Alnum}][\\p{Alnum} ]*";

private static final DateTimeFormatter INPUTFORMATTER = new DateTimeFormatterBuilder()
.appendOptional(DateTimeFormatter.ofPattern("yyyy MM dd"))
Expand All @@ -43,6 +44,7 @@ public class Date {

/**
* Converts String to LocalDate
*
* @param date in the format yyyy mm dd.
*/
public Date(String date) {
Expand All @@ -55,7 +57,7 @@ public Date(String date) {

public Date(LocalDate date) {
requireNonNull(date);
//checkArgument(isValidDescription(desc), MESSAGE_CONSTRAINTS);
// checkArgument(isValidDescription(desc), MESSAGE_CONSTRAINTS);
this.date = date;
parseDate();
}
Expand All @@ -69,13 +71,21 @@ private void parseDate() {
*/
public static boolean isValidDescription(String test) {
return test == null; // put this for now
//return test.matches(VALIDATION_REGEX);
// return test.matches(VALIDATION_REGEX);
}

public LocalDate getDate() {
return date;
}

public static Date now() {
return new Date(LocalDate.now());
}

public Date plus(Frequency freq) {
return new Date(this.getDate().plus(freq.getPeriod()));
}

@Override
public String toString() {
return date.toString();
Expand All @@ -85,7 +95,7 @@ public String toString() {
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof Date // instanceof handles nulls
&& fullTime.equals(((Date) other).fullTime)); // state check
&& fullTime.equals(((Date) other).fullTime)); // state check
}

@Override
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/seedu/address/model/util/Frequency.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package seedu.address.model.util;

import java.time.Period;
import java.time.temporal.TemporalAmount;

/**
* An enum containing commonly used time periods as frequency.
*/
public enum Frequency {
DAILY(Period.ofDays(1)), WEEKLY(Period.ofDays(7)), FORTNIGHTLY(Period.ofDays(14)), MONTHLY(
Period.ofMonths(1)), QUARTERLY(Period.ofMonths(3)), ANUALLY(Period.ofYears(1));

private final TemporalAmount period;

Frequency(TemporalAmount period) {
this.period = period;
}

public TemporalAmount getPeriod() {
return period;
}
}

0 comments on commit 780e96d

Please sign in to comment.