forked from nus-cs2113-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from yyangdaa/yangda
Implement Splitter
- Loading branch information
Showing
12 changed files
with
326 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package seedu.budgetbuddy; | ||
|
||
public class SplitExpense { | ||
private final String amount; | ||
private final String description; | ||
private final String numberOfPeople; | ||
|
||
public SplitExpense(String amount, String numberOfPeople, String description) { | ||
this.amount = amount; | ||
this.numberOfPeople = numberOfPeople; | ||
this.description = description; | ||
} | ||
|
||
public String getNumberOfPeople() { | ||
return numberOfPeople; | ||
} | ||
|
||
public String getAmount() { | ||
return amount; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public double calculateAmountPerPerson() { | ||
double amountValue = Double.parseDouble(amount); | ||
double numberOfPeopleValue = Double.parseDouble(numberOfPeople); | ||
return amountValue / numberOfPeopleValue; | ||
} | ||
|
||
public Boolean isExpenseSettled() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Number of People: " + numberOfPeople + " Amount: " + amount + " Description: " + | ||
description + " Amount per person: " + calculateAmountPerPerson(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package seedu.budgetbuddy; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import seedu.budgetbuddy.exception.BudgetBuddyException; | ||
|
||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
public class SplitExpenseList { | ||
private static final Logger LOGGER = Logger.getLogger(SplitExpenseList.class.getName()); | ||
protected ArrayList <SplitExpense> splitexpenses; | ||
public SplitExpenseList(ArrayList<SplitExpense> splitexpenses){ | ||
this.splitexpenses = splitexpenses; | ||
} | ||
|
||
public SplitExpenseList() { | ||
this.splitexpenses = new ArrayList<>(); | ||
} | ||
|
||
public int size() { | ||
return splitexpenses.size(); | ||
} | ||
|
||
public List<SplitExpense> getSplitExpenses() { | ||
return splitexpenses; | ||
} | ||
|
||
public void listSplitExpenses() { | ||
LOGGER.info("Listing splitexpenses..."); | ||
|
||
try { | ||
System.out.println("Split Expenses: "); | ||
for (int i = 0; i < splitexpenses.size(); i++) { | ||
SplitExpense splitexpense = splitexpenses.get(i); | ||
|
||
if (splitexpense == null) { | ||
LOGGER.warning("Expense object at index " + i + " is null"); | ||
continue; | ||
} | ||
System.out.print(i+1 + " | "); | ||
System.out.print("Amount: " + splitexpense.getAmount()); | ||
System.out.print(" Number of People: " + splitexpense.getNumberOfPeople()); | ||
System.out.print(" Description: " + splitexpense.getDescription()); | ||
System.out.println(" Amount per person: " + splitexpense.calculateAmountPerPerson()); | ||
} | ||
System.out.println("-----------------------------------------------------------------------------"); | ||
|
||
} catch (Exception e) { | ||
LOGGER.log(Level.SEVERE, "An error occurred while listing expenses.", e); | ||
} | ||
} | ||
|
||
public void addSplitExpense(String amount, String numberOfPeople, String description ) throws BudgetBuddyException { | ||
assert amount != null : "Amount should not be null"; | ||
assert description != null : "Description should not be null"; | ||
LOGGER.info("Adding split expense..."); | ||
|
||
double amountDouble; | ||
try{ | ||
amountDouble = Double.parseDouble(amount); | ||
} catch (NumberFormatException e) { | ||
throw new BudgetBuddyException("Invalid amount format. Amount should be a number."); | ||
} | ||
|
||
if (amountDouble < 0){ | ||
throw new BudgetBuddyException("Expenses should not be negative."); | ||
} | ||
|
||
SplitExpense splitexpense = new SplitExpense(amount, numberOfPeople, description); | ||
splitexpenses.add(splitexpense); | ||
} | ||
} |
Oops, something went wrong.