Skip to content
Open

done #27

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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,10 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
</project>
5 changes: 5 additions & 0 deletions src/main/java/io/zipcoder/Animal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.zipcoder;

public interface Animal {
String speak();
}
120 changes: 119 additions & 1 deletion src/main/java/io/zipcoder/Application.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,123 @@
package io.zipcoder;


public class Application {
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

/**
* Edited by Frankie
*/

public class Application implements Comparator<Pet> {
Integer petCount;
String petType;
String petName;
ArrayList<Pet> pets = new ArrayList<Pet>();
Pet p;
Scanner input = new Scanner(System.in);
Scanner input1 = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);

public void run() {
String welcome = welcomeMessage();
System.out.println(welcome + "\nHow many pets do you have?\n");
int petCount = input.nextInt();
setNumberOfPets(petCount);
System.out.println("Now lets get to know them.\n");
for (int i = 1; i <= petCount; i++) {
System.out.println("Please enter the type of pet #" + i + ": ");
String petType = input1.nextLine();
System.out.println("Please enter the name of pet #" + i + ": ");
String petName = input2.nextLine();
addPet(petType, petName);
}

System.out.println("Your pets...\n");
System.out.println(listPetsSorted());
}

public String welcomeMessage() {
return "Welcome interesting person whom I've never met. Please tell me some things about yourself.";
}

public void setNumberOfPets(int petCount) {
this.petCount = petCount;
}

public int getNumberOfPets() {
return this.petCount;
}

public void setTypeOfPet(String petType) {
this.petType = petType;
}

public String getTypeOfPet() {
return this.petType;
}

public void setPetName(String petName) {
this.petName = petName;
}

public String getPetName() {
return this.petName;
}

public void addPet(String petType, String petName) {
if (petType.equals("Dog")) {
this.p = new Dog(petName);
} else if (petType.equals("Cat")) {
this.p = new Cat(petName);
} else if (petType.equals("Bird")) {
this.p = new Bird(petName);
}
this.pets.add(p);
}

public boolean getApplication(String petType) {
String listMF = listPetsSorted();
CharSequence petTy = petType;
return listMF.contains(petTy);
}

public String listPetsSorted() {
Pet[] temp = pets.toArray(new Pet[pets.size()]);
pets.clear();
Arrays.sort(temp, sortPets);
StringBuilder printOut = new StringBuilder();
for (Pet pet : temp) {
printOut.append(String.format("%s : %s\n", pet.getType(), pet.getPetName()));
}
return printOut.toString();
}

public Comparator<Pet> sortPets = new Comparator<Pet>() {
public int compare(Pet o1, Pet o2) {

int nameCounter = (o1.getPetName().length() > o2.getPetName().length() ? o1.getPetName().length() : o2.getPetName().length());
for (int i = 0; i < nameCounter; i++) {
if (o1.getPetName().charAt(i) < o2.getPetName().charAt(i)) {
return 1;
} else if (o1.getPetName().charAt(i) > o2.getPetName().charAt(i)) {
return -1;
}
}
return 0;
}
};

public int compare(Pet p1, Pet p2) {
int nameCounter = (p1.getPetName().length() > p2.getPetName().length() ? p1.getPetName().length() : p2.getPetName().length());
for (int i = 0; i < nameCounter; i++) {
if (p1.getPetName().charAt(i) < p2.getPetName().charAt(i)) {
return 1;
} else if (p1.getPetName().charAt(i) > p2.getPetName().charAt(i)) {
return -1;
}
}
return 0;
}
}
41 changes: 41 additions & 0 deletions src/main/java/io/zipcoder/Bird.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.zipcoder;

