Variables aren't getting updated with the proper values












0















I'm making a java program for the system used at Luas Station to purchase tickets. The Luas is a type of train in the Ireland.



I have a few different types of tickets which involves various prices etc. After a customer selects what ticket to buy I give them an option to return to the beginning of the 'chooseTickets' to buy more tickets. At the end, the program I have assigned the value of all of the customer's desired tickets as the completePrice variable. the idea is to allow for the customer to continue adding tickets and for that variable to continuously add the prices of them to the variable until they're ready to pay. It works except when I go back to purchase for tickets and it is the same type, i.e a standard adult ticket twice. The price of 2.50 is not added to another 2.50, it is overwritten by another 2.5. Below is the code for the same:



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










share|improve this question




















  • 1





    if anyone could look through my code and make any suggestions i would really appreciate it. What code?

    – GBlodgett
    Nov 15 '18 at 2:11











  • sorry! added it now

    – Sean Kelly
    Nov 15 '18 at 2:14






  • 2





    Are you asking why adultFinalPrice = (adultTicket*adultTicketQty); overwrites adultFinalPrice?

    – shmosel
    Nov 15 '18 at 2:21








  • 1





    I think you wanted adultFinalPrice += (adultTicket*adultTicketQty);

    – GBlodgett
    Nov 15 '18 at 2:25






  • 1





    Plus, calling a method within itself is a bad habit to repeat something, use a proper while loop, then ticketChoice == "flexi" isn't how to compare strings... Other than that, might be a good idea to follow a tutorial on using a debugger in your favorite IDE

    – cricket_007
    Nov 15 '18 at 2:36
















0















I'm making a java program for the system used at Luas Station to purchase tickets. The Luas is a type of train in the Ireland.



I have a few different types of tickets which involves various prices etc. After a customer selects what ticket to buy I give them an option to return to the beginning of the 'chooseTickets' to buy more tickets. At the end, the program I have assigned the value of all of the customer's desired tickets as the completePrice variable. the idea is to allow for the customer to continue adding tickets and for that variable to continuously add the prices of them to the variable until they're ready to pay. It works except when I go back to purchase for tickets and it is the same type, i.e a standard adult ticket twice. The price of 2.50 is not added to another 2.50, it is overwritten by another 2.5. Below is the code for the same:



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










share|improve this question




















  • 1





    if anyone could look through my code and make any suggestions i would really appreciate it. What code?

    – GBlodgett
    Nov 15 '18 at 2:11











  • sorry! added it now

    – Sean Kelly
    Nov 15 '18 at 2:14






  • 2





    Are you asking why adultFinalPrice = (adultTicket*adultTicketQty); overwrites adultFinalPrice?

    – shmosel
    Nov 15 '18 at 2:21








  • 1





    I think you wanted adultFinalPrice += (adultTicket*adultTicketQty);

    – GBlodgett
    Nov 15 '18 at 2:25






  • 1





    Plus, calling a method within itself is a bad habit to repeat something, use a proper while loop, then ticketChoice == "flexi" isn't how to compare strings... Other than that, might be a good idea to follow a tutorial on using a debugger in your favorite IDE

    – cricket_007
    Nov 15 '18 at 2:36














0












0








0








I'm making a java program for the system used at Luas Station to purchase tickets. The Luas is a type of train in the Ireland.



I have a few different types of tickets which involves various prices etc. After a customer selects what ticket to buy I give them an option to return to the beginning of the 'chooseTickets' to buy more tickets. At the end, the program I have assigned the value of all of the customer's desired tickets as the completePrice variable. the idea is to allow for the customer to continue adding tickets and for that variable to continuously add the prices of them to the variable until they're ready to pay. It works except when I go back to purchase for tickets and it is the same type, i.e a standard adult ticket twice. The price of 2.50 is not added to another 2.50, it is overwritten by another 2.5. Below is the code for the same:



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










share|improve this question
















I'm making a java program for the system used at Luas Station to purchase tickets. The Luas is a type of train in the Ireland.



