C#
i need help creating the instance of stream reader to read from a file the using statement will also
close the StreamReader
the txt files are \"NumofAutos.txt\" which is 5
and the txt files for \"AutoData.txt\"
which is Corolla
2013
23000
6
FORD
F150
2012
31000
8
BMW
328XI
2009
42000
3
CHEVY
Volt
2013
39000
5
NISSAN
Cube
2012
17500
5
this is the code that i have
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AutoInventory
{
//Class that holds the entire information related to Auto
class Auto
{
//Attributes
private string maker;
private string model;
private int year;
private double price;
private int inStock;
private double inventoryCost;
public static double totalInventoryCost = 0;
//Constructor
public Auto(string maker, string model, int year, double price, int inStock)
{
this.maker = maker;
this.model = model;
this.year = year;
this.price = price;
this.inStock = inStock;
}
//Setter methods
public void setMaker(string maker)
{
this.maker = maker;
}
public void setModel(string model)
{
this.model = model;
}
public void setYear(int year)
{
this.year = year;
}
public void setPrice(double price)
{
this.price = price;
}
public void setInStock(int stock)
{
this.inStock = stock;
}
//Getter Methods
public string getMaker()
{
return maker;
}
public string getModel()
{
return model;
}
public int getYear()
{
return year;
}
public double getPrice()
{
return price;
}
public int getInStock()
{
return inStock;
}
//Readonly Property
public double getInventoryCost()
{
return inventoryCost;
}
//Method that updates inventory
public void calcInventoryCost()
{
inventoryCost = price * inStock;
totalInventoryCost += inventoryCost;
}
}
//Main class
class Program
{
static void Main(string[] args)
{
//Getting number of autos
int count = getCount();
//Creating an array with size obtained
Auto[] autos = new Auto[count];
//Loading data
loadData(autos);
//Displaying and reading menu options
int option = menuOptions();
//Loop till user quits
while (true)
{
//Calling appropriate function
switch (option)
{
case 1: displayData(autos); break;
case 2: addAuto(autos); break;
case 3: sellAuto(autos); break;
case 4: saveExit(autos); Environment.Exit(0); break;
default: Console.WriteLine(\"\ \ Invalid Option.... \ \ \"); break;
}
option = menuOptions();
}
}
//Function that fetches the number of autos from file
public static int getCount()
{
int count = 0;
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(\"c:/NumofAuto.txt\"))
{
string line;
//Reading count value
while ((line = sr.ReadLine()) != null)
{
count = Convert.ToInt32(line);
}
}
}
catch (Exception e)
{
//Handling exceptions
Console.WriteLine(\"The file could not be read:\");
Console.WriteLine(e.Message);
}
return count;
}
//Method that loads data from file to array
public static void loadData(Auto[] autos)
{
int i, length = autos.Length;
try
{
/.
Science 7 - LAND and SEA BREEZE and its Characteristics
C#i need help creating the instance of stream reader to read from .pdf
1. C#
i need help creating the instance of stream reader to read from a file the using statement will also
close the StreamReader
the txt files are "NumofAutos.txt" which is 5
and the txt files for "AutoData.txt"
which is Corolla
2013
23000
6
FORD
F150
2012
31000
8
BMW
328XI
2009
42000
3
CHEVY
Volt
2013
39000
5
NISSAN
Cube
2012
17500
5
this is the code that i have
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
2. namespace AutoInventory
{
//Class that holds the entire information related to Auto
class Auto
{
//Attributes
private string maker;
private string model;
private int year;
private double price;
private int inStock;
private double inventoryCost;
public static double totalInventoryCost = 0;
//Constructor
public Auto(string maker, string model, int year, double price, int inStock)
{
this.maker = maker;
this.model = model;
this.year = year;
this.price = price;
this.inStock = inStock;
}
//Setter methods
public void setMaker(string maker)
{
this.maker = maker;
}
public void setModel(string model)
{
this.model = model;
}
public void setYear(int year)
{
this.year = year;
}
public void setPrice(double price)
3. {
this.price = price;
}
public void setInStock(int stock)
{
this.inStock = stock;
}
//Getter Methods
public string getMaker()
{
return maker;
}
public string getModel()
{
return model;
}
public int getYear()
{
return year;
}
public double getPrice()
{
return price;
}
public int getInStock()
{
return inStock;
}
//Readonly Property
public double getInventoryCost()
{
return inventoryCost;
}
//Method that updates inventory
public void calcInventoryCost()
{
4. inventoryCost = price * inStock;
totalInventoryCost += inventoryCost;
}
}
//Main class
class Program
{
static void Main(string[] args)
{
//Getting number of autos
int count = getCount();
//Creating an array with size obtained
Auto[] autos = new Auto[count];
//Loading data
loadData(autos);
//Displaying and reading menu options
int option = menuOptions();
//Loop till user quits
while (true)
{
//Calling appropriate function
switch (option)
{
case 1: displayData(autos); break;
case 2: addAuto(autos); break;
case 3: sellAuto(autos); break;
case 4: saveExit(autos); Environment.Exit(0); break;
default: Console.WriteLine(" Invalid Option.... "); break;
}
option = menuOptions();
}
}
//Function that fetches the number of autos from file
public static int getCount()
{
5. int count = 0;
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("c:/NumofAuto.txt"))
{
string line;
//Reading count value
while ((line = sr.ReadLine()) != null)
{
count = Convert.ToInt32(line);
}
}
}
catch (Exception e)
{
//Handling exceptions
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
return count;
}
//Method that loads data from file to array
public static void loadData(Auto[] autos)
{
int i, length = autos.Length;
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("c:/AutoData.txt"))
{
string maker, model;
int year, inStock;
double price;
6. //Reading data from file and storing in array
for (i = 0; i < length; i++)
{
maker = sr.ReadLine();
model = sr.ReadLine();
year = Convert.ToInt32(sr.ReadLine());
price = Convert.ToDouble(sr.ReadLine());
inStock = Convert.ToInt32(sr.ReadLine());
Auto temp = new Auto(maker, model, year, price, inStock);
temp.calcInventoryCost();
autos[i] = temp;
}
}
}
catch (Exception e)
{
//Handling exceptions
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
//Method that prints menu options
public static int menuOptions()
{
int option;
//Diplay menu
Console.WriteLine(" **** MENU **** ");
Console.Write(" 1 - Display 2 - Add Auto 3 - SellAuto 4 - SaveExit Your option:
");
option = Convert.ToInt32(Console.ReadLine());
//Read option
return option;
}
//Method that displays data available in array
public static void displayData(Auto[] autos)
{
7. //Looping and printing each auto data
for (int i = 0; i < autos.Length; i++)
{
Console.WriteLine(" ");
Console.WriteLine(autos[i].getMaker() + " " + autos[i].getModel() + " " +
autos[i].getYear() + " " + autos[i].getPrice() + " " + autos[i].getInStock());
}
Console.WriteLine(" ");
}
//Method that adds auto to file
public static void addAuto(Auto[] autos)
{
int pos = -1;
string maker, model;
int year, inStock;
double price;
//Reading data
Console.Write(" Input Maker: ");
maker = Console.ReadLine();
Console.Write(" Input Model: ");
model = Console.ReadLine();
Console.Write(" Input Year: ");
year = Convert.ToInt32(Console.ReadLine());
Console.Write(" Input Price: ");
price = Convert.ToDouble(Console.ReadLine());
Console.Write(" Input InStock: ");
inStock = Convert.ToInt32(Console.ReadLine());
//Getting the position of auto
for (int i = 0; i < autos.Length; i++)
{
if (maker.Equals(autos[i].getMaker()))
{
pos = i;
}
}
//If auto doesn;t exist
8. if (pos == -1)
{
//Resize and add new auto
Array.Resize(ref autos, (autos.Length + 1));
Auto temp = new Auto(maker, model, year, price, inStock);
autos[autos.Length - 1] = temp;
Console.WriteLine(" Auto add Success.... ");
}
else
{
//If already exists just increment stock
autos[pos].setInStock(autos[pos].getInStock() + inStock);
Console.WriteLine(" Auto already exist!! Stock updated.... ");
}
Console.WriteLine(" ");
}
//Method that sells auto
public static void sellAuto(Auto[] autos)
{
int pos = -1;
string maker;
//Reading information from user
Console.Write(" Input Maker: ");
maker = Console.ReadLine();
//Looping and getting data
for (int i = 0; i < autos.Length; i++)
{
if (maker.Equals(autos[i].getMaker()))
{
pos = i;
}
}
//If auto doesn'e exist
if (pos == -1)
{
Console.WriteLine(" Error!!!! No Auto exists with the given name.... ");
9. }
else
{
//If exits but doesn't have enough stock
if ((autos[pos].getInStock()) - 1 < 0)
{
Console.WriteLine(" Error!!!! Stock not available.... ");
}
else
{
//Updating the stock
autos[pos].setInStock(autos[pos].getInStock() - 1);
Console.WriteLine(" Auto Sale Success.... ");
}
}
}
//Method that writes data from array to file
public static void saveExit(Auto[] autos)
{
//Writting data to file
using (StreamWriter file = new StreamWriter("C:/AutoData.txt"))
{
//Fetching data from array and writting to file
foreach (Auto auto in autos)
{
file.WriteLine(auto.getMaker());
file.WriteLine(auto.getModel());
file.WriteLine(auto.getYear());
file.WriteLine(auto.getPrice());
file.WriteLine(auto.getInStock() + 1);
}
}
Console.WriteLine(" Save Success.... ");
}
}
}
10. Solution
using System;
using System.IO;
namespace AutoInventory
{
//Class that holds the entire information related to Auto
class Auto
{
//Attributes
private string maker;
private string model;
private int year;
private double price;
private int inStock;
private double inventoryCost;
public static double totalInventoryCost = 0;
//Constructor
public Auto(string maker, string model, int year, double price, int inStock)
{
this.maker = maker;
this.model = model;
this.year = year;
this.price = price;
this.inStock = inStock;
}
//Setter methods
public void setMaker(string maker)
{
this.maker = maker;
}
public void setModel(string model)
{
this.model = model;
}
11. public void setYear(int year)
{
this.year = year;
}
public void setPrice(double price)
{
this.price = price;
}
public void setInStock(int stock)
{
this.inStock = stock;
}
//Getter Methods
public string getMaker()
{
return maker;
}
public string getModel()
{
return model;
}
public int getYear()
{
return year;
}
public double getPrice()
{
return price;
}
public int getInStock()
{
return inStock;
}
//Readonly Property
public double getInventoryCost()
{
12. return inventoryCost;
}
//Method that updates inventory
public void calcInventoryCost()
{
inventoryCost = price * inStock;
totalInventoryCost += inventoryCost;
}
}
//Main class
class Program
{
static void Main(string[] args)
{
//Getting number of autos
int count = getCount();
//Creating an array with size obtained
Auto[] autos = new Auto[count];
//Loading data
loadData(autos);
//Displaying and reading menu options
int option = menuOptions();
//Loop till user quits
while (true)
{
//Calling appropriate function
switch (option)
{
case 1: displayData(autos); break;
case 2: addAuto(autos); break;
case 3: sellAuto(autos); break;
case 4: saveExit(autos); Environment.Exit(0); break;
default: Console.WriteLine(" Invalid Option.... "); break;
}
option = menuOptions();
}
13. }
//Function that fetches the number of autos from file
public static int getCount()
{
int count=0;
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("f://NumofAutos.txt"))
{
//Reading count value
int.TryParse(sr.ReadLine().ToString(), out count);
}
}
catch (Exception e)
{
//Handling exceptions
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
return count;
}
//Method that loads data from file to array
public static void loadData(Auto[] autos)
{
int i, length = autos.Length;
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("f://AutoData.txt"))
{
string maker, model;
int year, inStock;
double price;
14. //Reading data from file and storing in array
for (i = 0; i < length; i++)
{
maker = sr.ReadLine().ToString();
model = sr.ReadLine().ToString();
int.TryParse(sr.ReadLine().ToString(), out year);
// year = Convert.ToInt32(sr.ReadLine());
double.TryParse(sr.ReadLine().ToString(), out price);
//price = Convert.ToDouble(sr.ReadLine());
int.TryParse(sr.ReadLine().ToString(), out inStock);
//inStock = Convert.ToInt32(sr.ReadLine());
//Auto temp = new Auto(maker, model, year, price, inStock);
//temp.calcInventoryCost();
// Console.WriteLine(maker+", "+ model+", "+ year+", "+ price+", "+ inStock+" ");
autos[i] = new Auto(maker, model, year, price, inStock);
autos[i].calcInventoryCost();
}
}
}
catch (Exception e)
{
//Handling exceptions
Console.WriteLine("The file AutoData.txt could not be read:");
Console.WriteLine(e.Message);
}
}
//Method that prints menu options
public static int menuOptions()
{
int option;
//Diplay menu
Console.WriteLine(" **** MENU **** ");
Console.Write(" 1 - Display 2 - Add Auto 3 - SellAuto 4 - SaveExit Your option: ");
option = Convert.ToInt32(Console.ReadLine());
//Read option
return option;
15. }
//Method that displays data available in array
public static void displayData(Auto[] autos)
{
//Looping and printing each auto data
for (int i = 0; i < autos.Length; i++)
{
Console.WriteLine(" ");
Console.WriteLine(autos[i].getMaker() + " " + autos[i].getModel() + " " + autos[i].getYear()
+ " " + autos[i].getPrice() + " " + autos[i].getInStock());
}
Console.WriteLine(" ");
}
//Method that adds auto to file
public static void addAuto(Auto[] autos)
{
int pos = -1;
string maker, model;
int year, inStock;
double price;
//Reading data
Console.Write(" Input Maker: ");
maker = Console.ReadLine();
Console.Write(" Input Model: ");
model = Console.ReadLine();
Console.Write(" Input Year: ");
year = Convert.ToInt32(Console.ReadLine());
Console.Write(" Input Price: ");
price = Convert.ToDouble(Console.ReadLine());
Console.Write(" Input InStock: ");
inStock = Convert.ToInt32(Console.ReadLine());
//Getting the position of auto
for (int i = 0; i < autos.Length; i++)
{
if (maker.Equals(autos[i].getMaker()))
{
16. pos = i;
}
}
//If auto doesn;t exist
if (pos == -1)
{
//Resize and add new auto
Array.Resize(ref autos, (autos.Length + 1));
Auto temp = new Auto(maker, model, year, price, inStock);
autos[autos.Length - 1] = temp;
Console.WriteLine(" Auto add Success.... ");
}
else
{
//If already exists just increment stock
autos[pos].setInStock(autos[pos].getInStock() + inStock);
Console.WriteLine(" Auto already exist!! Stock updated.... ");
}
Console.WriteLine(" ");
}
//Method that sells auto
public static void sellAuto(Auto[] autos)
{
int pos = -1;
string maker;
//Reading information from user
Console.Write(" Input Maker: ");
maker = Console.ReadLine();
//Looping and getting data
for (int i = 0; i < autos.Length; i++)
{
if (maker.Equals(autos[i].getMaker()))
{
pos = i;
}
}
17. //If auto doesn'e exist
if (pos == -1)
{
Console.WriteLine(" Error!!!! No Auto exists with the given name.... ");
}
else
{
//If exits but doesn't have enough stock
if ((autos[pos].getInStock()) - 1 < 0)
{
Console.WriteLine(" Error!!!! Stock not available.... ");
}
else
{
//Updating the stock
autos[pos].setInStock(autos[pos].getInStock() - 1);
Console.WriteLine(" Auto Sale Success.... ");
}
}
}
//Method that writes data from array to file
public static void saveExit(Auto[] autos)
{
//Writting data to file
using (StreamWriter file = new StreamWriter("f://AutoData.txt"))
{
//Fetching data from array and writting to file
foreach (Auto auto in autos)
{
file.WriteLine(auto.getMaker());
file.WriteLine(auto.getModel());
file.WriteLine(auto.getYear());
file.WriteLine(auto.getPrice());
file.WriteLine(auto.getInStock() + 1);
}
}