Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import java.io.FileWriter; import javafx.application.Application; import javafx.event.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.*; import javafx.geometry.*; public class Orders extends Application { // Interior of
import java.io.FileWriter; import javafx.application.Application; import javafx.event.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.*; import javafx.geometry.*; public class Orders extends Application { // Interior of window // Attributes are GUI components (buttons, text fields, etc.) // are declared here. private Stage stage; // The entire window, including title bar and borders private Scene scene; // Interior of window // Choose a pane ... GridPane used here private VBox root = new VBox(8); private GridPane gpTop = new GridPane(); private FlowPane fpBot = new FlowPane(8,8); // Components private Label lblName = new Label("Item Name:"); private Label lblNum = new Label("Number:"); private Label lblCost = new Label("Cost:"); private Label lblAmount = new Label("Amount:"); private TextField tfName = new TextField(); private TextField tfNum = new TextField(); private TextField tfCost = new TextField(); private TextField tfAmount = new TextField(); //Buttons private Button btn1 = new Button("Calculate"); private Button btn2 = new Button("Save"); private Button btn3 = new Button("Clear"); private Button btn4 = new Button("Exit"); public static final String FILE_NAME ="121Lab1.csv"; // Main just instantiates an instance of this GUI class public static void main(String[] args) { launch(args); } // Called automatically after launch sets up javaFX public void start(Stage _stage) throws Exception { stage = _stage; // save stage as an attribute stage.setTitle("AButera Item Order Calculator"); // set the text in the title bar // add all components gpTop.addRow(0,lblName, tfName); //align right GridPane.setHalignment(lblName, HPos.RIGHT); //add component gpTop.addRow(1,lblNum, tfNum); //align right GridPane.setHalignment(lblNum, HPos.RIGHT); //add component gpTop.addRow(2,lblCost, tfCost); //align right GridPane.setHalignment(lblCost, HPos.RIGHT); //add component gpTop.addRow(3,lblAmount, tfAmount); //align right GridPane.setHalignment(lblAmount, HPos.RIGHT); //add component gpTop.setAlignment(Pos.CENTER); //add buttons fpBot.getChildren().addAll(btn1, btn2, btn3, btn4); fpBot.setAlignment(Pos.CENTER); //Add grid and flowpane root.getChildren().addAll(gpTop,fpBot); scene = new Scene(root, 400, 150); // create scene of specified size // with given layout stage.setScene(scene); // associate the scene with the stage //-----------------------------------------Extra code start from here-------------------------------------------------- //set the amount field not editable tfAmount.setEditable(false); //Creating EventHandler EventHandlerPart 1: GUI Layout (start with Lab01 code) Part 1 is from "Lab01: GUI Applications with 1/0", where you create a GUI that contains right-aligned labels for text fields as shown. Label, field and button object names should be self-documenting representations of the GUI components. Place all of the components in a GridPane, then add the GridPane to the root VBox. Add buttons to control the actions that will take place on the screen. Your screen should now look similar to the screen shot below. Have the "Amount owed" field not allow input. Format the amount owed result to have two decimal places. See the String class in the Javadocs regarding how to format a number (String.format). {Your Name) Item Orders Calcul... Item name: Number of: Cost: Amount Owed: Calculate Save Clear Exit Adding controls for the buttons should start with the easier ones first. [Your Name) Item Orders Calcul Item name: Number of: Exit Clear Calculate Cost: Amount Owed Save Exit the program Clears the text fields. Can setText() to null or (blank) Multiply the "Number of by the cost per item, place result in the Amount owed field. Formatted to two decimal places. Assume all numbers entered are valid. Opens a text file for writing, use file name 121Lab1.csv. Write in comma-separated format, the item's name, the number of items, cost per item and the calculated amount owned. Each time the user clicks Save, first execute the calculate code. The calculation code should only appear once in your program. Each click of Save should append new information to the file. Calculate Save Clear Exit Loadhandler = new EventHandler () { @Override public void handle(ActionEvent evt) { // Get the button that was clicked Button btn = (Button)evt.getSource(); // Switch on its name switch(btn.getText()) { case "Calculate": calculateAmount(); break; case"Save": //first calculate the amount calculateAmount(); //then saving to csv file in append mode String name = tfName.getText(); String number = tfNum.getText(); String cost = tfCost.getText(); String amount = tfAmount.getText(); FileWriter writer = null; if(!name.equals("") && !number.equals("") && !cost.equals("") && !amount.equals("")) { try{ writer = new FileWriter(FILE_NAME,true); //open file in append mode (true) writer.append("'"+name+"'"); writer.append(","); writer.append(number); writer.append(","); writer.append(cost); writer.append(","); writer.append(amount); writer.append(' '); } catch (Exception e) { //System.out.println("Unexpected Exception: "+ e); System.exit(1); }finally { try { writer.flush(); writer.close(); } catch (Exception e) { //e.printStackTrace(); System.exit(1); } } } break; case"Clear": tfName.setText(""); tfNum.setText(""); tfCost.setText(""); tfAmount.setText(""); break; case "Exit": System.exit(0); break; } } }; //adding action handler to all buttons to listen to the actions btn1.setOnAction(handler); btn2.setOnAction(handler); btn3.setOnAction(handler); btn4.setOnAction(handler); stage.show(); // display the stage (window) } //function to calculate the amount private void calculateAmount() { try { String sN1 = tfNum.getText(); String sN2 = tfCost.getText(); double dN1 = Double.parseDouble(sN1); double dN2 = Double.parseDouble(sN2); double dAmt = dN1 * dN2; //set amount formatting to two decimal places String formatted = String.format("%.2f", dAmt); tfAmount.setText("" + formatted); }catch(Exception e){ } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started