Finish 10-FEB assignment

This commit is contained in:
Logen Kain 2021-02-13 11:05:55 -05:00
parent 0e08cb5b7a
commit a0eabce325
8 changed files with 315 additions and 85 deletions

View File

@ -0,0 +1,12 @@
public class SmartWatch extends Watch {
private int batteryPercentage;
void setPercentage(int setPercentage) {
batteryPercentage = setPercentage;
}
@Override
void printItem() {
System.out.println(hours + ":" + mins + " " + batteryPercentage + "%");
}
}

View File

@ -0,0 +1,16 @@
public class Watch {
protected int hours;
protected int mins;
void setHours(int setHours) {
hours = setHours;
}
void setMins(int setMins) {
mins = setMins;
}
void printItem() {
System.out.println(hours + ":" + mins);
}
}

View File

@ -0,0 +1,41 @@
import java.util.ArrayList;
public class WatchManager {
public static void main(String[] args) {
SmartWatch watch1;
SmartWatch watch2;
Watch watch3;
ArrayList<Watch> watchList = new ArrayList<Watch>();
int i;
watch1 = new SmartWatch();
watch1.setHours(5);
watch1.setMins(32);
watch1.setPercentage(10);
watch2 = new SmartWatch();
watch2.setHours(17);
watch2.setMins(49);
watch2.setPercentage(65);
watch3 = new Watch();
watch3.setHours(6);
watch3.setMins(25);
watchList.add(watch1);
watchList.add(watch3);
watchList.add(watch2);
for(i = 0; i < watchList.size(); ++i) {
watchList.get(i).printItem();
}
/* Modified For Loop
for( Watch watch : watchList){
watch.printItem();
}
*/
}
}

View File

