I've been trying to figure this out, asking advise from people but that was just misleading me and now I'm confusing on how this should be done. Can someone help me out with this code?
This is just for me to learn so I can get a better grasp on this. This program is supposed to read a sequential access file called auto.txt, store the values into a engine object and display it on the listbox.
Form1.cs
Code:
namespace Automobile
{
public partial class Form1 : Form
{
private const String FILENAME = "auto.txt";
public ArrayList autoArray = new ArrayList();
private int objCounter = 0;
enum autoIndex
{
cylinders,
horsepower,
torque
};
private void btnDisplay_Click(object sender, EventArgs e)
{
try
{
openBookTitleFile();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex.ToString());
}
}
private void openBookTitleFile()
{
if (File.Exists(FILENAME))
{
StreamReader autoReader = new StreamReader(FILENAME);
while (autoReader.Peek() != -1) // Loop until end of file
{
String autoInfo = autoReader.ReadLine();
String[] autoAttribute = autoInfo.Split(',');
objCounter++;
Engine eng = new Engine();
eng.Cylinders = autoAttribute;
eng.HorsePower = autoAttribute[Convert.ToInt32(autoIndex.horsepower)];
eng.Torque = autoAttribute[Convert.ToInt32(autoIndex.torque)];
autoArray.Add(eng[objCounter]);
}
// Close the file
autoReader.Close();
foreach (Engine objEngine in autoArray)
{
lstbCarSpecs.Items.Add(objEngine.ToString());
}
}
else
{
Console.WriteLine("File Doesn't Exist!");
Application.Exit();
}
}
}
}
Engine.cs
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace Automobile
{
class Engine
{
private int cylinders;
private int horsePower;
private int torque;
public int Cylinders
{
get { return cylinders; }
set { cylinders = value; }
}
public int HorsePower
{
get { return horsePower; }
set { horsePower = value; }
}
public int Torque
{
get { return torque; }
set { torque = value; }
}
public override string ToString()
{
return String.Format(cylinders + ", " +
horsePower + ", " + torque);
}
}
}
auto.txt
Try not to be too harsh, I'm a newb
Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum