David Chen

David Chen

  • NA
  • 50
  • 45.8k

Bubble Sort Help

Sep 24 2010 9:43 PM
Just a little homework I have to do. Using OOP approach. Retrieve a data pass it to array then sort it(bubble sort) and then display in the right order like the follow. But im stuck on the bubble sorting part. Can anyone help? following code is right below
Thanks so much for reading! PS. I have also uploaded my code so far same as below as a zip file.

* Lunch Items *
$15.46    bento box a - chicken teriyaki, box combo  
$17.26    bento box b – sashimi, box combo  

* Dinner Items *
 $7.11    roe, 2 rolls   
 $8.10    tuna roll, 3 rolls  
 $6.30    vegetable sushi, 6 rolls  

Data/meals.csv
[CODE]lunch,bento box b - sashimi,box combo,$9.59
dinner,vegetable sushi,6 rolls,$3.50
dinner,tuna roll,3 rolls,$4.50
dinner,roe, 2 rolls,$3.95
lunch,bento box a - chicken teriyaki,box combo,$8.59[/CODE]
FileIOManager/FileLocation.cs
[CODE]
using System;
namespace FileIOManager
{
    static class FileLocation
    {
        public const string INPUT_FILE = "../../Data/meals.csv";
    }
}
[/CODE]
FileIOManager/FileReader.cs
[CODE]using System;
using System.IO;

namespace FileIOManager
{
    static public class FileReader
    {
        private static int GetLineCount()
        {
             StreamReader sr = new StreamReader(FileLocation.INPUT_FILE);
             int counter = 0;
             while (!sr.EndOfStream)
             {
                  counter++;
                  sr.ReadLine();
             }
             sr.Close();
             return counter;
         }
     public static string[] ReadLines()
     {
          int totalItems  = GetLineCount();

          string[] itemDetails = new string[totalItems];

          StreamReader sr = new StreamReader(FileLocation.INPUT_FILE);
         
          string itemDetail;
          int counter = 0;
            while (!sr.EndOfStream){
               itemDetail = sr.ReadLine();
                if (itemDetail.Trim() != "")
                    itemDetails[counter++] = itemDetail;
            }

            sr.Close();

            return itemDetails;
        }
    }
}[/CODE]
MealMenu/MenuItem.cs
[CODE]using System;

namespace MealMenu
{
    class MenuItem
    {
        private string entre = "";
        private string entreType = "";
        private string mealTime = "";
        private string cost;
        private double price;

        public string GetEntre{get{return entre;}}
        public string GetEntreType{get{return entreType;}}
        public string GetMealTime{get{return mealTime;}}
        public double GetPrice{get{return price;}}

        public MenuItem(string csvData)
        {
            string[] items = csvData.Split(',');

            mealTime = items[0];
            entre = items[1];
            entreType = items[2];
            cost = items[3];
            price = Convert.ToDouble(cost.Replace('$', ' ').Trim())*1.8;
        }
     
    }
}[/CODE]
program.cs
[CODE]using System;
using FileIOManager;
using MealMenu;

namespace Assignment3_Starter
{
    class Program
    {
        string[] mealList;
        
        static void Main(string[] args)
        {
            string[] mealContent = FileReader.ReadLines();
            MenuItem[] mealList = new MenuItem[mealContent.Length];
           // mealList[0] = new MenuItem
            for (int i = 0; i < mealContent.Length; i++)
            {
                mealList[i] = new MenuItem(mealContent[i]);
            }
            Console.ReadLine();
        }
    }
}[/CODE]

Attachment: help.zip

Answers (5)