📄 carsalessystem.java
字号:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
* This class is the main JFrame of the application, and deals with the car collection.
* It also creates instances of the other panels (add car, view car, etc) and incorporates
* them into the main frame. This is hierarchically, the highest class.
* @
*
* PUBLIC FEATURES:
* // Constructors
* public CarSalesSystem(String f)
*
* // Methods
* public void aboutMenuItemClicked()
* public void actionPerformed(ActionEvent ev)
* public void addCarUpdateListener(Object listener) * public int addNewCar(Car c)
* public void closing()
* public void componentHidden(ComponentEvent ev)
* public void componentMoved(ComponentEvent ev)
* public void componentResized(ComponentEvent ev)
* public void componentShown(ComponentEvent ev)
* public static double[] convertToRange(String s)
* public Car[] getAllCars()
* public boolean getCarsUpdated()
* public double getStatistics(int type)
* public static void main(String[] args)
* public Car[] search(int minAge, int maxAge)
* public Car[] search(int minPrice, int maxPrice, double minDistance, double maxDistance)
* public void setCarsUpdated()
* public void stateChanged(ChangeEvent ev)
* public static Car[] vectorToCar(Vector v)
*
* COLLABORATORS:
* AboutDialog, CarsCollection, WindowCloser, WelcomePanel, AddCarPanel
* ShowAllCarsPanel, SearchByAgePanel, SearchByOtherPanel
*
* @version 1.0, 16 Oct 2004
* @author Adam Black
*/
public class CarSalesSystem extends JFrame implements ActionListener, ComponentListener, ChangeListener
{
/**
* the current version of CarSalesSystem
*/
public static final double APP_VERSION = 1.0;
/**
* is used as the parameter in the 'getStatistics(int type)' method to indicate you wish to find the
* total number of cars in the system
*/
public static final int CARS_COUNT = 0; /**
* is used as the parameter in the 'getStatistics(int type)' method to indicate you wish to find the
* total number of manufacturers in the system
*/
public static final int MANUFACTURERS_COUNT = 1;
/**
* is used as the parameter in the 'getStatistics(int type)' method to indicate you wish to find the
* average price, from the entire collection of cars in the system
*/
public static final int AVERAGE_PRICE = 2;
/**
* is used as the parameter in the 'getStatistics(int type)' method to indicate you wish to find the
* average distance travelled (in kilometers) from the entire collection of cars in the system
*/
public static final int AVERAGE_DISTANCE = 3;
/**
* is used as the parameter in the 'getStatistics(int type)' method to indicate you wish to find the
* average age from the entire collection of cars in the system
*/
public static final int AVERAGE_AGE = 4;
private String file;
private AboutDialog aboutDlg;
private boolean carsUpdated = false;
private Vector registeredListeners = new Vector();
private CarsCollection carCollection;
private JPanel topPanel = new JPanel(new BorderLayout());
private JPanel titlePanel = new JPanel(new GridLayout(2, 1));
private JLabel statusLabel = new JLabel();
private JLabel pictureLabel = new JLabel();
private JLabel carCoLabel = new JLabel("My Car Company", JLabel.CENTER);
private JLabel salesSysLabel = new JLabel("Car Sales System", JLabel.CENTER);
private JTabbedPane theTab = new JTabbedPane(JTabbedPane.LEFT);
private JMenuBar menuBar = new JMenuBar();
private JMenu fileMenu = new JMenu("File");
private JMenuItem aboutItem = new JMenuItem("About");
private JMenuItem exitItem = new JMenuItem("Exit");
private WindowCloser closer = new WindowCloser();
/**
* @param f existing binary file for storing/retrieving car data
*/
public CarSalesSystem(String f)
{
super("Car Sales");
addWindowListener(closer);
addComponentListener(this);
theTab.addChangeListener(this);
setSize(780, 560);
Container c = getContentPane();
carCollection = new CarsCollection();
file = f;
try
{
carCollection.loadCars(file);
}
catch (java.io.FileNotFoundException exp)
{
System.out.println("The data file, 'cars.dat' doesn't exist. Plase create an empty file named 'cars.dat'");
System.exit(0);
}
// empty cars.dat file, this error should be ignored
catch (java.io.EOFException exp){}
catch (java.io.IOException exp)
{
System.out.println("The data file, 'cars.dat' is possibly corrupted. Please delete it and create a new empty data file named cars.dat");
System.exit(0);
}
catch (Exception exp)
{
System.out.println("There was an error loading 'cars.dat'. Try deleting and creating a new empty file named 'cars.dat'");
System.exit(0);
}
String currentFont = carCoLabel.getFont().getName();
carCoLabel.setFont(new Font(currentFont, Font.BOLD, 26));
salesSysLabel.setFont(new Font(currentFont, Font.PLAIN, 16));
// create menu bar
menuBar.add(fileMenu);
fileMenu.add(aboutItem);
fileMenu.add(exitItem);
aboutItem.addActionListener(this);
exitItem.addActionListener(this);
// add menu bar
setJMenuBar(menuBar);
// set border on status bar label to make it look like a panel
statusLabel.setBorder(new javax.swing.border.EtchedBorder());
// load the picture into the top panel
pictureLabel.setIcon(new ImageIcon("vu.png"));
titlePanel.add(carCoLabel);
titlePanel.add(salesSysLabel);
topPanel.add(pictureLabel, "West");
topPanel.add(titlePanel, "Center");
WelcomePanel welcomePanel = new WelcomePanel(this, f);
AddCarPanel addCarPanel = new AddCarPanel(this);
ShowAllCarsPanel showAllCarsPanel = new ShowAllCarsPanel(this);
SearchByAgePanel searchByAgePanel = new SearchByAgePanel(this);
SearchByOtherPanel searchByOtherPanel = new SearchByOtherPanel(this);
theTab.add("Welcome", welcomePanel);
theTab.add("Add a Car", addCarPanel);
theTab.add("Show all makes and models", showAllCarsPanel);
theTab.add("Search on age", searchByAgePanel);
theTab.add("Search on Price and Distance traveled", searchByOtherPanel);
theTab.addChangeListener(showAllCarsPanel);
theTab.addChangeListener(welcomePanel);
theTab.setSelectedIndex(0);
c.setLayout(new BorderLayout());
c.add(topPanel, "North");
c.add(theTab, "Center");
c.add(statusLabel, "South");
}
/**
* about menu clicked, show about dialog
*/
public void aboutMenuItemClicked()
{
// if it doesn't exist, create a new instance, otherwise display the current reference
if (aboutDlg == null)
aboutDlg = new AboutDialog(this, "About", true);
aboutDlg.showAbout();
}
/**
* receives and handles menu click events
*
* @param ev ActionEvent object
*/
public void actionPerformed(ActionEvent ev)
{
if (ev.getSource() == aboutItem)
aboutMenuItemClicked();
else if (ev.getSource() == exitItem)
closing();
}
/**
* adds an object to receive notifications when a car is added to the system
*
* @param listener a listener object
*/
public void addCarUpdateListener(Object listener)
{
if (!(listener == null))
registeredListeners.add(listener);
}
/**
* add a new car using the CarCollection class
*
* @param c car object to add
* @return whether successful or not. See CarCollection.addCar() for more info
*/
public int addNewCar(Car c)
{
return carCollection.addCar(c);
}
/**
* handles closing events for the Car Sales System. Saves any updated data to a binary file
*/
public void closing()
{
boolean ok;
if (carsUpdated)
{
do
{
try
{
carCollection.saveCars(file);
ok = true;
}
catch (java.io.IOException exp)
{
int result = JOptionPane.showConfirmDialog(this, "The data file could not be written, possibly because you don't have access to this location.\nIf you chose No to retry you will lose all car data from this session.\n\nWould you like to reattempt saving the data file?", "Problem saving data", JOptionPane.YES_NO_OPTION);
// checks if user wants to reattempt saving the data file
if (result == JOptionPane.YES_OPTION)
ok = false;
else
ok = true;
}
}
while (!ok);
}
System.exit(0); //shut down jvm
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -