Skip to content
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
62 changes: 62 additions & 0 deletions src/main/java/io/zipcoder/Application.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,67 @@
package io.zipcoder;

import java.util.*;

public class Application {
public static void main(String[] args) {
/**

Create a program that asks the user how many pets they have.
Once you know how many pets they have, ask them what kind of pet each one is,
along with each pet's name.
For now your program should just hold onto the user input and
print out the list at the end; we'll modify this in part 3.*/
int totalPets;
String petType;
String petName;
ArrayList<PetClass> petInfo = new ArrayList<PetClass>();

Scanner scan = new Scanner(System.in);
System.out.println("Hello! How many pets do you have?");
totalPets = scan.nextInt();
scan.nextLine();

//ask them what kind of pet each one is, along with each pet's name.
for (int i = 0; i < totalPets; i++) {
System.out.println("What kind of pet is this?(Dog,Cat or Reptile)");
//waits for input to be entered before continuing on to the nextline
petType = scan.nextLine();
System.out.println("What is the name of this pet?");
petName = scan.nextLine();
//compares incoming string to case and stores appropriate object for info entered by user
switch (petType.toLowerCase()) {
case "dog":
petInfo.add(new Dog(petName));
break;
case "cat":
petInfo.add(new Cat(petName));
break;
case "reptile":
petInfo.add(new Reptile(petName));
break;
default:
System.out.printf("%s is an invalid pet type.", petType);
}

}

//iterate over an arrayList using an enhanced for loop. use getter to return name and call method
for (PetClass obj : petInfo) {
System.out.printf("My pet %s says %s%n", obj.getName(), obj.speak());
}
//will call comparable
Collections.sort(petInfo);
int count = 0;
for (PetClass obj : petInfo) {
count++;
System.out.printf("Pet %d is %s%n", count, obj.getName());
}
//will call comparator
Collections.sort(petInfo, new PetCompare());
for (PetClass obj : petInfo) {
System.out.printf("I am a %s%n.", obj.getClass());
}
}

}

21 changes: 21 additions & 0 deletions src/main/java/io/zipcoder/Cat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.zipcoder;

public class Cat extends PetClass {

//Constructor uses super keyword to invoke constructor of parent class
public Cat(String name) {
super(name);
}

@Override
public String speak() {
return "Meow";
}

/* 1.super keyword, It is used inside a subclass method definition to call a method defined in the super class.
* Private methods of the super-class can't be called. Only public and protected methods of the super-class can
* be called by the super keyword.
* 2.super keyword is also used by class constructors to invoke constructors of its parent class**/
}


13 changes: 13 additions & 0 deletions src/main/java/io/zipcoder/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.zipcoder;

public class Dog extends PetClass {

public Dog(String name) {
super(name);
}

@Override
public String speak() {
return "Woof";
}
}
64 changes: 64 additions & 0 deletions src/main/java/io/zipcoder/PetClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.zipcoder;

import java.lang.Comparable;

public abstract class PetClass implements Comparable<PetClass> {
//Since the Petclass has an abstract method, need to declare this class abstract.

//The Pet class must have a name field with setters and getters.
String name;

public PetClass(String name) {

this.name = name;
}

public String getName() {

return name;
}

public void setName(String name) {

this.name = name;
}
//The Pet class must have a speak method that each subclass overrides.
//abstract method

/**
* All the child classes should override this method. There is no point to implement this
* method in parent class. Making this method abstract would be a good choice because making
* it abstract will force subclasses to implement this method (or will get compile error)
*/
public abstract String speak();

@Override
public int compareTo(PetClass other) {
int equality = this.name.compareTo(other.name);
if (equality != 0) {
return equality;
}
return this.getClass().getSimpleName().compareTo(other.name.getClass().getSimpleName());
}

}


/**
* Notes on Comparable and CompareTo().
* To sort an Object by its property, you have to make the Object implement the Comparable interface
* and override the compareTo() method.A comparable object is capable of comparing itself with another object.
* The class itself must implements the java.lang.Comparable interface to compare its instances. When comparing strings
* we have 3 possible outcomes. Int of 0 which means the strings are equal. Int of -1 means string
* on the left is greater lexigraphically than whats on the right(comparing value). Int of 1 means
* whats on the left is smaller lexigraphically than whats on the right(comparing value).
* In this case I compared the current instance of the objects name to the previous(or next instance of the object..
* code for compareTo does that). if it's 0 it falls through to the second return statement. if returns 1 or -1 then
* it returns that value.
* The second return statement compares class Name. If pets name is equal will compare classname.
*/





