Reputation: 135
the problem that I am asking is probably very simple for you guys, but however I am a complete beginner in java programing and got stuck in this question. The problem is that I have to use multiple methods and arguments as well as adding variables.
For example, I have to type in a userinput and add up different integers variable together. I can compile the program but somehow the addition for variables are wrong.
In this example, when I typed in car2 with sports model, the addition and subtraction of variables are wrong. Thank you very much and any help is greatly appreciated.
// Methods.java
import javax.swing.*; // import swing lib
public class MethodsTest
{
// global int access for all methods
static int basicPrice = 0;
static int carPaints = 0;
static int sportsModel = 0;
static int discount = 0;
static int priceTotal = 0;
public static void main(String[] args)
{
carModel();
System.exit(0);
}
public static void carModel()
{
String askCarTypes = "";
String carTypes = "";
askCarTypes = JOptionPane.showInputDialog
("Car1 or Car2?");
carTypes = typesOfCar(askCarTypes);
String askSportsModel = "";
String carSportsModel = "";
askSportsModel = JOptionPane.showInputDialog
("Sports Model? (y/n)");
carSportsModel = sportsModelCar(askSportsModel);
JOptionPane.showMessageDialog
(null, "Basic Price: " + basicPrice + "\n" +
"Car Paints: " + carPaints + "\n" +
"Car Model: " + sportsModel + "\n" +
"Discount: " + discount + "\n" +
"Total: " + priceTotal);
return;
}
/* Calculating basic price w/o solar panel
String args = types */
public static String typesOfCar(String types)
{
String a = "";
if (firstCar(types))
{
basicPrice = basicPrice + 20000;
priceTotal = basicPrice;
}
else if (secondCar(types))
{
basicPrice = basicPrice + 20000;
carPaints = carPaints + 2000;
priceTotal = basicPrice + carPaints;
}
else
{
JOptionPane.showMessageDialog
(null, "Sorry we have no price available for that model.");
return a;
}
return a;
}
/* Calculating price w/ sports
String args = acc */
public static String sportsModelCar(String acc)
{
String b = "";
if (car1SportsModel(acc))
{
sportsModel = sportsModel + 5000;
priceTotal = basicPrice + sportsModel;
}
else if (car2SportsModel(acc))
{
sportsModel = sportsModel + 5000;
discount = discount - 500;
priceTotal = basicPrice + carPaints + sportsModel + discount;
}
return b;
}
public static boolean firstCar (String types)
{
if (types.equals("car1"))
{
return true;
}
else
{
return false;
}
}
public static boolean secondCar (String types)
{
if (types.equals("car2"))
{
return true;
}
else
{
return false;
}
}
public static boolean car1SportsModel (String acc)
{
if (acc.equals("y"))
{
return true;
}
else
{
return false;
}
}
public static boolean car2SportsModel (String acc)
{
if (acc.equals("y"))
{
return true;
}
else
{
return false;
}
}
}
/* Output with car2 and sportsModel
Basic Price: 20000
Car Paints: 2000
Car Model: 5000
Discount: 0
Total: 25000 */
the discount should show -500 and the total should be 26500.
Upvotes: 0
Views: 118
Reputation: 3332
You aren't setting the value "discount".
if (car1SportsModel(acc))
{
sportsModel = sportsModel + 5000;
priceTotal = basicPrice + sportsModel;
}
else if (car2SportsModel(acc))
{
sportsModel = sportsModel + 5000;
discount = discount - 500;
priceTotal = basicPrice + carPaints + sportsModel + discount;
}
The car1SportsModel
and car2SportsModel
methods are functionally equivalent, and so it is impossible to hit the else statement in the above code snippet. discount
is never getting set and so it stays as its original 0 value.
In fact, you shouldn't even have these functions. Instead, why don't you just do something like this?
if (acc.equals("y"))
In fact, you should probably create a Car
class of some kind and use that to compare things.
Upvotes: 1