| Homework #1 | CMSC 298P |
| Due Wed Jan 12, 9:00 am | Object-Oriented Programming I for C++ Programmers |
| Type of Homework: Open | Winter 2005 |
Objective
This homework will give you practice designing object-based solutions to problems. In addition, it will allow you to practice arrays of references.
Overview
For this homework you will implement a program that keeps track of customers and cars in a car rental system store. Your program will allow employees to add customers to the database, to add cars, to change the status of a car (from available to rented), etc. You will design and implement all the classes needed to complete this application. More details about the program are provided in the Specifications section. You may want to take a look at the Sample Run section before you read the detailed description.
This homework will be graded as follows:
Program Tasks Overview
You will write a program that enables a car rental system employee to complete the following tasks:
Assumptions
What you must implement, in a nutshell
You must design a solution to the car rental application. The design will describe each of the classes that are part of the application and which methods each one has. We have intentionally only provided the most critical classes. You are expected to design your own classes for representing the basic entities that the program manipulates. You will document your design in the design.txt file, which is part of the code distribution. See the section on design documentation below. After completing your design, you will implement it. The number of classes you decide to use is up to you, however you have to follow a set of restrictions specified below.
Before you read these specifications, you should access the files associated with this homework by checking out the project named p1. The code distribution provides you with the following:
Database Representation
The database of the car rental application will be represented using two sorted lists:
How you represent these lists is part of your design. You can follow any approach you understand is necessary as long as it adheres to the specifications of the homework. Notice that you will not use any sorting algorithm in this project. You will keep your lists sorted by inserting elements in the appropriate position in the list.
Required Classes
You must implement and use the following classes:
Customer
This class represents a customer. A customer is identified by a name (firstname and lastname), address, and an array of cars that the customer currently has rented. As long as you defined an array of cars as part of the instance variables of this class you will be okay. We leave it up to you to define the methods you will need.
CarRentalSystem
This class represents the car rental application. Actually, this is the class we will use to test your program. Access to the car rental application is through this class. The methods' prototypes and behaviors are defined next:
Several of the methods to be described below generate a set of error conditions identified with the following integer values:
|
Condition |
Integer Value |
|
CORRECT |
0 (No error) |
| INVALID_NAME |
1 |
| INVALID_CAR |
2 |
| NONZERO_CARS |
3 |
| INVALID_CAR_RENTED |
4 |
boolean addCar(String make, int year, String tag)
The year parameter represents the year the car was released. The make parameter refers to the car's make (e.g., Toyota, Pissan, etc.). This method will return true if the add operation was completed successfully and false if the car is already part of the database.
String findCar(String tag)
If the car is not currently rented, the format of the String to return is:
CarString ® Make: "make", Year: year, Tag: tag
where make, year, and tag correspond to the car's make, year and tag. If the car is currently rented then the format is:
CarStringRented ® Make: "make", Year: year, Tag: tag (RENTED)
String findCars(String make, int year)
If make is null then the make is ignored in the search process. If year is -1 the year value is ignored in the search process. The method will return null if no cars satisfy the search criteria. The format of the String to be returned is a sequence of CarString each on a line by itself. We refer to this list as the CarList.
boolean addCustomer(String firstName, String lastName,
String address)
The collection of customers is kept sorted by customer name. For example Rose Johnson will appear before Albert Smith. The method will return true if the add operation was successful and false if the customer is already part of the database.
String findCustomer(String firstName, String lastName)
The format of the String to return is one of the following. If the customer has no rented cars the result is:
CustomerString ®
If the customer has rented any cars the result is:
CustomerString ®
where the format CarList was given above.
int removeCustomer(String firstName, String lastName)
The following error conditions can arise while removing a customer from the database. Each of these conditions will be verified in the order to be presented next. Once an error condition has been recognized, the method will return that condition and ignore the rest. For this method the error conditions are:
If neither of these errors occurs, the value CORRECT should be returned.
int rentACar(String firstName, String lastName, String tag)
The following error conditions can arise as a result of this operation. These conditions must be tested in the following order. Once an error condition has been recognized, the method will return that condition and ignore the rest.
If none of these errors occurs, the value CORRECT should be returned.
int returnACar(String firstName, String lastName, String tag)
The following error conditions can arise as a result of this operation. These conditions must be tested in the following order. Once an error condition has been recognized, the method will return that condition and ignore the rest.
If none of these errors occurs, the value CORRECT should be returned.
String allCars()
The format of the string is defined by CarList above. A value of null will be returned if there are no cars in the database.
String allCustomers()
The format of the String to be returned is a sequence of CustomerString each on a line by itself. We refer to this list as the CustomerList. A value of null will be returned if there are no customers in the database.
String toString()
This is just the list of cars followed by the
list of customers, that is:
DatabaseString
®
Additional Classes
As part of your design you will need to define classes in addition to the ones described above. Think carefully about the classes you will need. A good design can simplify dramatically the amount of time you will spend implementing your design. We recommend you implement a class called Name which represents a customer's name. It will have, among other methods, a method that will compare two names so you can insert names in sorted order.
Requirements
Because of the difficulty in grading emailed submissions, we will start deducting points unless you submit via the submit option or the submit server.
The following describes the format of the design.txt file you are expected to provide. Your design file should have two parts:
At the end of this page you will find a class Example.java and the output that it produces. Access to the application is through the CarRentalSystem class, which you will implement. The class Example.java can be found in the code distribution.
Challenge Problem
Remember that you are not required to implement the following problem. Please visit the course web page for information regarding challenge problems. IMPORTANT: If you decide to complete the challenge problem you must provide its implementation in the file called Challenge.java.
The challenge problem consists of defining a GUI for the car rental application. The GUI should allow a user to complete all the tasks associated with the CarRentalSystem class. Feel free to define your interface as you understand is best, providing any error messages you understand are appropriate. Remember to define a main method in the Challenge class that allow us to run your program.
Submission
Submit your project using the submit project option associated with Eclipse. Remember to complete your time log before submitting your homework.
Example.java
(This is provided in the code distribution.)
public class Example {
public static void loadCar(CarRentalSystem carRentalSystem,String make, int year, String tag) {
System.out.println("Adding car: \"" + make + "\"");
carRentalSystem.addCar(make, year, tag);
System.out.println("***Rental Car Database***");
System.out.println(carRentalSystem);
System.out.println();
}
public static void loadCustomer(CarRentalSystem carRentalSystem, String firstname, String lastname, String address) {
System.out.println("Adding customer: " + firstname + " " + lastname);
carRentalSystem.addCustomer(firstname, lastname, address);
System.out.println("***Rental Car Database***");
System.out.println(carRentalSystem);
System.out.println();
}
public static void main(String[] args) {
String results = "", info;
CarRentalSystem CarRentalSystem = new CarRentalSystem();
/* Adding some cars and printing the database status after each add */ loadCar(CarRentalSystem, "Noyota", 2004, "MD101"); loadCar(CarRentalSystem, "Tonda", 1989, "VA201"); loadCar(CarRentalSystem, "Pissan", 1976, "DC301");
/* Adding some customers and printing the database status after each add */
loadCustomer(CarRentalSystem, "John", "Smith", "8th Street Bethesda CA");
loadCustomer(CarRentalSystem, "Rose", "Peterson", "101 Road RiverTown CA");
System.out.println("***All cars***\n" + CarRentalSystem.allCars() + "\n");
System.out.println("***All customers***\n" + CarRentalSystem.allCustomers());
System.out.println("\n***Looking for all Noyota cars***");
if ((info = CarRentalSystem.findCars("Noyota", -1)) != null)
System.out.println(info);
System.out.println("\n***Looking for cars released on or after 1989");
if ((info = CarRentalSystem.findCars(null, 1989)) != null)
System.out.println(info);
System.out.println("\n***John Smith is renting the car with tag \"MD101\"");
if (CarRentalSystem.rentACar("John", "Smith", "MD101") == 0)
System.out.println(CarRentalSystem);
System.out.println("\n***Looking for the car with tag \"DC301\"");
if ((info = CarRentalSystem.findCar("DC301"))!= null)
System.out.println(info);
System.out.println("\n***John Smith is returning the car with tag \"DC301\"");
if (CarRentalSystem.returnACar("John", "Smith", "DC301") == 0)
System.out.println(CarRentalSystem);
}
}
Output of Example.java
Adding car: "Noyota"
***Rental Car Database***
Make: "Noyota", Year: 2004, Tag: MD101
Adding car: "Tonda"
***Rental Car Database***
Make: "Noyota", Year: 2004, Tag: MD101
Make: "Tonda", Year: 1989, Tag: VA201
Adding car: "Pissan"
***Rental Car Database***
Make: "Noyota", Year: 2004, Tag: MD101
Make: "Pissan", Year: 1976, Tag: DC301
Make: "Tonda", Year: 1989, Tag: VA201
Adding customer: John Smith
***Rental Car Database***
Make: "Noyota", Year: 2004, Tag: MD101
Make: "Pissan", Year: 1976, Tag: DC301
Make: "Tonda", Year: 1989, Tag: VA201
Name: John Smith
Address: 8th Street Bethesda CA
No cars rented.
Adding customer: Rose Peterson
***Rental Car Database***
Make: "Noyota", Year: 2004, Tag: MD101
Make: "Pissan", Year: 1976, Tag: DC301
Make: "Tonda", Year: 1989, Tag: VA201
Name: Rose Peterson
Address: 101 Road RiverTown CA
No cars rented.
Name: John Smith
Address: 8th Street Bethesda CA
No cars rented.
***All cars***
Make: "Noyota", Year: 2004, Tag: MD101
Make: "Pissan", Year: 1976, Tag: DC301
Make: "Tonda", Year: 1989, Tag: VA201
***All customers***
Name: Rose Peterson
Address: 101 Road RiverTown CA
No cars rented.
Name: John Smith
Address: 8th Street Bethesda CA
No cars rented.
***Looking for all Noyota cars***
Make: "Noyota", Year: 2004, Tag: MD101
***Looking for cars released on or after 1989
Make: "Noyota", Year: 2004, Tag: MD101
Make: "Tonda", Year: 1989, Tag: VA201
***John Smith is renting the car with tag "MD101"
Make: "Noyota", Year: 2004, Tag: MD101
Make: "Pissan", Year: 1976, Tag: DC301
Make: "Tonda", Year: 1989, Tag: VA201
Name: Rose Peterson
Address: 101 Road RiverTown CA
No cars rented.
Name: John Smith
Address: 8th Street Bethesda CA
Cars (rented):
Make: "Noyota", Year: 2004, Tag: MD101
***Looking for the car with tag "DC301"
Make: "Pissan", Year: 1976, Tag: DC301
***John Smith is returning the car with tag "DC301"