jeudi 16 novembre 2017

if-statement overwriting my counter everytime in java

I'm trying to write a program that calculates the cost of a train ticket. I'm currently doing the method that adds any discounts to the current cost, I'm using if statements for it but every time it overwrites my previous calculation. How do I stop this? Also, while I'm at it can anyone give me a hint as to how to make it so that it remembers how many tickets they've put in so far e.g. if the user enters that they have 2 under 10's, then says they have 3 under sixteens and then 2 students but they have only bought 5 tickets, how could I catch this and stop it from happening?

here is my method so far

public static double calcDiscounts(double a, double[] b) {

    double counter = 0;
    double current = b[1];
    double amount = b[0];

        System.out.println("You may be valid for discounts. Please answer these questions :"
                + "\nDo you have any under 10's travelling with you?\nType 1 for Yes\nType 2 for no");
        if (input.nextInt() == 1) {
            System.out.println("How many under 10's will be travelling with you?");
            int under = input.nextInt();
            if (under > amount - 1) {
                System.out.println("Invalid input - there must be at least one adult travelling alongside.");
            }
            double temp = a * under;
            counter += current - temp;


            System.out.println("The cost of your ticket(s) is now " + counter);
        }
        else {
            counter  = current;
        }

        System.out.println("Is anybody travelling with you under 16?\nType 1 for Yes\nType 2 for no");
        if (input.nextInt() == 1) {
            System.out.println("How many under 16's will be travelling with you?");
            int sixteen = input.nextInt();
            if (sixteen > amount) {
                System.out.println("Invalid input - you don't have this many tickets.");
            } else {
                double temp = a * 0.50;
                double next = temp * sixteen;
                counter = current - next;
                System.out.println("The cost of your ticket(s) is now " + counter);
            }
        }
        else {
            counter  = current;
        }

        System.out.println("Is anybody travel a student?\nType 1 for Yes\nType 2 for no");
        if (input.nextInt() == 1) {
            System.out.println("How many students will be travelling?");
            int student = input.nextInt();
            if (student > amount) {
                System.out.println("Invalid input - you don't have this many tickets.");
            } else {
                double temp = a * 0.25;
                double next = temp * student;
                counter = current - next;

                System.out.println("The cost of your ticket(s) is now " + counter);
            }
        }
        else {
            counter  = current;
        }

        System.out.println("Is anybody travelling over 60?\nType 1 for Yes\nType 2 for no");
        if (input.nextInt() == 1) {
            System.out.println("How many over 60's will be travelling?");
            int sixty = input.nextInt();
            if (sixty > amount) {
                System.out.println("Invalid input - you don't have this many tickets.");
            } else {
                double temp = a * 0.60;
                double next = temp * sixty;
                counter = current - next;

                System.out.println("The cost of your ticket(s) is now " + counter);
            }
        }
        else {
            counter  = current;
        }
    return counter;
}

Aucun commentaire:

Enregistrer un commentaire