Kindly help me change below the C++ codes into Java codes. #include #include #include #include #include using namespace std; /************************************************************************************************************ Question 1 ************************************************************************************************************/ // Class to store name and crates pair class Pick { public: Pick(string name, int crates) { _name = name; _crates = crates; } string _name; int _crates; }; // Sums the total of crates in a vector int getTotal(vector vector) { int total = 0; for(Pick pick : vector) total += pick._crates; return total; } // Gets the deatails of who's stocking or selling and quantity Pick recordPick(string message) { string name; int crates; cout << message << endl << endl; cout << "Enter your name : "; cin >> name; cout << "How many crates of eggs : "; cin >> crates; Pick pick (name, crates); return pick; } // Prints Totals void printTotals(vector inventory, vector sales) { cout << "-----------------------------------" << endl; cout << "Total Stock : " << getTotal(inventory) << endl; cout << "-----------------------------------" << endl; cout << "Total Sold : " << getTotal(sales) << endl; cout << "-----------------------------------" << endl << endl; } int main() { char option; vector inventory; vector sales; do { printTotals(inventory, sales); cout << endl << "What do you wish to do, Stock (s) or Sell (x) ? "; cin >> option; cout << endl; if(toupper(option) == 'S') { Pick stockPick = recordPick("Stock Eggs"); inventory.push_back(stockPick); } else if(toupper(option) == 'X') { Pick salePick = recordPick("Sell Eggs"); if(salePick._crates > getTotal(inventory) - getTotal(sales)) cout << "You don't have enough stock to fulfil this Sale" << endl << endl; else sales.push_back(salePick); } else cout << "Option Not Recognised. Press \"s\" to Stock or \"x\" to Sell or \"q\" to Quit" << endl; } while(toupper(option) != 'Q'); return 0; }
Kindly help me change below the C++ codes into Java codes.
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <numeric>
using namespace std;
/************************************************************************************************************
Question 1
************************************************************************************************************/
// Class to store name and crates pair
class Pick
{
public:
Pick(string name, int crates)
{
_name = name;
_crates = crates;
}
string _name;
int _crates;
};
// Sums the total of crates in a vector
int getTotal(vector<Pick> vector)
{
int total = 0;
for(Pick pick : vector)
total += pick._crates;
return total;
}
// Gets the deatails of who's stocking or selling and quantity
Pick recordPick(string message)
{
string name;
int crates;
cout << message << endl << endl;
cout << "Enter your name : ";
cin >> name;
cout << "How many crates of eggs : ";
cin >> crates;
Pick pick (name, crates);
return pick;
}
// Prints Totals
void printTotals(vector<Pick> inventory, vector<Pick> sales)
{
cout << "-----------------------------------" << endl;
cout << "Total Stock : " << getTotal(inventory) << endl;
cout << "-----------------------------------" << endl;
cout << "Total Sold : " << getTotal(sales) << endl;
cout << "-----------------------------------" << endl << endl;
}
int main()
{
char option;
vector<Pick> inventory;
vector<Pick> sales;
do
{
printTotals(inventory, sales);
cout << endl << "What do you wish to do, Stock (s) or Sell (x) ? ";
cin >> option;
cout << endl;
if(toupper(option) == 'S')
{
Pick stockPick = recordPick("Stock Eggs");
inventory.push_back(stockPick);
}
else if(toupper(option) == 'X')
{
Pick salePick = recordPick("Sell Eggs");
if(salePick._crates > getTotal(inventory) - getTotal(sales))
cout << "You don't have enough stock to fulfil this Sale" << endl << endl;
else
sales.push_back(salePick);
}
else
cout << "Option Not Recognised. Press \"s\" to Stock or \"x\" to Sell or \"q\" to Quit" << endl;
}
while(toupper(option) != 'Q');
return 0;
}
Step by step
Solved in 3 steps with 2 images