Initial Commit

This commit is contained in:
2012-01-22 16:14:32 -05:00
commit f5e306f67f
155 changed files with 5097 additions and 0 deletions

View File

@@ -0,0 +1,102 @@
package com.VehicleExcetionProject;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
/**
* this class will allow the user to enter a new vehicle into the database
*/
public class AddVehicleDialog extends JFrame implements ActionListener{
private static final long serialVersionUID = 3165335189780349167L;
private JButton ok;
private JTextArea model;
private JTextArea make;
private JTextArea year;
private JTextArea hp;
private JTextArea cap;
/**
* Creates a new AddVehicleDialog
*/
public AddVehicleDialog() {
super();
setTitle("Add Vehicle");
setMinimumSize(new Dimension(590, 20));
//create a JPanel to hold the text area and button
JPanel panel = new JPanel();
ok = new JButton("Ok");
ok.addActionListener(this);
model = new JTextArea();
model.setEditable(true);
model.setColumns(10);
model.setRows(1);
make = new JTextArea();
make.setEditable(true);
make.setColumns(10);
make.setRows(1);
year = new JTextArea();
year.setEditable(true);
year.setColumns(10);
year.setRows(1);
hp = new JTextArea();
hp.setEditable(true);
hp.setColumns(10);
hp.setRows(1);
cap = new JTextArea();
cap.setEditable(true);
cap.setColumns(10);
cap.setRows(1);
panel.add(new JLabel("Model: "));
panel.add(model);
panel.add(new JLabel("Make: "));
panel.add(make);
panel.add(new JLabel("Year: "));
panel.add(year);
panel.add(new JLabel("Horse Power: "));
panel.add(hp);
panel.add(new JLabel("Capacity: "));
panel.add(cap);
panel.add(ok);
//add the JPanel to the frame, and display
getContentPane().add(panel);
pack();
setVisible(true);
}
/**
* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == ok) {
try{
if(model.getText().equals("truck")){
MainWindow.db.addVehicle(new Truck(make.getText(), Integer.parseInt(year.getText()), Double.parseDouble(hp.getText()), Double.parseDouble(cap.getText())));
}
if(model.getText().equals("bus")){
MainWindow.db.addVehicle(new Bus(make.getText(), Integer.parseInt(year.getText()), Double.parseDouble(hp.getText()), Integer.parseInt(cap.getText())));;
}
setVisible(false);
} catch(Exception ex){
ex.printStackTrace();
}
}
}
}

View File

@@ -0,0 +1,109 @@
/**
* Bus class with super class
*
* @author Jonathan Warning
* @version 11.01.10
*/
package com.VehicleExcetionProject;
public class Bus extends Vehicle {
public final int MAXPASSENGERS = 50;
private int numOfPassengers;
/*
* constructs a Bus object
*/
public Bus() {
super();
numOfPassengers = 0;
}
/*
* constructs a Bus object
*
* @param String aMake the make of the vehicle
*
* @param int aYear the year of the vehicle
*
* @param double aHorsepower the horsepower of the vehicle
*
* @param int aNumOfPassengers the number of passsengers
*/
public Bus(String aMake, int aYear, double aHorsepower, int aNumOfPassengers) {
super(aMake, aYear, aHorsepower);
if (aNumOfPassengers > MAXPASSENGERS)
throw new OverloadedBusException(
"Number of passengers cannot exceed " + MAXPASSENGERS);
else if (aNumOfPassengers < 0)
throw new NegativeBusPassengerException(
"Number of passegers cannot be less than 0.");
else
numOfPassengers = aNumOfPassengers;
}
/*
* sets the number of passengers on the Bus
*
* @param int numOfPassengers the number of passengers on the Bus
*/
public void setPassengers(int aNumOfPassengers) {
if (aNumOfPassengers > MAXPASSENGERS)
throw new OverloadedBusException(
"Number of passengers cannot exceed " + MAXPASSENGERS);
else if (aNumOfPassengers < 0)
throw new NegativeBusPassengerException(
"Number of passegers cannot be less than 0.");
else
numOfPassengers = aNumOfPassengers;
}
/*
* gets the number of passengers on the Bus
*
* @return int numOfPassengers the number of passengers on the Bus
*/
public int getPassengers() {
return numOfPassengers;
}
/*
* calculates miles per gallon
*
* @return double milesPerGal the miles per gallon of the Bus
*/
public double calculateMPG() {
return milesPerGal = 10000 / numOfPassengers / horsepower;
}
/*
* String with all of the parameters of the Bus
*
* @return int year year of the Bus
*
* @return String make make of the Bus
*
* @return double horsepower horsepower of the Bus
*
* @return int numOfPassengers number of passengers in Bus
*/
@Override
public String toString() {
return "The " + super.year + ", " + super.horsepower + " horsepower "
+ super.make + " is carrying " + numOfPassengers + " people.";
}
/*
* Compares two Busses number of passengers
*
* @return boolean are the busses number of passengers equal
*/
@Override
public boolean equals(Object otherObj) {
if (otherObj instanceof Bus) {
Bus otherBus = (Bus) otherObj;
return (numOfPassengers == otherBus.numOfPassengers);
} else
return false;
}
}

View File

@@ -0,0 +1,15 @@
package com.VehicleExcetionProject;
public class InvalidHorsepowerException extends RuntimeException {
private static final long serialVersionUID = -3775197702871511450L;
public InvalidHorsepowerException() {
}
public InvalidHorsepowerException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,15 @@
package com.VehicleExcetionProject;
public class InvalidMakeException extends RuntimeException {
private static final long serialVersionUID = -7536710583729590772L;
public InvalidMakeException() {
}
public InvalidMakeException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,14 @@
package com.VehicleExcetionProject;
public class InvalidTowingCapacityException extends RuntimeException {
private static final long serialVersionUID = -3525837906090455018L;
public InvalidTowingCapacityException() {
}
public InvalidTowingCapacityException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,14 @@
package com.VehicleExcetionProject;
public class InvalidYearException extends RuntimeException {
private static final long serialVersionUID = 7185150702544438062L;
public InvalidYearException() {
}
public InvalidYearException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,96 @@
package com.VehicleExcetionProject;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* this is the main window and class of this application
*/
public class MainWindow extends JFrame implements ActionListener{
private JButton search;
private JButton addVehicle;
private JButton addFromFile;
private JButton removeVehicle;
private JButton showAllVehicles;
private JFileChooser fc;
public static VehicleDB db;
/**
* Generated Serial Version ID
*/
private static final long serialVersionUID = 1841715561053331517L;
public MainWindow() {
setTitle("Vehicle Database");
JPanel panel = new JPanel();
search = new JButton("Search");
addVehicle = new JButton("Add Vehicle");
addFromFile = new JButton("Add From File");
removeVehicle = new JButton("Remove Vehicle");
showAllVehicles = new JButton("Show All Vehicles");
search.addActionListener(this);
addVehicle.addActionListener(this);
addFromFile.addActionListener(this);
removeVehicle.addActionListener(this);
showAllVehicles.addActionListener(this);
panel.add(search);
panel.add(addVehicle);
panel.add(addFromFile);
panel.add(removeVehicle);
panel.add(showAllVehicles);
add(panel);
setVisible(true);
this.setMinimumSize(new Dimension(680,70));
fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
db = new VehicleDB();
}
/**
* main method, called when the application starts
* @param args
*/
public static void main(String[] args){
new MainWindow();
}
/**
* called when a button is clicked
* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == search) {
new ShowAllMakeDialog();
}
if (e.getSource() == addVehicle) {
new AddVehicleDialog();
}
if (e.getSource() == addFromFile) {
if(fc.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION){
db.processFile(fc.getSelectedFile());
}
}
if (e.getSource() == removeVehicle) {
new RemoveVehicleDialog();
}
if (e.getSource() == showAllVehicles) {
new ShowAllDialog(db.toString());
}
}
}

View File

@@ -0,0 +1,14 @@
package com.VehicleExcetionProject;
public class NegativeBusPassengerException extends RuntimeException {
private static final long serialVersionUID = 2864975327138368535L;
public NegativeBusPassengerException() {
}
public NegativeBusPassengerException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,14 @@
package com.VehicleExcetionProject;
public class OverloadedBusException extends RuntimeException {
private static final long serialVersionUID = -1979540068583138964L;
public OverloadedBusException() {
}
public OverloadedBusException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,63 @@
package com.VehicleExcetionProject;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
/**
* This class will be the delete vehicle dialog
*/
public class RemoveVehicleDialog extends JFrame implements ActionListener {
private static final long serialVersionUID = -36245710370532319L;
private JButton ok;
private JTextArea editText;
/**
* Creates a new RemoveVehicleDialog
*/
public RemoveVehicleDialog(){
super();
setTitle("Delete Vehicles");
setMinimumSize(new Dimension(590, 20));
//create a JPanel to hold the text area and button
JPanel panel = new JPanel();
ok = new JButton("Ok");
editText = new JTextArea();
JLabel label = new JLabel("Enter Vehicle Make To Remove");
ok.addActionListener(this);
editText.setEditable(true);
editText.setColumns(25);
editText.setRows(1);
panel.add(label);
panel.add(editText);
panel.add(ok);
//add the JPanel to the frame, and display
getContentPane().add(panel);
pack();
setVisible(true);
}
/**
* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == ok) {
MainWindow.db.deleteVehicle(editText.getText());
setVisible(false);
}
}
}

View File

@@ -0,0 +1,65 @@
package com.VehicleExcetionProject;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
* This panel will be used to display all Vehicles in the VechicleDB
*/
public class ShowAllDialog extends JFrame {
private static final long serialVersionUID = -8416144493079733535L;
private JButton ok;
private JTextArea results;
/**
* Creates a new ShowAllDialog
*/
public ShowAllDialog(String list){
super();
setTitle("Show All Vehicles");
setMinimumSize(new Dimension(590, 460));
//create a JPanel to hold the text area and button
JPanel panel = new JPanel();
ok = new JButton("Ok");
results = new JTextArea();
ButtonListener listener = new ButtonListener();
ok.addActionListener(listener);
results.setEditable(false);
results.setText(list);
results.setColumns(50);
results.setRows(25);
JScrollPane scrollPane = new JScrollPane(results);
setPreferredSize(new Dimension(450, 110));
add(scrollPane, BorderLayout.CENTER);
panel.add(scrollPane);
panel.add(ok);
//add the JPanel to the frame, and display
getContentPane().add(panel);
pack();
setVisible(true);
}
/**
* Listener class for this JPanel
*/
private class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
ShowAllDialog.this.setVisible(false);
}
}
}

View File

@@ -0,0 +1,66 @@
package com.VehicleExcetionProject;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
/**
* This class will be the dialog that will ask the user for a specific make to show
*/
public class ShowAllMakeDialog extends JFrame implements ActionListener {
private static final long serialVersionUID = 1750326106927701404L;
private JButton ok;
private JTextArea editText;
/**
* Creates a new ShowAllMakeDialog
*/
public ShowAllMakeDialog() {
super();
setTitle("Search");
setMinimumSize(new Dimension(590, 20));
//create a JPanel to hold the text area and button
JPanel panel = new JPanel();
ok = new JButton("Ok");
editText = new JTextArea();
JLabel label = new JLabel("Enter Vehicle Make To Show");
ok.addActionListener(this);
editText.setEditable(true);
editText.setColumns(25);
editText.setRows(1);
panel.add(label);
panel.add(editText);
panel.add(ok);
//add the JPanel to the frame, and display
getContentPane().add(panel);
pack();
setVisible(true);
}
/**
* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == ok) {
StringBuilder sb = new StringBuilder();
for(Vehicle item : MainWindow.db.findVehicle(editText.getText()))
sb.append(item.toString()+"\n");
new ShowAllDialog(sb.toString());
setVisible(false);
}
}
}

View File

@@ -0,0 +1,43 @@
package com.VehicleExcetionProject;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class Test {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test window = new Test();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Test() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

View File

@@ -0,0 +1,106 @@
/**
* Truck class with superclass
*
* @author Jonathan Warning
* @version 11.01.10
*/
package com.VehicleExcetionProject;
public class Truck extends Vehicle {
public final double MINCAPACITY = 0.5;
private double towingCapacity;
/*
* constructs a Truck object
*/
public Truck() {
super();
towingCapacity = 0;
}
/*
* constructs a Truck object
*
* @param String aMake the make of the vehicle
*
* @param int aYear the year of the vehicle
*
* @param double aHorsepower the horsepower of the vehicle
*
* @param double aTowingCapacity the towing capacity of the Truck
*/
public Truck(String aMake, int aYear, double aHorsepower,
double aTowingCapacity) {
super(aMake, aYear, aHorsepower);
if (aTowingCapacity < MINCAPACITY)
throw new InvalidTowingCapacityException(
"Towing capacity cannot be less than " + MINCAPACITY);
else
towingCapacity = aTowingCapacity;
}
/*
* sets the towing capacity of the truck
*
* @param double aTowingCapacity the towing capacity of the truck
*/
public void setTowingCapacity(double aTowingCapacity) {
if (aTowingCapacity < MINCAPACITY)
throw new InvalidTowingCapacityException("Towing capacity cannot be less than " + MINCAPACITY);
else
towingCapacity = aTowingCapacity;
}
/*
* gets the towing capacity of the truck
*
* @return double aTowingCapacity the towing capacity of the Truck
*/
public double getTowingCapacity() {
return towingCapacity;
}
/*
* calculates miles per gallon
*
* @return double milesPerGal the miles per gallon of the Truck
*/
public double calculateMPG() {
return milesPerGal = 5000 / towingCapacity / horsepower;
}
/*
* String with all of the parameters of the Truck
*
* @return int year year of the Truck
*
* @return String make make of the Truck
*
* @return double horsepower horsepower of the Truck
*
* @return double towingCapacity the towing capactiy of the truck
*/
@Override
public String toString() {
return "The towing capacity of the " + super.year + ", "
+ super.horsepower + " horsepower " + super.make + " is "
+ towingCapacity + " tons.";
}
/*
* Compares two trucks towing capacities
*
* @return boolean are the trucks towing capacities equal
*/
@Override
public boolean equals(Object otherObj) {
if (otherObj instanceof Truck) {
Truck otherTruck = (Truck) otherObj;
return ((towingCapacity == otherTruck.towingCapacity));
} else
return false;
}
}

View File

@@ -0,0 +1,176 @@
/**
* Vehicle class with subclasses
*
* @author Jonathan Warning
* @version 10.20.10
*/
package com.VehicleExcetionProject;
public abstract class Vehicle implements Cloneable, Comparable<Vehicle> {
protected String make;
protected int year;
protected double horsepower;
protected double milesPerGal;
/*
* constructs a vehicle object
*/
public Vehicle() {
make = "";
year = 0;
horsepower = 0;
if (make.length() == 0)
throw new InvalidMakeException("Vehicle must parameters.");
if (year <= 0)
throw new InvalidYearException("Vehicle must parameters.");
if (horsepower <= 0)
throw new InvalidHorsepowerException("Vehicle must parameters.");
}
/*
* constructs a vehicle object
*
* @param String aMake the make of the vehicle
*
* @param int aYear the year of the vehicle
*
* @param double aHorsepower the horsepower of the vehicle
*/
public Vehicle(String aMake, int aYear, double aHorsepower) {
if (aMake.length() == 0)
throw new InvalidMakeException("Vehicle must have a make.");
else
make = aMake;
if (aYear <= 0)
throw new InvalidYearException("Vehicle must have been made A.D.");
else
year = aYear;
if (aHorsepower <= 0)
throw new InvalidHorsepowerException(
"Vehicle must have positive horsepower.");
else
horsepower = aHorsepower;
}
/*
* Sets the make of the vehicle
*
* @param String aMake the make of the vehicle
*/
public void setMake(String aMake) {
if (aMake.length() == 0)// (aMake.equalsIgnoreCase(""))
throw new InvalidMakeException("Vehicle must have a make.");
else
make = aMake;
}
/*
* Sets the year of the vehicle
*
* @param int aYear the year of the vehicle
*/
public void setYear(int aYear) {
if (aYear <= 0)
throw new InvalidYearException("Vehicle must have been made A.D.");
else
year = aYear;
}
/*
* Sets the horsepowers of the vehicle
*
* @param double aHorsepower the horsepower of the vehicle
*/
public void setHorsepower(double aHorsepower) {
if (aHorsepower <= 0)
throw new InvalidHorsepowerException(
"Vehicle must have positive horsepower.");
else
horsepower = aHorsepower;
}
/*
* gets the make of the vehicle
*
* @return String make the make of the vehicle
*/
public String getMake() {
return make;
}
/*
* gets the year of the vehicle
*
* @return int year the year of the vehicle
*/
public int getYear() {
return year;
}
/*
* gets the horsepower of the vehicle
*
* @return double horsepower the horsepower of the vehicle
*/
public double getHorsepower() {
return horsepower;
}
/*
* abstract method to calculate MPG classes that implement Vehicle must the
* calculateMPG method
*/
public abstract double calculateMPG();
/*
* String with all of the parameters of the class
*
* @return int year year of the Vehicle
*
* @return String make make of the Vehicle
*
* @return double horsepower horsepower of the Vehicle
*/
public String toString() {
return "The " + year + " " + make + " has " + horsepower + " hp.";
}
/*
* Compares two Vehicles milesPerGal
*
* @return boolean are the objects milesPerGal equal
*/
public boolean equals(Object otherObj) {
Vehicle otherVehicle = (Vehicle) otherObj;
return (this.milesPerGal == otherVehicle.milesPerGal);
}
/*
* makes a deep copy of a Vehicle object
*
* @return Object a deep copy of the object
*/
@Override
public Object clone() throws CloneNotSupportedException {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
/**
* compares vehicle by make
* (non-Javadoc)
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public int compareTo(Vehicle vehicle){
return this.make.compareTo( vehicle.make );
}
}

View File

@@ -0,0 +1,109 @@
package com.VehicleExcetionProject;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
/**
* This class will be a vehicle database
*/
public class VehicleDB{
private ArrayList<Vehicle> array;
/**
* Creates a new VehicleDB
*/
public VehicleDB(){
array = new ArrayList<Vehicle>(0);
}
/**
* returns a human readable string
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString(){
StringBuffer sb = new StringBuffer();
for(Vehicle item : array)
sb.append(item.toString() +"\n");
return sb.toString();
}
/**
* added the vehicle to the database
* @param aVehicle
*/
public void addVehicle(Vehicle aVehicle){
array.add(aVehicle);
Collections.sort(array);
}
/**
* finds the all occurrences of a vehicle with the make
* @param make
* @return ta list containing all the vehicles with the provided make
*/
public ArrayList<Vehicle> findVehicle(String make){
ArrayList<Vehicle> temp = new ArrayList<Vehicle>(0);
for(Vehicle item : array)
if(item.getMake().equals(make))
temp.add(item);
return temp;
}
/**
* removes the all occurrences of a vehicle with the make
* @param make
*/
public void deleteVehicle(String make){
Object[] temp = array.toArray();
for(int index = 0; index < temp.length; index++)
if(((Vehicle)temp[index]).getMake().equals(make)){
temp[index] = null;
}
array.removeAll(array);
for(Object item : temp)
if(item != null)
array.add((Vehicle)item);
}
/**
* process the file into the VehicleDB
* @param selectedFile
*/
public void processFile(File selectedFile) {
Scanner scan = null;
try {
scan = new Scanner(selectedFile);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
if(scan != null){
String model;
while(scan.hasNextLine()){
model = scan.next();
if(model.toCharArray()[0] != '#'){
try {
if(model.equals("truck")){
addVehicle(new Truck(scan.next(), scan.nextInt(), scan.nextDouble(), scan.nextDouble()));
}
if(model.equals("bus")){
addVehicle(new Bus(scan.next(), scan.nextInt(), scan.nextDouble(), scan.nextInt()));
}
} catch (Exception e) {
// e.printStackTrace();
}
}
}
}
}
}