List and classes

Updated on February 22, 2018 in [C] Beginner
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
22 on February 7, 2018

Hi, i’ve been working on a little project. I’ want the user to add a city, name and temperature and I want to use a class for it “stad”. But I can’t get it to work, I guess I have to have a list in the class somehow. I’ve been back and forth many times but I have no idea.. Any help would be appreciated, the code is below.

  • Liked by
Reply
16 on February 7, 2018

Is a city a class and are you trying to make a list of said class in another object?

on February 7, 2018

stad is the class. And I see now that my code isn’t in my previous post..

And in the class I have 

public string City { get; set; }
public string Name { get; set; }
public int Temperature { get; set; }

So I want the user to enter a city, name and temperature and add that to a list. Then i want to be able to loop it as well (so the user can see everything he wrote)

 

https://dotnetfiddle.net/36LYao


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TemperaturIoLikaStöderÖvning13._6
{
 class Program
 {
 static void Main(string[] args)
 {
 myIntList temperatureList = new myIntList();
 myStringList cityList = new myStringList();
 myNameList nameList = new myNameList();
 int menyVal;
bool running = true;
 while (running)
 {
 Console.WriteLine("1. Add a new city");
 Console.WriteLine("2. Control all the temperatures in the cities");
 Console.WriteLine("3. Quit");
 string str = Console.ReadLine();
 int.TryParse(str, out menyVal);
switch (menyVal)
 {
 case 1:
Console.WriteLine("Add a city");
 string city = Console.ReadLine();
 Console.WriteLine("Add a name");
 string Name = Console.ReadLine();
 Console.WriteLine("Add a temperature");
 int Temperature = int.Parse(Console.ReadLine());
temperatureList.AddTemperature(Temperature);
 cityList.AddCity(city);
 nameList.AddName(Name);
Console.WriteLine(temperatureList);
 break;
case 2:
 Console.WriteLine("Testar loop");
break;
 case 3:
running = false;
 break;
 }
 }
 Console.ReadLine();
 }
 }
 public class myIntList
 {
 private List<int> temperature = new List<int>();
public void AddTemperature(int temp)
 {
 temperature.Add(temp);
 }
 public int GetTemp(int i)
 {
 return temperature[i];
 }
}
 public class myStringList
 {
 private List<string> cityList = new List<string>();
public void AddCity(string city)
 {
 cityList.Add(city);
 }
 public string GetCity(int i)
 {
 return cityList[i];
 }
 }
 public class myNameList
 {
 private List<string> nameList = new List<string>();
public void AddName(string name)
 {
 nameList.Add(name);
 }
 public string GetCity(int i)
 {
 return nameList[i];
 }
 }
}
Wise
on February 7, 2018

Is there a difference between a City and a Name?

on February 7, 2018

No, it should be the same, I guess I have them both, i’ll get rid of Name, but the question still remains ..

on February 7, 2018

What you can do is create a class called City and give it the properties. CityName, Tempreture. I am not sure what your name list is for at this point but if it is a city property just add it to the class. You can then add the behaviors for the city to change the temperature and if you want even rename it. If you are familiar with how to read a UML I can write one up and post it for you.

on February 7, 2018

I’m new at this but I’m happy for all the help I can get so if you want to write it up i’d be greatful. I will redo my project and start over as well

on February 7, 2018

on February 7, 2018

Now that you have the class you can create instances of it and assign it the values the user inputs.

Then add them to a list of City objects. Now that should get in a good spot. If you want to be able to search for cities fast you could use a dictionary where the key is name of the city and the value is the actual city object. 

Dictionaries are very fast to search through since you can search by key.

on February 7, 2018

Guess I weren’t able to read it as well as I thought^^ But it’s a footstep! I haven’t done so much class stuff …

 

class City
{
public string CityName { get; set; }
public float Temperature { get; set; }

public City (string CityName, float Temperature)
{
this.CityName = CityName;
this.Temperature = Temperature;
}

public void ChangeTemp(float Temperature)
{
public float temperature { get; set; }
}
}

So the void changeTemp is all wrong somehow ?

Wise
on February 7, 2018

I think you’re misusing getters and setters. Try just making the variables in City normal, non-getter/setter variables.

 

on February 7, 2018

You can cut out the getter and setters all together and just use regular variables until you have a full understanding of the getter and setter works

 

Show more replies
  • Liked by
Reply
Cancel
1 on February 8, 2018

 

I’ll repeat the information about the code background one more.
The user is supposed to be able to add a city and temperature.

Through the menu, the user can control the temperature, the average temperature, where it’s coldest and hottest.

So, i’ve been able to add cities, temperatures and the average temperature.

I’m not sure i’ve done the average temperature the right way according to the “task”.

I guess I need some calculation inside the class ?

So what i’ve done so far, is following. 

Code: https://dotnetfiddle.net/uB6igU

class:https://dotnetfiddle.net/4js6Jy

on February 8, 2018

I’m not sure what that Math.Round function does but the formula to calculate the average of a set is the sum of all the values divided by the numbers of items in the set. If you need to keep track of a min and max temp you should probably add those variables to the class and then you can make a function that can calculate the average temp for you that way.

Any functionality that you need specific to a city like calculate the average temp of it should be done on the object it self. This way it will be easier to identify errors if they come up.

 

Show more replies
  • Liked by
Reply
Cancel
1 on February 8, 2018

The math round make it to two decimals instead of ex, 43,5455354354353.

Yeah, I got an average calculation in main menu and it works, but I’m not sure how I can get it in the class with all the links to the list etc…

on February 8, 2018

sum =  sum + cityList[i].temperature / cityList.Count;

remove the /cityList.Count from this line. Then once your out the loop and have the sum of all the temps you can do sum = sum / cityList.count

 

Show more replies
  • Liked by
Reply
Cancel