TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Jesse Pazzi
NA
23
3k
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.
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.IO;
namespace
ConsoleApplication7
{
class
Auto
{
//calling atributes for car options
private
string
Maker;
private
string
Model;
private
int
Year;
private
double
Price;
private
int
inStock;
private
double
inventoryCost;
public
static
double
totalInventoryCost = 0;
///////////////// end of options of car options //////////////////////////
//listing constructor for above items.
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;
}
//////////////////////////////en of constructors //////////////////////////////////
//Assigning returns
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;
}
///////////////////////en of returns /////////////////////////
//calculating price and stock of autos
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
{
////path to txt file for Number of cars
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
{
//////path to txt file to get car data
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: "
);
////Menu to select your options
change = Convert.ToInt32(Console.ReadLine());
return
change;
}
public
static
void
displayData(Auto[] at)
///array to get options from selected items.
{
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)
///array to store what its being selected from menu.
{
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)
///array when you select to sell a car
{
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)
/////array to save data selected
{
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
{
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();
}
}
}
Reply
Answers (
6
)
Below is the code . i want do it using only one for loop.
Difference Between Arrays and Collections ?