Question: This is the question and answer is below. I want to convert the code from c++ to java. CS 2336 - PROJECT 4 - Redbox
This is the question and answer is below. I want to convert the code from c++ to java.
CS 2336 - PROJECT 4 - Redbox Inventory System
Project Due: 11/21 by 11:59 PM
KEY ITEMS: Key items are marked in red. Failure to include or complete key items will incur additional deductions
as noted beside the item.
Submission:
The file containing main must be named Main.java. (-5 points)
The project files must be in packages that start with RedBox.* (-5 points)
All project deliverables are to be submitted in eLearning
o Zip the contents of the src directory into a single zipped file
o Make sure the zipped file has a .zip extension (not .tar, .rar, .7z, etc.) (-5 points)
o Add your project's presentation link in the comments section in eLearning
Programs must compile and run with Java SE 13.
Each student is responsible for developing unit test cases to ensure the program works as expected.
Type your name and netID in the comments at the top of all files submitted. (-5 points)
Objectives:
Implement a binary search tree class in Java
Utilize a binary search tree
Create a modular code solution with multi-packages
Use the Singleton pattern to create and manage the Scanner object
Use Java Generics to create generic classes and methods
Problem: Redbox needs a program to track inventory and to generate a report for their DVD rental kiosks. Given
a log of transactions including renting and returning DVDs as well as adding and removing DVD titles, the program
will need to process each transaction and create a report after all transactions have been processed. The
generated report will list all DVD titles stored in the kiosk as well as how many of each disc are in the kiosk.
Details:
The inventory will be held in a binary search tree that you will create (-15 points if not)
Use the DVD title to determine node placement in the tree
The binary tree will be seeded with an inventory file
Once seeded, the program will parse a transaction log to update the inventory
There are five possible transactions:
o Add
Add a new title
Create a new node and insert it into the tree
Add copies to an existing title
Find the title in the tree and increase the number of available copies by the
amount listed
o Remove copies of an existing title
Find the title in the tree and reduce the number of available copies by the amount listed
If number available is zero and no copies are rented out, delete the node from the tree
There will not be more copies removed than available.
o Rent a DVD
Reduce available amount of an existing title by one and increase rented amount by one
o Return a DVD
Increase available amount of an existing title by one and reduce rented amount by one
Classes:
Node
o Members
Data field that will hold a Movie object [Title (string - case insensitive), Available (integer),
Rented (integer)]
Left (node pointer)
Right (node pointer)
o Methods
Overloaded constructor
Mutators
Accessors
Binary Search Tree
o Binary Search Tree class must use generics (-5 points if not)
o Members
Root (node pointer)
o Methods
Mutator
Accessor
Insert (recursive) (-5 points if not)
Search (recursive) (-5 points if not)
Delete
Other methods that are necessary to interact with a binary search tree
Remember that methods should be generic to be used on a binary tree regardless
of the problem and data type.
User Interface and Input: There is no user interface for this program. Input will be loaded from two files,
inventory.dat and transaction.log. The inventory.dat is assumed to be error free and it will be
read first by your program and loaded into a binary search tree. Each line (except the last line which may not have
a newline character) in inventory.dat will be formatted as follows (NOTE:
"
After processing the inventory file, begin processing transaction.log. Each line of the file should follow one
of the following formats (Note: Command is case insensitive):
aDd "
ReMove "
rent "
retUrn "
The transaction file may contain errors due to network disruptions from the main server. For each line in the
transaction log, validate that it follows one of the formats listed above. If it is the correct format, process the
transaction. If the
file (as described below). All numbers are expected to be integers. To be valid, the line must follow the format
exactly. Also, do not assume that a title in the transaction log will be in the tree.
Output: A file named error.log will be created only if any lines of transaction.log are invalid.
Error.log will contain all invalid entries of the transaction file.
At the end of the program, create a formatted report file that contains each title, the number of copies available
to rent for that title as well as the number of copies that are currently rented. The titles should be listed in
alphabetical order (without the double quotes). The report should be arranged in three formatted columns that
line up the data nicely:
Title
Copies available
Copies rented
Title Available Rented
-----------------------------------------------------------
Collateral Beauty 2 2
Doctor Strange 1 2
Hacksaw Ridge 0 1
Write the report to a file named redbox_kiosk.txt
Program Flow:
Program starts in main() method
Load inventory.dat
Create and populate the Binary Search Tree
Load transaction.log
Process all transactions
Output final inventory to file
Unit Testing:
A unit test method is required to test each of the commands listed in the write-up above. These methods will be
used by the unit testing framework to test the accuracy of your code.
AddTitle(...)
RemoveTitle(...)
RentTitle(...)
ReturnTitle(...)
Grading:
Coding standards, style and comments (20 Points)
o Create a multi-package code solution (8 Points)
o Use Singleton pattern for scanner instance (2 Points)
o Coding standards and comments (10 Points)
Unit testing methods x 4, one for each of the methods mentioned above (12 Points)
The rest of the grade will be broken down as follows:
AddTitle(...) (12 Points)
RemoveTitle(...) (12 Points)
RentTitle(...) (12 Points)
ReturnTitle(...) (12 Points)
Generic Binary Search Tree (20 Points)
Notes:
No startup code solution will be provided. You are expected to start a new solution from scratch and layer
your solution in a way similar to project #3.
All *.dat files are assumed to be local to the current executable.
Your program is expected to have basic file validation.
Your program must use a Singleton pattern for Scanner instance. This ensures that only one instance of
the Scanner object is created throughout your program.
You must implement and utilize a generic binary search tree class. Your class should not inherit any
existing Java binary tree class.
Deliverables: Up to 10 minutes project video presentation (MS Teams) with a functioning program
including Unit Testing methods.
Answer:
Main.cpp
#include "Node.h"
#include "BinaryTree.h"
#include
#include
#include
#include
using namespace std;
/**
*Creates a Binary Tree of type Node with a given input file.
*
*@param input file from which the binary tree's information will be pulled from
*@return Binary Tree of type Node
*/
BinaryTree
/**
*Reads and processes the transactions onto the Binary Tree with a given input file
*
*@param input file from which the transactions will be pulled from
*@param tree Binary Tree of type Node
*/
void readTransaction(ifstream &input, BinaryTree
/**
*Adds copies of movies to the tree.
*If the Binary Tree does not have a given movie, add it to the tree.
*
*@param line the string with info of the transaction
*@param tree Binary Tree of type Node
*/
void add(string line, BinaryTree
/**
*Removes copies of movies to the tree.
*If the movie has no more copies, remove it from the tree.
*
*@param line the string with info of the transaction
*@param tree Binary Tree of type Node
*/
void remove(string line, BinaryTree
/**
*Rents movies.
*Decrement available copies and increment rented copies by 1.
*
*@param line the string with info of the transaction
*@param tree Binary Tree of type Node
*/
void rent(string line, BinaryTree
/**
*Returns movies.
*Increment available copies and Decrement rented copies by 1.
*
*@param line the string with info of the transaction
*@param tree Binary Tree of type Node
*/
void returnMovie(string line, BinaryTree
/**
*Find a substring from the given string between quotation marks.
*
*@param line the string with info of the transaction
*/
string parseTitle(string line);
/**
*Find the first occurence of a number.
*
*@param line the string with info of the transaction
*/
int parseInt(string line);
/**
*Check if a movie is already in the tree.
*
*@param line the string with info of the transaction
*@param tree Binary Tree of type Node
*@return if the movie is in the tree
*/
bool checkExists(BinaryTree
int main()
{
//create tree
ifstream input("inventory.dat");
BinaryTree
input.close();
//process transactions
input.open("transaction.log");
readTransaction(input, tree);
input.close();
//print tree in alphabetical order
ofstream out("redbox_kiosk.txt");
tree.printOrder(tree.getRoot(), out);
out.close();
return 0;
}
BinaryTree
{
//creat the tree
BinaryTree
string line;
getline(input, line);
//gather info for root node
string title = parseTitle(line);
int available = parseInt(line.substr(line.rfind('"')));
int rented = parseInt(line.substr(line.rfind(',')));
//set the root of the tree
tree.setRoot(new Node(title, available, rented));
while (getline(input, line))
{
//gather info for node to insert
title = parseTitle(line);
available = parseInt(line.substr(line.rfind('"')));
rented = parseInt(line.substr(line.rfind(',')));
//insert node into tree
tree.insert(tree.getRoot(), new Node(title, available, rented));
}
return tree;
}
void readTransaction(ifstream &input, BinaryTree
{
string line;
ofstream output("error.log");
while (getline(input, line))
{
//check for a proper command
//make sure there are only 3 or 1 characters after the last quotation mark
string check = line.substr(line.find_last_of('"'));
int icheck = (int)check.length();
if (line.rfind("add", 0) == 0 && icheck == 3)
{
string check2 = line.substr(0, line.find_first_of('"'));
int icheck2 = (int)check2.length();
if (icheck2 == 4) // make sure begginning is "add "
{
add(line, tree);
continue;
}
}
else if (line.rfind("remove", 0) == 0 && icheck == 3)
{
string check2 = line.substr(0, line.find_first_of('"'));
int icheck2 = (int)check2.length();
//make sure that the movie is in the tree
if (checkExists(tree, line) && icheck2 == 7) //make sure beginning is "remove "
{
remove(line, tree);
continue;
}
}
else if (line.rfind("rent", 0) == 0 && icheck == 1)
{
string check2 = line.substr(0, line.find_first_of('"'));
int icheck2 = (int)check2.length();
//make sure that the movie is in the tree
if (checkExists(tree, line) && icheck2 == 5) //make sure beginning is "rent "
{
rent(line, tree);
continue;
}
}
else if (line.rfind("return", 0) == 0 && icheck == 1)
{
string check2 = line.substr(0, line.find_first_of('"'));
int icheck2 = (int)check2.length();
//make sure that the movie is in the tree
if (checkExists(tree, line) && icheck2 == 7) //make sure beginning is "return "
{
returnMovie(line, tree);
continue;
}
}
//output to error log
output << line << endl;
}
}
void add(string line, BinaryTree
{
//gather info for adding a movie
string title = parseTitle(line);
int add = parseInt(line);
//search for the node
Node *node = tree.search(tree.getRoot(), new Node(title));
//if node does not exist in tree, insert it
if (!node)
{
Node *newNode = new Node(title, add, 0);
node = tree.insert(tree.getRoot(), newNode);
return;
}
//increase available copies by add amount
node->setAvailable(node->getAvailable() + add);
}
void remove(string line, BinaryTree
{
//gather info for movie to remove
string title = parseTitle(line);
int remove = parseInt(line);
//find the node of the movie
Node *node = tree.search(tree.getRoot(), new Node(title));
//subtract remove amount
node->setAvailable(node->getAvailable() - remove);
//if number of available movies is less than 0, remove the node
if (node->getAvailable() <= 0 && node->getRented() <= 0)
{
tree.remove(tree.getRoot(), node);
}
}
void rent(string line, BinaryTree
{
//gather info for movie to rent
string title = parseTitle(line);
//find the node of the movie
Node *node = tree.search(tree.getRoot(), new Node(title));
//decrease available amount and increase rented amount by 1
node->setRented(node->getRented() + 1);
node->setAvailable(node->getAvailable() - 1);
}
void returnMovie(string line, BinaryTree
{
//gather info for movie to return
string title = parseTitle(line);
//find the node of the movie
Node *node = tree.search(tree.getRoot(), new Node(title));
//increase available amount and decrease rented amount by 1
node->setAvailable(node->getAvailable() + 1);
node->setRented(node->getRented() - 1);
}
string parseTitle(string line)
{
//return string between quotation marks
int start = line.find_first_of('"');
int end = line.find_last_of('"');
return line.substr(start + 1, end - start - 1);
}
int parseInt(string str)
{
//finds the first occurence of a number
int num;
int index = str.find_first_of("0123456789");
if (index > 0)
{
char s = str[index];
num = atoi(&s);
}
return num;
}
bool checkExists(BinaryTree
{
//checks to see if a given node exists
string title = parseTitle(line);
Node *n = tree.search(tree.getRoot(), new Node(title));
return n;
}
Node.cpp
#include "Node.h"
#include
Node::Node()
{
}
Node::Node(std::string title)
{
this->title = title;
}
Node::Node(std::string title, int available, int rented)
{
this->title = title;
this->available = available;
this->rented = rented;
}
Node::Node(std::string title, int available, int rented, Node *left, Node *right)
{
this->title = title;
this->available = available;
this->rented = rented;
this->left = left;
this->right = right;
}
void Node::copyNode(Node n)
{
this->title = n.title;
this->available = n.available;
this->rented = n.rented;
}
bool Node::operator==(const Node &right)
{
return title == right.title;
}
bool Node::operator>(const Node &right)
{
return title > right.title;
}
bool Node::operator<(const Node &right)
{
return title < right.title;
}
std::ostream& operator<<(std::ostream &out, const Node &node)
{
out << std::setw(30) << std::left << node.title;
out << std::setw(5) << std::right << node.available << std::setw(5) << node.rented;
return out;
}
Node::~Node()
{
}
BinaryTree.h
#ifndef BINARY_TREE_H
#define BINARY_TREE_H
#include
#include
template
class BinaryTree
{
private:
T *root;
public:
void setRoot(T *root) { this->root = root; }
T *getRoot() { return root; }
T *min(T *root);
T *insert(T *root, T *node);
T *search(T *root, T *node);
T *remove(T *root, T *node);
void printOrder(T *root, std::ostream &out);
};
template
T* BinaryTree
{
T *current = root;
//find left most leaf
while (current->getLeft())
{
current = current->getLeft();
}
return current;
}
template
T* BinaryTree
{
//base case
if (!root)
{
root = node;
}
//what we're inserting belongs on the left
if (*node < *root)
{
root->setLeft(insert(root->getLeft(), node));
}
//what we're inserting belongs on the right
else if (*node > *root)
{
root->setRight(insert(root->getRight(), node));
}
return root;
}
template
T* BinaryTree
{
//base case
if (!root || *root == *node)
{
return root;
}
//what we're searching for is on the right
if (*root < *node)
{
return search(root->getRight(), node);
}
//what we're searching for is on the left
return search(root->getLeft(), node);
}
template
T* BinaryTree
{
//base case
if (!root)
{
return root;
}
//what we're deleting is on the left
if (*node < *root)
{
root->setLeft(remove(root->getLeft(), node));
}
//what we're deleting is on the right
else if (*node > *root)
{
root->setRight(remove(root->getRight(), node));
}
//we've found what we're deleting
else
{
//0 children
if (!root->getLeft() && !root->getRight())
{
delete root;
root = nullptr;
}
//1 child
else if (!root->getLeft())
{
T *temp = root->getRight();
root->setTitle(temp->getTitle());
root->setAvailable(temp->getAvailable());
root->setRented(temp->getRented());
root->setLeft(temp->getLeft());
root->setRight(temp->getRight());
delete temp;
return root;
}
else if (!root->getRight())
{
T *temp = root->getLeft();
root->setTitle(temp->getTitle());
root->setAvailable(temp->getAvailable());
root->setRented(temp->getRented());
root->setLeft(temp->getLeft());
root->setRight(temp->getRight());
delete temp;
return root;
}
//2 children
else
{
T *temp = min(root->getRight());
root->copyNode(*temp);
root->setRight(remove(root->getRight(), root));
}
}
return root;
}
template
void BinaryTree
{
//we've reached the end of a leaf
if (!root)
{
return;
}
//process left
printOrder(root->getLeft(), out);
//process node
out << *root << std::endl;
//process right
printOrder(root->getRight(), out);
}
#endif
Node.h
#ifndefNODE_H
#define NODE_H
#include
class Node
{
private:
std::string title;
int available;
int rented;
Node *left = nullptr;
Node *right = nullptr;
public:
Node();
Node(std::string title);
Node(std::string title, int available, int rented);
Node(std::string title, int available, int rented, Node *left, Node *right);
//Accessors
std::string getTitle() { return title; }
int getAvailable() { return available; }
int getRented() { return rented; }
Node* getLeft() { return left; }
Node* getRight() { return right; }
//Mutators
void setTitle(std::string title) { this->title = title; }
void setAvailable(int available) { this->available = available; }
void setRented(int rented) { this->rented = rented; }
void setLeft(Node *left) { this->left = left; }
void setRight(Node *right) { this->right = right; }
void copyNode(Node n);
//Operator
bool operator==(const Node &right);
bool operator>(const Node &right);
bool operator<(const Node &right);
friend std::ostream& operator<<(std::ostream &out, const Node &node);
~Node();
};
#endif
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
