Visual Graphical Unit Conversion Calculator Using CSV Config File
This project will build on what you learned about File I/O and reading
CSV Files
in Unit 7, as well as ArrayLists in Unit 6. We will also build on your experience with doing unit conversion calculations in Unit 3: Lab 3.
It is very common for software applications to use configuration files for storing data that can be changed externally from the program code itself so that programs can be modified externally without the need to compile and deploy new versions in cases of minor configuration changes. Configuration data should be stored externally in a database or configuration files rather than in the code itself.
In this case, we store conversion factors in a .csv file that is read in at program start-up. For example, we can store a conversion factor such as Foot-Inch as a key, which is read from the .csv into a Java Map object, which once loaded from the csv, we can easily look its value of 12.0 in this case, then multiply the conversion factor by an input value to get the result. You are provided with an IntelliJ application project GUI (graphical user interface), which mimics the Google conversion tool. You will modify this tool as an exercise.
Spinner
To get started, install the code (and config file) for the application, follow the instructions below:
You will notice that Volume and Mass are not included conversion types in the app at this point. But it is pretty easy to add new conversion types to our app, as you will see. It is also very easy to test conversion factors since all the conversion mapping functionality is self-contained in the ConversionMap class since ConversionMap has a main method that can be used for testing lookups outside the GUI. Here are the steps for testing lookup and testing conversion lookups and adding support for new Conversion types.
Step 1: Download and Install the IntelliJ Idea Project File
For this project, you will have to do very little new coding. However, you will need to learn to work with a larger multi-file Java project. The first step is to download the following IntelliJ project: JSpinnerConversionNoMassVolume.zip (I will send the zip file).
and extract the zip file into your IntelliJ home directory. You do not need to create a folder JSpinnerConversionNoMassVolume folder first in IntelliJ since the extraction will take care of creating the folder for you. Again, extract into your default IdeaProjects directory.
IntelliJ by default uses /IdeaProjects as the home directory. My project for example is at c:\users\gener\IdeaProjects, where my userName is "gener". Once extracted the project should, therefore, be located at c:\\IdeaProjects\JSpinnerConversion.
Once you have extracted the project, you can simply Open Project 'JSpinnerConversion' using the Open menu in IntelliJ and the project will display something like the following.
JSpinner Project
After you have installed the project, if necessary, you can click on the project tab on the left in IntelliJ to expand the project.
The source code and resources used for this project are located in several sub-directories under src\main and src\main\java. You can run the project JSpinnerConversion from IntelliJ and if you have set things up correctly, you see the ConversionTool GUI as shown above. Try it out.
There are other Runnables in the project including ReadCSVFromScanner and ConversionMap. These are regular command-line programs, without GUIs, but understanding these will help you understand how everything fits together. These two classes have main methods that are used for testing these classes independent from the GUI. You should run each of these in order to understand what they do. You can add your own test cases in the steps below.
Step 2: Working with CSV files:
Be sure to browse the JSpinnerConversion project directories and become familiar with the different packages and resources.
There are several main classes that you can run in this project. We will start with the class ReadCSVWithScanner.
Examine the code in this class and the ConversionsLong.csv file in the resources directory. ReadCsvWithScanner parses the ConversionsLong.csv file and returns an ArrayList of the model type , which stores the unit conversion factor stored in the CSV file by name.
Open ReadCSVWithScanner.java in the IntelliJ editor. Then select ReadCSVWithScanner from the list of programs available to run or press the Green arrow at the top of the editor. ReadCSVWithScanner will the CSV configuration file from the resources directory and print the formatted data as was below. Use the Run menu or the green arrow to select which Runnable to run, in this case, ReadCSVWithScanner. The main in this class will load the csv and print out the results. A portion of the results is shown below.
readCSV
Take a look at the source code for ReadCSVWithScanner. The CSV file that is read in, is located under src\main\resources. The main class files are located inside the package folder edu.dccc.ui. Below that package folder, there is another package folder called edu.dccc.model.
Java data models are simple data structure classes used for storing the row elements from CSV files or databases and therefore are located in the model folder. We saw for example the Employee model in Unit 7.
Finally, there is a edu.dccc.config package that contains the ConversionMap and ReadCSVWithScanner classes. These are used for loading and handling the conversion settings.
Running ReadCSVWithScanner tests that we can correctly read in the required CSV files and print out Java object representations of the data as text.
Note: To run a main class in IntelliJ, open the class you want to run in the editor. Select the Run menu. If your class appears, you can run it. If not, select the blank Run submenu. If you have your class open in the editor, it should appear in the list of available runnable programs and you can select it from the blank Run submenu. You can also open a Runnable file in the editor and press the Green button at the top of the class to run it.
Save a screenshot of the ReadCsvFromScanner text output and submit your results to the assignment.
Step 3: Test several conversion operations that are not yet included in your version of the tool.
Your extracted version of the Conversion Tool GUI does not currently support Volume and Mass conversions. We will make a few simple changes to the GUI to add that support in Step 4. But before we get to that, let's test a few mass and volume conversions using our ConversionMap class to make sure we can look up these conversions. The new conversation types are already supported in the configurations file.Look at the conversionsLong.csv file and find some Mass or Volume conversions to test. Choose three for four of each to test (examples):
kilogram-pound
ounce-gram
pound-ounce
quart-pint
gallon-cup
liter-quart
quart-liter
gallon-liter
etc.
Add three or four different test conversion print statements for Volume and three or four for Mass in the main method of ConversionMap to test conversions of your choice. There are several print test messages already in main that you can use as your pattern example. Run ConversionMap with test output and include your modified ConversionMap java class file and screenshots in your submission.
Step 4: Add Volume and Mass Support to the GUI class ConversionGUI - Modify the ConversionGUI class as follows:
Add "Mass" and "Volume" Strings to the conversionTypesArray String Array, which is used to set the conversion types spinner options. Then create massTypesModel and volumeTypeModel variables to use for setting the left and right spinners for Mass and Volume. Here is the lengthTypeModel as an example to see how this is done:
CyclingSpinnerListModel lengthTypeModel = new CyclingSpinnerListModel(lengthTypesArray);
CyclingSpinnerListModel lengthTypeModel2 = new CyclingSpinnerListModel(lengthTypesArray2);
Add two sets up models similar to the above for Mass and Volume, four statements in all.
Add elseif's for setting the Mass and Volume models for Mass and Volume in the the conversion type change event, following the pattern of Area:
else if (conversionTypeSpinner.getValue().equals("Area")) {
conversionSelectionA.setModel(areaTypesModel);
conversionSelectionB.setModel(areaTypesModel2);
}
Here's an Example for Mass: (Copy this into your code and add another else if for "Volume":
else if (conversionTypeSpinner.getValue().equals("Mass")) {
conversionSelectionA.setModel(massTypesModel);
conversionSelectionB.setModel(massTypesModel2);
}
Step 5:
Rerun JSpinnerConversion. You should now see the two new ConversionTypes Mass and Volume and you should be able to run calculations on these. Easy.
Submit your modified Java class files and screenshots in your ConversionGUI class.
Don't forget to also include your code changes and test output for ConversionMap and test output for ReadCSVFromScanner.