Purpose: To learn how to read and write the contents of a text file. This problem emphasizes the design and coding of an object-oriented application using multiple classes, objects, and methods.

Problem: This problem extends the previous work from The Great Cal Poly Underground Part A by adding the ability to load, save, and restore the existing state of the underground to a text file.

Input and Output

Your program must accept the input shown below using the system console. Note that all inputs are case insensitive. All output should be displayed to the console window. Be sure to clearly label each output.

New Commands:

Save. Saves the state of the underground to a text file selected by the user. If the file already exists, prompt the user to overwrite the existing file or cancel.

Restore. Restores the underground from a previously saved text file. If the file doesnt exist, display an appropriate message.

Take. Removes the artifact from the current room and puts in the adventurers backpack. The backpack can hold at most three items. Attempting to take more than this limit will display an appropriate message.

Drop. Removes the artifact from the adventurers backpack and places it in the current room if empty. If the room already has an artifact, it is swapped with the existing room artifact. In this case, the existing room artifact goes in the backpack.

Inventory. Displays the contents of the adventurers backpack.

Help. Display a list of commands along with a corresponding brief explanation of each command.

File Format

The GCPU file format for the underground is a data file that can be easily viewed and edited by any text editor. Besides saving an existing session, this format allows the user to easily deploy the underground for alternative configurations.

The text file uses a key=value structure. The first term on each line is the key, which determines the nature of the remaining data for that line. A sample format appears below:

StartLocation=row, col
Artifact=name, row, col [repeats for each artifact on the map]
Inventory=name [repeats for each artifact in the backpack]
SpecialArtifact=name, state [repeats for each special artifact]

A sample test file follows. The first line is the location of the adventurer. The second through seventh lines each start with Artifact and represent the location of the artifacts in the Underground. The next line starts with Inventory and represent the contents of the backpack. In this case, the backpack holds the paper from the clearing. The remaining lines show the state of any special artifacts (on or off).

StartLocation=3,0
Artifact=picture,2,0
Artifact=lunch,2,1
Artifact=gazebo,1,1
Artifact=exam,1,0
Artifact=sign,0,0
Inventory=paper
SpecialArtifact=paper,on

Test Data and Calculations

Using the sample data provided, build a table of test data and results. The format for your test table should be as follows. Be sure to include the result from your test in the column at the right. see image.

Test File

After finishing running the required tests from your test table, save the state of the underground as GCPU1.txt and submit this file with your project. To properly test the restore function, quit and restart the program, then restore the previously saved session.

Interfaces, Classes, and Responsibilities

This project extends your previous work by adding the interfaces and classes shown below. These structures can be supplemented as necessary. You may create any additional classes, variables, and methods as appropriate for your finished program.

Interface name: Touchable
Methods required: String touch( )

Implement the Touchable interfaces for all Artifacts. Check your existing work to be sure that the interface requirements are met.

Class name: Backpack
Instance Variables:
private ArrayList < Artifact> contents
Instance Constants:
private final int limit = 3;
Methods required:
public boolean setArtifact(Artifact artifact) to add an artifact to the backpack subject to the limit. A
response of true means that artifact was accepted; false means it exceeded the limit and was rejected.
public Artifact getArtifact(int index) to see an artifact at location index
public Artifact removeArtifact(int index) to remove and return the artifact at location index

The backpack class will hold artifacts that are taken and return artifacts that are dropped by the user.

Sample Output for a Partial Session:

Welcome to the Great Cal Poly Underground! (Version 2.0)

You have entered a box canyon.
This looks like the Voorhis Ecological Reserve.
A cavernous opening in the canyon wall lies just ahead of you.
There is an opening to the north.

> inventory
Your backpack is empty.

>look
box canyon
This looks like the Voorhis Ecological Reserve.
A cavernous opening in the canyon wall lies just ahead of you.
There is an opening to the north.
There is a paper here.

>take
[paper] taken.

>inventory
You have a paper.

> take
There is nothing to take.

>n
You have entered the Kellogg Mansion.
This is the former home of William Kellogg.
There is an opening to the east.

>drop
[paper] dropped
[picture] taken

>look
Kellogg Mansion
This is the former home of William Kellogg.
There is an opening to the east.
There is a paper here.

>examine
The paper appears blank.

>inventory
You have a picture.

>save
Enter file to save >GCPU1.txt
File already exists. Overwrite (Y/N)? Y
Save complete.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.