Before I begin I'd like to state that I'm very much a beginner when it comes to writing code in general so I do apologise if what I'm asking may seem extremely basic.
With that being said I'm struggling with returning methods in my code. I am able to write this program without splitting it into methods as I have but I am told that when coding it is good practice to split it up in methods so that debugging can be easier.
The following code seems to have a few major flaws.
public static boolean hybridNot()
{
String typeCar = input("Hybrid or Electric?");
boolean electric = false;
if (typeCar.equalsIgnoreCase("Electric"))
{
electric = true;
}
else if (typeCar.equalsIgnoreCase("Hybrid"))
{
electric = false;
}
else
{
print("Sorry I didn't understand that");
}
return;
}
public static boolean solarNot()
{
String panelsMaybe = input("Solar panel?");
boolean solarPanel = false;
if (panelsMaybe.equalsIgnoreCase("Yes"))
{
solarPanel = true;
}
else if (panelsMaybe.equalsIgnoreCase("No"))
{
solarPanel = false;
}
else
{
print("Sorry I didn't understand that");
}
return;
}
public static int discountNot()
{
final int basicPrice = 20000;
final int electricCost = 2000;
final int solarCost = 5000;
boolean electric = hybridNot();
boolean solarPanel = solarNot();
int totalPrice;
if ((solarPanel = true) || (electric = true))
{
totalPrice = basicPrice + solarCost + electricCost - 500;
}
else if ((solarPanel = true) || (electric = false))
{
totalPrice = basicPrice + solarCost;
}
else if ((solarPanel = false) || (electric = true))
{
totalPrice = basicPrice + electricCost;
}
else
{
totalPrice = basicPrice;
}
return;
}
public static void totalCost()
{
final int basicPrice = 20000;
final int electricCost = 2000;
final int solarCost = 5000;
final int discountCost = 500;
boolean hybrid = hybridNot();
boolean solarPanel = solarNot();
int finalPrice = 0;
finalPrice = discountNot();
if (finalPrice >= 26500)
{
print("Basic Price: " + basicPrice + "\n" + "Electric model: " + electricCost + "\n" + "Solar Panel: " + solarCost + "\n" + "Discount: " + discountCost);
}
else if (finalPrice >= 25000)
{
print("Basic Price: " + basicPrice + "\n" + "Solar Panel: " + solarCost);
}
else if (finalPrice >= 22000)
{
print("Basic Price: " + basicPrice + "\n" + "Electric model: " + electricCost);
}
else
{
print("Basic Price: " + basicPrice);
}
print("Total: " + finalPrice);
}
For some reason the methods hybridNot and solarNot seem to repeat themselves before moving on to the next method. To me it seems that I might have a problem with what I'm returning at the end of the method but I honestly can't figure out what's wrong. The method totalCost seems to ignore the if statement in the method discountNot and the boolean values aren't passed correctly to totalCost and I only get the the value when finalPrice >= 26500.
Again, I'm new to Java in general and I'm also new to stackoverflow (so hi!!) so please tell me if I'm doing something wrong and I'll look to correctly next time round! Thank you!!
returnstatement in a method that has a return type declared (booleanfor example) without accopmanying thatreturnwith the value you wish to return.totalCost();is still being completely ignored.