/**
* Edited by Frankie
*/
public class Bird extends Pet {
String bird;
String type;

public Bird() {
this.bird = "";
this.type = "Bird";
}

public Bird(String name) {
setPetName(name);
this.type = "Bird";
}

@Override
public String getType() {
return type;
}

public void setPetName(String word) {
this.bird = word;
}

public String getPetName() {
return this.bird;
}

public String speak() {
Animal dog = new Animal() {
public String speak() {
return "Squawk";
}
};
return dog.speak();
}
}
40 changes: 40 additions & 0 deletions src/main/java/io/zipcoder/Cat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.zipcoder;
/**
* Edited by Frankie
*/
public class Cat extends Pet {
String name;
String type;

public Cat() {
this.name = "";
this.type = "Cat";
}

public Cat(String name) {
setPetName(name);
this.type = "Cat";
}

@Override
public String getType() {
return type;
}

public void setPetName(String name) {
this.name = name;
}

public String getPetName() {
return this.name;
}

public String speak() {
Animal dog = new Animal() {
public String speak() {
return "Meow";
}
};
return dog.speak();
}
}
38 changes: 38 additions & 0 deletions src/main/java/io/zipcoder/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.zipcoder;
/**
* Edited by Frankie
*/
public class Dog extends Pet {
String name;
String type;
public Dog(){
this.name = "";
this.type = "Dog";
}
public Dog(String name){
setPetName(name);
this.type = "Dog";
}

@Override
public String getType() {
return type;
}

public void setPetName(String name){
this.name = name;
}

public String getPetName() {
return name;
}

public String speak() {
Animal dog = new Animal() {
public String speak() {
return "I just met you and I love you!";
}
};
return dog.speak();
}
}
29 changes: 29 additions & 0 deletions src/main/java/io/zipcoder/Pet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.zipcoder;
/**
* Edited by Frankie
*/
public abstract class Pet implements Animal {
String name;
String type;

public Pet() {
this.name = "";
this.type = "";
}

public void setPetName(String petName) {
this.name = petName;
}

public String getPetName() {
return this.name;
}

public void setPetType(String petType) {
this.type = petType;
}

public String getType() {
return type;
}
}
10 changes: 10 additions & 0 deletions src/main/java/io/zipcoder/Run.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.zipcoder;
/**
* Edited by Frankie
*/
public class Run {
public static void main(String[] args) {
Application application = new Application();
application.run();
}
}
61 changes: 61 additions & 0 deletions src/test/java/io/zipcoder/ApplicationTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,66 @@
package io.zipcoder;


import org.junit.Assert;
import org.junit.Test;

import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;

/**
* MADE BY APRIL!
*/
public class ApplicationTest {

Application application = new Application();

@Test
public void welcomeUserTest() {
String message = "Welcome interesting person whom I've never met. Please tell me some things about yourself.";
String actual = application.welcomeMessage();
Assert.assertEquals(message, actual);
}

@Test
public void getNumberOfPetsTest() {
int petCount = 2;
application.setNumberOfPets(petCount);
boolean numberOFPetsHasBeenSet = application.getNumberOfPets() == (petCount);
assertTrue(numberOFPetsHasBeenSet);
}

@Test
public void getTypeOfPetTest() {
String petType = "Dog";
application.setTypeOfPet(petType);
boolean typeOfPet = application.getTypeOfPet().equals(petType);
assertTrue(typeOfPet);
}

@Test
public void getPetNameTest() {
String petName = "Doug";
application.setPetName(petName);
boolean nameHasBeenSet = application.getPetName().equals(petName);
assertTrue(nameHasBeenSet);
}

@Test
public void listPetsSortedTest() {
String typeOfPet = "Dog";
String petName = "Doug";
application.addPet(typeOfPet, petName);
String actual = application.listPetsSorted();
String expected = "Dog : Doug\n";
assertEquals(expected, actual);
}

@Test
public void addPetTest() {
String typeOfPet = "Dog";
String petName = "Doug";
application.addPet(typeOfPet, petName);
boolean petHasBeenAdded = application.getApplication(typeOfPet);
assertTrue(petHasBeenAdded);
}
}
Loading