Fixed all other issues. Currently trying to deduct 10% from currentPowerLevel in the function fightCrime. However, without a local variable I have to deal with a users inputted number and deduct 10% from it. Problem being that normally I would have a local variable with a set number that I could simply revisit and subtract the number multiplied by .1.
package superherodriver;
/**
*
* @author Daniel
*/
public class SuperHero
{
private String name;
private String superPower;
private boolean canFly;
private int crimesStopped;
private final double MAX_POWER_LEVEL;
private double currentPowerLevel;
public SuperHero()
{
MAX_POWER_LEVEL = 100;
name = "";
superPower = "";
canFly = false;
crimesStopped = 0;
currentPowerLevel = 0;
}
//Define non-default constructor
public SuperHero(String aName, String aSuperPower, double aMaxPowerLevel, boolean aCanFly)
{
MAX_POWER_LEVEL = aMaxPowerLevel;
this.name = aName;
this.superPower = aSuperPower;
this.canFly = aCanFly;
this.crimesStopped = 0;
this.currentPowerLevel = MAX_POWER_LEVEL;
}
//Define getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSuperPower() {
return superPower;
}
public void setSuperPower(String superPower) {
this.superPower = superPower;
}
public boolean isCanFly() {
return canFly;
}
public void setCanFly(boolean canFly) {
this.canFly = canFly;
}
public int getCrimesStopped() {
return crimesStopped;
}
public void setCrimesStopped(int crimesStopped) {
this.crimesStopped = crimesStopped;
}
public double getCurrentPowerLevel() {
return currentPowerLevel;
}
public void setCurrentPowerLevel(double currentPowerLevel) {
this.currentPowerLevel = currentPowerLevel;
}
}
package superherodriver;
import java.util.Scanner;
import java.util.Random;
/**
*
* @author Daniel
*/
public class SuperHeroDriver {
static Scanner kb = new Scanner(System.in);
public static SuperHero superman;
public static SuperHero wonderwomen;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String name, superPower, answer;
double powerLevel;
boolean fly;
System.out.println("What is the name of your favorite super hero?");
name = kb.nextLine();
System.out.println("What is your super hero's power?");
superPower = kb.nextLine();
System.out.println("Can your super hero fly? (yes or no)");
while(true){
answer = kb.nextLine();
if(answer.equalsIgnoreCase("yes")){
fly = true;
break;
} else if(answer.equalsIgnoreCase("no")){
fly = false;
break;
} else {
System.out.println("Sorry, you did not enter an approved response. Please answer yes or no.");
}
}
do{
System.out.println("What is your starting power level? Choose between (0-100)");
powerLevel = kb.nextDouble();
} while(powerLevel < 0 || powerLevel > 100);
kb.nextLine();
// Non-default contructor
superman = new SuperHero(name, superPower, powerLevel, fly);
// Default constructor
wonderwomen = new SuperHero();
System.out.println("What is the name of your favorite super hero?");
wonderwomen.setName(kb.nextLine());
System.out.println("What is your super hero's power?");
wonderwomen.setSuperPower(kb.nextLine());
System.out.println("Can your super hero fly? (yes or no)");
while(true){
answer = kb.nextLine();
if(answer.equalsIgnoreCase("yes")){
wonderwomen.setCanFly(fly = true);
break;
} else if(answer.equalsIgnoreCase("no")){
wonderwomen.setCanFly(fly = false);
break;
} else {
System.out.println("Sorry, you did not enter an approved response. Please answer yes or no.");
}
}
do{
System.out.println("What is your starting power level? Choose between (0-100)");
wonderwomen.setCurrentPowerLevel(kb.nextDouble());
} while(wonderwomen.getCurrentPowerLevel() < 0 || wonderwomen.getCurrentPowerLevel() > 100);
do{
fightCrime(superman, wonderwomen);
System.out.println("Superman's current power level is " + superman.getCurrentPowerLevel() + " and stopped " + superman.getCrimesStopped() + " crimes.");
System.out.println("Wonder woman's current power level is " + wonderwomen.getCurrentPowerLevel() + " and stopped " + wonderwomen.getCrimesStopped() + " crimes.");
}while(superman.getCurrentPowerLevel() > 0 && superman.getCrimesStopped() < 10);
}
public static void fightCrime(SuperHero superman, SuperHero wonderwomen){
Random randomNumber = new Random();
int ranNum1, ranNum2;
ranNum1 = randomNumber.nextInt(11) + 1;
ranNum2 = randomNumber.nextInt(11) + 1;
// Stop crime if random number is Even, lose power if odd
if(ranNum1 % 2 == 0){
System.out.println("Crime was stopped!");
superman.setCrimesStopped(superman.getCrimesStopped() + 1);
}
else{
System.out.println("My powers are getting weaker!");
superman.setCurrentPowerLevel(superman.getCurrentPowerLevel() - 10);
}
if(ranNum2 % 2 == 0){
System.out.println("Crime was stopped!");
wonderwomen.setCrimesStopped(wonderwomen.getCrimesStopped() + 1);
}
else{
System.out.println("My powers are getting weaker!");
wonderwomen.setCurrentPowerLevel(wonderwomen.getCurrentPowerLevel() - 10);
}
}
}