From c76965c0d959eafa2c1110fd3475f3d27101c5e0 Mon Sep 17 00:00:00 2001 From: gballreich Date: Fri, 19 Jan 2024 15:40:24 -0800 Subject: [PATCH 1/2] finished all of the methods and passed jUnit test --- LetterInventory.iml | 16 +++++++++++ src/driver/Driver.java | 4 +-- src/inventory/LetterInventory.java | 35 ++++++++++++++++++++---- tests/inventory/LetterInventoryTest.java | 6 ++-- 4 files changed, 50 insertions(+), 11 deletions(-) diff --git a/LetterInventory.iml b/LetterInventory.iml index 0f07f6c..576359b 100644 --- a/LetterInventory.iml +++ b/LetterInventory.iml @@ -24,5 +24,21 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/driver/Driver.java b/src/driver/Driver.java index 51cd3d9..3b9b7b3 100644 --- a/src/driver/Driver.java +++ b/src/driver/Driver.java @@ -5,8 +5,8 @@ public class Driver { public static void main(String[] args) { - LetterInventory inv = new LetterInventory(); - //LetterInventory inv = new LetterInventory("WashingtonState"); + //LetterInventory inv = new LetterInventory(); + LetterInventory inv = new LetterInventory("WashingtonState"); System.out.println(inv); System.out.println(inv.get('e')); diff --git a/src/inventory/LetterInventory.java b/src/inventory/LetterInventory.java index 18020ba..7a22ef1 100644 --- a/src/inventory/LetterInventory.java +++ b/src/inventory/LetterInventory.java @@ -33,6 +33,11 @@ public LetterInventory(){ */ public LetterInventory(String text) { //TODO + this(); + for(int i = 0; i<= text.length()-1; i++){ + add(text.charAt(i)); + } + } /** @@ -44,9 +49,13 @@ public LetterInventory(String text) { * @param c a-z or A-Z character * @return index of the character */ - public int getIndex(char c) { + public int getIndex(char c)throws IllegalArgumentException{ //TODO - return 0; + int num = (int)Character.toUpperCase(c);; + if(num >= 'A' && num <= 'Z') { + return num - 'A'; + } + throw new IllegalArgumentException("Enter a valid number"); } /** @@ -55,6 +64,7 @@ public int getIndex(char c) { */ public void add(char c) { //TODO + inventory[getIndex(c)]++; } /** @@ -63,6 +73,7 @@ public void add(char c) { */ public void subtract(char c) { //TODO + inventory[getIndex(c)]--; } /** @@ -71,7 +82,7 @@ public void subtract(char c) { */ public int get(char c) { //TODO - return 0; + return inventory[getIndex(c)]; } /** @@ -80,8 +91,12 @@ public int get(char c) { * @param count the number of occurrences of the character c; if count < 0 * IllegalArgumentException is thrown */ - public void set(char c, short count) { + public void set(char c, short count) throws IllegalArgumentException{ //TODO + if(count < 0){ + throw new IllegalArgumentException("Please enter a number larger than 0"); + } + inventory[getIndex(c)] = count; } /** @@ -91,6 +106,9 @@ public void set(char c, short count) { */ public boolean contains(char c) { //TODO + if(inventory[getIndex(c)] > 0){ + return true; + } return false; } @@ -100,7 +118,11 @@ public boolean contains(char c) { */ public int size() { //TODO - return 0; + int count = 0; + for(int i=0; i<= inventory.length-1; i++){ + count += inventory[i]; + } + return count; } /** @@ -109,6 +131,9 @@ public int size() { */ public boolean isEmpty() { // TODO + if(size() == 0){ + return true; + } return false; } diff --git a/tests/inventory/LetterInventoryTest.java b/tests/inventory/LetterInventoryTest.java index f243715..9456e57 100644 --- a/tests/inventory/LetterInventoryTest.java +++ b/tests/inventory/LetterInventoryTest.java @@ -1,12 +1,10 @@ package inventory; - import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; - class LetterInventoryTest { -/* + static LetterInventory washington; static LetterInventory empty; static LetterInventory atoz; @@ -125,5 +123,5 @@ void isEmpty() { assertFalse(atoz.isEmpty()); assertFalse(washington.isEmpty()); } -*/ + } \ No newline at end of file From aba7c177e95cf63a5d6d82be7024a06d8746c06c Mon Sep 17 00:00:00 2001 From: gballreich Date: Tue, 26 Mar 2024 10:50:42 -0700 Subject: [PATCH 2/2] finished all of the methods and passed jUnit test --- tests/inventory/LetterInventoryTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/inventory/LetterInventoryTest.java b/tests/inventory/LetterInventoryTest.java index 9456e57..632a6e6 100644 --- a/tests/inventory/LetterInventoryTest.java +++ b/tests/inventory/LetterInventoryTest.java @@ -16,6 +16,7 @@ void setUp() { atoz = new LetterInventory("abcdefghijklmnopqrstuvwxyz"); } + @Test void getIndex() { for (int i = 0; i < LetterInventory.ALPHABET_SIZE; i++) {