In: Computer Science
C++ and Java Zoo File Manager
I need to complete these files:
#include <iostream>
#include <jni.h>
using namespace std;
void GenerateData() //DO NOT TOUCH CODE IN THIS METHOD
{
JavaVM *jvm; // Pointer to the JVM (Java Virtual Machine)
JNIEnv *env; // Pointer to native interface
//================== prepare loading of Java VM
============================
JavaVMInitArgs vm_args; // Initialization arguments
JavaVMOption* options = new JavaVMOption[1]; // JVM invocation
options
options[0].optionString = (char*) "-Djava.class.path="; // where to
find java .class
vm_args.version = JNI_VERSION_1_6; // minimum Java version
vm_args.nOptions = 1; // number of options
vm_args.options = options;
vm_args.ignoreUnrecognized = false; // invalid options make the JVM
init fail
//=============== load and initialize Java VM and JNI interface
=============
jint rc = JNI_CreateJavaVM(&jvm, (void**)&env,
&vm_args); // YES !!
delete options; // we then no longer need the initialisation
options.
if (rc != JNI_OK) {
// TO DO: error processing...
cin.get();
exit(EXIT_FAILURE);
}
//=============== Display JVM version
=======================================
cout << "JVM load succeeded: Version ";
jint ver = env->GetVersion();
cout << ((ver >> 16) & 0x0f) << "." <<
(ver & 0x0f) << endl;
jclass cls2 = env->FindClass("ZooFileWriter"); // try to find
the class
if (cls2 == nullptr) {
cerr << "ERROR: class not found !";
}
else { // if class found, continue
cout << "Class MyTest found" << endl;
jmethodID mid = env->GetStaticMethodID(cls2, "createZooFile",
"()V"); // find method
if (mid == nullptr)
cerr << "ERROR: method void createZooFile() not found !"
<< endl;
else {
env->CallStaticVoidMethod(cls2, mid); // call method
cout << endl;
}
}
jvm->DestroyJavaVM();
cin.get();
}
void AddAnimal()
{
/*
TODO: Write proper code to add an animal to your vector (or
array)
*/
}
void RemoveAnimal()
{
/*
TODO: Write proper code to remove an animal from your vector (or
array. Remmber to re-allocate proper size if using array)
*/
}
void LoadDataFromFile()
{
/*
TODO: Write proper code to load data from input file (generated
using JNI) into vector/array.
*/
}
void SaveDataToFile()
{
/*
TODO: Write proper code to store vector/array to file.
*/
}
void DisplayMenu()
{
/*
TODO: write proper code to display menu to user to select
from
*/
}
int main()
{
GenerateData();
return 1;
}
// Zooooo.cpp :
//
#include "stdafx.h"
#include <iostream>
#include<string.h>
#include<string>
#include<conio.h>
#include<fstream>
using namespace std;
int i = 0;//veriable to get indexes of array or to use as array
size
string Array[1];//Array to store animal
void AddAnimal()
{
Array[0] = { "Lion"};//animal name
cout << "\nAnimal Inserted Successfuly!";
}
void RemoveAnimal()
{
Array->erase();//array will be
erased....completely
cout << "\nArray Erased Successfuly!";
}
void LoadDataFromFile()
{
//so, there you need to add your file location as i
did...
string file_name = "jni.txt";//there put the
location of your file like-> C:/user/java/jni.txt
fstream file;
file.open(file_name);//opening the file
if (file.fail())//if file address is wrong or file not
found then we will give error so put right address here...
cout << "file opening
Error!";
else
{
string line;//string to get line
from file
while (getline(file,line ))//get
line from file until the end from data of file...
{
Array[i] =
line;//now line will bw stored to array...
i++;
}
cout << "\nData Loded From
File Successful!";
}
}
void SaveDataToFile()
{
//you can change the address of file or name that i
used here...
fstream file;
string file_name = "data_file.txt";//same do here
where you want to save data otherwise if file not found you will
recieve error
file.open(file_name);//open file
if (file.fail())
cout << "\nFile Opening
Failed!";
else
{
int array_size = 0;
while (array_size<=i)
{
file <<
Array[array_size] << endl;//getting data from array and
saving it to file
array_size++;
}
cout << "\nData on File Saved
Successfuly!";
}
}
void DisplayMenu()
{
int option;
menue:
cout <<
"\n\t\tMENUE\n-------------------------------------\n----------------------------------------";
cout << "\n\n1.Add Animal\n2.Remove
Animal\n3.Load Data From File\n4.Save Data to File\n5.Exit\n\nENTER
YOUR OPTION: ";
cin >> option;
switch (option)
{
case 1://if user press 1
AddAnimal();//function will be
clled
_getche();
goto menue;
case 2://if press 2
RemoveAnimal();//function will be
clled
_getche();
goto menue;
case 3://if press 3
LoadDataFromFile();//function will
be clled
_getche();
goto menue;
case 4://if press 4
SaveDataToFile();//function will be
clled
_getche();
goto menue;
case 5://if press 5
return;//stop execution of program
will end....
default://if user will not press right option...
cout << "\nInvalid
Option!";
cout << "\nPress any key to
contniue...";
_getche();
goto menue;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
DisplayMenu();//calling the menue function to display
Menue...
return 0;
}
if you feel any Query please do a Comment
Give me a thumbs up