📄 carsalessystem.java
字号:
public void componentHidden(ComponentEvent ev) {}
public void componentMoved(ComponentEvent ev) {}
/**
* receives events when JFrame is resized and ensures the application doesn't get resized
* below a minimum size which could cause display problems in the application
*
* @param ev ComponentEvent object
*/
public void componentResized(ComponentEvent ev)
{
if (this == ev.getComponent())
{
Dimension size = getSize();
if (size.height < 530)
size.height = 530;
if (size.width < 675)
size.width = 675;
setSize(size);
}
}
public void componentShown(ComponentEvent ev) {}
/**
* Converts a range string such as "50-100" or "20+" to a double array with minimum and maximum
* bounds
*
* @param s a string containing a valid range. "200", "100-200", "20+" are all examples
* of valid ranges
* @return array of 2 elements. First element indicating minimum bounds, second element
* indicating maximum bounds. A value of -1 for minimum and maximum bounds indicates an error
* in converting the parameter to a proper range. A value of -1 for the maximum bounds and
* a value > 0 for the minimum bounds indicates the maximum bounds is infinity in theory.
* If both bounds are greater than 0, this indicates a valid range a to b, where a and b
* can be equal.
*/
public static double[] convertToRange(String s)
{
String[] parts = s.trim().split("-");
double[] bounds = new double[2];
try
{
// if true the range is either of the form 'm+' or 'm'
if (parts.length == 1)
{
String c = s.substring(s.length() - 1);
// if in the form "m+"
if (c.equals("+"))
{
// get lower bounds from the string
bounds[0] = Double.parseDouble(s.substring(0, s.length() - 1));
// no upper maximum specified, infinite
bounds[1] = -1;
}
// if true the number is of the form 'm'
else
{
// upper bounds == lower bounds. The range is actually just a single number
bounds[0] = Double.parseDouble(s);
bounds[1] = bounds[0];
}
}
// if true, in the form "m-n"
else if(parts.length == 2)
{
bounds[0] = Double.parseDouble(parts[0]);
bounds[1] = Double.parseDouble(parts[1]);
if (bounds[0] > bounds[1])
{
//incorrect bounds, example, 10-5
bounds[0] = -1;
bounds[1] = -1;
}
}
}
catch (NumberFormatException exp)
{
//incorrect bounds format
bounds[0] = -1;
bounds[1] = -1;
}
return bounds;
}
/**
* gets the entire list of cars from CarsCollection
*
* @return array of cars containing individual details
*/
public Car[] getAllCars()
{
return carCollection.getAllCars();
}
/**
* checks if the cars have been updated since last program launch
*
* @return boolean indicating whether the cars have been updated
*/
public boolean getCarsUpdated()
{
return carsUpdated;
}
/**
* Retrieves statistics about the car collection
*
* @param type can be either CARS_COUNT, MANUFACTURERS_COUNT, AVERAGE_PRICE, AVERAGE_DISTANCE or
* AVERAGE_AGE
* @return a value with the queried statistic returned
*/
public double getStatistics(int type)
{
double result = 0;
if (type == CARS_COUNT)
{
result = carCollection.carsCount();
}
else if (type == MANUFACTURERS_COUNT)
{
result = carCollection.manufacturerCount();
}
else if (type == AVERAGE_PRICE)
{
result = carCollection.getAveragePrice();
}
else if (type == AVERAGE_DISTANCE)
{
result = carCollection.getAverageDistance();
}
else if (type == AVERAGE_AGE)
{
result = carCollection.getAverageAge();
}
return result;
}
/**
* initialize the CarSalesSystem.
*
* @param args name of binary file to use is the only argument needed
*/
public static void main(String[] args)
{
CarSalesSystem carSales = new CarSalesSystem("cars.dat");
carSales.setVisible(true);
}
/**
* search by age, using the CarsCollection
*
* @param minAge minimum age of car
* @param maxAge maimum age of car
* @return array of cars that match the search criteria
*/
public Car[] search(int minAge, int maxAge)
{
return carCollection.search(minAge, maxAge);
}
/**
* Search by age using the CarsCollection
*
* @param minPrice minimum price of car
* @param maxPrice maximum price of car
* @param minDistance minimum distance travelled by car
* @param maxDistance maximum distance travelled by car
* @return array of cars that match the search criteria
*/
public Car[] search(int minPrice, int maxPrice, double minDistance, double maxDistance)
{
return carCollection.search(minPrice, maxPrice, minDistance, maxDistance);
}
/**
* call this method to alert the Car Sales System that a car has been added, and also send
* messages to all registered car update listeners
*/
public void setCarsUpdated()
{
carsUpdated = true;
for (int i = 0; i < registeredListeners.size(); i++)
{
Class[] paramType = {CarUpdateEvent.class};
Object[] param = {new CarUpdateEvent(this)};
try
{
//get a reference to the method which we want to invoke to the listener
java.lang.reflect.Method callingMethod = registeredListeners.get(i).getClass().getMethod("carsUpdated", paramType);
//invoke the method with our parameters
callingMethod.invoke(registeredListeners.get(i), param);
}
catch (NoSuchMethodException exp)
{
System.out.println("Warning, 'public carsUpdated(CarEvent)' method does not exist in " + registeredListeners.get(i).getClass().getName() + ". You will not receive any car update events");
}
catch (IllegalAccessException exp)
{
System.out.println("Warning, the 'public carUpdated(CarEvent)' method couldn't be called for unknown reasons, You will not receive any car update events");
}
catch (Exception exp){}
}
}
/**
* the selected tab in the JTabbedPane has been changed, so change the title bar text
*
* @param ev ChangeEvent object
*/
public void stateChanged(ChangeEvent ev)
{
if (ev.getSource() == theTab)
statusLabel.setText("Current panel: " + theTab.getTitleAt(theTab.getSelectedIndex()));
}
/**
* converts a vector to a car array
*
* @param v vector containing array of Car objects only
* @return Car array containing car objects from the vector
*/
public static Car[] vectorToCar(Vector v)
{
Car[] ret = new Car[v.size()];
for (int i = 0; i < v.size(); i++)
{
ret[i] = (Car)v.elementAt(i);
}
return ret;
}
class WindowCloser extends WindowAdapter
{
/**
* calls the car sales system's main closing event
*
* @param ev WindowEvent object
*/
public void windowClosing(WindowEvent ev)
{
closing();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -