Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • team13/tdat1006
  • eivindrt/tdat1006
  • alexholt/idatt1002
  • elinang/idatt1002
  • surya/idatt1002
  • kjellit/idata-1002-demo-project
  • eskillr/idatt1002
  • G1-06/idatt-1002-2022-1-06
  • emillaa/idatt1002
  • andreksv/idatt-1002-2023-9
  • melane/idatt-1002-2023-5
  • ramtinfs/idatt1002
  • surya/idatx1005
  • itatt1005_2024_group15/purchase-planner
  • systemutvikling-gruppe-19/idatt-1005-2024-group-19
15 results
Show changes
Commits on Source (22)
Showing
with 469 additions and 151 deletions
package no.ntnu.idatt1002.demo;
import no.ntnu.idatt1002.demo.view.MyWindow;
/**
* Use this class to start the application
* @author nilstes
*/
public class MyApp {
/**
* Main method for my application
*/
public static void main(String[] args) throws Exception {
MyWindow window = new MyWindow("The Window");
window.setVisible(true);
}
}
package no.ntnu.idatt1002.demo.data;
public class Expense extends Item{
private ExpenseCategory category;
/**
* This constructor uses the super constructor to set the fields for 'amount' and 'recurring' and then sets the
* category. A IllegalArgumentException from this constructor if the category is left blank. The description
* field is left to the default blank string.
*
* @param amount The amount of the current Expense object as a double.
* @param recurring True if the current Expense repeats at regular intervals.
* @param category The category to which the Expense belongs to, provided as a value of ExpenseCategory.
* @param date The date of the Expense at format "dd.mm.yy".
*/
public Expense(double amount, boolean recurring, ExpenseCategory category, String date) {
super(amount, recurring, date);
if(category == null) {
throw new IllegalArgumentException("The income must belong to a category.");
}
this.category = category;
}
/**
* This constructor uses the super constructor to set the fields for 'amount', 'description' and 'recurring'
* and then sets the category. A IllegalArgumentException from this constructor if the category is left blank.
* @param description A description of the Expense as a String.
* @param amount The amount of the current Expense object as a double.
* @param recurring True if the current income repeats at regular intervals.
* @param category The category to which the Expense belongs to, provided as a value of ExpenseCategory
* @param date The date of the Expense at format "dd.mm.yy".
*/
public Expense(String description, double amount, boolean recurring, ExpenseCategory category, String date) {
super(description, amount, recurring, date);
if(category == null) {
throw new IllegalArgumentException("The income must belong to a category.");
}
this.category = category;
}
/**
* The method returns the category to which the Expense belongs as a value of the ExpenseCategory enum class.
* @return The category the Expense belongs to as a value of the ExpenseCategory enum class.
*/
public ExpenseCategory getCategory() {
return category;
}
/**
* The method sets the category of an expense to a value of the ExpenseCategory enum class.
* @param expenseCategory The category to which the expense belongs as a value of the ExpenseCategory enum.
*/
public void setCategory(ExpenseCategory expenseCategory) {
if(expenseCategory == null) {
throw new IllegalArgumentException("The income must belong to a category.");
}
this.category = expenseCategory;
}
}
package no.ntnu.idatt1002.demo.data;
public enum ExpenseCategory {
FOOD,
CLOTHES,
BOOKS,
OTHER,
}
package no.ntnu.idatt1002.demo.data;
import java.util.ArrayList;
public class ExpenseOverview {
private ArrayList<Expense> expenses;
public ExpenseOverview(){
this.expenses = new ArrayList<>();
}
}
package no.ntnu.idatt1002.demo.data;
/**
* The Income class inherits from the Item class. The Item class additionally has a private field for an
* enum value of IncomeCategory.
*
* @author Hanne-Sofie
*/
public class Income extends Item{
private IncomeCategory category;
/**
* This constructor uses the super constructor to set the fields for 'amount' and 'recurring' and then sets the
* category. A IllegalArgumentException from this constructor if the category is left blank. The description
* field is left to the default blank string.
*
* @param amount The amount of the current income object as a double.
* @param recurring True if the current income repeats at regular intervals.
* @param category The category to which the Income belongs to, provided as a value of IncomeCategory.
* @param date The date of the Income at format "dd.mm.yy".
*/
public Income(double amount, boolean recurring, IncomeCategory category, String date) {
super(amount, recurring, date);
if(category == null) {
throw new IllegalArgumentException("The income must belong to a category.");
}
this.category = category;
}
/**
* This constructor uses the super constructor to set the fields for 'amount', 'description' and 'recurring'
* and then sets the category. A IllegalArgumentException from this constructor if the category is left blank.
* @param description A description of the income as a String.
* @param amount The amount of the current income object as a double.
* @param recurring True if the current income repeats at regular intervals.
* @param category The category to which the income belongs to, provided as a value of IncomeCategory.
* @param date The date of the Income at format "dd.mm.yy".
*/
public Income(String description, double amount, boolean recurring, IncomeCategory category, String date) {
super(description, amount, recurring, date);
if(category == null) {
throw new IllegalArgumentException("The income must belong to a category.");
}
this.category = category;
}
/**
* The method returns the category to which the Income belongs as a value of the IncomeCategory enum.
* @return The category to which the Income belongs to as a value of the IncomeCategory enum.
*/
public IncomeCategory getCategory() {
return category;
}
/**
* The method sets the category of an item to a value of the Category enum class.
* @param category The category to which the Income belongs to as a value of the IncomeCategory enum.
*/
public void setCategory(IncomeCategory category) {
if(category == null) {
throw new IllegalArgumentException("The income must belong to a category.");
}
this.category = category;
}
}
package no.ntnu.idatt1002.demo.data;
public enum IncomeCategory {
SALARY,
STUDENT_LOAN,
GIFT
}
package no.ntnu.idatt1002.demo.data;
import java.util.ArrayList;
public class IncomeOverview extends ItemOverview {
private ArrayList<Income> income;
public IncomeOverview(){
this.income = new ArrayList<>();
}
}
package no.ntnu.idatt1002.demo.data;
/**
* The Item class represents a good or service purchased in real life. The item belongs to a category and
* has an amount, description and a date. The description may be left blank, but the Item must belong to a category and must
* have a price.
* @author HanneSofie
*
*/
public class Item {
private String description = "";
private double amount;
private boolean recurring;
private String date; // Format example: 09.08.23
/**
* The constructor of a new Item object takes in an amount as a double. If the amount is left blank, an
* IllegalArgumentException is thrown.
* @param amount price of an Item as a float.
*/
public Item (double amount, boolean recurring, String date){
if(amount <= 1.0f || date.isBlank()) {
throw new IllegalArgumentException("Both amount and date must be provided.");
} else {
this.amount = amount;
this.recurring = recurring;
this.date = date;
}
}
/**
* The constructor instantiates a new Item object with a category, a description and a price as arguments. It
* overloads the first constructor to set the category and price and then sets the description of the item.
* If either 0category or price is left blank, an IllegalArgumentException is thrown.
* @param description A description of the item as a String.
* @param amount The price of the item as a float.
*/
public Item (String description, double amount, boolean recurring, String date){
this(amount, recurring, date);
this.description=description;
}
/**
* The method returns the description of the given Item object as a String.
* @return The description of the Item as a String.
*/
public String getDescription() {
return description;
}
/**
* The method sets the description of the Item object equal to the passed String. The String may be empty.
* @param description The new description of the Item object as a String.
*/
public void setDescription(String description) {
this.description = description;
}
/**
* The method returns the price of the given Item object as a float.
* @return The amount of the Item as a float.
*/
public double getAmount() {
return amount;
}
/**
* The method changes the price of the given Item to the passed price as a float.
* @param amount The new price of the Item as a float.
*/
public void setAmount(double amount) {
if(amount <= 1.0f ) {
throw new IllegalArgumentException("A positive amount must be provided.");
}
this.amount = amount;
}
/**
* The method returns true if the transaction is recurring, false otherwise.
* @return True if the transaction is a recurring one, false otherwise.
*/
public boolean isRecurring() {
return recurring;
}
/**
* The method changes the boolean value of the 'recurring' field of the Item object.
* @param recurring A boolean value being true if the Item is recurring and false otherwise.
*/
public void setRecurring(boolean recurring) {
this.recurring = recurring;
}
/**
* The method returns the date of the item object (income/expense).
* @return The date of the transaction.
*/
public String getDate() {
return date;
}
/**
* Sets the date field of the Item object to a new String provided as an argument.
* @param newDate The new date with which to record the Item.
*/
public void setDate(String newDate) {
if(date.isBlank()) {
throw new IllegalArgumentException("A date must be provided.");
}
this.date = newDate;
}
/* @Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (obj.getClass() != this.getClass()) {
return false;
}
final Item otherItem = (Item) obj;
return true;
*//*return Objects.equals(true);*//*
}*/
}
package no.ntnu.idatt1002.demo.data;
import java.util.ArrayList;
/**
* ItemOverview is a class for storing and getting
* information on items. It is meant for storing EITHER
* Income or Expense
*/
public abstract class ItemOverview {
ArrayList<Item> items;
/**
* An "empty" class constructor.
*/
public ItemOverview(){
this.items = new ArrayList<>(); //ArrayList for storing item´s
}
/**
* Class constructor that takes in an ArrayList of Item´s as argument
* @param items An ArrayList of the Item´s you want to overview
*/
public ItemOverview(ArrayList<Item> items){
this.items = items;
}
/**
* Get an ArrayList of every item.
* @return item ArrayList.
*/
public ArrayList<Item> getItems() {
return items;
}
/**
* Add an Item object to items.
* @param newItem The Item you want to add.
*/
public void addItem(Item newItem){
if(items.contains(newItem)){
throw new IllegalArgumentException("This item is already registered");
}
items.add(newItem);
}
/**
* Get the sum of all Item´s in items
* @return Sum of all Item´s
*/
public double getTotalSum(){
return items.stream().map(Item::getAmount).mapToDouble(Double::doubleValue).sum();
}
}
package no.ntnu.idatt1002.demo.data;
import java.util.Date;
import java.util.List;
/**
* This is just a simple Java-bean
* @author nilstes
*/
public class MyEntity {
private String id;
private String name;
public MyEntity() {
}
public MyEntity(String id, String name) {
this.id = id;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
package no.ntnu.idatt1002.demo.repo;
import no.ntnu.idatt1002.demo.data.MyEntity;
import java.util.Arrays;
import java.util.List;
/**
* Repository for the MyEntity-entity
*
* @author nilstes
*/
public class MyEntityRepo {
/**
* Get object with given id
*
* @param id the entity id
* @return an instance of MyEntity
*/
public MyEntity getMyEntity(String id) {
// Get connection (maybe use pool?)
// Do some SQL
// Return som real data
return new MyEntity("id", "name");
}
public List<MyEntity> findMyEntities(String someParameter) {
// Get connection (maybe use pool?)
// Do some SQL
// Return som real data
return Arrays.asList(new MyEntity("id1", "name1"), new MyEntity("id2", "name2"));
}
public void addMyEntity(MyEntity obj) {
// Get connection (maybe use pool?)
// Do some SQL
}
public void deleteMyEntity(String id) {
// Get connection (maybe use pool?)
// Do some SQL
}
}
package no.ntnu.idatt1002.demo.view;
import no.ntnu.idatt1002.demo.repo.MyEntityRepo;
import no.ntnu.idatt1002.demo.data.MyEntity;
import java.awt.*;
import javax.swing.*;
/**
* Main window for my application!
*
* @author nilstes
*/
public class MyWindow extends JFrame {
/**
* Constructor for window
*
* @param title Title ow the window
* @see Image
*/
public MyWindow(String title) {
super(title);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(new GridLayout(1, 1, 1, 1));
MyEntityRepo dao = new MyEntityRepo();
MyEntity object = dao.getMyEntity("id");
add(new JLabel(object.getName()));
pack();
}
}
package no.ntnu.idatt1002.demo.data;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class ExpenseTest {
@Test
@DisplayName("The Expense constructor throws exceptions when it should.")
void constructorThrows(){
assertThrows(IllegalArgumentException.class, () -> new Expense("description", 8.5f, false, null, "03.03.23"));
assertThrows(IllegalArgumentException.class, () -> new Expense("description", -10.0f, false, ExpenseCategory.BOOKS, "03.03.23"));
};
@Test
@DisplayName("The Expense constructor does not throw exceptions when it should not.")
void constructorDoesThrow() {
assertDoesNotThrow(() -> new Expense("description", 59.9f, false, ExpenseCategory.BOOKS, "03.03.23"));
}
}
\ No newline at end of file
package no.ntnu.idatt1002.demo.data;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class IncomeOverviewTest {
IncomeOverview incomeOverview;
@BeforeEach
public void createItemOverView(){
incomeOverview = new IncomeOverview();
}
@Test
@DisplayName("addItem method throws exception when it should")
void addItemThrows(){
Income income = new Income("description", 59.9f, false, IncomeCategory.SALARY, "03.03.23");
assertThrows(IllegalArgumentException.class, () -> {incomeOverview.addItem(income); incomeOverview.addItem(income);});
}
@Test
@DisplayName("addItem method does not throw exception when it should not")
void addItemDoesNotThrow(){
Income expense = new Income("description", 59.9f, false, IncomeCategory.GIFT, "03.03.23");
Income income = new Income("anotherDescription", 6.5f, true, IncomeCategory.SALARY, "02.03.23");
assertDoesNotThrow(() -> {incomeOverview.addItem(expense); incomeOverview.addItem(income);});
}
@Test
@DisplayName("getTotalSum method gives correct amount")
void getTotalSumCorrectAmount(){
incomeOverview.addItem(new Income("description1", 59.9f, false, IncomeCategory.SALARY, "03.03.23"));
incomeOverview.addItem(new Income("description2", 62.4f, true, IncomeCategory.GIFT, "01.02.21"));
incomeOverview.addItem(new Income("description3", 9.81f, false, IncomeCategory.SALARY, "05.07.23"));
double totalIncome = 59.9f + 62.4f + 9.81f;
assertEquals(Math.round(incomeOverview.getTotalSum()), Math.round(totalIncome));
}
}
\ No newline at end of file
package no.ntnu.idatt1002.demo.data;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class IncomeTest {
@Test
@DisplayName("The Income constructor throws exceptions when it should.")
void constructorThrows(){
assertThrows(IllegalArgumentException.class, () -> new Income("description", 8.5f, false, null, "03.03.23"));
assertThrows(IllegalArgumentException.class, () -> new Income("description", -10.0f, false, IncomeCategory.SALARY, "03.03.23"));
};
@Test
@DisplayName("The Income constructor does not throw exceptions when it should not.")
void constructorDoesThrow() {
assertDoesNotThrow(() -> new Income("description", 59.9f, false, IncomeCategory.SALARY, "03.03.23"));
}
}
\ No newline at end of file
package no.ntnu.idatt1002.demo.data;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class ItemTest {
@Test
@DisplayName("The Item constructor throws exceptions when it should.")
void constructorThrows(){
assertThrows(IllegalArgumentException.class, () -> new Item("description", 0f, false, "03.03.23"));
assertThrows(IllegalArgumentException.class, () -> new Item("description", -10.0f, false, "03.03.23"));
assertThrows(IllegalArgumentException.class, () -> new Item("description", -10.0f, false, ""));
};
@Test
@DisplayName("The Item constructor does not throw exceptions when it should not.")
void constructorDoesThrow() {
assertDoesNotThrow(() -> new Item("description", 59.9f, false, "03.03.23"));
}
}
\ No newline at end of file
package no.ntnu.idatt1002.demo.repo;
import no.ntnu.idatt1002.demo.data.MyEntity;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class MyEntityRepoTest {
@Test
public void testThatWeCanReadMyEntityFromDatabase() {
MyEntity e = new MyEntityRepo().getMyEntity("id");
assertEquals(e.getName(), "name");
}
}
\ No newline at end of file