I am working in a lemonade stand game in Java I have some conditions to determine the "perfect recipe" I see a lot of repeated code that I am sure it can be improved with a loop but I am having a hard time figuring it out.
double perfectPrice = 0;
int perfectIce = 0;
///perfect price and ice depending on weather and temperature
if(temperature > 58){
switch(weather[randomWeather]){
case "Sunny":
perfectPrice = 0.23;
perfectIce = 5;
customers -= 20;
break;
case "Hazy":
perfectPrice = 0.21;
perfectIce = 5;
customers -= 25;
break;
case "Cloudy":
perfectPrice = 0.19;
perfectIce = 4;
customers -= 30;
break;
case "Overcast":
perfectPrice = 0.17;
perfectIce = 3;
customers -= 35;
break;
case "Rainny":
perfectPrice = 0.15;
perfectIce = 2;
customers -= 40;
break;
}
}
if(temperature > 69){
switch(weather[randomWeather]){
case "Sunny":
perfectPrice = 0.25;
perfectIce = 7;
customers -= 17;
break;
case "Hazy":
perfectPrice = 0.23;
perfectIce = 6;
customers -= 21;
break;
case "Cloudy":
perfectPrice = 0.21;
perfectIce = 5;
customers -= 25;
break;
case "Overcast":
perfectPrice = 0.19;
perfectIce = 4;
customers -= 30;
break;
case "Rainny":
perfectPrice = 0.17;
perfectIce = 3;
customers -= 35;
break;
}
}
if(temperature > 80){
switch(weather[randomWeather]){
case "Sunny":
perfectPrice = 0.28;
perfectIce = 9;
customers -= 10;
break;
case "Hazy":
perfectPrice = 0.25;
perfectIce = 8;
customers -= 15;
break;
case "Cloudy":
perfectPrice = 0.22;
perfectIce = 7;
customers -= 20;
break;
case "Overcast":
perfectPrice = 0.20;
perfectIce = 6;
customers -= 25;
break;
case "Rainny":
perfectPrice = 0.18;
perfectIce = 5;
customers -= 30;
break;
}
}
if(temperature > 90){
switch(weather[randomWeather]){
case "Sunny":
perfectPrice = 0.32;
perfectIce = 10;
customers -= 0;
break;
case "Hazy":
perfectPrice = 0.28;
perfectIce = 9;
customers -= 12;
break;
case "Cloudy":
perfectPrice = 0.25;
perfectIce = 8;
customers -= 18;
break;
case "Overcast":
perfectPrice = 0.23;
perfectIce = 7;
customers -= 23;
break;
case "Rainny":
perfectPrice = 0.21;
perfectIce = 6;
customers -= 30;
break;
}
}
I came up with this trying to figure out but I am getting negative results for customers something is wrong I can't seem to figure it out
double perfectPrice = 0;
int perfectIce = 0;
double [] tempPrice = {0.23,0.25,0.28,0.32};
int [] tempOptions = {58,69,80,90};
int [] tempIce = {5,7,9,10};
int [] tempCustomers = {20,15,10,0};
///perfect price and ice depending on weather and temperature
//if(temperature > 58){
switch(weather[randomWeather]){
case "Sunny":
for(int p=0; p<4; p++){
if(temperature > tempOptions[p]){
System.out.println("Temperature " + temperature);
System.out.println("Options " + tempOptions[p]);
perfectPrice = tempPrice[p];
perfectIce = tempIce[p];
customers = customers - tempCustomers[p];
}
}
case "Hazy":
for(int p=0; p<4; p++){
if(temperature > tempOptions[p]){
perfectPrice = tempPrice[p] - 0.03;
perfectIce = tempIce[p] - 1;
customers = customers - (tempCustomers[p] + 5);
}
}
case "Cloudy":
for(int p=0; p<4; p++){
if(temperature > tempOptions[p]){
perfectPrice = tempPrice[p] - 0.06;
perfectIce = tempIce[p] - 2;
customers = customers - (tempCustomers[p] + 10);
}
}
case "Overcast":
for(int p=0; p<4; p++){
if(temperature > tempOptions[p]){
perfectPrice = tempPrice[p] - 0.09;
perfectIce = tempIce[p] - 3;
customers = customers - (tempCustomers[p] + 15);
}
}
case "Rainny":
for(int p=0; p<4; p++){
if(temperature > tempOptions[p]){
perfectPrice = tempPrice[p] - 0.12;
perfectIce = tempIce[p] - 4;
customers = customers - (tempCustomers[p] + 20);
}
}
}