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]
Loading
Bryian TanPosted Sep 27, 2010, 12:17 PM
I though you have to write your own bubble sort algorithm? Do some reading on bubble sort. Anyway, I'll show you how to separate the lunch and dinner items into the array.
int tempLunchCount = 0;
int tempDinnerCount = 0;
// mealList[0] = new MenuItem
for (int i = 0; i < mealContent.Length; i++)
{
mealList[i] = new MenuItem(mealContent[i]); //this is what you have originally
//if mealtime is lunch, insert into mealListLuncg array
if (mealList[i].GetMealTime.ToLower() == "lunch")
{
mealListLunch[tempLunchCount] = mealList[i];
tempLunchCount++;
}
//if mealtime is dinner, insert into mealListDinner array
if (mealList[i].GetMealTime.ToLower() == "dinner")
{
mealListDinner[tempDinnerCount] = mealList[i];
tempDinnerCount++;
}
}
//now write the bubble sort algorithm to sort the array of MeneItem objects. here is some hints.
1) This sorting string code snippet will give you a jump start.
2) do some reading on bubble sort. Is very easy, you can get this done in less than 10 lines of code.
3) Let said you have 2 objects in the mealListLunch with the entree "bento box B" and "bento box A". Use the string.CompareTo() method to compare the "entree" string. if the first value is greater than the second value, then swap their position in the array.
BubbleSort(mealListLunch);
BubbleSort(mealListDinner);
David ChenPosted Sep 26, 2010, 10:05 AM
sorting using strings
int lunchCount = 0;
int dinnerCount = 0;
int lunchIndex = 0;
int dinnerIndex = 0;
foreach (string m in mealContent)
{
if (m.ToLower().Contains("lunch,"))
{
lunchCount++;
}
if (m.ToLower().Contains("dinner,"))
dinnerCount++;
}
var mealListLunch = new string[lunchCount];
var mealListDinner = new string[dinnerCount];
foreach (string m2 in mealContent)
{
if (m2.ToLower().Contains("lunch,"))
{
mealListLunch.SetValue(m2, lunchIndex);
lunchIndex++;
}
if (m2.ToLower().Contains("dinner,"))
{
mealListDinner.SetValue(m2, dinnerIndex);
dinnerIndex++;
}
}
Array.Sort(mealListLunch);
Array.Sort(mealListDinner);
Bryian TanPosted Sep 25, 2010, 6:20 PM
Remember that you have this code in the Main
for (int i = 0; i < mealContent.Length; i++)
{
mealList[i] = new MenuItem(mealContent[i]);
}
Let expand that.
First, define two new counter and initialize it to 0
int tempLunchCount = 0;
int tempDinnerCount = 0;
Then complete the pseudocode.
for (int i = 0; i < mealContent.Length; i++)
{
mealList[i] = new MenuItem(mealContent[i]);
if (mealList[i].GetMealTime.ToLower() == "lunch")
{
//populate the mealListLunch
}
if (mealList[i].GetMealTime.ToLower() == "dinner")
{
//populate the mealListDinner
}
}
Then
BubbleSort(mealListLunch )
BubbleSort(mealListDinner)
QnA
"
also the link you gave me about sorting string, it says
public static void sortEm(String [] array, int len)"
From the output you posted here, I think the sort is based on the entree and entree is a string.
"what is the int len? the string[] array should be the lunch and the dinner arrays right?"
len is the length of the mealListLunch/mealListDinner array object
I think I gave you enough information to complete your project. Good luck.
Thanks,
Bryian Tan
David ChenPosted Sep 25, 2010, 12:41 AM
but can you give more hints on putting the items in m array that contains "lunch," into the lunch array
also the link you gave me about sorting string, it says
what is the int len? the string[] array should be the lunch and the dinner arrays right?
thanks again
Bryian TanPosted Sep 24, 2010, 11:55 PM
This sorting string code snippet will give you a jump start.
I would suggest to create two additional MenuItem object to hold the lunch and dinner data. Here is the code snippet to get you started.
int lunchCount = 0;
int dinnerCount = 0;
foreach (string m in mealContent)
{
if (m.ToLower().Contains("lunch,"))
lunchCount++;
if (m.ToLower().Contains("dinner,"))
dinnerCount++;
}
MenuItem[] mealListLunch = new MenuItem[lunchCount];
MenuItem[] mealListDinner = new MenuItem[dinnerCount];
Now, you have to figure out how to populate the mealListLunch and mealListDinner, sort them and display the results.