Convert from Java to C++ import java.io.*; import java.lang.*; import java.util.
ID: 3572104 • Letter: C
Question
Convert from Java to C++
import java.io.*;
import java.lang.*;
import java.util.*;
public class HelloWorld
{
public static void main(String[] args)throws Exception
{
int ch;
Scanner s=new Scanner(System.in);
while(true)
{
System.out.println("Menu");
System.out.println("1. Sale Product");
System.out.println("2. Ending day sale report");
System.out.println("3. Ending month sale report");
System.out.println("4. Ending year sale report");
System.out.println("0.Exit");
System.out.println("Enter your choice");
ch=s.nextInt();
if(ch==1)
saleproduct(s);
else if(ch==2)
dayreport();
else if(ch==3)
monthreport();
else if(ch==4)
yearreport();
else
System.exit(0);
}
}
public static void saleproduct(Scanner s)throws Exception
{
String tno;
int nA;
int nB;
double Aprice;
double Bprice;
double amt;
int ch;
BufferedWriter b=new BufferedWriter(new FileWriter(new File("Sale_2016_07_20.txt")));
System.out.println("Sale report");
do
{
System.out.println("Enter transaction number");
tno=s.next();
System.out.println("Enter number of Model A units sold:");
nA=s.nextInt();
System.out.println("Enter the price of model A unit:");
Aprice=s.nextDouble();
System.out.println("Enter number of Model B units sold:");
nB=s.nextInt();
System.out.println("Enter the price of model B unit:");
Bprice=s.nextDouble();
System.out.println("Enter the amount paid of customer");
amt=s.nextDouble();
System.out.println(tno+" "+nA+" "+Aprice+" "+nB+" "+Bprice+" "+amt+" ");
b.write(tno+" "+nA+" "+Aprice+" "+nB+" "+Bprice+" ");
System.out.println("Do u want to enter another trnscation details:");
ch=s.nextInt();
}while(ch==1);
b.close();
}
public static void dayreport()throws Exception
{
String saleline;
String[] temp;
int nA=0;
int nB=0;
double Aprice=0;
double Bprice=0;
int t1,t2;
double t3,t4;
double totalMoney=0;
BufferedReader r=new BufferedReader(new FileReader("Sale_2016_07_20.txt"));
while((saleline=r.readLine())!=null)
{
temp=saleline.split(" ");
t1=Integer.parseInt(temp[1]);
t2=Integer.parseInt(temp[3]);
t3=Double.parseDouble(temp[2]);
t4=Double.parseDouble(temp[4]);
nA=nA+t1;
nB=nB+t2;
Aprice+=t1*t3;
Bprice+=t2*t4;
totalMoney+=Aprice+Bprice;
}
r.close();
System.out.println("Report on the day is");
System.out.println(nA+" "+Aprice+" "+nB+" "+Bprice+" "+totalMoney);
BufferedWriter b=new BufferedWriter(new FileWriter("SaleInMonth_2016_06.txt",true));
int day=20;
b.write(day+" "+nA+" "+Aprice+" "+nB+" "+Bprice+" ");
b.close();
}
public static void monthreport() ()throws Exception
{
}
public static void yearreport() ()throws Exception
{
}
}
Explanation / Answer
//.h file code:
#include <string>
#include <vector>
#include <iostream>
#include <stdexcept>
class HelloWorld
{
static void main(std::vector<std::wstring> &args) throw(std::exception);
public:
static void saleproduct(Scanner *s) throw(std::exception);
static void dayreport() throw(std::exception);
static void monthreport() throw(std::exception);
static void yearreport() throw(std::exception);
};
//.cpp file code:
void HelloWorld::main(std::vector<std::wstring> &args) throw(std::exception)
{
int ch;
Scanner *s = new Scanner(System::in);
while (true)
{
std::wcout << std::wstring(L"Menu") << std::endl;
std::wcout << std::wstring(L"1. Sale Product") << std::endl;
std::wcout << std::wstring(L"2. Ending day sale report") << std::endl;
std::wcout << std::wstring(L"3. Ending month sale report") << std::endl;
std::wcout << std::wstring(L"4. Ending year sale report") << std::endl;
std::wcout << std::wstring(L"0.Exit") << std::endl;
std::wcout << std::wstring(L"Enter your choice") << std::endl;
ch = s->nextInt();
if (ch == 1)
{
saleproduct(s);
}
else if (ch == 2)
{
dayreport();
}
else if (ch == 3)
{
monthreport();
}
else if (ch == 4)
{
yearreport();
}
else
{
exit(0);
}
}
}
void HelloWorld::saleproduct(Scanner *s) throw(std::exception)
{
std::wstring tno;
int nA, nB, ch;
double Aprice, Bprice;
double amt;
FileWriter tempVar(new File(L"Sale_2016_07_20.txt"));
BufferedWriter *b = new BufferedWriter(&tempVar);
std::wcout << std::wstring(L"Sale report") << std::endl;
do
{
std::wcout << std::wstring(L"Enter transaction number") << std::endl;
tno = s->next();
std::wcout << std::wstring(L"Enter number of Model A units sold:") << std::endl;
nA = s->nextInt();
std::wcout << std::wstring(L"Enter the price of model A unit:") << std::endl;
Aprice = s->nextDouble();
std::wcout << std::wstring(L"Enter number of Model B units sold:") << std::endl;
nB = s->nextInt();
std::wcout << std::wstring(L"Enter the price of model B unit:") << std::endl;
Bprice = s->nextDouble();
std::wcout << std::wstring(L"Enter the amount paid of customer") << std::endl;
amt = s->nextDouble();
std::wcout << tno << std::wstring(L" ") << nA << std::wstring(L" ") << Aprice << std::wstring(L" ") << nB << std::wstring(L" ") << Bprice << std::wstring(L" ") << amt << std::wstring(L" ") << std::endl;
b->write(tno + std::wstring(L" ") + nA + std::wstring(L" ") + Aprice + std::wstring(L" ") + nB + std::wstring(L" ") + Bprice + std::wstring(L" "));
std::wcout << std::wstring(L"Do u want to enter another trnscation details:") << std::endl;
ch = s->nextInt();
}while (ch == 1);
delete b;
}
void HelloWorld::dayreport() throw(std::exception)
{
std::wstring saleline;
std::vector<std::wstring> temp;
int nA = 0;
int nB = 0;
double Aprice = 0;
double Bprice = 0;
int t1, t2;
double t3, t4;
double totalMoney = 0;
FileReader tempVar(L"Sale_2016_07_20.txt");
BufferedReader *r = new BufferedReader(&tempVar);
while ((saleline = r->readLine()) != L"")
{
//JAVA TO C++ CONVERTER TODO TASK: There is no direct native C++ equivalent to the Java String 'split' method:
temp = saleline.split(L" ");
t1 = std::stoi(temp[1]);
t2 = std::stoi(temp[3]);
t3 = std::stod(temp[2]);
t4 = std::stod(temp[4]);
nA = nA + t1;
nB = nB + t2;
Aprice += t1*t3;
Bprice += t2*t4;
totalMoney += Aprice + Bprice;
}
delete r;
std::wcout << std::wstring(L"Report on the day is") << std::endl;
std::wcout << nA << std::wstring(L" ") << Aprice << std::wstring(L" ") << nB << std::wstring(L" ") << Bprice << std::wstring(L" ") << totalMoney << std::endl;
FileWriter tempVar2(L"SaleInMonth_2016_06.txt",true);
BufferedWriter *b = new BufferedWriter(&tempVar2);
int day = 20;
b->write(std::to_wstring(day) + std::wstring(L" ") + nA + std::wstring(L" ") + Aprice + std::wstring(L" ") + nB + std::wstring(L" ") + Bprice + std::wstring(L" "));
delete b;
}
void HelloWorld::monthreport() throw(std::exception)
{
}
void HelloWorld::yearreport() throw(std::exception)
{
}