0

I have a txt file with infos about some mobile phones.

Type;Size;Color;New-OrUsed;Warrantee(month);Price(HUF) iPhone SE;64;Gold;New;0;95000 iPhone 6S;64;Silver;New;3;130000 iPhone 5S;16;Black;New;5;60000 iPhone 5S;32;Gold;New;6;75000 iPhone 5S;32;RoseGold;New;8;66500 iPhone 7;32;Black;Used;10;135000 iPhone X;256;Silver;New;12;400000 iPhone 6S;128;Silver;New;3;173000 iPhone 8;128;Gold;New;12;256000 iPhone 7;64;Red;Used;4;155000 iPhone 8 Plus;64;Silver;New;4;285000 iPhone 6S Plus;64;Black;Used;8;180000 iPhone 7 Plus;32;Red;Used;6;192000 

I would like to list all of them, like below:

Type (Only once, whatever how many of them);How many does the text have of this type; how many color does the text have of this type

iPhone 5S;3;3 

I could list the types with a hashset, but I have no idea how to count the different colors, and the number of the type.

My code:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Collections; namespace nyilvantartas { class Program { struct adatok { public string tipus, szin, allapot; public int meret, garancia, ar; } static void Main(string[] args) { StreamReader f = new StreamReader("termekek.txt", Encoding.Default); HashSet<string> tipusok = new HashSet<string>(); List<int> hanyvan = new List<int>(); string[] hanyvann = new string[20]; string s; string[] ss = new string[4]; adatok[] adatok = new adatok[1000]; int db = 0; s = f.ReadLine(); while (!f.EndOfStream) { s = f.ReadLine(); ss = s.Split(';'); adatok[db].tipus = ss[0]; adatok[db].meret = int.Parse(ss[1]); adatok[db].szin = ss[2]; adatok[db].allapot = ss[3]; adatok[db].garancia = int.Parse(ss[4]); adatok[db].ar = int.Parse(ss[5]); db++; } f.Close(); int ezustar = 0, gari=0; double legolcsobb = 500000,legdragabb=0; for (int i = 0; i < db; i++) { tipusok.Add(adatok[i].tipus); if (adatok[i].szin=="Silver") { ezustar += adatok[i].ar; } if (adatok[i].ar>legdragabb) { legdragabb = adatok[i].ar; } if (adatok[i].ar<legolcsobb) { legolcsobb = adatok[i].ar; } gari += adatok[i].garancia; } legdragabb /= legolcsobb; gari /= db; string[] tipusokk = new string[13]; for (int i = 0; i < db; i++) { tipusok.Add(adatok[i].tipus); } for (int i = 0; i < db; i++) { hanyvann[i] = adatok[i].tipus; } Console.WriteLine("2.Feladat: {0} db iPhone található a listában.",+db); Console.WriteLine("3.Feladat: Az összes ezüst színű készülék {0} Ft-ba kerülne",+ezustar); Console.WriteLine("4.Feladat:"); foreach (var item in tipusok) { Console.WriteLine(item); } Console.WriteLine("5.Feladat: Átlagossan {0} hónap garanciát kapunk",gari); Console.WriteLine("6.Felaadat: {0}-szor kerül többe a legdrágább a legolcsóbbnál.",legdragabb); Console.ReadKey(); } } } 
4
  • 2
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the FAQ and How to Ask. Commented Oct 31, 2017 at 15:37
  • Sorry. I extend it with the whole code. Commented Oct 31, 2017 at 15:38
  • try using LINQ queries Commented Oct 31, 2017 at 15:39
  • Could it work if I put the types and colors in an array, and I count the similars? Commented Oct 31, 2017 at 15:41

2 Answers 2

1
  1. Create a class to hold the phone information:

    class PhoneInfo { public string type; public int size; public string Color; public string newOrUsed; public int warrantyMonth; public decimal price; } 
  2. Read in the text file and create a List of objects. I would suggest reading about String.Split.

    List<PhoneInfo> phones; 
  3. Use LINQ to query the List.

    var phoneCounts = from p in phones group p by p.type into pg select new { type, countOfType = pg.Count(), countOfColors = pg.Select(p => p.color).Distinct().Count() }; 
Sign up to request clarification or add additional context in comments.

Comments

0

I could not open the link you sent, you can use hashset or any other data structure, but i prefer to use anonymous data,even you can build your own class and parse your data.

 var rawData = System.IO.File.ReadAllText("aa.txt"); var data = rawData.Split(new[] {Environment.NewLine}, StringSplitOptions.None).Select(k => { var segments = k.Split(';'); return new { Type = segments[0], Size = segments[1], Color = segments[2], IsNew = segments[3], Warranty = segments[4], Price = segments[5] };}); var report = data.GroupBy(k => k.Type).Select(p => new { Type = p.Key, TypesCount = p.Count(), ColorVarityCount = p.Select(o => o.Color).Distinct().Count() }); 

1 Comment

I would recommend you cache the Split result in a variable rather than re-splitting per field.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.