This assignment requires you to design a class that rents a car by user input and by reading from a file.
There are 2 parts to this assignment. The first is a basic class that interacts with a tester, the second is a class that allows the Car class to interact with file input.
This assignment requires a comment above each class which includes: Name, PID, 8B account, Email address, and a 2-3 sentence *high-level* description of what the class does.
You must also include a comment above each method that is a *high-level* description of what the method does.
You should also include inline comments throughout your code where necessary.
Use a multi-line comment (/* */).
You must observe an 80 column limit. No line of your program may be longer than the 80th column on the screen.
Begin by copying this code.
Member variables:
There are 2 constructors for this class.
You will create accessing methods for your member variables.
You will create a toString method which takes no parameters and returns a String.
Begin by copying this code into a file called RentalAgency.java. There is one conditional written for you, it is one of the more simple conditionals, but it might help to clear up confusion and get you started.
While we are
often asked to write interactive software for users, we may build it
and test it to run from a textfile of input. That is our boss or our
colleagues or us ourself may define a bunch of test cases -- by giving
the exact set of "input" that should be read in the program to test
out certain cases. These serves as documentation of the kind of input
the program expects and the test cases that have been run on it.
For Part B, write a class RentalAgency that takes input from a text file,
not dynamically from user input. We've provided 2 sample .txt files
for you to test with. However you need to create your own test file
to submit along with the assignment. You will receive points for the
quality of your test file (3pts, excellent, 2 pts OK, 1pt subpar, 0pts
missing). In fact, we are requiring you to submit your test case
early (by Tuesday 11:59pm week 2 -- using bundle). THEN, we suggest that
you share your test cases on webboard and test your code with other
people's test cases! There will be a prize awarded for the BEST test
case submitted.
You may revise your code up until Thurs 11:59pm Week 2 -- when you will
run bundle again. You can, at that point, turn in your favorite test
case with the program. This can be one you created yourself, or it
can be one that you modified from others. However, you must
have a comment in the first line of your test case giving attribution
to anyone else who contributed to your test case (or the ideas you
used to create it).
This file will be very similar to RentACar.java, except it will read from a file. When writing Part A, you should have read through RentACar.java and understood what was going on. If you haven't done so, do so now. It is mostly the same, just reading from different mediums.
The purpose of Car[] cars is to hold up to 10 instances of cars. It will allow you to know which cars variables should be accessed.
After the BufferedReader is created, you should have a while loop that reads in one line of the text file at a time until the entire file has been read.
Each Iteration of the Loop: Should split the line read on a space, then save determine what needs to be donei, and declare a Car variable (should initialize to null, NOT default constructor).
You should have 11 conditionals. One for each of these possible inputs.
You should have a variable String type; that always reads in the first word on each line, and will be holding the action the user wishes to take.
Rent: If type = "rent", and if 10 cars haven't been rented so far, you need to read in the rest of the line, which will be holding the parameters for a car. The second word on the line will be the color, store it. The third word is the number of doors. You first need to format it from a String to an integer ((new Integer(String)).intValue()), then you should store that int. The 5 remaining words are the stations (1-10) that the user wishes to preset their radio to, so the fourth word needs to be formatted to an int, then added at the first position in an int [] and so on. There needs to be some checking at this point. If the file only has 4 stations, for example, then you should catch that, print an error message and set the unfilled stations to 0 (or rock). Also, if there are no words after "rent", you should call the default constructor. Now you should have 3 variables: String color, int doors, int[] playlist. These variables should be sent to your constructor and the car that you initialized to null above the loop should be set to this, and this new car should be put in the next open index in the cars array.
Done: If type = "done", you should stop reading input, and end the program without System.exit(0). In fact, you should not use System.exit(0) anywhere in your code.
If type = anything else besides rent or done, you should: save the number refering to which car the user wishes to access, then get the actual corresponding car object from the car array. NOTE: In the text file, 1 refers to the first car, which i actually index 0 in the cars array. If the user is trying to access a car that has not been created yet, so the cars array holds null for that position, you should print an error message and continue to the next line in the text file.
Note: You should set the car object you created above the loop to the car object you get from the cars array.
Drive: If type = "drive", read in the third word from the line and convert it to an int. This is the number of miles the specified car has driven. Attempg to add the miles to that car, if it makes the car run out of gas, set that car in the cars array to null, and continue to the next line.Driven: If type = "driven", print how many cars that specific car has driven.
Fill: If type = "fill", read in the third word and convert it to an int, this is how many gallons to add to the car. Call the appropriate Car method.
Gas: If type = "gas", print how much gas remains in the tank of that specific car.
MPG: If type = "mpg", print how many mpg that specific car does.
Doors: If type = "doors", print how many doors that specific car has.
Color: If type = "color", print the color of that specific car.
Specs: If type = "specs", print the car.
Music: If type = "music", print out the current stations for that specific car. Then read in the third word on the line and convert it to an int. This will be the station that the user wants to change. Make sure the station is within 1-5, if it isn't, print an error message and read the next line. Then read in the fourth word and convert it to an int. This will be the new channel the user wants. Make sure the channel is within 1-10, if it isn't, print an error message and read the next line. If everything is valid, print a message stating what station is being changed to what channel, call the appropriate method to make the change, and then print all of that specific cars new preset stations.
If none of these cases get caught, then stop reading from the file and end normally.
All of this should be in a try-catch block. You should be catching any exception, printing an error message, and ending the program normally.
For Part A you MUST use our interactive tester, which can be found here. For Part B, you MUST use our test files, test1.txt and test2.txt, and you MUST create your own test file and submit it with the rest of your assignment. You should name it myTest2.txt.
The output should match the output below exactly. (You can use the output to determine default values, etc.)
For the second tester, each block of code is a different test case. They are to check error messages. So after eash block, your program should end normally.
Spacing does not necessarily have to be exactly the same, but the messages should be.
As mentioned above, these are NOT testing ALL cases, you will be expected to test more then what these test.
By Thursday, April 9th, 11:59pm:
When you are ready to turn in your homework, first open a terminal window by right clicking on the empty area of your computer's desktop (have to use
lab computers or remotely connect from your home computer using SSH or Putty)
and select "Open a New Terminal". Then, run bundlePSA1A from your PSA1A/ directory.
Files are required to be submitted for PSA1A:
-bash-2.05b$ cd ~/PSA1A -bash-2.05b$ bundlePSA1A Good; all required files are present: myTest1.txt Do you want to go ahead and turnin these files? [y/n] y OK. Proceeding. Performing turnin of approx. XXXX bytes Copying to /home/linux/ieng6/cs8b/turnin.dest/cs8bxx.PSA1A . Done. Total bytes written: XXXX Please check to be sure that's reasonable. Turnin successful.
Make sure the Turnin is successful, otherwise we won't receive your submission! . If you want to make changes after you've turned in your homework, make the changes and run bundlePSA1A again. The program will warn you that a "previously turned-in file exists" and ask if "you wish to over-write this existing file". Respond in the affirmative by typing y and pressing the Enter key.
By Saturday, April 11th, 11:59pm:(Note:myTest2.java is your favorite test case after seeing all other ones form your peers, or a modified version.)
When you are ready to turn in your homework, first open a terminal window by ri
ght clicking on the empty area of your computer's desktop (have to uselab computers or remotely connect from your home computer using SSH or Putty)and select "Open a New Terminal". Then, run bundlePSA1B fro
m your PSA1B/ directory.
Files are required to be submitted for PSA1B:
-bash-2.05b$ cd ~/PSA1B
-bash-2.05b$ bundlePSA1B
Good; all required files are present:
Car.java
RentalAgency.java
myTest2.txt
Do you want to go ahead and turnin these files? [y/n] y
OK. Proceeding.
Performing turnin of approx. XXXX bytes
Copying to /home/linux/ieng6/cs8b/turnin.dest/cs8bxx.PSA1B
.
Done.
Total bytes written: XXXX
Please check to be sure that's reasonable.
Turnin successful.
Make sure the Turnin is successful, otherwise we won't receive your submission! . If you want to make changes after you've turned in your homework, make the changes and run bundlePSA1B again. The program will warn you that a "previously turned-in file exists" and ask if "you wish to o
ver-write this existing file". Respond in the affirmative by typing y
code> and pressing the Enter key.