I have a few different types of tickets which involves various prices etc. After a customer selects what ticket to buy I give them an option to return to the beginning of the 'chooseTickets' to buy more tickets. At the end, the program I have assigned the value of all of the customer's desired tickets as the completePrice variable. the idea is to allow for the customer to continue adding tickets and for that variable to continuously add the prices of them to the variable until they're ready to pay. It works except when I go back to purchase for tickets and it is the same type, i.e a standard adult ticket twice. The price of 2.50 is not added to another 2.50, it is overwritten by another 2.5. Below is the code for the same:



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







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 6:56









Abhinav

392412




392412










asked Nov 15 '18 at 2:09









Sean KellySean Kelly

53




53








  • 1





    if anyone could look through my code and make any suggestions i would really appreciate it. What code?

    – GBlodgett
    Nov 15 '18 at 2:11











  • sorry! added it now

    – Sean Kelly
    Nov 15 '18 at 2:14






  • 2





    Are you asking why adultFinalPrice = (adultTicket*adultTicketQty); overwrites adultFinalPrice?

    – shmosel
    Nov 15 '18 at 2:21








  • 1





    I think you wanted adultFinalPrice += (adultTicket*adultTicketQty);

    – GBlodgett
    Nov 15 '18 at 2:25






  • 1





    Plus, calling a method within itself is a bad habit to repeat something, use a proper while loop, then ticketChoice == "flexi" isn't how to compare strings... Other than that, might be a good idea to follow a tutorial on using a debugger in your favorite IDE

    – cricket_007
    Nov 15 '18 at 2:36














  • 1





    if anyone could look through my code and make any suggestions i would really appreciate it. What code?

    – GBlodgett
    Nov 15 '18 at 2:11











  • sorry! added it now

    – Sean Kelly
    Nov 15 '18 at 2:14






  • 2





    Are you asking why adultFinalPrice = (adultTicket*adultTicketQty); overwrites adultFinalPrice?

    – shmosel
    Nov 15 '18 at 2:21








  • 1





    I think you wanted adultFinalPrice += (adultTicket*adultTicketQty);

    – GBlodgett
    Nov 15 '18 at 2:25






  • 1





    Plus, calling a method within itself is a bad habit to repeat something, use a proper while loop, then ticketChoice == "flexi" isn't how to compare strings... Other than that, might be a good idea to follow a tutorial on using a debugger in your favorite IDE

    – cricket_007
    Nov 15 '18 at 2:36








1




1





if anyone could look through my code and make any suggestions i would really appreciate it. What code?

– GBlodgett
Nov 15 '18 at 2:11





if anyone could look through my code and make any suggestions i would really appreciate it. What code?

– GBlodgett
Nov 15 '18 at 2:11













sorry! added it now

– Sean Kelly
Nov 15 '18 at 2:14





sorry! added it now

– Sean Kelly
Nov 15 '18 at 2:14




2




2





Are you asking why adultFinalPrice = (adultTicket*adultTicketQty); overwrites adultFinalPrice?

– shmosel
Nov 15 '18 at 2:21







Are you asking why adultFinalPrice = (adultTicket*adultTicketQty); overwrites adultFinalPrice?

– shmosel
Nov 15 '18 at 2:21






1




1





I think you wanted adultFinalPrice += (adultTicket*adultTicketQty);

– GBlodgett
Nov 15 '18 at 2:25





I think you wanted adultFinalPrice += (adultTicket*adultTicketQty);

– GBlodgett
Nov 15 '18 at 2:25




1




1





Plus, calling a method within itself is a bad habit to repeat something, use a proper while loop, then ticketChoice == "flexi" isn't how to compare strings... Other than that, might be a good idea to follow a tutorial on using a debugger in your favorite IDE

– cricket_007
Nov 15 '18 at 2:36





Plus, calling a method within itself is a bad habit to repeat something, use a proper while loop, then ticketChoice == "flexi" isn't how to compare strings... Other than that, might be a good idea to follow a tutorial on using a debugger in your favorite IDE

– cricket_007
Nov 15 '18 at 2:36












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311436%2fvariables-arent-getting-updated-with-the-proper-values%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311436%2fvariables-arent-getting-updated-with-the-proper-values%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values