just checking if i am missing something.. any comments from you guys will be appreciated.
thanks.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace ConsoleApplication7
- {
- class Auto
- {
-
- private string Maker; private string Model; private int Year; private double Price; private int inStock; private double inventoryCost; public static double totalInventoryCost = 0;
-
-
- 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;
- }
- 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;
- }
-
-
- 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;
- }
- public double getInventoryCost()
- {
- return inventoryCost;
- }
-
-
- public void calcInventoryCost()
- {
- inventoryCost = Price * inStock;
- totalInventoryCost += inventoryCost;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace ConsoleApplication7
- {
- class AutoDemo
- {
- static void Main(string[] args)
- {
- int num = getCars(); Auto[] el = new Auto[num]; loadData(el);int selection = Menu(); while (true)
- {switch (selection)
- {
- case 1: displayData(el); break; case 2: el = addAuto(el); break; case 3: el = sellAuto(el); break; case 4: saveExit(el); Environment.Exit(0); break;
- } selection = Menu();
- }
- }
- public static int getCars()
- {
- int number = 0;
- try
- {
- using (StreamReader sr = new StreamReader(@"C:\Users\jesse.cantu\Documents\Visual Studio 2012\Projects\Auto\ConsoleApplication7\bin\Debug\NumofAuto.txt"))
- {int.TryParse(sr.ReadLine().ToString(), out number);}
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- return number;
- }
- public static void loadData(Auto[] at)
- {
- int e, length = at.Length;
- try
- {
- using (StreamReader sr = new StreamReader(@"C:\Users\jesse.cantu\Documents\Visual Studio 2012\Projects\Auto\ConsoleApplication7\bin\Debug\AutoData.txt"))
- {
- string maker, model;
- int year, inStock;
- double price;
- for (e = 0; e < length; e++)
- {
- maker = sr.ReadLine().ToString();
- model = sr.ReadLine().ToString();
- int.TryParse(sr.ReadLine().ToString(), out year);
- double.TryParse(sr.ReadLine().ToString(), out price);
- int.TryParse(sr.ReadLine().ToString(), out inStock);
- at[e] = new Auto(maker, model, year, price, inStock);
- at[e].calcInventoryCost();
- }
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- public static int Menu()
- {
- int change;
- Console.Write("\n\n 1 - Display \n\n 2 - Add Auto \n\n 3 - SellAuto \n\n 4 - SaveExit \n\n\n Your option: ");
- change = Convert.ToInt32(Console.ReadLine());
- return change;
- }
- public static void displayData(Auto[] at)
- {
- for (int i = 0; i < at.Length; i++){Console.WriteLine(at[i].getMaker() + " " + at[i].getModel() + " " + at[i].getYear() + " " + at[i].getPrice() + " " + at[i].getInStock());}
- }
- public static Auto[] addAuto(Auto[] at)
- {
- int pos = -1;
- string maker, model;
- int year, inStock;
- double price;
- Console.Write("\n Please enter the Maker: ");
- maker = Console.ReadLine();
- Console.Write("\n Please enter the Model: ");
- model = Console.ReadLine();
- Console.Write("\n Please enter the Year: ");
- year = Convert.ToInt32(Console.ReadLine());
- Console.Write("\n Please enter the Price: ");
- price = Convert.ToDouble(Console.ReadLine());
- Console.Write("\n Please enter if is InStock: ");
- inStock = Convert.ToInt32(Console.ReadLine());
- for (int i = 0; i < at.Length; i++)
- {
- if (maker.Equals(at[i].getMaker()))
- {
- pos = i;
- }
- }
- if (pos == -1)
- {
- Array.Resize(ref at, (at.Length + 1));
- Console.WriteLine(at.Length);
- at[at.Length - 1] = new Auto(maker, model, year, price, inStock);
- Console.WriteLine("\n you added a car!");
- }
- else
- {
- at[pos].setInStock(at[pos].getInStock() + inStock);
- Console.WriteLine("\n Car is in Stock!");
- }
- return at;
- }
- public static Auto[] sellAuto(Auto[] at)
- {
- int pos = -1;
- string maker;
- Console.Write("\n Please Enter the Maker: ");
- maker = Console.ReadLine();
- for (int i = 0; i < at.Length; i++)
- {
- if (maker.Equals(at[i].getMaker()))
- {
- pos = i;
- }
- }
- if (pos == -1)
- {
- Console.WriteLine("ERROR~!");
- }
- return at;
- }
- public static void saveExit(Auto[] at)
- {
- using (StreamWriter file = new StreamWriter(@"C:\Users\jesse.cantu\Documents\Visual Studio 2012\Projects\Auto\ConsoleApplication7\bin\Debug\AutoData.txt"))
- {
- foreach (Auto auto in at)
- {
- file.WriteLine(auto.getMaker());
- file.WriteLine(auto.getModel());
- file.WriteLine(auto.getYear());
- file.WriteLine(auto.getPrice());
- file.WriteLine(auto.getInStock() + 1);
- }
- }
- Console.WriteLine("All data is saved!");
- Console.ReadLine();
- }
- }
- }