@ -1,11 +1,25 @@
public class Flower extends Plant {
private String isAnnual;
private boolean isAnnual;
private String color;
public Flower(String name, String cost, String isAnnual, String color){
super(name, cost);
this.isAnnual = isAnnual;
this.color = color;
public Flower () {
super();
this.isAnnual = false;
this.color = "TBD";
}
public void setPlantType(boolean isAnnual){
this.isAnnual=isAnnual;
}
public boolean getPlantType(){
return this.isAnnual;
}
public void setColorOfFlowers(String color){
this.color=color;
}
public String getColorOfFlowers(){
return this.color;
}
@Override

View File

@ -0,0 +1,91 @@
import java.util.ArrayList;
import java.util.Scanner;
public class GardenManager{
private ArrayList<Plant> myGarden = new ArrayList<Plant>();
public ArrayList<Plant> getMyGardenList(){
return myGarden;
}
public void addPlant(Plant plant){
myGarden.add(plant);
}
public Plant makePlant(String name, String cost){
Plant plant = new Plant();
plant.setPlantName(name);
plant.setPlantCost(cost);
return plant;
}
public Flower makeFlower(String name, String cost, String isAnnual,
String color){
Flower flower = new Flower();
flower.setPlantName(name);
flower.setPlantCost(cost);
if (isAnnual.equals("false")){
flower.setPlantType(false);
}
else{
flower.setPlantType(true);
}
flower.setColorOfFlowers(color);
return flower;
}
public ArrayList<Plant> inputPlants(Scanner scnr){
String pName, pCost, pColor, isAnnual;
String s;
String[] splitString;
Plant currPlant;
ArrayList<Plant> garden = new ArrayList<Plant>();
while(true){
//plant has name and cost 3 inputs
//flower has all 5 inputs
System.out.println("Please enter your plant: ");
System.out.println("type name cost (annual) (color)");
s = scnr.nextLine();
splitString = s.split(" ");
//Check for exit
if (splitString[0].equals("-1")){break;}
//Check if too many words or too few words
if (splitString.length > 5) {
System.out.println("Too many words!!!");
continue;
}
else if ((splitString.length !=3) &&
(splitString.length !=5)){
System.out.println("Expecting 3 or 5 words!; -1 to quit");
continue;
}
//Set generic strings to vars for readability
pName = splitString[1];
pCost = splitString[2];
// Plants take 3
if (splitString.length == 3){
currPlant = makePlant(pName, pCost);
}
// Flowers take 5
else{
//get remaining strings
isAnnual = splitString[3];
pColor = splitString[4];
currPlant = makeFlower(pName, pCost, isAnnual, pColor);
}
addPlant(currPlant);
System.out.println();
}
return garden;
}
}

View File

@ -1,91 +1,19 @@
import java.util.Scanner;
import java.util.ArrayList;
public class MyGarden{
public static void main(String[] args){
Scanner scnr = new Scanner(System.in);
ArrayList<Plant> myGarden;
myGarden = inputPlants(scnr);
GardenManager myGarden = new GardenManager();
printArrayList(myGarden);
myGarden.inputPlants(scnr);
printArrayList(myGarden.getMyGardenList());
}
public static void printArrayList(ArrayList<Plant> garden){
for (Plant plant:garden){
plant.printInfo();
}
}
public static ArrayList<Plant> inputPlants(Scanner scnr){
String pName, pCost, pAnnual, pColor;
String s;
String[] splitString;
boolean isInt;
Plant currPlant;
ArrayList<Plant> garden = new ArrayList<Plant>();
while(true){
//plant has name and cost 3 inputs
//flower has all 5 inputs
System.out.println("Please enter your plant: ");
System.out.println("type name cost (annual) (color)");
s = scnr.nextLine();
splitString = s.split(" ");
//Check for exit
if (isInteger(splitString[0])){
if (Integer.parseInt(splitString[0]) == -1){break;}
}
//Check if too many words or too few words
if (splitString.length > 5) {
System.out.println("Too many words!!!");
continue;
}
else if ((splitString.length !=3) &&
(splitString.length !=5)){
System.out.println("Expecting 3 or 5 words!; -1 to quit");
continue;
}
//Set generic strings to vars for readability
pName = splitString[1];
pCost = splitString[2];
// Plants take 3
if (splitString.length == 3){
currPlant = new Plant(pName, pCost);
garden.add(currPlant);
}
// Flowers take 5
else{
//get remaining strings
pAnnual = splitString[3];
pColor = splitString[4];
currPlant = new Flower(pName, pCost, pAnnual, pColor);
garden.add(currPlant);
}
System.out.println();
}
return garden;
}
public static boolean isInteger(String s){
boolean isInt = false;
try{
Integer.parseInt(s);
isInt = true;
}
catch (NumberFormatException ex){
}
return isInt;
}
}

View File

@ -0,0 +1,108 @@
import org.junit.*;
/****************************************************
* MyGardenJUnitTest - To test Flower, Plant, and the Garden Manager.
*
* @author Resendiz
* @version Feb 2021
****************************************************/
public class MyGardenJUnitTest {
/******************************************************
* Test default constructor for plant
*****************************************************/
@Test
public void testPlantConstructor() {
Plant p = new Plant();
Assert.assertEquals("Plant should have a constructor that sets plant name to: No Name ", "No Name",
p.getPlantName() );
Assert.assertEquals("Plant should have a constructor that sets plant cost to: 0.0 ", "0.0",
p.getPlantCost() );
}
/******************************************************
* Test default constructor for flower
*****************************************************/
@Test
public void testFlowerConstructor() {
Flower f = new Flower();
Assert.assertEquals("Flower should have called super and set plant name to: No Name ", "No Name",
f.getPlantName() );
Assert.assertEquals("Flower should have called super and set plant cost to: 0.0 ", "0.0",
f.getPlantCost() );
Assert.assertFalse("Flower should have a constructor that sets is Annual to false ", f.getPlantType());
Assert.assertEquals("Flower should have a constructor that sets color of flower to TBD ", "TBD",
f.getColorOfFlowers() );
}
/******************************************************
* Test setters for Plant
*****************************************************/
@Test
public void testPlantSetters() {
Plant p = new Plant();
p.setPlantName("Apple Tree");
p.setPlantCost("50.99");
Assert.assertEquals("Plant's name should be: Apple Tree ", "Apple Tree",
p.getPlantName() );
Assert.assertEquals("Plant's cost should be set to 50.99 ", "50.99",
p.getPlantCost() );
}
/******************************************************
* Test setters for Flower
*****************************************************/
@Test
public void testFlowerSetters() {
Flower f = new Flower();
f.setPlantName("Rose");
f.setColorOfFlowers("Red");
f.setPlantType(true);
Assert.assertTrue("Roses should be annual plants ", f.getPlantType());
Assert.assertEquals("Roses should have color Red ", "Red",
f.getColorOfFlowers() );
}
/******************************************************
* Test Garden Manager Constructor
*****************************************************/
@Test
public void testGardenManagerConstructor() {
GardenManager gm = new GardenManager();
Assert.assertEquals("The manager should create an empty ArrayList of Plants", 0, gm.getMyGardenList().size());
}
/******************************************************
* Test Adding plants and flowers to My Garden
*****************************************************/
@Test
public void testAddingPlantsAndFlowers() {
GardenManager gm = new GardenManager();
Plant p = new Plant();
p.setPlantName("Spirea");
p.setPlantCost("10");
Flower f = new Flower();
f.setPlantName("Rose");
f.setPlantCost("6");
f.setPlantType(false);
f.setColorOfFlowers("White");
gm.addPlant(p);
gm.addPlant(f);
Assert.assertEquals("There should be two plants in the ArrayList", 2, gm.getMyGardenList().size());
}
}

View File

@ -1,10 +1,15 @@
public class Plant{
protected String name, cost;
public Plant(String name, String cost){
this.name = name;
this.cost = cost;
public Plant(){
this.name = "No Name";
this.cost = "0.0";
}
/* public Plant(String name, String cost){
this.name = name;
this.cost = Double.parseDouble(cost);
}
*/
public void printInfo(){
System.out.println("Plant Information:");
@ -12,4 +17,19 @@ public class Plant{
System.out.println("\tCost: " + cost + "\n");
}
public void setPlantName(String name){
this.name = name;
}
public String getPlantName(){
return this.name;
}
public void setPlantCost(String cost){
this.cost = cost;
}
public String getPlantCost(){
return this.cost;
}
}