Reputation: 249
I'm stuck on this program and any help would be appreciated.
There are 3 methods: inputPhone, outputPhones, and Main. The inputPhone runs in a loop and accepts user input. This information should be stored in an array. Until the loop is broken, the user will continue putting in information. If broken, every phone inputted will be displayed.
I'm using ref parameters. I need to store those values in the Main method, then pass the array through the method.
I'm just looking for a push in the right direction; an example would be great.
EDIT: Problem has been solved. Thank you so much for your input guys. I'll add in your suggestions soon to clean up my code to have it run a little nicer.
Upvotes: 0
Views: 596
Reputation: 6366
First of all I would create a separate class for storing your phones data:
public class Phone
{
public string Manufacturer { get; set; }
public string Model { get; set; }
public bool HasCord { get; set; }
public double Price { get; set: }
}
Then instead of:
static void inputPhone(ref string manufacturer, ref string model, ref bool hasCord, ref double price)
You could have:
static Phone GetPhone()
And you could create an instance of a Phone inside GetPhone method and return the object that is filled in with appropriate data.
Instead of:
double[] prices = new double[100];
string[] manufacturers = new string[100];
string[] models = new string[100];
bool[] hasCords = new bool[100];
You could then have:
List<Phone> phones = new List<Phone>();
And then after each call to GetInput (previously: inputPhone) add it to the list:
phones.Add(GetPhone());
Then change the outputPhones to:
static void DisplayPhones(List<Phone> phones)
So all in all your code would look like this:
static Phone GetPhone()
{
Phone phone = new Phone();
Console.Write("Enter the phone manufacturer: ");
phone.Manufacturer = Console.ReadLine();
Console.Write("Enter the phone model: ");
phone.Model = Console.ReadLine();
Console.Write("Is it cordless? [Y or N]: ");
phone.HasCord = Console.ReadLine().ToUpper() == "Y";
Console.Write("Enter the phone price: ");
phone.Price = Convert.ToDouble(Console.ReadLine());
return phone;
}
static void DisplayPhones(List<Phone> phones)
{
for (int i = 0; i < phones.Count; i++)
{
Console.WriteLine("==Phone #{0}==", i);
Console.WriteLine("Phone Manufacturer: {0}", phones[i].Manufacturer);
Console.WriteLine("Phone Model: {0}", phones[i].Model);
Console.WriteLine("Has Cord: {0}", phones[i].HasCord ? "Yes" : "No");
Console.WriteLine("Phone Price: {0}", phones[i].Price);
}
Console.WriteLine("Number of phones entered: {0}", phones.Count);
}
static void Main(string[] args)
{
List<Phone> phones = new List<Phone>();
bool shouldContinue = true;
do
{
phones.Add(GetPhone());
Console.Write("Would like to process another phone? [Y or N]: ", shouldContinue);
shouldContinue = Console.ReadLine().ToUpper() == "Y";
} while (shouldContinue == true);
if (shouldContinue == false)
{
DisplayPhones(phones);
}
}
Upvotes: 2
Reputation: 3024
Why not make phone a class:
public class Phone
{
public double Price {get;set;}
public string Manufacturer {get;get;}
public string Model {get;set;}
public bool HasCord{get;set;}
public override string ToString()
{
return string.Format("Phone Manufacturer: {0}\nPhone Model {1}\nHas Cord: {2}\nPhone Price{3}", this.Manufacturer, this.Model, this.HasCord ? "Yes" : "No", this.Price);
}
}
Then, you can change your program to be more object oriented and look like:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace newProg
{
class YourApp
{
private Phone inputPhone()
{
Console.Write("Enter the phone manufacturer: ");
string manufacturer = Console.ReadLine();
Console.Write("Enter the phone model: ");
string model = Console.ReadLine();
Console.Write("Is it cordless? [Y or N]: ");
bool hasCord = Console.ReadLine().ToUpper() == "Y";
Console.Write("Enter the phone price: ");
double price = Convert.ToDouble(Console.ReadLine());
return new Phone {
Manufacturer = manufacturer,
Model = model,
HasCord = hasCord,
Price = price;
};
}
static void outputPhones(List<Phone> phones)
{
foreach (var index = 0; index < phones.Length; index++)
{
Console.WriteLine("==Phone #{0}==", index);
Console.WriteLine(phone.ToString());
}
Console.WriteLine("Number of phones entered: {0}", phones.Length);
}
static void Main(string[] args)
{
List<Phone> phones = new List<Phone>();
bool Continue = true;
do
{
phones.Add(inputPhone());
Console.Write("Would like to process another phone? [Y or N]: ", Continue);
Continue = Console.ReadLine().ToUpper() == "Y";
} while (Continue == true);
if (Continue == false)
{
outputPhones(phones);
}
}
}
Upvotes: 1
Reputation: 216283
This your code rewritten to take advantage of a List<Phone>
instead of arrays. As you can see the whole code is simplified a lot comparing to an array based approach
class Phone
{
// Property to store the fields of a Phone object
public string Manifacturer {get;set;}
public string Model {get;set;}
public string IsCordless {get;set;}
public decimal Price {get;set;}
// No parameters to pass, but returns a Phone instance
static Phone inputPhone()
{
Phone p = new Phone();
Console.Write("Enter the phone manufacturer: ");
p.Manufacturer = Console.ReadLine();
Console.Write("Enter the phone model: ");
p.Model = Console.ReadLine();
Console.Write("Is it cordless? [Y or N]: ");
p.IsCordless = Console.ReadLine().ToUpper() == "Y";
Console.Write("Enter the phone price: ");
p.Price = Convert.ToDecimal(Console.ReadLine());
return p;
}
// Pass the list and loop on every element
static void outputPhones(List<Phone> pList)
{
foreach (Phone o in pList)
{
Console.WriteLine("Phone Manufacturer: {0}", p.Manufacturer);
Console.WriteLine("Phone Model: {0}", p.Model);
Console.WriteLine("Has Cord: {0}", p.IsCordless ? "Yes" : "No");
Console.WriteLine("Phone Price: {0}", p.Price);
}
Console.WriteLine("Number of phones entered: {0}", pList.Count);
}
static void Main(string[] args)
{
// Initialize an empty Phone list
List<Phone> pList = new List<Phone>();
bool continueLoop = true;
do
{
// Get the input from the user and add to the list
Phone p = inputPhone();
pList.Add(p);
Console.Write("Would like to process another phone? [Y or N]: ", continueLoop);
continueLoop = Console.ReadLine().ToUpper() == "Y";
} while (continueLoop == true);
// output the list
outputPhones(pList);
}
}
Upvotes: 1
Reputation: 6279
You'll need to access a particular item of the array using array[numberOfPhones]
, then pass it using ref array[numberOfPhones]
.
do
{
inputPhone(ref manufacturers[numberOfPhones],
ref models[numberOfPhones],
ref hasCords[numberOfPhones],
ref prices[numberOfPhones]);
numberOfPhones++; // Increase the number of phones afterwards
Console.Write("Would like to process another phone? [Y or N]: ", Continue);
Continue = Console.ReadLine().ToUpper() == "Y";
} while (Continue == true);
Upvotes: 1
Reputation: 224
Make all thos arrays properties of that class Phone and you will have just to access them where you want in that class. Beside of that you can use out
insted of ref
and not be aware of initialize everything befor run the code.
Upvotes: 1