变量未使用正确的值进行更新

时间:2018-11-15 02:09:54

标签: java

我正在为Luas Station用来购买门票的系统制作一个Java程序。 Luas是爱尔兰的一种火车。

我有几种不同类型的门票,涉及各种价格等。在客户选择要购买的门票后,我给他们一个选择,可以返回“ chooseTickets”的开头购买更多的门票。最后,我已将程序中所有客户所需票证的值分配为completePrice变量。这个想法是允许客户继续添加票证,并允许该变量将票证价格不断添加到变量中,直到他们准备付款为止。它可以正常工作,除非我回购票,并且是同一类型,即两次成人票。 2.50的价格不会添加到另一个2.50,而是被另一个2.5覆盖。下面是相同的代码:

import java.util.Scanner; //imports the scanner class to allow for user input

public class ticketPurchase {
//Adding global variables to be used by various methods
static Scanner input = new Scanner(System.in);
static String menuChoice;
static double childTicket = 1.20;
static double adultTicket = 2.50;
static double studentTicket = 1.70;
static double adultFlexi = 12.00;
static double childFlexi = 8.00;
static double studentFlexi;
static String ticketType;
static String ticketChoice;
static int adultTicketQty;
static int childTicketQty;
static int studentTicketQty;
static String destinationZone;
static double adultFinalPrice;
static double childFinalPrice;
static double studentFinalPrice;
static double flexiAdultFinalPrice;
static double flexiChildFinalPrice;
static double completePrice;
static String moreTicketChoice2 = "0";

public static void main(String[] args) {
    menu(); //calling the menu method within the main method to start the process
}
public static void menu() {
    if(moreTicketChoice2.equals("1")){
        System.out.println("Press 2 to purchase Standard Tickets");
        System.out.println("Press 3 to purchase Flexi tickets");
        System.out.println("press 4 to purchase student tickets");
    }
    else if(moreTicketChoice2.equals("0")) {
        System.out.println("Press 1 for information");
        System.out.println("Press 2 to purchase Standard Tickets");
        System.out.println("Press 3 to purchase Flexi tickets");
        System.out.println("press 4 to purchase student tickets");
    }
    menuChoice = input.next(); //allowing user to choose what tickets to buy or just to see information of services

    switch(menuChoice) { //switch statement to record and output information based on users input
    case "1":{ //prints information regarding pricing, ticket age restrictions and support
        System.out.println("The standard ticket may be a single or return ticket for an adult (16+) or a child");
        System.out.println("The flexi ticket covers all journeys for one 24 hour period for either a child or an adult");
        System.out.println("A single ticket's price depends on the journey length, single or return and if it is for an adult or a child");
        System.out.println("a Flexi ticket for a child costs €8.00 and a Flexi ticket for an adult costs €12.00");
        System.out.println("Our Customer Care telephone number for this terminal is 0830462920, please call if further support is required");
        menu();
        break;
    }
    case "2":{
        ticketChoice = "standard"; //records the value of standard within the ticketChoice global variable
        chooseTickets(); //initiates the choose tickets method
        break;
    }
    case "3":{
        ticketChoice = "flexi"; //record the value of Flexi within the ticketChoice global variable
        chooseTickets(); 
        break;
    }
    case "4":{
        ticketChoice = "student";
        chooseTickets();
    }
    case "a":{ //allows user to enter the admin interface 
        admin();
        break;
    }
    default:{ //allows for user to input a value outside of the options and notify's them to try again
        System.out.println("Invalid choice, please choose from 1 to 3");
        break;
    }
    }

}
public static void chooseTickets() { //payment method to choose quantity, destination zone and final price
    System.out.println("You have chosen to purchase " + ticketChoice + " ticket(s)");
    if(ticketChoice.equals("student")) { //allows user to purchase student's tickets
        ticketType = "student";
        System.out.println("Please enter the quantity of tickets: ");
        studentTicketQty = input.nextInt();
        System.out.print("please enter your destination zone (1, 2, 3, 4): ");
        destinationZone = input.next();
        ticketChoice = "student"; 
            studentFinalPrice = (studentTicket*studentTicketQty);
        switch(destinationZone){ //adjusts price to account for the destination's zone chosen by user
        case "1":{
            studentFinalPrice = studentFinalPrice + (studentTicketQty*0);
            break;
        }
        case "2":{
            studentFinalPrice = studentFinalPrice + (studentTicketQty*0.50);
            break;
        }
        case "3":{
            studentFinalPrice = studentFinalPrice + (studentTicketQty*1.0);
            break;
        }
        case "4":{
            studentFinalPrice = studentFinalPrice + (studentTicketQty*1.50);
            break;
        }
        default:{
            System.out.println("you have entered an invalid choice please choose from 1 to 4");
            chooseTickets();
            break;
        }
        }
        System.out.print("would you like to purchase more tickets? enter 1 if so, 2 if not: "); //allows user to purchase other tickets 
        moreTicketChoice2 = input.next();
        if(moreTicketChoice2.equals("1")) {
            menu();
        }
        else if(moreTicketChoice2.equals("2")){
            System.out.println("you have chosen against purchasing more tickets");
            payment();
        }


    }
    else {
        System.out.print("please enter 1 for children's tickets and 2 for adult's: ");
        String ticketAgeGroup = input.next();
        switch(ticketAgeGroup) { //allows user to choose quantity and destination based on choice of adult or child
        case "2":{//case for adult tickets
            System.out.println("you have chosen adults ticket");
            ticketType = "adult";
            System.out.print("Please enter the quantity of tickets: ");
            adultTicketQty = input.nextInt();
            System.out.print("please enter your destination zone (1, 2, 3, 4): ");
            destinationZone = input.next();
            if(ticketChoice == "flexi") { //if statement to calculate the finalPrice variable value if the ticketChoice is Flexi
                flexiAdultFinalPrice = (adultFlexi*adultTicketQty);
            }
            else {
                adultFinalPrice = (adultTicket*adultTicketQty); //else calculates the finalPrice variable value if the ticketChoice is standard
            }
            switch(destinationZone){ // switch statement to calculate the final price depending on the destination's zone and their extra amount.
            case "1":{
                adultFinalPrice = adultFinalPrice + (adultTicketQty*0); 
                break;
            }
            case "2":{
                adultFinalPrice = adultFinalPrice + (adultTicketQty*.50); //calculation to add the extra amount for the destination
                break;
            }
            case "3":{
                adultFinalPrice = adultFinalPrice + (adultTicketQty*1.0);
                break;
            }
            case "4":{
                adultFinalPrice = adultFinalPrice + (adultTicketQty*1.50);
                break;
            }
            default:{
                System.out.println("you have entered an invalid choice please choose from 1 to 4");
                break;
            }
            } //end of the switch statement

            System.out.print("Would you like to purchase more tickets? enter 1 if so, 2 if not: "); //allows user to purchase other tickets 
            moreTicketChoice2 = input.next();
            if(moreTicketChoice2.equals("1")) { //if statement to allow a customer to purchase more tickets if required
                menu();
            }
            else {
                System.out.println("you have chosen against purchasing more tickets");
                payment(); //proceeds to the payment method
            }


            break;
        }

        case "1":{ //case for children's tickets
            System.out.println("you have chosen children's ticket");
            ticketType = "child";
            System.out.println("Please enter the quantity of tickets: ");
            childTicketQty = input.nextInt();
            System.out.print("please enter your destination zone (1, 2, 3, 4): ");
            destinationZone = input.next();
            if(ticketChoice == "flexi") { //adjusts the price if user chooses the flexi option 
                flexiChildFinalPrice = (childFlexi*childTicketQty);
            }
            else {
                childFinalPrice = (childTicket*childTicketQty);
            }
            switch(destinationZone){ //adjusts price to account for the destination's zone chosen by user
            case "1":{
                childFinalPrice = childFinalPrice + (childTicketQty*0);
                break;
            }
            case "2":{
                childFinalPrice = childFinalPrice + (childTicketQty*.50);
                break;
            }
            case "3":{
                childFinalPrice = childFinalPrice + (childTicketQty*1.0);
                break;
            }
            case "4":{
                childFinalPrice = childFinalPrice + (childTicketQty*1.50);
                break;
            }
            default:{
                System.out.println("you have entered an invalid choice please choose from 1 to 4");
                chooseTickets();
                break;
            }
            }

            System.out.print("would you like to purchase more tickets? enter 1 if so, 2 if not: "); //allows user to purchase other tickets 
            moreTicketChoice2 = input.next();
            if(moreTicketChoice2.equals("1")) {
                menu();
            }
            else if(moreTicketChoice2.equals("2")){
                System.out.println("you have chosen against purchasing more tickets");
                payment();
            }
            break;
        }
    }
    }
    }
public static void payment() { //method to complete the payment process for the purchase
    completePrice = adultFinalPrice + childFinalPrice + studentFinalPrice + flexiAdultFinalPrice + flexiChildFinalPrice;
    System.out.println("the total due is €" + completePrice);


}
public static void printTickets() { //method to notify the customer that their tickets are printing 

}
public static void admin() { //method to control the admin's interface

}

}

In this photo i want an output of 2.4 not 1.2

0 个答案:

没有答案
相关问题