Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a bug in the program below. I'm suppose to highlight the lowest and largest value in each column. However it is only WORKING

I have a bug in the program below. I'm suppose to highlight the lowest and largest value in each column. However it is only WORKING when there's one value in the column. I'll list the code and the file I'm running with.

Driver

=========

/** * MvGuiFx, Class representing the GUI for Displaying a DisneyWorld District 5 Sales Report * @author Jeannette Kartchner */

import java.io.File; import java.io.FileNotFoundException; import java.text.NumberFormat;

import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.HPos; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.Tooltip; import javafx.stage.FileChooser; import javafx.stage.Stage; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.text.Font;

public class MyGuiFx extends Application {; private double[][] sales; public static final int MAX_STORES = 6; public static final int MAX_ITEMS = 6; private NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(); Button readFileBtn, exitBtn, copyFileBtn; GridPane dataPane;

/** * Lets the user choose a file to read the sales information and displays * the information on the screen * @throws FileNotFoundException */ public void readFile() throws FileNotFoundException { File selectedFile;

FileChooser chooser = new FileChooser(); chooser.setTitle("Choose a file to read retail items' sales information"); if ((selectedFile = chooser.showOpenDialog(null)) != null) { // Read the file sales = TwoDimRaggedArrayUtility.readFile(selectedFile); } //clear table clearTable(); //display on the screen int row,col; double total; for(row=0;row

//display row totals for(row=0;row

//find the row with largest number of columns int columns = 0; for(row=0;row columns) columns = sales[row].length;

//display column totals for(col=0;col

//find highest in each column for(col=0;col

//find lowest in each column for(col=0;col

} /** * Lets the user choose name and location to copy the file used for * the information on the screen * @throws FileNotFoundException */ public void copyFile() throws FileNotFoundException { File selectedFile;

FileChooser chooser = new FileChooser(); chooser.setTitle("Name and Location of copied file"); if ((selectedFile = chooser.showSaveDialog(null)) != null) { // Read the file TwoDimRaggedArrayUtility.writeToFile(sales,selectedFile); } }

// Handler class. private class ButtonEventHandler implements EventHandler { @Override public void handle(ActionEvent e) { //handler for Load Sales Data if (e.getSource() == readFileBtn) {

try { readFile(); copyFileBtn.setVisible(true); } catch (FileNotFoundException e1) { e1.printStackTrace(); } }

//handler for Load Sales Data if (e.getSource() == copyFileBtn) {

try { copyFile(); } catch (FileNotFoundException e1) { e1.printStackTrace(); }

//handler for Exit button } else if (e.getSource() == exitBtn)

System.exit(0); } }

@Override public void start(Stage stage) { Tooltip buttonToolTipArr[] = new Tooltip[5]; buttonToolTipArr[0] = new Tooltip( "Load sales data from a file and Display"); buttonToolTipArr[1] = new Tooltip("Exit Application");

// Main Pane BorderPane MainPane = new BorderPane();

// Create Title Pane, add title label and add it to the top of the Main // Pane HBox titlePanel = new HBox(); titlePanel.setAlignment(Pos.BASELINE_CENTER); Label titleLbl = new Label("DisneyWorld District 5 Sales Report "); titleLbl.setFont(new Font(30)); titleLbl.setTextFill(Color.BLUE);

titlePanel.getChildren().add(titleLbl); MainPane.setTop(titlePanel);

// CenterPane VBox centerPane = new VBox(); centerPane.setAlignment(Pos.CENTER);

// columnHeader Pane HBox columnHeaderPane = new HBox(10); columnHeaderPane.setAlignment(Pos.CENTER);

int i,j; dataPane = new GridPane(); dataPane.setAlignment(Pos.BASELINE_CENTER); dataPane.add(new Label(" "), 0, 0); dataPane.add(new Label("Books"), 1, 0); dataPane.add(new Label("Tsum Tsum"), 2, 0); dataPane.add(new Label("Trading Pins"), 3, 0); dataPane.add(new Label("Star Wars"), 4, 0); dataPane.add(new Label("Lego"), 5, 0); dataPane.add(new Label("Marvel"), 6, 0); dataPane.add(new Label("Total"), 7, 0); for(i=1;i<8;i++) { dataPane.add(new Label(" "), 0,i); for(j = 1; j<8;j++) dataPane.add(new TextField(), i,j); } dataPane.add(new Label("Emporium"), 0, 1); dataPane.add(new Label("World Traveler"), 0, 2); dataPane.add(new Label("Discovery Trading Center"), 0, 3); dataPane.add(new Label("Merchant of Venus"), 0, 4); dataPane.add(new Label("Once Upon a Toy"), 0, 5); dataPane.add(new Label("Tatooine Traders"), 0, 6); dataPane.add(new Label("Total"), 0, 7); //put in buttons and labels for high and low Button high = new Button(" "); high.setStyle("-fx-background-color: green;"); dataPane.setHalignment(high, HPos.RIGHT);

Button low = new Button(" "); low.setStyle("-fx-background-color: red;"); dataPane.setHalignment(low, HPos.RIGHT); dataPane.add(high, 0,9); dataPane.add(low, 0,10); dataPane.add(new Label("Highest Sales in Category"), 1, 9); dataPane.add(new Label("Lowest Sales in Category"), 1,10);

// Create bottom Pane HBox bottomPane = new HBox(10); bottomPane.setAlignment(Pos.BASELINE_CENTER);

// Create buttons readFileBtn = new Button("Load Sales Data"); readFileBtn.setTooltip(buttonToolTipArr[0]); copyFileBtn = new Button("Copy File"); copyFileBtn.setTooltip(buttonToolTipArr[1]); copyFileBtn.setVisible(false); exitBtn = new Button("Exit"); exitBtn.setTooltip(buttonToolTipArr[2]);

// add event handler to buttons readFileBtn.setOnAction(new ButtonEventHandler()); copyFileBtn.setOnAction(new ButtonEventHandler()); exitBtn.setOnAction(new ButtonEventHandler());

// add buttons to bottomPane bottomPane.getChildren().addAll(readFileBtn, copyFileBtn, exitBtn); MainPane.setBottom(bottomPane);

// add panes to center pane centerPane.getChildren().addAll(dataPane);

MainPane.setCenter(centerPane);

Scene scene = new Scene(MainPane, 1200, 400); stage.setScene(scene);

// Set stage title and show the stage. stage.setTitle("District Sales Report"); stage.show(); }

/** * Clears the numeric fields on the screen */ public void clearTable() { TextField field= new TextField(); int i,j; for(i=1;i<8;i++) { for(j = 1; j<8;j++) dataPane.add(new TextField(), i,j); } } public static void main(String[] args) { launch(args); } }

Utility code

===========

import java.io.*; import java.util.Scanner;

public class TwoDimRaggedArrayUtility {

public static double[][] readFile(File file) { double[][] arr = null; try { Scanner in = new Scanner(file); int rows = 0; while(in.hasNextLine()){ ++rows; in.nextLine(); } in.close(); in = new Scanner(file); String line; arr = new double[rows][]; for(int i = 0; i < arr.length; ++i){ line = in.nextLine(); String[] nums = line.split(" "); arr[i] = new double[nums.length]; for(int j = 0; j < arr[i].length; ++j){ arr[i][j] = Double.valueOf(nums[j]); } } in.close(); } catch (IOException e) { e.printStackTrace(); } return arr; }

public static void writeToFile(double[][] arr, File file){ try { BufferedWriter bw = new BufferedWriter(new FileWriter(file)); for(int i = 0; i < arr.length; ++i){ for(int j = 0; j < arr[i].length; ++j){ bw.write(arr[i][j] + " "); } bw.write(" "); } bw.close(); } catch (IOException e) { e.printStackTrace(); } }

public static double getTotal(double[][] arr){ double total = 0; for(int i = 0; i < arr.length; ++i){ for(int j = 0; j < arr[i].length; ++j){ total += arr[i][j]; } } return total; }

public static double getRowTotal(double[][] arr, int row) { double total = 0; for(int i = 0; i < arr[row].length; ++i){ total += arr[row][i]; } return total; }

public static double getColumnTotal(double[][] arr, int col) { double total = 0; for(int i = 0; i < arr.length; ++i){ if(col < arr[i].length) total += arr[i][col]; } return total; }

public static double getLowestInColumn(double[][] arr, int col) { double total = 0; for(int i = 0; i < arr.length; ++i){ if(col < arr[i].length) total += arr[i][col]; } return total; }

public static double getHighestInColumn(double[][] arr, int col) { double max = Double.NEGATIVE_INFINITY; for(int i = 0; i < arr.length; ++i){ if(col < arr[i].length && arr[i][col] < max) max = arr[i][col]; } return max; } public static double getHighestInRow(double[][] dataSet1, int i) {

// TODO Auto-generated method stub

double max = dataSet1[i][0];

if(dataSet1.length!=0){

for(int j=1;j

if(dataSet1[i][j] < max)

max = dataSet1[i][j];

}

}

return max;

}

public static double getHighestInArray(double[][] dataSet2) {

// TODO Auto-generated method stub

double max = dataSet2[0][0];

for(int i=0;i

for(int j=0;j

if(dataSet2[i][j] < max )

max = dataSet2[i][j];

}

}

return max;

}

public static double getLowestInArray(double[][] dataSet1) {

// TODO Auto-generated method stub

double min = dataSet1[0][0];

for(int i=0;i

for(int j=0;j

if(dataSet1[i][j] > min )

min = dataSet1[i][j];

}

}

return min;

}

public static double getAverage(double[][] dataSet1) {

// TODO Auto-generated method stub

double sum = 0;

int count =0;

for(int i=0;i

for(int j=0;j

count++;

sum = sum + dataSet1[i][j];

}

}

return (sum/count);

}

public static double getLowestInRow(double[][] dataSet1, int i) {

// TODO Auto-generated method stub

double min = dataSet1[i][0];

for(int j=0;j

if(dataSet1[i][j] > min )

min = dataSet1[i][j];

}

return min;

}

}

problem: 6598.23 is the only one that is highlighted red as the lowest - the lowest values in in column should be red, and the highest green...so it only works sort of when a column only has one value

--------------------txt file--------------------

1253.65 4566.50 2154.36 7532.45 3388.44 6598.23 2876.22 3576.24 1954.66 4896.23 2855.29 2386.36 5499.29 2256.76 3623.76 4286.29 5438.48 3184.38 3654.65 3455.76 6387.23 1235.55 2657.46 3265.34 2256.38 8935.26

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

Step: 3

blur-text-image

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

Multidimensional Array Data Management In Databases

Authors: Florin Rusu

1st Edition

1638281483, 978-1638281481

More Books

Students also viewed these Databases questions