Skip to content

Commit

Permalink
Enhanced currency converter code
Browse files Browse the repository at this point in the history
- Added debug statements for better understanding of program flow.
- Improved comments for increased readability.
- Updated currency conversion to handle MYR as the target currency.
- Resolved errors and improved overall code structure."
  • Loading branch information
talha1230 committed Jan 7, 2024
1 parent 0a9c457 commit 9e92e52
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
48 changes: 23 additions & 25 deletions currencyconvertermainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CurrencyConverterMainWindow::CurrencyConverterMainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::CurrencyConverterMainWindow)
{
// Set up the UI components and connections
ui->setupUi(this);
manager = new QNetworkAccessManager(this);
connect(manager, &QNetworkAccessManager::finished, this, &CurrencyConverterMainWindow::onManagerFinished);
Expand All @@ -21,51 +22,41 @@ CurrencyConverterMainWindow::CurrencyConverterMainWindow(QWidget *parent) :
setStyleSheet("QMainWindow { background-color: #2c3e50; }");

// Set the background color and text color of QTextEdit widgets
ui->uservalue->setStyleSheet("QTextEdit {"
"background-color: #34495e;"
"border: 1px solid #ccc;"
"border-radius: 5px;"
"padding: 5px;"
"color: #f0f0f0;"
"}");
ui->useroutput->setStyleSheet("QTextEdit {"
"background-color: #34495e;"
"border: 1px solid #ccc;"
"border-radius: 5px;"
"padding: 5px;"
"color: #f0f0f0;"
"}");
ui->uservalue->setStyleSheet("QTextEdit {" "background-color: #34495e;" "border: 1px solid #ccc;" "border-radius: 5px;" "padding: 5px;" "color: #f0f0f0;""}");
ui->useroutput->setStyleSheet("QTextEdit {""background-color: #34495e;" "border: 1px solid #ccc;" "border-radius: 5px;" "padding: 5px;" "color: #f0f0f0;""}");

// Set the background color and text color of QPushButton
ui->currencyButton->setStyleSheet("QPushButton { background-color: #3498db; color: white; padding: 5px; border-radius: 5px; }"
"QPushButton:hover { background-color: #2980b9; }"
"QPushButton:pressed { background-color: #1a5276; }");
ui->currencyButton->setStyleSheet("QPushButton { background-color: #3498db; color: white; padding: 5px; border-radius: 5px; }" "QPushButton:hover { background-color: #2980b9; }" "QPushButton:pressed { background-color: #1a5276; }");

// Initialize and populate the currency combo box
QStringList currencyList = {"USD", "EUR", "GBP", "JPY"}; // Add more currencies as needed
QStringList desiredCurrency = {"MYR"};
ui->currencyComboBox->addItems(currencyList);
ui->desiredCurrencyComboBox->addItems(desiredCurrency);


// Connect the combo box selection change signal to the appropriate slot
connect(ui->currencyComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &CurrencyConverterMainWindow::onCurrencySelectionChanged);
}

void CurrencyConverterMainWindow::onManagerFinished(QNetworkReply* reply)
{
// Handle the completion of the network request

if (reply->error()) {
qDebug() << reply->errorString();
// Display an error message if the request fails
qDebug() << "Network Error: " << reply->errorString();
QMessageBox::critical(this, "Error", "There was an issue with the currency conversion API: " + reply->errorString());
return;
}

// Parse the API response
QString answer = reply->readAll();
qDebug() << "API Response: " << answer; // Print the entire API response

QJsonDocument jsonResponse = QJsonDocument::fromJson(answer.toUtf8());

if (!jsonResponse.isObject()) {
// Handle invalid JSON response
qDebug() << "Invalid JSON response";
QMessageBox::critical(this, "Error", "Invalid JSON response from the currency conversion API.");
return;
Expand All @@ -74,44 +65,50 @@ void CurrencyConverterMainWindow::onManagerFinished(QNetworkReply* reply)
QJsonObject jsonObject = jsonResponse.object();

if (!jsonObject.contains("conversion_rates") || !jsonObject["conversion_rates"].isObject()) {
// Handle missing or invalid conversion rates in the JSON response
qDebug() << "Conversion rates not found in JSON response";
QMessageBox::critical(this, "Error", "Conversion rates not found in the JSON response from the currency conversion API.");
return;
}

// Extract conversion rates for the target currency
QJsonObject conversionRatesObject = jsonObject["conversion_rates"].toObject();

if (!conversionRatesObject.contains("MYR")) { // Check if the target currency exists in the conversion rates
if (!conversionRatesObject.contains("MYR")) {
// Check if the target currency exists in the conversion rates
qDebug() << "Target currency not found in conversion rates";
QMessageBox::critical(this, "Error", "Target currency not found in conversion rates.");
return;
}

// Assign the conversion rate for the target currency
conversionRate = conversionRatesObject["MYR"].toDouble();
}

void CurrencyConverterMainWindow::convertCurrency()
{
// Perform currency conversion
bool conversionSuccessful = false;

// Get the user input
double input = ui->uservalue->toPlainText().toDouble();

// Calculate the converted output
double output = input * conversionRate;

// Print debug information
qDebug() << "Input: " << input;
qDebug() << "Conversion Rate: " << conversionRate;
qDebug() << "Output: " << output;

// Ensure conversion rate is valid (not zero)
if (conversionRate != 0.0) {
// Display the converted output
ui->useroutput->setText(QString::number(output));
conversionSuccessful = true;
}

// Display a message only if the conversion was successful
if (conversionSuccessful) {
// Indicate a successful conversion
QMessageBox::information(this, "Success", "The currency conversion has taken place successfully.");
} else {
// Display an error message for an invalid conversion rate
QMessageBox::critical(this, "Error", "Invalid conversion rate. Please try again.");
}
}
Expand All @@ -125,5 +122,6 @@ void CurrencyConverterMainWindow::onCurrencySelectionChanged()

CurrencyConverterMainWindow::~CurrencyConverterMainWindow()
{
// Destructor to clean up resources
delete ui;
}
3 changes: 3 additions & 0 deletions currencyconvertermainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
</property>
</widget>
<widget class="QTextEdit" name="useroutput">
<property name="enabled">
<bool>false</bool>
</property>
<property name="geometry">
<rect>
<x>200</x>
Expand Down

0 comments on commit 9e92e52

Please sign in to comment.