I am trying to write this program in java.
I can read the string from the constructor and separate the string by using delimiters. However, every time, I try to call methods such as promote()
for those values, the value just remains the same. The boolean method promote()
displays true
, but the level just doesn't increases.
For example: if the string from the constructor is : "Harry#wizard#broom"
, then:
name <- "Harry"
level <- "wizard"
supplies <- "broom"
name
, level
and broom
are all the private data instances (I don't know if I should be using data instances or local variables).
However, whenever I call harry.promote()
it returns true
but the level just stays the same, it doesn't get promoted. The initial value in level
seems to overwrite it.
I have written the following code:
import java.util.Scanner;
import java.io.*;
public class Magician
{
private String name;
private String level;
private String supplies;
private double health;
private int galleons;
public Magician(String phrase) //phrase will be sth like this: Harry#wizard#broom#staff
{
health = 4000;
galleons = 200;
Scanner firstScan = new Scanner(details);
firstScan.useDelimiter("#");
name = firstScan.next();
level = firstScan.next();
if (firstScan.hasNext())
{
supplies = firstScan.next();
while (firstScan.hasNext())
{
supplies = supplies + " " + firstScan.next();
}
}
else
{
supplies ="";
}
}
public String getName()
{
return name;
}
public String getLevel()
{
return level;
}
public int getGalleons()
{
return galleons;
}
public double getHealth()
{
return health;
}
public String getSupplies()
{
return supplies;
}
//Mutators
public void setName(String nameUpdate)
{
name = nameUpdate;
}
public void setLevel(String levelUpdate)
{
level =levelUpdate;
}
public void setGalleons(int galleonsUpdate)
{
galleons = galleonsUpdate;
}
public void setHealth(double healthUpdate)
{
health = healthUpdate;
}
public void setSupplies(String suppliesUpdate)
{
supplies = suppliesUpdate;
}
// boolean promote, promotes a level up
public boolean promote()
{
if (level == "apprentice")
{
level = "wizard";
galleons +=100;
return true;
}
else if (level == "wizard")
{
level = "mage";
galleons +=100;
return true;
}
else if (level == "mage")
{
level = "sorcerer";
galleons +=100;
return true;
}
else if (level == "sorcerer")
{
level = "shaman";
galleons +=100;
return true;
}
else if (level == "shaman")
{
return false;
}
return true;
}
public boolean downgradeLevel()
{
if (level == "shaman")
{
level = "sorcerer";
return true;
}
else if (level == "sorcerer")
{
level = "mage";
return true;
}
else if (level == "mage")
{
level = "wizard";
return true;
}
else if (level == "wizard")
{
level = "apprentice";
return true;
}
else if (level == "apprentice")
{
return false;
}
if(galleons>= 100)
{
galleons -=100;
}
else
{
galleons =0;
}
return true;
}