diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/wardrobecollection/ClothingItem.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/wardrobecollection/ClothingItem.java new file mode 100644 index 00000000..154f5f7c --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/wardrobecollection/ClothingItem.java @@ -0,0 +1,72 @@ +package com.codedifferently.lesson16.wardobecollection; + +import java.util.ArrayList; + +public class ClothingItem { + // Member variables + private String name; + private String type; + private String color; + private String size; + private double value; + private PersonalCloset.Season season; + + // Constructor + public ClothingItem (String name, String type, String color, String size, double value, PersonalCloset.Season season) { + this.name = name; + this.type = type; + this.color = color; + this.size = size; + this.value = value; + this.season = season; + } + + // Getters and Setters + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public String getSize() { + return size; + } + + public void setSize(String size) { + this.size = size; + } + + public double getValue() { + return value; + } + + public void setValue(double value) { + this.value = value; + } + + public PersonalCloset.Season getSeason() { + return season; + } + + public void setSeason(PersonalCloset.Season season) { + this.season = season; + } +} \ No newline at end of file diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/wardrobecollection/PersonalCloset.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/wardrobecollection/PersonalCloset.java new file mode 100644 index 00000000..ea5c1a26 --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/wardrobecollection/PersonalCloset.java @@ -0,0 +1,170 @@ +package com.codedifferently.lesson16.wardobecollection; + +import java.util.List; +import java.util.Map; +import java.util.ArrayList; +import java.util.HashMap; + +// Personal closet class to represent persons wardrobe collection +public class PersonalCloset { + // Enum for seaons + public enum Season { + FALL, + WINTER, + SPRING, + SUMMER, + ALL_SEASON + } + + // Member variables + private String ownerName; + private int maxCapacity; + private double totalValue; + private boolean isOrganized; + private ArrayList items; + private HashMap seasonalItems; + + // Constructor for personal closet + public PersonalCloset (String ownerName, int maxCapacity) { + this.ownerName = ownerName; + this.maxCapacity = maxCapacity; + this.totalValue = 0.0; + this.isOrganized = false; + this.items = new ArrayList<>(); + this.seasonalItems = new HashMap<>(); + } + + // Core methods + + // Method adds item to closet + public boolean addItem(ClothingItem item) { + // If closet is full, cannot add item + if (items.size() >= maxCapacity) { + return false; + } + + // Adding item to closet and increasing total value of closet + items.add(item); + totalValue += item.getValue(); + + // Checks what season item is meant for and keeps track of number of items in that season + Season season = item.getSeason(); + seasonalItems.put(season, seasonalItems.getOrDefault(season, 0) + 1); + + // Returns true if item is added + return true; + } + + // Method removes item from closet + public void removeItem(ClothingItem item) throws ItemNotFoundException { + // If item is not in closet, throws an error + if (!items.contains(item)) { + throw new ItemNotFoundException("Item is not in closet."); + } + + // Remove item from closet and decreases toal value of closet + items.remove(item); + totalValue -= item.getValue(); + + // Grab clothing item based on season and decrease count + Season season = item.getSeason(); + seasonalItems.put(season, seasonalItems.get(season) - 1); + } + + + // Method creates outfit by selecting items based on the season + public List createOutfit(Season season) { + // Creating empty list that stores clothing items + List outfit = new ArrayList<>(); + + // Iterating through all items in the closet and grabbing item at index + for (int i = 0; i < items.size(); i++) { + ClothingItem item = items.get(i); + + // Check if clothing item matches particular season or is good for all seasons + if (item.getSeason() == season || item.getSeason() == Season.ALL_SEASON) { + //add item to list + outfit.add(item); + } + } + // Returns final list of clothing items in an outfit + return outfit; + } + + // Method organizes closet by type of item and color + public void organizeCloset() { + // Create a map where key is type of clothing and value is list of clothing items of that type + Map> organized = new HashMap<>(); + + + // Iterate through every item in closet + for (int i = 0; i < items.size(); i++) { + ClothingItem item = items.get(i); + String type = item.getType(); + + // Check if that type of clothing item is in the list + List itemList = organized.get(type); + if (itemList == null) { + // If it doesn’t exist, make a new list and put it in the map + itemList = new ArrayList<>(); + organized.put(type, itemList); + } + + // Adding items to list by type + itemList.add(item); + } + + // closet is organized + isOrganized = true; + } + + // method returns a map shwoing how many items are in closet based on season + public Map getSeasonalItem() { + return new HashMap<>(seasonalItems); + } + + // getters and setters + public String getOwnerName() { + return ownerName; + } + + public void setOwnerName(String ownerName) { + this.ownerName = ownerName; + } + + public int getMaxCapacity() { + return maxCapacity; + } + + public void setMaxCapacity(int maxCapacity) { + this.maxCapacity = maxCapacity; + } + + public double getTotalValue() { + return totalValue; + } + + public void setTotalValue(double totalValue) { + this.totalValue = totalValue; + } + + public boolean isOrganized () { + return isOrganized; + } + + public void setOrganized(boolean isOrganized) { + this.isOrganized = isOrganized; + } + + public List getItems() { + return new ArrayList<>(items); + } + + // Custom exception if item is not found in closet + public static class ItemNotFoundException extends Exception{ + public ItemNotFoundException(String message) { + super(message); + } + } +} + diff --git a/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/wardrobecollectiontest/PersonalClosetTest.java b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/wardrobecollectiontest/PersonalClosetTest.java new file mode 100644 index 00000000..9b54535b --- /dev/null +++ b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/wardrobecollectiontest/PersonalClosetTest.java @@ -0,0 +1,61 @@ +package com.codedifferently.lesson16; + +import org.junit.jupiter.api.Test; + +public class PersonalClosetTest { + PersonalCloset closet; + private Mop sesonalItems; + + @BeforeEach + void setUp() { + PersonalCloset = new PersonalCloset ("Karen", 4); + ClothingItem sweater = new ClothingItem("") + } + + @Test + public void testAddItemToEmptyCloset { + // Arrange + + // Act + + // Assert + } + +@Test +public void testaddItemToFullCloset { + // Arrange + + // Act + + // Assert + +} + +@Test +public void testremoveExistingItem { + // Arrange + + // Act + + // Assert +} + +@Test +public void testcreateOutfit { + // Arrange + + // Act + + // Assert +} + +@Test +public void testorganizeCloset { + // Arrange + + // Act + + // Assert +} + +} \ No newline at end of file diff --git a/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BankAccount.java b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BankAccount.java new file mode 100644 index 00000000..44e732f5 --- /dev/null +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BankAccount.java @@ -0,0 +1,131 @@ +package main.java.com.codedifferently.lesson17.bank; + +import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; +import java.util.Set; + +/** Represents a bank account. */ +public class BankAccount { + + private final Set owners; + private final String accountNumber; + private double balance; + private boolean isActive; + + /** + * Creates a new checking account. + * + * @param accountNumber The account number. + * @param owners The owners of the account. + * @param initialBalance The initial balance of the account. + */ + public BankAccount(String accountNumber, Set owners, double initialBalance) { + this.accountNumber = accountNumber; + this.owners = owners; + this.balance = initialBalance; + isActive = true; + } + + /** + * Gets the account number. + * + * @return The account number. + */ + public String getAccountNumber() { + return accountNumber; + } + + /** + * Gets the owners of the account. + * + * @return The owners of the account. + */ + public Set getOwners() { + return owners; + } + + /** + * Deposits funds into the account. + * + * @param amount The amount to deposit. + */ + public void deposit(double amount) throws IllegalStateException { + if (isClosed()) { + throw new IllegalStateException("Cannot deposit to a closed account"); + } + if (amount <= 0) { + throw new IllegalArgumentException("Deposit amount must be positive"); + } + balance += amount; + } + + /** + * Withdraws funds from the account. + * + * @param amount + * @throws InsufficientFundsException + */ + public void withdraw(double amount) throws InsufficientFundsException { + if (isClosed()) { + throw new IllegalStateException("Cannot withdraw from a closed account"); + } + if (amount <= 0) { + throw new IllegalStateException("Withdrawal amount must be positive"); + } + if (balance < amount) { + throw new InsufficientFundsException("Account does not have enough funds for withdrawal"); + } + balance -= amount; + } + + /** + * Gets the balance of the account. + * + * @return The balance of the account. + */ + public double getBalance() { + return balance; + } + + /** Closes the account. */ + public void closeAccount() throws IllegalStateException { + if (balance > 0) { + throw new IllegalStateException("Cannot close account with a positive balance"); + } + isActive = false; + } + + /** + * Checks if the account is closed. + * + * @return True if the account is closed, otherwise false. + */ + public boolean isClosed() { + return !isActive; + } + + @Override + public int hashCode() { + return accountNumber.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof CheckingAccount other) { + return accountNumber.equals(other.accountNumber); + } + return false; + } + + @Override + public String toString() { + return "CheckingAccount{" + + "accountNumber='" + + accountNumber + + '\'' + + ", balance=" + + balance + + ", isActive=" + + isActive + + '}'; + } +} diff --git a/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BusinessCheckingAccount.java b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BusinessCheckingAccount.java new file mode 100644 index 00000000..f1060d64 --- /dev/null +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BusinessCheckingAccount.java @@ -0,0 +1,10 @@ +package com.codedifferently.lesson17.bank; + +import java.util.Set; + +public class BusinessCheckingAccount extends CheckingAccount { + public BusinessCheckingAccount( + String accountNumber, Set owners, double initialBalance) { + super(accountNumber, owners, initialBalance); + } +} diff --git a/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/SavingsAccount.java b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/SavingsAccount.java new file mode 100644 index 00000000..3556d716 --- /dev/null +++ b/lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/SavingsAccount.java @@ -0,0 +1,9 @@ + + +public class SavingsAccount extends BankAccount { + + @Override + public boolean canWithdrawWithCheck() { + return false; + } +} \ No newline at end of file