Ok, so first off this code is insanely messy and very very long. So if anyone has any ideas on how I can reduce all the variables and make it just run nicer that would be nice.
I have been trying many different types of layouts and saving forms and combined it has just clustered my code quite badly, so any suggestions in that area would help VERY much.
What I need to help with in the source code is this.
1. Need my Report label to display the text in an organized fashion
2. Implement a scrollbar function to appear when the report list is longer than 3, excluding the title row.
3. Make the buttons non expandable, I want them to stay a certain set size.
4. (optional) Find a way to cut down on the amount of variables I have. If you suggest using "this", PLEASE PLEASE PLEASE explain, as that part of Java greatly confuses me.
For the scrollbar, I spent about 2 hours trying to figure it out, and finally just scraped it because I kept getting lost.
I am not lazy and I do not give up, so when i ask for help, its because I honestly do not understand what I am doing, so all help is greatly appreciated.
Here is the source code, I posted the three text files below as well.
To use my program, only the Load and Report buttons work this far. Click LOAD, pick one of my files (I have been using the severalrecords.txt) and then click REPORT.
I need the text label for report to nicely format the information, title row on the top, then each user's data displayed on the next line below.
Once it hits 4 rows, I want the Jscrollbar feature to show up as well.
Again, please EXPLAIN what you all do, because I want to learn. :)
Source Code
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.Scanner;
import javax.swing.*;
public class ProjectEngine {
/*******************************************************************************************
* @author Austin Anderson This program will display a Calculator
*******************************************************************************************/
private static JButton buttonLoad;
private static JButton buttonReport;
private static JButton buttonMinMPG;
private static JButton buttonMaxMPG;
private static JButton buttonAvgMPG;
private static JLabel Fname1 = new JLabel("");
private static JLabel Lname1 = new JLabel("");
private static JLabel Fares1 = new JLabel("");
private static JLabel GasMoney1 = new JLabel("");
private static JLabel Miles1 = new JLabel("");
private static JLabel Gallons1 = new JLabel("");
private static JLabel MPG1 = new JLabel("");
private static JLabel Profit1 = new JLabel("");
private static JLabel Fname2 = new JLabel("");
private static JLabel Lname2 = new JLabel("");
private static JLabel Fares2 = new JLabel("");
private static JLabel GasMoney2 = new JLabel("");
private static JLabel Miles2 = new JLabel("");
private static JLabel Gallons2 = new JLabel("");
private static JLabel MPG2 = new JLabel("");
private static JLabel Profit2 = new JLabel("");
private static JLabel Fname3 = new JLabel("");
private static JLabel Lname3 = new JLabel("");
private static JLabel Fares3 = new JLabel("");
private static JLabel GasMoney3 = new JLabel("");
private static JLabel Miles3 = new JLabel("");
private static JLabel Gallons3 = new JLabel("");
private static JLabel MPG3 = new JLabel("");
private static JLabel Profit3 = new JLabel("");
private static JLabel Fname4 = new JLabel("");
private static JLabel Lname4 = new JLabel("");
private static JLabel Fares4 = new JLabel("");
private static JLabel GasMoney4 = new JLabel("");
private static JLabel Miles4 = new JLabel("");
private static JLabel Gallons4 = new JLabel("");
private static JLabel MPG4 = new JLabel("");
private static JLabel Profit4 = new JLabel("");
private static JLabel Fname5 = new JLabel("");
private static JLabel Lname5 = new JLabel("");
private static JLabel Fares5 = new JLabel("");
private static JLabel GasMoney5 = new JLabel("");
private static JLabel Miles5 = new JLabel("");
private static JLabel Gallons5 = new JLabel("");
private static JLabel MPG5 = new JLabel("");
private static JLabel Profit5 = new JLabel("");
private static JLabel Fname6 = new JLabel("");
private static JLabel Lname6 = new JLabel("");
private static JLabel Fares6 = new JLabel("");
private static JLabel GasMoney6 = new JLabel("");
private static JLabel Miles6 = new JLabel("");
private static JLabel Gallons6 = new JLabel("");
private static JLabel MPG6 = new JLabel("");
private static JLabel Profit6 = new JLabel("");
private static JLabel Fname7 = new JLabel("");
private static JLabel Lname7 = new JLabel("");
private static JLabel Fares7 = new JLabel("");
private static JLabel GasMoney7 = new JLabel("");
private static JLabel Miles7 = new JLabel("");
private static JLabel Gallons7 = new JLabel("");
private static JLabel MPG7 = new JLabel("");
private static JLabel Profit7 = new JLabel("");
private static JLabel Fname8 = new JLabel("");
private static JLabel Lname8 = new JLabel("");
private static JLabel Fares8 = new JLabel("");
private static JLabel GasMoney8 = new JLabel("");
private static JLabel Miles8 = new JLabel("");
private static JLabel Gallons8 = new JLabel("");
private static JLabel MPG8 = new JLabel("");
private static JLabel Profit8 = new JLabel("");
private static JLabel start_display = new JLabel("Please load data.");
private static JLabel report_display = new JLabel("No report data yet.");
private static JLabel min_display = new JLabel("No minimum data yet.");
private static JLabel max_display = new JLabel("No maximum data yet.");
private static JLabel avg_display = new JLabel("No average data yet.");
static JFrame frame = new JFrame("Welcome!");
static JPanel panel_startup = new JPanel();
static JPanel panel_report = new JPanel();
static JPanel panel_MinMPG = new JPanel();
static JPanel panel_MaxMPG = new JPanel();
static JPanel panel_AvgMPG = new JPanel();
static JPanel panel_top = new JPanel();
static JPanel panel_middle = new JPanel();
static JPanel panel_bottom = new JPanel();
static JPanel panel_main = new JPanel();
static JPanel panel_upper = new JPanel();
static Array[] al;
static String report = "";
static double mpg1, mpg2, mpg3;
static double gal1, gal2, gal3;
static double prof1, prof2, prof3;
static double mil1, mil2, mil3;
static double gasM1, gasM2, gasM3;
static double far1, far2, far3;
CardLayout ourLayout;
JPanel scrollPane = new JPanel();
JScrollPane scroller = new JScrollPane(scrollPane);
static DecimalFormat dmft = new DecimalFormat("0.00");
public static void main(String arg[]) {
ProjectEngine t = new ProjectEngine();
t.ProjectGUI();
}
public void ProjectGUI() {
panel_startup.setBackground(Color.white);
panel_startup.setLayout(new GridLayout(1, 5));
buttonLoad = new JButton("Load");
buttonLoad.setMaximumSize(new Dimension(1, 1));
buttonLoad.addActionListener(new e());
panel_startup.add(buttonLoad);
buttonReport = new JButton("Report");
buttonReport.setMaximumSize(new Dimension(1, 1));
buttonReport.addActionListener(new e());
panel_startup.add(buttonReport);
buttonMinMPG = new JButton("Minimum MPG");
buttonMinMPG.setMaximumSize(new Dimension(1, 1));
buttonMinMPG.addActionListener(new e());
panel_startup.add(buttonMinMPG);
buttonMaxMPG = new JButton("Maximum MPG");
buttonMaxMPG.setMaximumSize(new Dimension(1, 1));
buttonMaxMPG.addActionListener(new e());
panel_startup.add(buttonMaxMPG);
buttonAvgMPG = new JButton("Average MPG");
buttonAvgMPG.setMaximumSize(new Dimension(1, 1));
buttonAvgMPG.addActionListener(new e());
panel_startup.add(buttonAvgMPG);
panel_top.setBackground(Color.white);
panel_top.setLayout(new GridLayout(1, 8));
panel_middle.setBackground(Color.white);
panel_middle.setLayout(new GridLayout(8, 8));
panel_middle.add(Fname1);
panel_middle.add(Lname1);
panel_middle.add(Fares1);
panel_middle.add(GasMoney1);
panel_middle.add(Miles1);
panel_middle.add(Gallons1);
panel_middle.add(MPG1);
panel_middle.add(Profit1);
panel_middle.add(Fname2);
panel_middle.add(Lname2);
panel_middle.add(Fares2);
panel_middle.add(GasMoney2);
panel_middle.add(Miles2);
panel_middle.add(Gallons2);
panel_middle.add(MPG2);
panel_middle.add(Profit2);
panel_middle.add(Fname3);
panel_middle.add(Lname3);
panel_middle.add(Fares3);
panel_middle.add(GasMoney3);
panel_middle.add(Miles3);
panel_middle.add(Gallons3);
panel_middle.add(MPG3);
panel_middle.add(Profit3);
panel_middle.add(Fname4);
panel_middle.add(Lname4);
panel_middle.add(Fares4);
panel_middle.add(GasMoney4);
panel_middle.add(Miles4);
panel_middle.add(Gallons4);
panel_middle.add(MPG4);
panel_middle.add(Profit4);
panel_middle.add(Fname5);
panel_middle.add(Lname5);
panel_middle.add(Fares5);
panel_middle.add(GasMoney5);
panel_middle.add(Miles5);
panel_middle.add(Gallons5);
panel_middle.add(MPG5);
panel_middle.add(Profit5);
panel_middle.add(Fname6);
panel_middle.add(Lname6);
panel_middle.add(Fares6);
panel_middle.add(GasMoney6);
panel_middle.add(Miles6);
panel_middle.add(Gallons6);
panel_middle.add(MPG6);
panel_middle.add(Profit6);
panel_middle.add(Fname7);
panel_middle.add(Lname7);
panel_middle.add(Fares7);
panel_middle.add(GasMoney7);
panel_middle.add(Miles7);
panel_middle.add(Gallons7);
panel_middle.add(MPG7);
panel_middle.add(Profit7);
panel_middle.add(Fname8);
panel_middle.add(Lname8);
panel_middle.add(Fares8);
panel_middle.add(GasMoney8);
panel_middle.add(Miles8);
panel_middle.add(Gallons8);
panel_middle.add(MPG8);
panel_middle.add(Profit8);
panel_report.setLayout(new FlowLayout());
panel_report.add(panel_top);
panel_report.add(panel_middle);
panel_report.add(panel_bottom);
panel_top.add(start_display);
panel_main.setLayout(new GridLayout(2, 1));
panel_main.setBackground(Color.white);
panel_main.add(panel_top);
panel_main.add(panel_startup);
frame.getContentPane().add(panel_main);
frame.setSize(100, 100);
frame.pack();
frame.setVisible(true);
}
public class e implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == buttonLoad) {
Load();
Calculations();
}
if (source == buttonReport) {
Report();
}
if (source == buttonMinMPG) {
Min();
}
if (source == buttonMaxMPG) {
Max();
}
if (source == buttonAvgMPG) {
Avg();
}
}
}
private static void ClearAll() {
report = "";
start_display.setText("");
report_display.setText("");
min_display.setText("");
max_display.setText("");
max_display.setText("");
mpg1 = 0;
mpg2 = 0;
mpg3 = 0;
gal1 = 0;
gal2 = 0;
gal3 = 0;
prof1 = 0;
prof2 = 0;
prof3 = 0;
mil1 = 0;
mil2 = 0;
mil3 = 0;
gasM1 = 0;
gasM2 = 0;
gasM3 = 0;
far1 = 0;
far2 = 0;
far3 = 0;
}
private static void Load() {
ClearAll();
JFileChooser chooser = new JFileChooser();
Scanner scanner = null;
int status = chooser.showOpenDialog(null);
if (status != JFileChooser.APPROVE_OPTION) {
System.out.println("No file was chosen.");
return;
}
try {
File file = chooser.getSelectedFile();
scanner = new Scanner(file);
} catch (IOException ioe) {
System.out.println("There was a problem opening the file.");
}
while (scanner.hasNext()) {
Fname1.setText(scanner.next());
Lname1.setText(scanner.next());
Fares1.setText(scanner.next());
GasMoney1.setText(scanner.next());
Miles1.setText(scanner.next());
Gallons1.setText(scanner.next());
Fname2.setText(scanner.next());
Lname2.setText(scanner.next());
Fares2.setText(scanner.next());
GasMoney2.setText(scanner.next());
Miles2.setText(scanner.next());
Gallons2.setText(scanner.next());
Fname3.setText(scanner.next());
Lname3.setText(scanner.next());
Fares3.setText(scanner.next());
GasMoney3.setText(scanner.next());
Miles3.setText(scanner.next());
Gallons3.setText(scanner.next());
}
}
private static void Calculations() {
mil1 = Double.parseDouble(Miles1.getText());
gal1 = Double.parseDouble(Gallons1.getText());
mpg1 = mil1 / gal1;
far1 = Double.parseDouble(Fares1.getText());
gasM1 = Double.parseDouble(GasMoney1.getText());
prof1 = far1 - gasM1;
MPG1.setText(dmft.format(mpg1));
Profit1.setText(dmft.format(prof1));
mil2 = Double.parseDouble(Miles2.getText());
gal2 = Double.parseDouble(Gallons2.getText());
mpg2 = mil2 / gal2;
far2 = Double.parseDouble(Fares2.getText());
gasM2 = Double.parseDouble(GasMoney2.getText());
prof2 = far2 - gasM2;
MPG2.setText(dmft.format(mpg2));
Profit2.setText(dmft.format(prof2));
mil3 = Double.parseDouble(Miles3.getText());
gal3 = Double.parseDouble(Gallons3.getText());
mpg3 = mil3 / gal3;
far3 = Double.parseDouble(Fares3.getText());
gasM3 = Double.parseDouble(GasMoney3.getText());
prof3 = far3 - gasM3;
MPG3.setText(dmft.format(mpg3));
Profit3.setText(dmft.format(prof3));
}
private static void Report() {
report = "First Name" + " Last Name" + " Fares" + " Gas Money"
+ " Miles" + " Gallons" + " MPG" + " Profit " + "\n"
+ Fname1.getText()
+ Lname1.getText()
+ Fares1.getText()
+ GasMoney1.getText()
+ Miles1.getText()
+ Gallons1.getText()
+ MPG1.getText()
+ Profit1.getText() + " "
+ "\n"
+ Fname2.getText()
+ Lname2.getText()
+ Fares2.getText()
+ GasMoney2.getText()
+ Miles2.getText()
+ Gallons2.getText()
+ MPG2.getText()
+ Profit2.getText() + " "
+ "\n"
+ Fname3.getText()
+ Lname3.getText()
+ Fares3.getText()
+ GasMoney3.getText()
+ Miles3.getText()
+ Gallons3.getText()
+ MPG3.getText()
+ Profit3.getText()
+ "\n";
start_display.setText(report);
System.out.println(report);
}
private static void Min() {
start_display.setText("" + min_display);
}
private static void Max() {
start_display.setText("" + max_display);
}
private static void Avg() {
start_display.setText("" + avg_display);
}
}
Text files (and yes, the client data is already put on to a newline in the txt file)
OneRecord.txt
John Doe 100.00 20.00 240.00 8.0
SeveralRecords.txt
John Doe 100.00 20.00 240.00 8.0
Roger Ellis 120.00 24.00 250.0 9.0
Larry Smith 130.00 25.00 250.0 7.0
8Records.txt
John Doe 100.00 20.00 240.00 8.0
Roger Ellis 120.00 24.00 250.0 9.0
Larry Smith 130.00 25.00 250.0 7.0
A B 200.00 39.0 300.0 13.0
C D 210.00 40.0 310.0 13.5
E F 220.00 41.0 320.0 14.0
G H 230.00 42.0 330.0 14.5
I J 240.00 43.0 340.0 15.0
Thank you guys sooooooo much in advance!
-Austin