Jesse Pazzi

Jesse Pazzi

  • NA
  • 23
  • 2.9k

Any Suggestions or comments on the following code please

Dec 11 2017 12:03 PM
just checking if i am missing something.. any comments from you guys will be appreciated.
thanks.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.IO;  
  7. namespace ConsoleApplication7  
  8. {  
  9. class Auto  
  10. {  
  11. //calling atributes for car options  
  12. private string Maker; private string Model; private int Year; private double Price; private int inStock; private double inventoryCost; public static double totalInventoryCost = 0;  
  13. ///////////////// end of options of car options //////////////////////////  
  14. //listing constructor for above items.  
  15. public Auto(string maker, string model, int year, double price, int inStock)  
  16. {  
  17. this.Maker = maker; this.Model = model; this.Year = year; this.Price = price; this.inStock = inStock;  
  18. }  
  19. public void setMaker(string maker)  
  20. {  
  21. this.Maker = maker;  
  22. }  
  23. public void setModel(string model)  
  24. {  
  25. this.Model = model;  
  26. }  
  27. public void setYear(int year)  
  28. {  
  29. this.Year = year;  
  30. }  
  31. public void setPrice(double price)  
  32. {  
  33. this.Price = price;  
  34. }  
  35. public void setInStock(int stock)  
  36. {  
  37. this.inStock = stock;  
  38. }  
  39. //////////////////////////////en of constructors //////////////////////////////////  
  40. //Assigning returns  
  41. public string getMaker()  
  42. {  
  43. return Maker;  
  44. }  
  45. public string getModel()  
  46. {  
  47. return Model;  
  48. }  
  49. public int getYear()  
  50. {  
  51. return Year;  
  52. }  
  53. public double getPrice()  
  54. {  
  55. return Price;  
  56. }  
  57. public int getInStock()  
  58. {  
  59. return inStock;  
  60. }  
  61. public double getInventoryCost()  
  62. {  
  63. return inventoryCost;  
  64. }  
  65. ///////////////////////en of returns /////////////////////////  
  66. //calculating price and stock of autos  
  67. public void calcInventoryCost()  
  68. {  
  69. inventoryCost = Price * inStock;  
  70. totalInventoryCost += inventoryCost;  
  71. }  
  72. }  
  73. }  
  74. using System;  
  75. using System.Collections.Generic;  
  76. using System.Linq;  
  77. using System.Text;  
  78. using System.Threading.Tasks;  
  79. using System.IO;  
  80. namespace ConsoleApplication7  
  81. {  
  82. class AutoDemo  
  83. {  
  84. static void Main(string[] args)  
  85. {  
  86. int num = getCars(); Auto[] el = new Auto[num]; loadData(el);int selection = Menu(); while (true)  
  87. {switch (selection)  
  88. {  
  89. case 1: displayData(el); breakcase 2: el = addAuto(el); breakcase 3: el = sellAuto(el); breakcase 4: saveExit(el); Environment.Exit(0); break;  
  90. } selection = Menu();  
  91. }  
  92. }  
  93. public static int getCars()  
  94. {  
  95. int number = 0;  
  96. try  
  97. ////path to txt file for Number of cars  
  98. using (StreamReader sr = new StreamReader(@"C:\Users\jesse.cantu\Documents\Visual Studio 2012\Projects\Auto\ConsoleApplication7\bin\Debug\NumofAuto.txt"))  
  99. {int.TryParse(sr.ReadLine().ToString(), out number);}  
  100. }  
  101. catch (Exception e)  
  102. {  
  103. Console.WriteLine(e.Message);  
  104. }  
  105. return number;  
  106. }  
  107. public static void loadData(Auto[] at)  
  108. {  
  109. int e, length = at.Length;  
  110. try  
  111. //////path to txt file to get car data  
  112. using (StreamReader sr = new StreamReader(@"C:\Users\jesse.cantu\Documents\Visual Studio 2012\Projects\Auto\ConsoleApplication7\bin\Debug\AutoData.txt"))  
  113. {  
  114. string maker, model;  
  115. int year, inStock;  
  116. double price;  
  117. for (e = 0; e < length; e++)  
  118. {  
  119. maker = sr.ReadLine().ToString();  
  120. model = sr.ReadLine().ToString();  
  121. int.TryParse(sr.ReadLine().ToString(), out year);  
  122. double.TryParse(sr.ReadLine().ToString(), out price);  
  123. int.TryParse(sr.ReadLine().ToString(), out inStock);  
  124. at[e] = new Auto(maker, model, year, price, inStock);  
  125. at[e].calcInventoryCost();  
  126. }  
  127. }  
  128. }  
  129. catch (Exception ex)  
  130. {  
  131. Console.WriteLine(ex.Message);  
  132. }  
  133. }  
  134. public static int Menu()  
  135. {  
  136. int change;  
  137. Console.Write("\n\n 1 - Display \n\n 2 - Add Auto \n\n 3 - SellAuto \n\n 4 - SaveExit \n\n\n Your option: "); ////Menu to select your options  
  138. change = Convert.ToInt32(Console.ReadLine());  
  139. return change;  
  140. }  
  141. public static void displayData(Auto[] at) ///array to get options from selected items.  
  142. {  
  143. 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());}  
  144. }  
  145. public static Auto[] addAuto(Auto[] at) ///array to store what its being selected from menu.  
  146. {  
  147. int pos = -1;  
  148. string maker, model;  
  149. int year, inStock;  
  150. double price;  
  151. Console.Write("\n Please enter the Maker: ");  
  152. maker = Console.ReadLine();  
  153. Console.Write("\n Please enter the Model: ");  
  154. model = Console.ReadLine();  
  155. Console.Write("\n Please enter the Year: ");  
  156. year = Convert.ToInt32(Console.ReadLine());  
  157. Console.Write("\n Please enter the Price: ");  
  158. price = Convert.ToDouble(Console.ReadLine());  
  159. Console.Write("\n Please enter if is InStock: ");  
  160. inStock = Convert.ToInt32(Console.ReadLine());  
  161. for (int i = 0; i < at.Length; i++)  
  162. {  
  163. if (maker.Equals(at[i].getMaker()))  
  164. {  
  165. pos = i;  
  166. }  
  167. }  
  168. if (pos == -1)  
  169. {  
  170. Array.Resize(ref at, (at.Length + 1));  
  171. Console.WriteLine(at.Length);  
  172. at[at.Length - 1] = new Auto(maker, model, year, price, inStock);  
  173. Console.WriteLine("\n you added a car!");  
  174. }  
  175. else  
  176. {  
  177. at[pos].setInStock(at[pos].getInStock() + inStock);  
  178. Console.WriteLine("\n Car is in Stock!");  
  179. }  
  180. return at;  
  181. }  
  182. public static Auto[] sellAuto(Auto[] at) ///array when you select to sell a car  
  183. {  
  184. int pos = -1;  
  185. string maker;  
  186. Console.Write("\n Please Enter the Maker: ");  
  187. maker = Console.ReadLine();  
  188. for (int i = 0; i < at.Length; i++)  
  189. {  
  190. if (maker.Equals(at[i].getMaker()))  
  191. {  
  192. pos = i;  
  193. }  
  194. }  
  195. if (pos == -1)  
  196. {  
  197. Console.WriteLine("ERROR~!");  
  198. }  
  199. return at;  
  200. }  
  201. public static void saveExit(Auto[] at) /////array to save data selected  
  202. {  
  203. using (StreamWriter file = new StreamWriter(@"C:\Users\jesse.cantu\Documents\Visual Studio 2012\Projects\Auto\ConsoleApplication7\bin\Debug\AutoData.txt")) ////using the txt file from data stored  
  204. {  
  205. foreach (Auto auto in at)  
  206. {  
  207. file.WriteLine(auto.getMaker());  
  208. file.WriteLine(auto.getModel());  
  209. file.WriteLine(auto.getYear());  
  210. file.WriteLine(auto.getPrice());  
  211. file.WriteLine(auto.getInStock() + 1);  
  212. }  
  213. }  
  214. Console.WriteLine("All data is saved!");  
  215. Console.ReadLine();  
  216. }  
  217. }  
  218. }  

Answers (6)