lundi 18 septembre 2017

Java: ERROR variable scopes in if statements

Problem:

An internet service provide offers three different subscription packages:

Package 1: $15.95 a month for up to 10 hours of service. Additional hours are $2.00 per hour. Package 2: $20.95 a month for up to 20 hours of service. Additional hours are $1.00 per hour. Package 3: $30.99 per month unlimited access.

Write a program which does the following:

Ask the user which plan they have.

Ask the user for the month number (1-12) of which month they are being billed.

Ask the user how many hours they used.

Display the cost of their bill.

Also display how much money Package 1 customers would would have saved if they purchased packages 2 or 3, and how much money Package 2 customers would have saved if they purchased Package 3.

If there were no savings, do not display this message.

Input Validation:

The plan number cannot be negative.

The plan number must be a value [1, 3].

The number of hours cannot be negative.

The number of hours cannot exceed the total number of hours for a given month.

Months with 30 days have 720 hours, months with 31 days have 744 hours, February with 28 days has 672 hours (don't worry about leap years).

Validate that the user does not enter a value greater than the total hours for a given month.

See the table below.

Months, Days, Hours.

January, 31, 744.

February, 28, 672.

March, 31, 744.

April, 30, 720.

May, 31, 744.

June, 30, 720.

July, 31, 744.

August, 31, 744.

September, 30, 720.

October, 31, 744.

November, 30, 720.

December, 31, 744.

My question.

I am having problems with my variables. I know there is better ways to code this program, however you should have in mind that this is my fourth week coding in java. I used too much If statements, and when I try to compile it; I get 12 errors with some variables. I assume that the variable scope of the variables it is blocked by a if statement somewhere. Because the complier is asking to inizialize the variables even thougt they were inizialized before. Any help will be appreciated :)

/*
 * Homework 04 Problem 05
 * Student: Kevin Crespin
 * CIN: 
 * Description: This program does the following:
 * Ask the user which plan they have.
 * Ask the user for the month number (1-12) of which month they are being billed.
 * Ask the user how many hours they used.
 * Display the cost of their bill.
 * Also display how much money Package 1 customers would would have saved if they purchased
 * packages 2 or 3, and how much money Package 2 customers would have saved if they purchased
 * Package 3. If there were no savings, do not display this message.
 */
// import scanner class
import java.util.Scanner;

