Computer Science 252
Problem Solving with Java
Fall 2013, The College of Saint Rose
SalesGraph Demo
A working demo of SalesGraph will appear below. Click inside the applet to interact with it.
SalesGraph BlueJ Project
Click here to download a BlueJ project for SalesGraph.
SalesGraph Source Code
The Java source code for SalesGraph is below. Click on a file name to download it.
import objectdraw.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/*
* Example SalesGraph: read in a series of "sales" values
* and keep track of the values by day of the week, graphing
* the results by daily average and maximum.
*
* Jim Teresco, The College of Saint Rose, CSC 252, Fall 2013
*
* $Id: SalesGraph.java 2276 2013-12-03 19:23:46Z terescoj $
*/
public class SalesGraph extends WindowController implements ActionListener {
private static final int MAX_SALES_PER_DAY = 300;
// graphical layout constants
private static final int BAR_BOTTOM = MAX_SALES_PER_DAY;
private static final int MARGIN = 5;
private static final int BAR_WIDTH = 20;
private static final int DAY_WIDTH = 2 * (MARGIN + BAR_WIDTH);
// bars for bar graph
private FilledRect [] maxGraphs = new FilledRect[7];
private FilledRect [] avgGraphs = new FilledRect[7];
// some GUI components we'll need to remember
private JLabel label;
private JTextField salesField;
private JButton enter;
// next day of week to enter
private int dayOfWeek = 0;
private static final String [] daysOfWeek = {
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday" };
// next day overall to ask for
private int dayNumber = 1;
// store max values per day
private int [] maxPerDay = new int[7];
// array of lists of daily sales by day of week
private ArrayList<Integer> [] salesByDay;
public void begin() {
// set up some GUI components
Container contentPane = getContentPane();
JPanel southPanel = new JPanel();
label = new JLabel("Enter Day 1 Value (Sunday)");
southPanel.add(label);
salesField = new JTextField(10);
southPanel.add(salesField);
enter = new JButton("Add");
southPanel.add(enter);
contentPane.add(southPanel, BorderLayout.SOUTH);
salesField.addActionListener(this);
enter.addActionListener(this);
contentPane.validate();
// set up graphs
for (int i = 0; i < 7; i++) {
avgGraphs[i] = new FilledRect(i * DAY_WIDTH + MARGIN,
BAR_BOTTOM, BAR_WIDTH, 1, canvas);
avgGraphs[i].setColor(Color.green);
maxGraphs[i] = new FilledRect(i * DAY_WIDTH + MARGIN + BAR_WIDTH,
BAR_BOTTOM, BAR_WIDTH, 1, canvas);
maxGraphs[i].setColor(Color.red);
}
// array of ArrayList references
salesByDay = (ArrayList<Integer>[])new ArrayList[7];
// construct individual ArrayLists
for (int i = 0; i < 7; i++) {
salesByDay[i] = new ArrayList<Integer>();
}
}
public void actionPerformed(ActionEvent e) {
// make sure we have a valid value to work with
int value = 0;
try {
value = Integer.parseInt(salesField.getText());
}
catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null,
"Please enter an integer value in the range 0-" + MAX_SALES_PER_DAY,
"Input error", JOptionPane.ERROR_MESSAGE);
salesField.setText("");
return;
}
// make sure we are in the proper range
if (value < 0 || value > MAX_SALES_PER_DAY) {
JOptionPane.showMessageDialog(null,
"Please enter an integer value in the range 0-" + MAX_SALES_PER_DAY,
"Input error", JOptionPane.ERROR_MESSAGE);
salesField.setText("");
return;
}
// now do something with the valid value
System.out.println("Hey! You entered " + value);
salesField.setText("");
// see if we have a new max sales number for this day of week
if (value > maxPerDay[dayOfWeek]) {
maxPerDay[dayOfWeek] = value;
maxGraphs[dayOfWeek].setHeight(value);
maxGraphs[dayOfWeek].moveTo(dayOfWeek * DAY_WIDTH + MARGIN + BAR_WIDTH, BAR_BOTTOM - value);
}
// add today's sales to the appropriate ArrayList of sales history
salesByDay[dayOfWeek].add(value);
// compute new average for this day of week
int total = 0;
for (int val : salesByDay[dayOfWeek]) {
total += val;
}
double average = 1.0*total / salesByDay[dayOfWeek].size();
// update graph
avgGraphs[dayOfWeek].setHeight(average);
avgGraphs[dayOfWeek].moveTo(dayOfWeek * DAY_WIDTH + MARGIN, BAR_BOTTOM - average);
// update day of week and day number
dayOfWeek = (dayOfWeek + 1) % 7;
dayNumber++;
label.setText("Enter Day " + dayNumber + " value (" + daysOfWeek[dayOfWeek] + ")");
}
}