Question

How could I take this code and make a JavaFX program with the same results? import java.util.Scanner; public class CellPhoneApp { private static int[] data = {8,16,20}; private static doubl...

How could I take this code and make a JavaFX program with the same results?

import java.util.Scanner;


public class CellPhoneApp {

    private static int[] data = {8,16,20};
    private static double[] dataPrice = {45.00,65,.00,99.00};

    private static int[] model ={100,110,200};
    private static double[] modelPrice= {299.95,399.95,499.95};

    private static String[] options = {"Phone Replacement Insurance","WiFi Hotspot Capability"};
    private static double[] optionsPrice ={5.00,10.00};

    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {

        int planIndex = getDataPlan();
        int modelIndex = getModel();
        int optionIndex = getOption();

        System.out.println("You choose Data Plan : "+data[planIndex]+" gigabytes per month");
        System.out.println("You choose Model     : Model "+model[modelIndex]);
        System.out.println("You choose option    : "+options[optionIndex]);
        double totalCost = dataPrice[planIndex]+modelPrice[modelIndex]+optionsPrice[optionIndex];
        System.out.println("Total Cost           : $"+String.format("%.2f",totalCost));

    }

    // ask user for a data plan until user selects teh right plan
    private static int getDataPlan() {

        while (true){

            System.out.println("[1] 8 gigabyes per month: $45.00 per month");
            System.out.println("[2] 16 gigabyes per month: $65.00 per month");
            System.out.println("[3] 20 gigabyes per month: $99.00 per month");

            System.out.println("Select a data plan (1 or 2 or 3)? ");
            int choice = scanner.nextInt();
            if(choice==1 || choice == 2 || choice ==3 ){
                return choice -1;
            }else{
                System.out.println("Sorry. That is not the right selection. Choose from 1, 2 or 3 only.");
            }
        }
    }
    // ask user for a model until user selects teh right model
    private static int getModel() {

        while (true){

            System.out.println("[1] Model 100: $299.95");
            System.out.println("[2] Model 110: $399.95");
            System.out.println("[3] Model 200: $499.95");

            System.out.println("Select a model (1 or 2 or 3)? ");
            int choice = scanner.nextInt();
            if(choice==1 || choice == 2 || choice ==3 ){
                return choice -1;
            }else{
                System.out.println("Sorry. That is not the right selection. Choose from 1, 2 or 3 only.");
            }
        }
    }
    // ask user for a option until user selects teh right option
    private static int getOption() {

        while (true){

            System.out.println("[1] Phone Replacement Insurance: $5.00 per month");
            System.out.println("[2] WiFi Hotspot Capability: $10.00 per month");

            System.out.println("Select a model (1 or 2)? ");
            int choice = scanner.nextInt();
            if(choice==1 || choice == 2 ){
                return choice -1;
            }else{
                System.out.println("Sorry. That is not the right selection. Choose from 1, or 2 only.");
            }
        }
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Java program to create a combo box and add event handler to it
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.collections.*;
import javafx.stage.Stage;
import javafx.scene.text.Text.*;
import javafx.scene.paint.*;
import javafx.scene.text.*;
public class combo_box_2 extends Application {

   // Launch the application
   public void start(Stage stage)
   {
       // Set title for the stage
       stage.setTitle("creating combo box ");

       // Create a tile pane
       TilePane r = new TilePane();

       // Create a label
       Label description_label =
                       new Label("This is a combo box example ");

       // dataplan
       String data_plan[] =
               { "8 gigabyes per month: $45.00 per month",
               "16 gigabyes per month: $65.00 per month",
               "20 gigabyes per month: $99.00 per month"};
      
       // model
       String model[] =
               { "Model 100: $299.95",
               "Model 110: $399.95",
               "Model 200: $499.95"};
      
       // type
       String type[] =
               { "Phone Replacement Insurance: $5.00 per month",
               "WiFi Hotspot Capability: $10.00 per month"};

       // Create a combo box
       final ComboBox combo_box =
                   new ComboBox(FXCollections
                           .observableArrayList(data_plan));
      
       final ComboBox combo_box2 =
               new ComboBox(FXCollections
                       .observableArrayList(model));
      
       final ComboBox combo_box3 =
               new ComboBox(FXCollections
                       .observableArrayList(type));
       // create a button
Button b = new Button("submit");

       // Label to display the selected menuitem
       final Label selected = new Label("");

      
       // Create action event
       EventHandler<ActionEvent> event =
               new EventHandler<ActionEvent>() {
           public void handle(ActionEvent e)
           {
               try{
               String plan = (String) combo_box.getValue();
               String model = (String) combo_box2.getValue();
               String option = (String) combo_box3.getValue();
              
               int planIndex = 0,modelIndex = 0,optionIndex = 0;
              
               if(plan.contains("8 gigabyes")){plan = "8 gigabyes per month";planIndex = 0;}
               if(plan.contains("16 gigabyes")){plan = "16 gigabyes per month";planIndex = 1;}
               if(plan.contains("20 gigabyes")){plan = "20 gigabyes per month";planIndex = 2;}
              
               if(model.contains("Model 100")){model = "Model 100";modelIndex = 0;}
               if(model.contains("Model 110")){model = "Model 110";modelIndex = 1;}
               if(model.contains("Model 200")){model = "Model 200";modelIndex = 2;}
              
               if(option.contains("Phone")){option = "Phone Replacement Insurance";optionIndex = 0;}
               if(option.contains("WiFi")){option = "WiFi Hotspot Capability";optionIndex = 1;}
              
           double[] dataPrice = {45.00,65.00,99.00};
           double[] modelPrice= {299.95,399.95,499.95};
           double[] optionsPrice ={5.00,10.00};
           double totalCost = dataPrice[planIndex]+modelPrice[modelIndex]+optionsPrice[optionIndex];
          
               selected.setText("You choose Data Plan : "+plan+
                               "\nYou choose Model : "+model+
                               "\nYou choose option : "+option+
                               "\nTotal Cost : $"+totalCost);
               }catch(Exception exception){
                   selected.setText("Select All Fileds");
               }
           }
       };
      

       // Set on action
       b.setOnAction(event);
              
       // Create a tile pane
       TilePane tile_pane = new TilePane(combo_box,combo_box2,combo_box3,b, selected);

       // Create a scene
       Scene scene = new Scene(tile_pane, 290, 350);

       // Set the scene
       stage.setScene(scene);

       stage.show();
   }

   public static void main(String args[])
   {
       // Launch the application
       launch(args);
   }
}

-D creating combo box 20 gigabyes per month: $99.00 per month Model 110: $399.95 WiFi Hotspot Capability: $10.00 per month su

Add a comment
Know the answer?
Add Answer to:
How could I take this code and make a JavaFX program with the same results? import java.util.Scanner; public class CellPhoneApp { private static int[] data = {8,16,20}; private static doubl...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • package cards; import java.util.ArrayList; import java.util.Scanner; public class GamePlay { static int counter = 0; private...

    package cards; import java.util.ArrayList; import java.util.Scanner; public class GamePlay { static int counter = 0; private static int cardNumber[] = {1,2,3,4,5,6,7,8,9,10,11,12,13}; private static char suitName[] = { 'c', 'd', 'h', 's' }; public static void main(String[] args) throws CardException, DeckException, HandException { Scanner kb = new Scanner(System.in); System.out.println("How many Players? "); int numHands = kb.nextInt(); int cards = 0; if (numHands > 0) { cards = 52 / numHands; System.out.println("Each player gets " + cards + " cards\n"); } else...

  • Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args)...

    Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...

  • I need the following java code commented import java.util.Scanner; public class Main { public static void...

    I need the following java code commented import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner (System.in); int productNo=0; double product1; double product2; double product3; double product4; double product5; int quantity; double totalsales=0; while(productNo !=0) System.out.println("Enter Product Number 1-5"); productNo=input.nextInt(); System.out.println("Enter Quantity Sold"); quantity=input.nextInt(); switch (productNo) { case 1: product1=1.00; totalsales+=(1.00*quantity); break; case 2: product2=2.00; totalsales+=(2.00*quantity); break; case 3: product3=6.00; totalsales+=(6.00*quantity); break; case 4: product4=23.00; totalsales+=(23.00*quantity); break; case 5: product5=17.00; totalsales+=(17.00*quantity); break;...

  • import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y,...

    import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y, z; double average; Scanner scan = new Scanner(System.in); System.out.println("Enter an integer value"); x = scan.nextInt( ); System.out.println("Enter another integer value"); y = scan.nextInt( ); System.out.println("Enter a third integer value"); z = scan.nextInt( ); average = (x + y + z) / 3; System.out.println("The result of my calculation is " + average); } } What is output if x = 0, y = 1 and...

  • import java.util.Scanner; public class MPGMain {    public static void main(String[] args)    {       ...

    import java.util.Scanner; public class MPGMain {    public static void main(String[] args)    {        Scanner input = new Scanner(System.in);               Mileage mileage = new Mileage();               System.out.println("Enter your miles: ");        mileage.setMiles(input.nextDouble());               System.out.println("Enter your gallons: ");        mileage.setGallons(input.nextDouble());               System.out.printf("MPG : %.2f",mileage.getMPG());           } } public class Mileage {    private double miles;    private double gallons;    public double getMiles()...

  • Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args)...

    Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); final int maxSize = 128; String[] titles = new String[maxSize]; int[] lengths = new int[maxSize]; int numDVDs = 0; String op; op = menu(stdIn); System.out.println(); while (!op.equalsIgnoreCase("q")) { if (op.equalsIgnoreCase("a")) { if (numDVDs < maxSize) numDVDs = addDVD(titles, lengths, numDVDs, stdIn); } else if (op.equalsIgnoreCase("t")) searchByTitle(titles, lengths, numDVDs, stdIn);    else if (op.equalsIgnoreCase("l")) searchByLength(titles, lengths, numDVDs, stdIn); System.out.println('\n');...

  • This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static...

    This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static void main(String args[]) throws IOException { Scanner inFile = new Scanner(new File(args[0])); Scanner keyboard = new Scanner(System.in);    TwoDArray array = new TwoDArray(inFile); inFile.close(); int numRows = array.getNumRows(); int numCols = array.getNumCols(); int choice;    do { System.out.println(); System.out.println("\t1. Find the number of rows in the 2D array"); System.out.println("\t2. Find the number of columns in the 2D array"); System.out.println("\t3. Find the sum of elements...

  • Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args)...

    Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args) { int[] matrix = {{1,2},{3,4},{5,6},{7,8}}; int columnChoice; int columnTotal = 0; double columnAverage = 0; Scanner input = new Scanner(System.in); System.out.print("Which column would you like to average (1 or 2)? "); columnChoice = input.nextInt(); for(int row = 0;row < matrix.length;++row) { columnTotal += matrix[row][columnChoice]; } columnAverage = columnTotal / (float) matrix.length; System.out.printf("\nThe average of column %d is %.2f\n", columnAverage, columnAverage); } } This program...

  • Hi. Could you help me with the code below? I need to validate the code below, using expressions that can carry out the actions or that make appropriate changes to the program’s state, using conditiona...

    Hi. Could you help me with the code below? I need to validate the code below, using expressions that can carry out the actions or that make appropriate changes to the program’s state, using conditional and iterative control structures that repeat actions as needed. The unit measurement is missing from the final output and I need it to offer options to lbs, oz, grams, tbsp, tsp, qt, pt, and gal. & fl. oz. Can this be added? The final output...

  • Taking this program how can the requirements under it be added in java? import java.util.Scanner; public...

    Taking this program how can the requirements under it be added in java? import java.util.Scanner; public class RetirementSavings {    private static final int MAX_SALARY = 300000;    private static final double MAX_SAVING_RATE = 0.3;    private static final double MAX_INTEREST_RATE = 0.2;    private static final int MAX_YEAR_EMPLOYED = 40;    public static void main(String[] args) {        double savings_rate, interest_rate;        int salary, years_employed;        String name = ""; // name is initialized with an...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT