Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am trying to make a GUI shipping calculator for the has you enter information and when you submit it it will give you the

I am trying to make a GUI shipping calculator for the has you enter information and when you submit it it will give you the ground total. It also has to have the output write to a text file for each country that is used. I have the code down for the console and have written the GUI to how it should look, but am not sure how to convert the same program that works fine in the console through the GUI. Please re-write the following code to imlement input and display on the GUI:

import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.InputMismatchException; import java.util.Scanner; import java.util.stream.IntStream;

import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.RadioButton; import javafx.scene.control.TextField; import javafx.scene.control.ToggleGroup; import javafx.scene.layout.GridPane; import javafx.stage.Stage;

/** *

ProgrammingAssingment_4

*

The ProgramminAssigment_4 program implements an application * that calculates shipping cost per country, per package, per weight.

* * */ public class ProgrammingAssignment_4 extends Application{ @Override public void start(Stage primaryStage) throws Exception { TextField comp = new TextField(); TextField addr = new TextField(); TextField weightclass1 = new TextField(); TextField weightclass2= new TextField(); TextField weightclass3= new TextField(); TextField weightclass4= new TextField(); TextField gtotal= new TextField(); Button btreset = new Button("Reset"); Button btsubmit = new Button("Submit"); final ToggleGroup group = new ToggleGroup();

RadioButton rb1 = new RadioButton("Local(US)"); rb1.setToggleGroup(group); rb1.setSelected(true);

RadioButton rb2 = new RadioButton("Canada"); rb2.setToggleGroup(group); RadioButton rb3 = new RadioButton("South America"); rb3.setToggleGroup(group); RadioButton rb4 = new RadioButton("Europe"); rb4.setToggleGroup(group); GridPane pane = new GridPane(); pane.setAlignment(Pos.CENTER); pane.setPadding(new Insets(50, 50, 50, 50)); pane.setHgap(10); pane.setVgap(10); pane.add(new Label("Cost of Shipping Calculator"), 1, 0); pane.add(new Label("Company Name: "), 0, 1); pane.add(comp, 1, 1); pane.add(new Label("Address: "), 0, 2); pane.add(addr, 1, 2); pane.add(new Label("Shipping to: "), 0, 3); pane.add(new Label("Now shipping to...................."),0,6); pane.add(new Label("0() {

@Override public void handle(ActionEvent e) { comp.clear(); addr.clear(); weightclass1.clear(); weightclass2.clear(); weightclass3.clear(); weightclass4.clear(); gtotal.clear(); } }); btsubmit.setOnAction(new EventHandler() {

@Override public void handle(ActionEvent e) { btsubmit.setText(gtotal.getText()); } }); } /** * This method is used to get a valid integer input between 1 and 4 from user. * Has a retry mechanism for a count of 32 * @return int */ public static int getUserInputBetween1And4() { Scanner input = new Scanner(System.in); int retries = 0; int retVal = 0;

while(retries < 1) { try { retVal = input.nextInt(); if(retVal < 1 || retVal > 4) { System.out.println("You have entered an invalid input. Please enter a valid input. Attempt 2: "); retries++; } else { break; } } catch (InputMismatchException e) { System.out.println("You have entered an invalid input. Please enter a valid input. Attempt 2: "); input.nextLine(); retries++; } } if (retries == 1) { retVal = input.nextInt(); if(retVal < 1 || retVal > 4) { System.out.println("You have entered two wrong entries, this service will be terminated if your next entry is wrong: "); retries++; } } if (retries == 2) { retVal = input.nextInt(); if(retVal < 1 || retVal > 4) { System.out.println("Program will exit"); System.exit(-1); } } return retVal; }

/** * Main method of the program. * Entire computation and method calls sit here. * @param args */ public static void main(String [] args){ //Scanner to take all users input Scanner input = new Scanner(System.in); //Declare variables int shippingLocation; String another; double totalShipping = 0; //variable for cost of all locations int counter = 0; String companyname = null; String address = null; String country = null;

BufferedWriter bw = null; FileWriter fw = null; String outFile = "ProgrammingAssignment_4.txt";

System.out.println("Enter the company name: "); companyname = input.nextLine();

System.out.println("Enter the address: "); address = input.nextLine();

//Output file to be written try { fw = new FileWriter(outFile); bw = new BufferedWriter(fw); bw.write("Company name: " + companyname); bw.newLine(); bw.write(" Address: " + address); bw.newLine(); bw.write("Shipping to:"); bw.newLine(); } catch (IOException e) { e.printStackTrace(); }

do{ //Displays the available shipping locations and prompts user for location System.out.println("Shipping Locations: " + "1, If Local(US) " + "2, If Canada " + "3, If South America " + "4, If Europe "); System.out.print("Select a shipping location: "); shippingLocation = getUserInputBetween1And4();

//creates an array for the number of packages the users can ship. //User can only input weights between 4 ranges int[] packageArray = new int[4];

/* Prompts the user for the quantity of packages within the given ranges, the input is entered to package array index 0 through 3. */ System.out.println("Enter the number of packages shipped or 0 if no packages."); for(int i=0; i < packageArray.length; i++) { if (i == 0) { System.out.print("Packages of weight range 0 < w <= 1: "); } if (i == 1) { System.out.print("Packages of weight range 1 < w <= 3: "); } if (i == 2) { System.out.print("Packages of weight range 3 < w <= 10: "); } if (i == 3) { System.out.print("Packages of weight range 10 < w <= 20: "); }

packageArray[i] = getUserInputBetween1And4(); } /* Outputs to console the cost of shipping and method calls for all shipping locations shipping Locally(US) */

double cost; if(shippingLocation == 1){ country = "US"; cost = localShipment(packageArray); totalShipping = totalShipping + cost;

System.out.println(" Cost of shipping to Local(US): $" +cost); } //Shipping to Canada else if(shippingLocation == 2){ country = "Canada"; cost = canadaShipment(packageArray); totalShipping = totalShipping + cost; System.out.println(" Cost of shipping to Canada: $" + cost); } //Shipping to South America else if(shippingLocation == 3){ country = "South America"; cost = southAmericaShipment(packageArray);

totalShipping = totalShipping + cost; System.out.println(" Cost of shipping: $" + cost); } //Shipping to Europe else{ country = "Europe"; cost = europeShipment(packageArray); totalShipping = totalShipping + cost; System.out.println(" Cost of shipping: $" + cost); }

//Write the country name and cost of shipping along with total packages to the output file. try { bw.write(String.format("%20s %15s", country+ ": ", "$" + cost)); bw.newLine(); } catch (IOException e) { e.printStackTrace(); } //prompt user whether they want to ship packages to a different location input = new Scanner(System.in); System.out.print("Do you wish to ship to a different location? (y/n)"); another =input.nextLine(); counter++;

//"do-while" location entered is less than 4 and if the user enters y for yes. //This loop terminated when user enters n for no. }while(counter < 4 && another.equalsIgnoreCase("yes") || another.equalsIgnoreCase("y")); //Displays the cost of shipping to all locations entered by user. System.out.println(" Total shipping cost to selected locations: $" + totalShipping); //Write total cost to the file try { bw.write(String.format("%20s %15s", "Grand total: ", "$" + totalShipping)); bw.newLine(); } catch (IOException ex) { ex.printStackTrace(); } finally { try { if (bw != null) bw.close(); if (fw != null) fw.close(); } catch (IOException ex) { ex.printStackTrace(); } } Application.launch(args); }

/** * Local shipment method called from main. * This return method calculates the cost of shipping by taking user input from package array * and multiplying by the price of then weight. This return method returns the cost of shipping. * @param packageArray * @return double Cost of shipment */ public static double localShipment(int[] packageArray){ double total = 0; for (int i =0; i < packageArray.length; i++) { double current =0; if (i == 0) { current = 3.5 * packageArray[i]; }else if (i == 1) { current = 5.5 * packageArray[i]; } else if (i == 2) { current = 8.5 * packageArray[i]; } else if (i == 3) { current = 10.5 * packageArray[i]; } total = total + current; } return total; } /** * Canada shipment method called from main. * This return method calculates the cost of shipping by taking user input from package array * and multiplying by the price of then weight. This return method returns the cost of shipping. * @param packageArray * @return double Cost of shipment */ public static double canadaShipment(int[] packageArray){ double total = 0; for (int i =0; i < packageArray.length; i++) { double current =0; if (i == 0) { current = 4.5 * packageArray[i]; }else if (i == 1) { current = 6.5 * packageArray[i]; } else if (i == 2) { current = 10.5 * packageArray[i]; } else if (i == 3) { current = 12.5 * packageArray[i]; } total = total + current; } return total; }

/** * South America shipment method called from main. * This return method calculates the cost of shipping by taking user input from package array * and multiplying by the price of then weight. This return method returns the cost of shipping. * @param packageArray * @return double Cost of shipment */ public static double southAmericaShipment(int[] packageArray){ double total = 0; for (int i =0; i < packageArray.length; i++) { double current =0;

if (i == 0) { current = 5.5 * packageArray[i]; }else if (i == 1) { current = 7.5 * packageArray[i]; } else if (i == 2) { current = 9.5 * packageArray[i]; } else if (i == 3) { current = 11.5 * packageArray[i]; } total = total + current; } return total; }

/** * Europe shipment method called from main. * This return method calculates the cost of shipping by taking user input from package array * and multiplying by the price of then weight. This return method returns the cost of shipping. * @param packageArray * @return double Cost of shipment */ public static double europeShipment(int[] packageArray){ double total = 0; for (int i =0; i < packageArray.length; i++) { double current =0; // if (i == 0) { current = 7.5 * packageArray[i]; }else if (i == 1) { current = 9.5 * packageArray[i]; } else if (i == 2) { current = 11.0 * packageArray[i]; } else if (i == 3) { current = 15.0 * packageArray[i]; } total = total + current; } return total; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

A Complete Guide To Data Science Essentials

Authors: Miguel

1st Edition

9358684992, 978-9358684995

More Books

Students also viewed these Databases questions

Question

Why could the Robert Bosch approach make sense to the company?

Answered: 1 week ago