/*This is a program that calculates Internet advertising rates based on what features/options you choose.
*
*
*/
import java.util.Scanner;
public class InternetAdvertising
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int numberOfWords;
//I assigned 0 values to both as Eclipse suggested
float textCost = 0;
float linkCost = 0;
float graphicCost;
//<=25 words is a flat fee of $.40 per word plus Base fee of $3.00
final float TEXT_FLAT_FEE = 0.40F;
final float TEXT_BASE_FEE = 3.00F;
//<=35 words is $.40 for the first 25 words and
//an additional $.35 per word up to and including 35 words plus Base fee of $3.00
final float LESS_OR_EQUAL_THAN_THIRTYFIVE = 0.35F;
//Over 35 words is a flat fee of $.32 per word with no base fee
final float MORE_THAN_THIRTYFIVE = 0.32F;
System.out.println("Welcome!");
System.out.print("Enter the number of words in your ad: ");
numberOfWords = in.nextInt();
if (numberOfWords <= 25)
{
textCost = TEXT_BASE_FEE + (TEXT_FLAT_FEE * numberOfWords);
}
else if (numberOfWords <= 35)
{
textCost = TEXT_BASE_FEE + (TEXT_FLAT_FEE * 25) + (numberOfWords - 25) * LESS_OR_EQUAL_THAN_THIRTYFIVE;
}
else if (numberOfWords > 35)
{
textCost = numberOfWords * MORE_THAN_THIRTYFIVE;
}
String addLink, advancePay;
char link, advPay;
final float LINK_FLAT_FEE = 14.95F;
final float THREE_MONTH_ADV_DISCOUNT = 0.10F;
System.out.print("Would you like to add a link (y = yes or n = no)? ");
addLink = in.next();
link = addLink.charAt(0);
link = Character.toLowerCase(link);
if (link == 'y')
{
System.out.print("Would you like to pay 3 months in advance " + "(y = yes or n = no)? ");
advancePay = in.next();
advPay = advancePay.charAt(0);
advPay = Character.toLowerCase(advPay);
switch (advPay)
{
case 'y':
linkCost = (3 * LINK_FLAT_FEE) - (3 * LINK_FLAT_FEE) * THREE_MONTH_ADV_DISCOUNT;
break;
case 'n':
linkCost = LINK_FLAT_FEE;
break;
}
}
else
{
linkCost = 0;
}
String addGraphic;
char graphic;
System.out.print("Would you like to add graphics/pictures” + “(S = Small, M = Medium, L = Large or N = None)? ");
addGraphic = in.next();
graphic = addGraphic.charAt(0);
graphic = Character.toUpperCase(graphic);
graphic = Character.toLowerCase(graphic);
switch (graphic)
{
case 's':
graphicCost = 19.07F;
break;
case 'm':
graphicCost = 24.76F;
break;
case 'l':
graphicCost = 29.33F;
break;
default:
graphicCost = 0;
}
float gst, totalBeforeGst, totalAfterGst;
final float GST_RATE = 0.05F;
totalBeforeGst = textCost + linkCost + graphicCost; //textCost & linkCost would not initialize
gst = totalBeforeGst * GST_RATE;
totalAfterGst = totalBeforeGst + (totalBeforeGst * GST_RATE);
System.out.printf("\t\t%-16s %11s\n", "Category", "Cost");
System.out.printf("\t\t%-16s %11.2f\n", "Text", textCost); //linkCost would not initialize
System.out.printf("\t\t%-16s %11.2f\n", "Link", linkCost); //textCost would not initialize
System.out.printf("\t\t%-16s %11.2f\n", "Graphic", graphicCost);
System.out.printf("\t\t%-16s %11.2f\n", "Total", totalBeforeGst);
System.out.printf("\t\t%-16s %11.2f\n", "GST", gst);
System.out.printf("\t\t%-16s %11.2f\n", "Total with GST", totalAfterGst);
}
}
I'm almost done with this code and Eclipse suggests that I assign 0 values to textCost and linkCost. Is there any other way to go around this problem. If I don't assign 0 values they get an error (The local variable XXX may not have been initialized). Can someone explain to me why this happens even though I have both variables assigned with equations?
Thanks.
EDIT: I did as suggested and declared the variables only when I'm going to need it. I also added some comments.