C# Last Lab Extremely Confused Due Tomorrow Help ASAP please

A word from our sponsor:

Printer-friendly version

Author: 

Blog About: 

I am having a great deal of trouble and I am extremely confused as to how to accomplish the lab requirements, questions are not a problem.
I can't get the json part for the currency name to go through and I cannot figure out why.
The list view needs to be changed to the name of the currency instead of the currency code.

https://oxr.readme.io/docs/latest-json
Sincerely,
Quincy Kurtz

I can send you the files via Skype, there are way to many project files to post all of the text, at [email protected]

I think this works.
https://github.com/QKurtz/Lab12.git

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CurrencyConversionProject
{
public partial class CurrencyCalculatorForm : Form
{
private Dollar dollarAmount = null;
private CurrencyRate currencyRate = null;
private const string EXCHANGE_RATE_URL =
"https://openexchangerates.org/api/latest.json?app_id=883cc1e...";
private const string CURRENCY_NAME_URL =
"https://openexchangerates.org/api/currencies.json";
public CurrencyCalculatorForm()
{
InitializeComponent();
}

private void CurrencyCalculatorForm_Load(object sender, EventArgs e)
{
try
{
////use this in second project for dictionary
ExchangeRateService exchangeRate =
new ExchangeRateService(EXCHANGE_RATE_URL);
CountryCodesToRates countryCodesToRate =
exchangeRate.DeserializeJsonData();

//dictionary with currency code and currency name
ExchangeRateService exchangeName =
new ExchangeRateService(CURRENCY_NAME_URL);
CountryCodesToNames countryCodesToName =
exchangeName.DeserializeJsonData();
//countryCodesToNames.CountryName.ToString();
////public Dictionary Rates { get; set;
//CompositeDictionary(countryCodesToRate.Rates, countryCodesToNames.Base);
//PopulateListView(countryCodesToRate.Rates);
dollarListBox.Items.AddRange(Dollar.GetDollarsList());
CompositeDictionary(countryCodesToName.CurrencyType);
//public Dictionary pizzl;
}
catch (Exception theException)
{
MessageBox.Show(theException.Message);
}
}

private void CompositeDictionary(Dictionary outputDictionary)
{
foreach (KeyValuePair pair in outputDictionary)
{
ListViewItem item = new ListViewItem(pair.Key);
item.SubItems.Add(pair.Value);
currencyListView.Items.Add(item);
}

}

private void PopulateListView(Dictionary ratesDictionary)
{
foreach (KeyValuePair pair in ratesDictionary)
{
ListViewItem item = new ListViewItem(pair.Key);
item.SubItems.Add(pair.Value.ToString("N"));
currencyListView.Items.Add(item);
}
}

private void convertButton_Click(object sender, EventArgs e)
{
try
{
CurrencyCalculator calculator = new CurrencyCalculator(dollarAmount, currencyRate);
outputLabel.Text = dollarAmount.DollarName + " US Dollar(s) = " +
calculator.EquivalencyResult.ToString("N") + " " + currencyRate.CurrencyName;
}
catch(DollarException theException)
{
MessageBox.Show(theException.Message);
}
catch(CurrencyRateException theException)
{
MessageBox.Show(theException.Message);
}
}

private void clearButton_Click(object sender, EventArgs e)
{
dollarListBox.SetSelected(0, true);
dollarListBox.SetSelected(0, false);
//dollarListBox.ClearSelected();
currencyListView.SelectedIndices.Clear();
currencyListView.EnsureVisible(0);
outputLabel.ResetText();
dollarAmount = null;
currencyRate = null;
}

private void currencyListView_SelectedIndexChanged(object sender, EventArgs e)
{
if (currencyListView.SelectedItems.Count > 0)
{
int itemIndex = currencyListView.FocusedItem.Index;
ListViewItem countryCode = currencyListView.Items[itemIndex];
ListViewItem.ListViewSubItem rate =
currencyListView.Items[itemIndex].SubItems[1];
currencyRate = new CurrencyRate(countryCode.Text,
Convert.ToDecimal(rate.Text));
}
}

private void dollarListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (dollarListBox.SelectedIndex >= 0)
{
dollarAmount = (Dollar)dollarListBox.SelectedItem;
}
}
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CurrencyConversionProject
{
public class CountryCodesToNames
{
public Dictionary CurrencyType { get; set; }
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CurrencyConversionProject
{
public class CountryCodesToRates
{
public string Disclaimer { get; set; }
public string License { get; set; }
public int TimeStamp { get; set; }
public string Base { get; set; }
public Dictionary Rates { get; set; }
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CurrencyConversionProject
{
public class CurrencyCalculator
{
public CurrencyCalculator(Dollar dollarAmount,
CurrencyRate currencyRate)
{
EquivalencyResult = ValidateDollar(dollarAmount) *
ValidateCurrencyRate(currencyRate);
}
public decimal EquivalencyResult { get; private set; }

private int ValidateDollar(Dollar dollarAmount)
{
if (dollarAmount == null)
{
throw new DollarException();
}
return dollarAmount.DollarValue;
}

private decimal ValidateCurrencyRate(CurrencyRate currencyRate)
{
if (currencyRate == null)
{
throw new CurrencyRateException();
}
return currencyRate.Rate;
}

}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CurrencyConversionProject
{
public class CurrencyRate
{
public CurrencyRate(/*string currencyCode*/string currencyName, decimal rate)
{
//CurrencyCode = currencyCode;
CurrencyName = currencyName;
Rate = rate;
}

//public string CurrencyCode { get; private set; }
public string CurrencyName { get; private set; }
public decimal Rate { get; private set; }
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CurrencyConversionProject
{
public class CurrencyRateException : Exception
{
public CurrencyRateException() : base("Select a country code.")
{

}
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CurrencyConversionProject
{
public class Dollar
{
public Dollar(int value)
{
DollarValue = value;
DollarName = ToString();
}
public string DollarName { get; private set; }
public int DollarValue { get; private set; }

public override string ToString()
{
return DollarValue.ToString("N0").PadLeft(3);
}

public static Object[] GetDollarsList()
{
List dollarsList = new List(new Dollar[]
{
new Dollar(1), new Dollar(5), new Dollar(10),
new Dollar(20), new Dollar(50), new Dollar(100)
});
return dollarsList.ToArray();
}
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CurrencyConversionProject
{
public class DollarException : Exception
{

public DollarException() : base("Select a dollar amount.")
{

}
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Newtonsoft.Json;
using System.Threading.Tasks;

namespace CurrencyConversionProject
{
public class ExchangeRateService
{
public ExchangeRateService(string url)
{
Url = url;
}

public string Url { get; private set; }

public T DeserializeJsonData() where T : new()
{
using (WebClient webClient = new WebClient())
{
try
{
string jsonData = webClient.DownloadString(Url);
return !string.IsNullOrEmpty(jsonData)
? JsonConvert.DeserializeObject(jsonData)
: new T();

}
catch (Exception)
{
throw;
}
}
}
}
}

Comments

my teachers comment

I do not understand my teachers comment.

There is nothing wrong with the other json link. The problem is with the code which incorrectly calls method DeserializeJsonData() with class CountryCodesToNames. The class CountryCodesToNames does not match the JSON payload returned by the service.

To fix the problem you need to review the documentation of the service, and understand the format of the JSON payload returned by the service when using the URL
https://openexchangerates.org/api/currencies.json The documentation shows that the JSON format does not match an object of type CountryCodesToNames (which is exactly what it says the error message that your code displays). Last hint: JSON payload matches a “C# Dictionary”

hugs :)
Michelle SidheElf Amaianna

Still need help ASAP

I still cannot figure out what is wrong with what I am doing, only reason the code isn't there is because there are over 10 cs files. which i just put up top

hugs :)
Michelle SidheElf Amaianna

trying to figure out how to

trying to figure out how to make a dictionary for this, i think that is what my teacher said.
{
"AED": "United Arab Emirates Dirham",
"AFN": "Afghan Afghani",
"ALL": "Albanian Lek",
/* ... */
}

hugs :)
Michelle SidheElf Amaianna

Still need help

I think I figured out that I need to determine how create the dictionary property without class name.

hugs :)
Michelle SidheElf Amaianna

I think the problem is your

I think the problem is your CountryCodesToNames class has a member called CountryName that is a dictionary instead of the class being a dictionary itself. So, when you call the DeserializeJsonData() method with that class as the template it fails. There is no value in the JSON called CountryName to match to it.

The REST endpoint returns JSON that a list of key-value pairs. So, the class that you are trying to put that data into should match that pattern.

EDIT: Looks like you figured this out on your own while I was typing.

I think you can use an instance of the Dictionary class directly, without creating your own class, or you can use inheritance to create your class as a child of the Dictionary class

--Brandon Young