From a88b77b7f2f63e4908c507ad6e0ed449cea3e0c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CKarensprojects22=E2=80=9D?= Date: Sat, 12 Apr 2025 19:03:30 -0400 Subject: [PATCH 01/11] feat: adds PersonalCloset and PersonalClosetTest file --- .../wardrobecollection/PersonalCloset.java | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/wardrobecollection/PersonalCloset.java 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..56915025 --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/wardrobecollection/PersonalCloset.java @@ -0,0 +1,92 @@ +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.seaonalItems = new HashMap<>(); + } + + //core methods + + // adds item to closet + public boolean addItem(ClothingItem item) { + return false; + } + + // removes item from closet + public void removeItem(ClothingItem item) { + // throws exception if item is not found in closet + public static class ItemNotFoundException extends Exception{ + + } + } + + // creates outfit by selecting items based on the season + public List createOutfit(Season season) { + return new ArrayList<>(); + } + + // organizes closet by type of item and color + public void organizeCloset() { + } + + // calculates amount of clothing items are in closet based on season + public Map getSeasonalItem() { + return new HashMap<>(); + } + + // getters and setters + public String getOwnerName() { + return ownerName; + } + + public void setOwnerName() { + this.ownerName = ownerName; + } + + public int getMaxCapacity() { + return maxCapacity; + } + + public double getTotalValue() { + return totalValue; + } + + public boolean isOrganized () { + return isOrganized; + } + + public List getItems() { + return new ArrayList<>(items); + } + +} + From 1903ec2fbb3f2182019e26b7f2927537fd8c250e Mon Sep 17 00:00:00 2001 From: Karen Alabi Date: Sat, 12 Apr 2025 20:21:43 -0400 Subject: [PATCH 02/11] feat: creates basic PersonalCloset class structure with enums, variables, and methods --- .../lesson16/wardrobecollection/PersonalCloset.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 56915025..6343a375 100644 --- 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 @@ -85,7 +85,7 @@ public boolean isOrganized () { } public List getItems() { - return new ArrayList<>(items); + return new ArrayList<>(items); } } From 4c78dbb2c5671c41281bbc732698d5afa6110dc8 Mon Sep 17 00:00:00 2001 From: Karen Alabi Date: Sun, 13 Apr 2025 06:34:09 -0400 Subject: [PATCH 03/11] feat: implments add item method with logic for closet item, closet value, and seaon count --- .../wardrobecollection/PersonalCloset.java | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) 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 index 6343a375..d6677215 100644 --- 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 @@ -21,7 +21,7 @@ public enum Season { private int maxCapacity; private double totalValue; private boolean isOrganized; - private ArrayList items; + private ArrayList items; private HashMap seasonalItems; // Constructor for personal closet @@ -31,23 +31,39 @@ public PersonalCloset (String ownerName, int maxCapacity) { this.totalValue = 0.0; this.isOrganized = false; this.items = new ArrayList<>(); - this.seaonalItems = new HashMap<>(); + this.seasonalItems = new HashMap<>(); } //core methods // adds item to closet public boolean addItem(ClothingItem item) { - return false; + // if closet is full, cannot add item + if (items.size(0) >= maxCapacity) { + return false; + } + + //adding item to closet and increasing closet 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 seaason = item.getSeason(); + seasonalItems.put(season, seasonalItems.getOrDefault(season, 0) + 1); + + // returns true if item is added + return true; } // removes item from closet public void removeItem(ClothingItem item) { - // throws exception if item is not found in closet - public static class ItemNotFoundException extends Exception{ + return; + } + // throws exception if item is not found in closet + public static class ItemNotFoundException extends Exception{ } - } + // creates outfit by selecting items based on the season public List createOutfit(Season season) { @@ -68,7 +84,7 @@ public String getOwnerName() { return ownerName; } - public void setOwnerName() { + public void setOwnerName(String ownerName) { this.ownerName = ownerName; } From eb78119f7785cc170f0c0e2e2ae87777d56cb148 Mon Sep 17 00:00:00 2001 From: Karen Alabi Date: Sun, 13 Apr 2025 07:02:28 -0400 Subject: [PATCH 04/11] feat: implements remove item method with custom exception for items not found in closet --- .../wardrobecollection/PersonalCloset.java | 55 ++++++++++++------- 1 file changed, 34 insertions(+), 21 deletions(-) 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 index d6677215..93c44e19 100644 --- 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 @@ -36,45 +36,52 @@ public PersonalCloset (String ownerName, int maxCapacity) { //core methods - // adds item to closet + // method adds item to closet public boolean addItem(ClothingItem item) { - // if closet is full, cannot add item - if (items.size(0) >= maxCapacity) { + // if closet is full, cannot add item + if (items.size(0) >= maxCapacity) { return false; } - //adding item to closet and increasing closet total value of closet - items.add(item); - totalValue += item.getValue(); + //adding item to closet and increasing closet 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 seaason = item.getSeason(); - seasonalItems.put(season, seasonalItems.getOrDefault(season, 0) + 1); + // checks what season item is meant for and keeps track of number of items in that season + Season seaason = item.getSeason(); + seasonalItems.put(season, seasonalItems.getOrDefault(season, 0) + 1); - // returns true if item is added - return true; + // returns true if item is added + return true; } - // removes item from closet - public void removeItem(ClothingItem item) { - return; - } + // method removes item from closet + public void removeItem(ClothingItem item) throws ItemNotFoundException { + // if item is not in closet, throws an error + if (!item.contains(item)) { + throw new ItemNotFoundException("Item is not in closet."); + } - // throws exception if item is not found in closet - public static class ItemNotFoundException extends Exception{ - } + //remove item from closet and decreases toal value of closet + item.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); + } - // creates outfit by selecting items based on the season + + // method creates outfit by selecting items based on the season public List createOutfit(Season season) { return new ArrayList<>(); } - // organizes closet by type of item and color + // method organizes closet by type of item and color public void organizeCloset() { } - // calculates amount of clothing items are in closet based on season + // method calculates amount of clothing items are in closet based on season public Map getSeasonalItem() { return new HashMap<>(); } @@ -104,5 +111,11 @@ 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); + } + } } From b2d760c07f0ef19cfaa92e439ed8176b50e6e5dd Mon Sep 17 00:00:00 2001 From: Karen Alabi Date: Sun, 13 Apr 2025 07:23:00 -0400 Subject: [PATCH 05/11] feat: implements createOutfit method to pick clothes based on season --- .../wardrobecollection/PersonalCloset.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) 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 index 93c44e19..56fbdcb3 100644 --- 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 @@ -73,8 +73,22 @@ public void removeItem(ClothingItem item) throws ItemNotFoundException { // method creates outfit by selecting items based on the season - public List createOutfit(Season season) { - return new ArrayList<>(); + 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 clothign 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 outfits; } // method organizes closet by type of item and color From c8b3b9d6f65fa3925b646e9ddfc47b8734ef2d6e Mon Sep 17 00:00:00 2001 From: Karen Alabi Date: Sun, 13 Apr 2025 09:38:00 -0400 Subject: [PATCH 06/11] fix: corrects items.size() and seasonal counts in Personal Closet class --- .../wardrobecollection/PersonalCloset.java | 71 ++++++++++++------- 1 file changed, 47 insertions(+), 24 deletions(-) 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 index 56fbdcb3..cd84c198 100644 --- 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 @@ -34,70 +34,93 @@ public PersonalCloset (String ownerName, int maxCapacity) { this.seasonalItems = new HashMap<>(); } - //core methods + // Core methods - // method adds item to closet + // Method adds item to closet public boolean addItem(ClothingItem item) { - // if closet is full, cannot add item - if (items.size(0) >= maxCapacity) { + // If closet is full, cannot add item + if (items.size() >= maxCapacity) { return false; } - //adding item to closet and increasing closet total value of closet + // 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 seaason = item.getSeason(); + // 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 + // Returns true if item is added return true; } - // method removes item from closet + // Method removes item from closet public void removeItem(ClothingItem item) throws ItemNotFoundException { - // if item is not in closet, throws an error - if (!item.contains(item)) { + // 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 - item.remove(item); + // 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 + // 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 + // Method creates outfit by selecting items based on the season public List createOutfit(Season season) { - // creating empty list that stores clothing items + // Creating empty list that stores clothing items List outfit = new ArrayList<>(); - // iterating through all items in the closet and grabbing item at index + // 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 clothign item matches particular season or is good for all seasons + // 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 outfits; + // Returns final list of clothing items in an outfit + return outfit; } - // method organizes closet by type of item and color + // 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); + } + + // Mark closet as organized + isOrganized = true; } - // method calculates amount of clothing items are in closet based on season + // method returns a map shwoing how many items are in closet based on season public Map getSeasonalItem() { - return new HashMap<>(); + return new HashMap<>(seasonalItems); } // getters and setters @@ -125,7 +148,7 @@ public List getItems() { return new ArrayList<>(items); } - // custom exception if item is not found in closet + // Custom exception if item is not found in closet public static class ItemNotFoundException extends Exception{ public ItemNotFoundException(String message) { super(message); From de685c493e73fa28e3e3427d6dfe4cd241494ec3 Mon Sep 17 00:00:00 2001 From: Karen Alabi Date: Mon, 14 Apr 2025 03:24:13 -0400 Subject: [PATCH 07/11] feat: creates Clothing Item class file --- .../lesson16/wardrobecollection/ClothingItem.java | 0 .../lesson16/wardrobecollection/PersonalCloset.java | 12 +++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/wardrobecollection/ClothingItem.java 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..e69de29b 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 index cd84c198..b088d038 100644 --- 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 @@ -114,7 +114,7 @@ public void organizeCloset() { itemList.add(item); } - // Mark closet as organized + // closet is organized isOrganized = true; } @@ -136,14 +136,24 @@ public int getMaxCapacity() { return maxCapacity; } + public void setMaxCapacity() { + this.maxCapacity = maxCapacity; + } + public double getTotalValue() { return totalValue; } + public void setTotalValue() { + this.totalValue = totalValue; + } + public boolean isOrganized () { return isOrganized; } + public void set + public List getItems() { return new ArrayList<>(items); } From 40a0b073bafa1f5b0df26d34cf11608e0e2543b7 Mon Sep 17 00:00:00 2001 From: Karen Alabi Date: Mon, 14 Apr 2025 03:52:13 -0400 Subject: [PATCH 08/11] chore: updates getter and setter methods for Personal Closet class --- .../lesson16/wardrobecollection/ClothingItem.java | 7 +++++++ .../lesson16/wardrobecollection/PersonalCloset.java | 10 ++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) 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 index e69de29b..3a259b20 100644 --- 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 @@ -0,0 +1,7 @@ +package com.codedifferently.lesson16.wardobecollection; + +import java.util.ArrayList; + +public class ClothingItem { + +} \ 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 index b088d038..ea5c1a26 100644 --- 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 @@ -136,7 +136,7 @@ public int getMaxCapacity() { return maxCapacity; } - public void setMaxCapacity() { + public void setMaxCapacity(int maxCapacity) { this.maxCapacity = maxCapacity; } @@ -144,15 +144,17 @@ public double getTotalValue() { return totalValue; } - public void setTotalValue() { + public void setTotalValue(double totalValue) { this.totalValue = totalValue; } public boolean isOrganized () { - return isOrganized; + return isOrganized; } - public void set + public void setOrganized(boolean isOrganized) { + this.isOrganized = isOrganized; + } public List getItems() { return new ArrayList<>(items); From 1ce5e47fa67934cee3238265e418531b188549b0 Mon Sep 17 00:00:00 2001 From: Karen Alabi Date: Mon, 14 Apr 2025 04:14:15 -0400 Subject: [PATCH 09/11] feat: adds basic Clothing Item class structure --- .../wardrobecollection/ClothingItem.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) 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 index 3a259b20..154f5f7c 100644 --- 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 @@ -3,5 +3,70 @@ 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 From 8dfe55748a8cab2ee9c17efd3ffc27cb8f2f3ee0 Mon Sep 17 00:00:00 2001 From: Karen Alabi Date: Mon, 14 Apr 2025 11:39:22 -0400 Subject: [PATCH 10/11] feat: adds basic class structure of PersonalClosetTest class --- .../PersonalClosetTest.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/wardrobecollectiontest/PersonalClosetTest.java 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 From 5dde5dea61f0f9f663257e5d93a2fd2c2d4c2f32 Mon Sep 17 00:00:00 2001 From: karensprojects22 Date: Wed, 16 Apr 2025 18:03:28 +0000 Subject: [PATCH 11/11] feat: implements initial bank account, business checking account, and sacings account class --- .../lesson17/bank/BankAccount.java | 131 ++++++++++++++++++ .../bank/BusinessCheckingAccount.java | 10 ++ .../lesson17/bank/SavingsAccount.java | 9 ++ 3 files changed, 150 insertions(+) create mode 100644 lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BankAccount.java create mode 100644 lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BusinessCheckingAccount.java create mode 100644 lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/SavingsAccount.java 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