Reputation: 49
I am trying to pass the value of price from the method below to a different method so I can subtract from it, for example, I want to make two more methods one subtracts 3 from double price and a different method that subtract 4 from price depending on the user answer to the if-else statements
public static double parkingPrice(double price){
int hours = inputInt("How many hours would you like to park? from 1 hour to 8 hours. ");
if(hours>=7 && hours<9){
price = 5.50;
}
else if (hours >=5 && hours<9){
price = 4.50;
}
else if (hours >=2 && hours<9){
price = 4.00;
}
else if (hours == 1 && hours<9) {
price = 3.00;
}
else {
System.out.println("Unvalid number please enter between 1 and 8.");
}
return price;
}
apologizes for the basic question, I'm very new to programming!
edit to show how I am trying to call this method into a different one
public static void calculate(){
double price;
price = parkingPrice(price);
}
Upvotes: 0
Views: 77
Reputation: 1813
I think what you are looking for is to pass the hour into the method and then return the price that goes with it:
public static void calculate(){
int hours = inputInt("How many hours would you like to park? from 1 hour to 8 hours. ");
if (hours < 1 || hours > 8) {
System.out.println("Unvalid number please enter between 1 and 8.");
}
else {
double price = parkingPrice(hours);
//do something with price here...
}
}
public static double parkingPrice(int hours){
double price = 0;
if(hours>=7 && hours<9){
price = 5.50;
}
else if (hours >=5 && hours<9){
price = 4.50;
}
else if (hours >=2 && hours<9){
price = 4.00;
}
else if (hours == 1 && hours<9) {
price = 3.00;
}
return price;
}
Upvotes: 0
Reputation: 23357
I think you don't really understand how parameters work. You have to enter actual values in there.
In your code
public static void calculate(){
double price;
price = parkingPrice(price);
}
you are not passing a value because price is not initialized yet. You could do either
public static void calculate(){
double price = 0;
price = parkingPrice(price);
}
or
public static void calculate(){
double price = parkingPrice(0);
}
for example. But you probably don't want either because it actually makes no sense for that function to have any parameters. It makes more sense to make it like vhthanh's answer and then calculate can be like
public static void calculate(){
double price = parkingPrice();
}
Upvotes: 1
Reputation: 228
You should use Exception
instead of only printing the error message or you need a default value for price
if the hours
is invalid. Here's the code assuming you want 2 different methods that will result different calculation.
public static double parkingPrice() throws Exception {
int hours = inputInt("How many hours would you like to park? from 1 hour to 8 hours. ");
if(hours>=7 && hours<9){
return 5.50;
}
else if (hours >=5 && hours<9){
return 4.50;
}
else if (hours >=2 && hours<9){
return 4.00;
}
else if (hours == 1) {
return 3.00;
}
else {
throw new Exception("Unvalid number please enter between 1 and 8.");
}
}
public static double subtractsThree() throws Exception {
return parkingPrice() - 3;
}
public static double subtractsFour() throws Exception {
return parkingPrice() - 4;
}
Upvotes: 2