17 changes: 17 additions & 0 deletions src/main/java/io/zipcoder/PetCompare.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.zipcoder;

import java.util.Comparator;

public class PetCompare implements Comparator<PetClass> {

@Override
public int compare(PetClass pet1, PetClass pet2) {
int equality = pet1.getClass().getSimpleName().compareTo(pet2.name.getClass().getSimpleName());
if (equality != 0) {
return equality;
}
return pet1.getName().compareTo(pet2.getName());
}
}
/** Implementation of comparator very similar to comparable. Did enough research to make app function.
* Need to research the difference between the 2. Still lacking some understanding here.*/
15 changes: 15 additions & 0 deletions src/main/java/io/zipcoder/Reptile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.zipcoder;

public class Reptile extends PetClass {

public Reptile(String name) {

super(name);
}

@Override
public String speak() {

return "Hiss!";
}
}
41 changes: 41 additions & 0 deletions src/test/java/io/zipcoder/CatTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.zipcoder;

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

import static org.junit.Assert.*;

public class CatTest {

@Test
public void testSpeak() {
//ARRANGE: create an instance of the object to act on
Cat myCat = new Cat("Snowball");
String expected = "Meow";
//ACT: call method on our object
String actual = myCat.speak();
//Assert: check outcome is what we expect
Assert.assertEquals(expected, actual);
}

//This is where we are testing the constructor of the parent class
@Test
public void testGetName() {
String expected = "Snowball";
Cat myCat = new Cat(expected);
String actual = myCat.getName();
Assert.assertEquals(expected, actual);

}

@Test
public void testSetName() {
String expected = "Whiskers";
Cat myCat = new Cat(expected);
myCat.setName(expected);
String actual = myCat.getName();
Assert.assertEquals(expected, actual);
}

}

36 changes: 36 additions & 0 deletions src/test/java/io/zipcoder/DogTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.zipcoder;

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

import static org.junit.Assert.*;

public class DogTest {

@Test
public void testSpeak() {
Dog myDog = new Dog("Fido");
String expected = "Woof";
String actual = myDog.speak();
Assert.assertEquals(expected, actual);
}

@Test
public void testGetName() {
String expected = "Fido";
Dog myDog = new Dog("Fido");
String actual = myDog.getName();
Assert.assertEquals(expected, actual);

}

@Test
public void testSetName() {
Dog myDog = new Dog("Fido");
String expected = "Fido";
myDog.setName(expected);
String actual = myDog.getName();
Assert.assertEquals(expected, actual);
}
}

28 changes: 28 additions & 0 deletions src/test/java/io/zipcoder/PetClassTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.zipcoder;

import org.junit.Test;

import static org.junit.Assert.*;

public class PetClassTest {
//Note PetClass is abstract and can not be instantiated
// How to test and abstract class?
//Create a concrete class that inherits the abstract
// class and then test the functions the concrete class
// inherits from the abstract class. So test the following
// in cat and dog tests

@Test
public void testGetName() {


}

@Test
public void testSetName() {
}

@Test
public void testSpeak() {
}
}
38 changes: 38 additions & 0 deletions src/test/java/io/zipcoder/ReptileTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.zipcoder;

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

import static org.junit.Assert.*;

public class ReptileTest {

@Test
public void testSpeak() {
Reptile myReptile = new Reptile("Alfred");
String expected = "Hiss!";
String actual = myReptile.speak();
Assert.assertEquals(expected, actual);
}

@Test
public void testGetName() {
Reptile myReptile = new Reptile("Alfred");
String expected = "Alfred";
String actual = myReptile.getName();
Assert.assertEquals(expected, actual);

}

@Test
public void testSetName() {
String expected = "Sneaky Snake";
Cat myReptile = new Cat(expected);
myReptile.setName(expected);
String actual = myReptile.getName();
Assert.assertEquals(expected, actual);
}
}