public class HW04P05{
public static void main (String[] args) {
        Scanner input = new Scanner(System.in);
        // USER MENU
        final String USER_MENU =
                " \n" +\
                "[1] Package 1: $15.95 a month for up to 10 hours of service. Additional hours are $2.00 per hour.\n" +
                "[2] Package 2: $20.95 a month for up to 20 hours of service. Additional hours are $1.00 per hour.\n" +
                "[3] Package 3: $30.99 per month unlimited access.\n" +
                " \n" +
                "Enter [1 - 3] for select your package: ";
        final int PACKAGE_1 = 1;
        final int PACKAGE_2 = 2;
        final int PACKAGE_3 = 3;
        System.out.print(USER_MENU);
        int choice = input.nextInt();
        System.out.println(" ");
        if (choice < 0) {
                System.out.println("The menu choice cannot be negative, must be a value [1 - 3]");
                System.out.println("The program will know exit.");
                System.exit(1);
        }
        else if (choice == 0) {
                System.out.println("The menu choice cannot be zero, must be a value [1 - 3]");
                System.out.println("The program will know exit.");
                System.exit(1);
        }
        else if (choice > 3) {
                System.out.println("The menu choice must be a value [1 - 3]");
                System.out.println("The program will know exit.");
                System.exit(1);
        }
        System.out.print("[1] January   [4] April  [7] July       [10] October\n");
        System.out.print("[2] February  [5] May    [8] August     [11] November\n");
        System.out.print("[3] March     [6] June   [9] September  [12] December\n");
        System.out.println(" ");
        System.out.print("Enter [1 - 12] for select billed month: ");
        int month = input.nextInt();
        if (month < 0) {
                System.out.println("The month cannot be negative, must be a value [1 - 12]");
                System.out.println("The program will know exit.");
                System.exit(1);
        }
        else if (month == 0) {
        System.out.println("The month cannot be zero, must be a value [1 - 12]");
        System.out.println("The program will know exit.");
        System.exit(1);
        }
        else if (month > 12) {
                System.out.println("The month must be a value [1 - 12]");
                System.out.println("The program will know exit.");
                System.exit(1);
        }
        double hours;
        double savings_1;
        double savings_2;
        double total_1;
        double total_2;
        final double total_1d = 15.95;
        final double total_2d = 20.95;
        final double total_3d = 30.99;
        // USER MENU SWITCH
        switch (choice) {
                case PACKAGE_1:
                        if ((month == 1) ^ (month == 3) ^ (month == 5) ^ (month == 7) ^ (month == 8) ^ (month == 10) ^ (month == 12)) {
                                System.out.print("Enter the number of hours the plan package was used: ");
                                hours = input.nextInt();
                                if (hours > 744) {
                                        System.out.println("ERROR: The number of hours cannot be higher than 744 on the month selected.");
                                        System.out.println("Now the program will exit.");
                                        System.exit(1);
                                }
                                else if (hours < 0) {
                                        System.out.println("ERROR: The number of hours cannot be negative.");
                                        System.out.println("Now the program will exit.");
                                        System.exit(1);
                                }
                                else {
                                        if (hours < 10 && hours >= 1){
                                                System.out.println("The cost of your bill is: $15.95");
                                        }
                                        else if (hours > 10 && hours <= 744) {
                                                total_1 = ((hours - 10) * 2) + 15.95;
                                                System.out.println("The cost of your bill is: $" + total_1);
                                        }
                                }
                        }
                        else if (month == 2) {
                                System.out.print("Enter the number of hours the plan package was used: ");
                                hours = input.nextInt();
                                if (hours > 672) {
                                        System.out.println("ERROR: The number of hours cannot be higher than 672 on February.");
                                        System.out.println("Now the program will exit.");
                                        System.exit(1);
                                }
                                else if (hours < 0) {
                                        System.out.println("ERROR: The number of hours cannot be negative.");
                                        System.out.println("Now the program will exit.");
                                        System.exit(1);
                                }
                                else {
                                        if (hours < 10 && hours >= 1){
                                                System.out.println("The cost of your bill is: $15.95");
                                        }
                                        else if (hours > 10 && hours <= 672) {
                                                total_1 = ((hours - 10) * 2) + 15.95;
                                                System.out.println("The cost of your bill is: $" + total_1);
                                        }
                                }
                        }
                        else {
                                System.out.print("Enter the number of hours the plan package was used: ");
                                hours = input.nextInt();
                                if (hours > 720) {
                                        System.out.println("ERROR: The number of hours cannot be higher than 722 on the month selected.");
                                        System.out.println("Now the program will exit.");
                                        System.exit(1);
                                }
                                else if (hours < 0) {
                                        System.out.println("ERROR: The number of hours cannot be negative.");
                                        System.out.println("Now the program will exit.");
                                        System.exit(1);
                        }
                                else {
                                        if (hours < 10 && hours >= 1){
                                                System.out.println("The cost of your bill is: $15.95");
                                        }
                                        else if (hours > 10 && hours <= 720) {
                                                total_1 = ((hours - 10) * 2) + 15.95;
                                                System.out.println("The cost of your bill is: $" + total_1);
                                        }
                                }
                        }
                case PACKAGE_2:
                        if ((month == 1) ^ (month == 3) ^ (month == 5) ^ (month == 7) ^ (month == 8) ^ (month == 10) ^ (month == 12)) {
                                if (choice == 2) {
                                System.out.print("Enter the number of hours the plan package was used: ");
                                hours = input.nextInt();
                                if (hours > 744) {
                                        System.out.println("ERROR: The number of hours cannot be higher than 744 on the selected month.");
                                        System.out.println("Now the program will exit.");
                                        System.exit(1);
                                }
                                else if (hours < 0) {
                                        System.out.println("ERROR: The number of hours cannot be negative.");
                                        System.out.println("Now the program will exit.");
                                        System.exit(1);
                                }
                                else {
                                        if (hours < 20 && hours >= 1) {
                                                System.out.println("The cost of your bill is: $20.95");
                                        }
                                        else if (hours > 20 && hours <= 744) {
                                                total_2 = (hours - 20) + 20.95;
                                                System.out.println("The cost of your bill is: $" + total_2);
                                        }
                                }
                                }
                                else {
                                        if (hours < 20 && hours >= 1) {
                                                System.out.println("The cost of package 2 bill is: $20.95");
                                                savings_1 = total_1 - total_2d;
                                                System.out.println("You could saved: $" + savings_1); 
                                        }
                                        else if (hours > 20 && hours <= 744) {
                                                total_2 = (hours - 20) + 20.95;
                                                System.out.println("The cost of package 2 bill is: $" + total_2);
                                                savings_1 = total_1 - total_2d;
                                                System.out.println("You could saved: $" + savings_1);
                                        }
                                }
                        
                        else if (month == 2) {
                                if (choice == 2) {
                                System.out.print("Enter the number of hours the plan package was used: ");
                                hours = input.nextInt();
                                if (hours > 672) {
                                        System.out.println("ERROR: The number of hours cannot be higher than 672 on February.");
                                        System.out.println("Now the program will exit.");
                                        System.exit(1);
                                }
                                else if (hours < 0) {
                                        System.out.println("ERROR: The number of hours cannot be negative.");
                                        System.out.println("Now the program will exit.");
                                        System.exit(1);
                                }
                                else {
                                        if (hours < 20 && hours >= 1){
                                                System.out.println("The cost of your bill is: $20.95");
                                        }
                                        else if (hours > 20 && hours <= 672) {
                                                total_2 = (hours - 20) + 20.95;
                                                System.out.println("The cost of your bill is: $" + total_2);
                                        }
                                }
                                }
                                else {
                                        if (hours < 20 && hours >= 1) {
                                                System.out.println("The cost of package 2 bill is: $20.95");
                                                savings_1 = total_1 - total_2d;
                                                System.out.println("You could saved: $" + savings_1); 
                                        }
                                        else if (hours > 20 && hours <= 672) {
                                                total_2 = (hours - 20) + 20.95;
                                                System.out.println("The cost of package 2 bill is: $" + total_2);
                                                savings_1 = total_1 - total_2d;
                                                System.out.println("You could saved: $" + savings_1);
                                        }
                                }
                        }
                        else {
                                if (choice == 2) {
                                System.out.print("Enter the number of hours the plan package was used: ");
                                hours = input.nextInt();
                                if (hours > 720) {
                                        System.out.println("ERROR: The number of hours cannot be higher than 722 on the selected month.");
                                        System.out.println("Now the program will exit.");
                                        System.exit(1);
                        }
                                else if (hours < 0) {
                                        System.out.println("ERROR: The number of hours cannot be negative.");
                                        System.out.println("Now the program will exit.");
                                        System.exit(1);
                                }
                                else {
                                        if (hours < 20 && hours >= 1){
                                                System.out.println("The cost of your bill is: $20.95");
                                        }
                                        else if (hours > 20 && hours <= 720) {
                                                total_2 = (hours - 20) + 20.95;
                                                System.out.println("The cost of your bill is: $" + total_2);
                                        }
                                }
                                }
                                else {
                                        if (hours < 20 && hours >= 1) {
                                                System.out.println("The cost of package 2 bill is: $20.95");
                                                savings_1 = total_1 - total_2d;
                                                System.out.println("You could saved: $" + savings_1); 
                                        }
                                        else if (hours > 20 && hours <= 720) {
                                                total_2 = (hours - 20) + 20.95;
                                                System.out.println("The cost of package 2 bill is: $" + total_2);
                                                savings_1 = total_1 - total_2d;
                                                System.out.println("You could saved: $" + savings_1);
                                        }
                                }
                        }
                case PACKAGE_3:
                        if ((month == 1) ^ (month == 3) ^ (month == 5) ^ (month == 7) ^ (month == 8) ^ (month == 10) ^ (month == 12)) {
                                if (choice == 3) {
                                System.out.print("Enter the number of hours you used your plan package: ");
                        hours = input.nextInt();
                                if (hours > 744) {
                                        System.out.println("Enter the number of hours the plan package was used: ");
                                        System.out.println("Now the program will exit.");
                                        System.exit(1);
                                }
                                else if (hours < 0) {
                                        System.out.println("ERROR: The number of hours cannot be negative.");
                                        System.out.println("Now the program will exit.");
                                        System.exit(1);
                                }
                                else {
                                        System.out.println("The cost of your bill is: $30.99");
                                }
                                }
                                else {
                                        System.out.println("The cost of package 3 bill is: $30.99");
                                        savings_2 = total_1 - total_3d;
                                        System.out.println("You could saved: $" + savings_2);
                                }
                        }
                        else if (month == 2) {
                                if (choice == 3) {
                                System.out.print("Enter the number of hours the plan package was used: ");
                                hours = input.nextInt();
                                if (hours > 672) {
                                        System.out.println("ERROR: The number of hours cannot be higher than 672 on February.");
                                        System.out.println("Now the program will exit.");
                                        System.exit(1);
                                }
                                else if (hours < 0) {
                                        System.out.println("ERROR: The number of hours cannot be negative.");
                                        System.out.println("Now the program will exit.");
                                        System.exit(1);
                                }
                                else {
                                        System.out.println("The cost of your bill is: $30.99");
                                }
                                }
                                else {
                                        System.out.println("The cost of package 3 bill is: $30.99");
                                        savings_2 = total_1 - total_3d;
                                        System.out.println("You could saved: $" + savings_2);
                                }
                        }        
                        else {
                                if (choice == 3) {
                                System.out.print("Enter the number of hours the plan package was used: ");
                                hours = input.nextInt();
                                if (hours > 720) {
                                        System.out.println("ERROR: The number of hours cannot be higher than 720 on the selected month.");
                                        System.out.println("Now the program will exit.");
                                        System.exit(1);
                                }
                                else if (hours < 0) {
                                        System.out.println("ERROR: The number of hours cannot be negative.");
                                        System.out.println("Now the program will exit.");
                                        System.exit(1);
                                }
                                else {
                                        System.out.println("The cost of your bill is: $30.99");
                                }
                                }
                                else {
                                        System.out.println("The cost of package 3 bill is: $30.99");
                                        savings_2 = total_1 - total_3d;
                                        System.out.println("You could saved: $" + savings_2);
                                }
                        }
        }
}
}
HW04P05.java:176: error: variable hours might not have been initialized
                                        if (hours < 20 && hours >= 1) {
                                            ^
HW04P05.java:178: error: variable total_1 might not have been initialized
                                                savings_1 = total_1 - total_2d;
                                                            ^
HW04P05.java:184: error: variable total_1 might not have been initialized
                                                savings_1 = total_1 - total_2d;
                                                            ^
HW04P05.java:214: error: variable hours might not have been initialized
                                        if (hours < 20 && hours >= 1) {
                                            ^
HW04P05.java:216: error: variable total_1 might not have been initialized
                                                savings_1 = total_1 - total_2d;
                                                            ^
HW04P05.java:222: error: variable total_1 might not have been initialized
                                                savings_1 = total_1 - total_2d;
                                                            ^
HW04P05.java:252: error: variable hours might not have been initialized
                                        if (hours < 20 && hours >= 1) {
                                            ^
HW04P05.java:254: error: variable total_1 might not have been initialized
                                                savings_1 = total_1 - total_2d;
                                                            ^
HW04P05.java:260: error: variable total_1 might not have been initialized
                                                savings_1 = total_1 - total_2d;
                                                            ^
HW04P05.java:286: error: variable total_1 might not have been initialized
                                        savings_2 = total_1 - total_3d;
                                                    ^
HW04P05.java:310: error: variable total_1 might not have been initialized
                                        savings_2 = total_1 - total_3d;
                                                    ^
HW04P05.java:334: error: variable total_1 might not have been initialized
                                    savings_2 = total_1 - total_3d;
                                                ^
12 errors

Aucun commentaire:

Enregistrer un commentaire