Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

finished all of the methods and passed jUnit test #16

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions LetterInventory.iml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,21 @@
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" scope="TEST">
<library name="JUnit5.8.1">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
4 changes: 2 additions & 2 deletions src/driver/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
35 changes: 30 additions & 5 deletions src/inventory/LetterInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

}

/**
Expand All @@ -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");
}

/**
Expand All @@ -55,6 +64,7 @@ public int getIndex(char c) {
*/
public void add(char c) {
//TODO
inventory[getIndex(c)]++;
}

/**
Expand All @@ -63,6 +73,7 @@ public void add(char c) {
*/
public void subtract(char c) {
//TODO
inventory[getIndex(c)]--;
}

/**
Expand All @@ -71,7 +82,7 @@ public void subtract(char c) {
*/
public int get(char c) {
//TODO
return 0;
return inventory[getIndex(c)];
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

Expand All @@ -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;
}

/**
Expand All @@ -109,6 +131,9 @@ public int size() {
*/
public boolean isEmpty() {
// TODO
if(size() == 0){
return true;
}
return false;
}

Expand Down
7 changes: 3 additions & 4 deletions tests/inventory/LetterInventoryTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,6 +16,7 @@ void setUp() {
atoz = new LetterInventory("abcdefghijklmnopqrstuvwxyz");
}


@Test
void getIndex() {
for (int i = 0; i < LetterInventory.ALPHABET_SIZE; i++) {
Expand Down Expand Up @@ -125,5 +124,5 @@ void isEmpty() {
assertFalse(atoz.isEmpty());
assertFalse(washington.isEmpty());
}
*/

}