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
Inked Will
NA
1
932
Breaking my code down into methods
Jan 27 2016 1:05 AM
So, I have this program that I can make work fine. Except the problem is that it requests user input and I want it to be repeatable. I want it to be possible to terminate the program or keep it going for calculations and utilizing methods in the process (as a way of practice).
Essentially what the program does is it takes user input, an area code and a time of call. It then uses a parallel array and matches the area code to a rate. It then calculates the cost of the call by multiplying the rate times the call time and displays it to the user.
The way I want it to work is that it first calls a getInfo() method. This method prompts the user for information and records it into some variables. It then sends these variables to the codeCheck() method. This method checks the given area code for one in the array. If it exists, it does some calculations and sends it back to the main program where it then displays the results of the work. It then asks the user if they want to input more details. If yes, it calls getInfo() and we repeat the process. If not, it thanks them for using the program and terminates. If they enter a wrong code, it immediately asks them if they want to try again.
The current code is listed below, but it lacks things like getInfo() and so on. This version compiles, but even if you enter a legitimate case (correct area code and a real time), it claims it is invalid.
class ChatAWhile
{
static void Main(string[] args)
{
int[] areaCodes = { 262, 414, 608, 715, 815, 920 };
double[] areaRates = { 0.07, 0.10, 0.05, 0.16, 0.24, 0.14 };
int callTime;
double callRate = 0;
int selectedCode;
double calculatedCost = 0;
bool isRealCode = false;
string retrySelect;
char retryOption;
Write("Please enter an area code: -> ");
selectedCode = Convert.ToInt32(ReadLine());
Write("How long was the call?: -> ");
callTime = Convert.ToInt32(ReadLine());
codeCheck(areaCodes, areaRates, selectedCode,
isRealCode, callRate, calculatedCost, callTime);
if (isRealCode)
{
WriteLine("You talked for {0} minutes at a rate of {1} per minute using the {2} area code.",
callTime, callRate, selectedCode);
WriteLine("This call costed you ${0}!", calculatedCost);
WriteLine("Would you like to enter another code? (Yes or No): -> ");
retrySelect = ReadLine();
retryOption = retrySelect[0];
retryOption = Char.ToLower(retryOption);
if (retryOption == 'y')
{
Write("Please enter an area code: -> ");
selectedCode = Convert.ToInt32(ReadLine());
Write("How long was the call?: -> ");
callTime = Convert.ToInt32(ReadLine());
codeCheck(areaCodes, areaRates, selectedCode,
isRealCode, callRate, calculatedCost, callTime);
}
else
{
WriteLine("Thank you for using the ChatAWhile program.");
}
}
// Else we'll let them know the area code they entered is bad.
else
{
WriteLine("The area code you selected is invalid, please enter a correct area code.");
WriteLine("Would you like to enter another code? (Yes or No): -> ");
retrySelect = ReadLine();
retryOption = retrySelect[0];
retryOption = Char.ToLower(retryOption);
if (retryOption == 'y')
{
Write("Please enter an area code: -> ");
selectedCode = Convert.ToInt32(ReadLine());
Write("How long was the call?: -> ");
callTime = Convert.ToInt32(ReadLine());
codeCheck(areaCodes, areaRates, selectedCode,
isRealCode, callRate, calculatedCost, callTime);
}
else
{
WriteLine("Thank you for using the ChatAWhile program.");
}
}
}
public static void codeCheck(int[] areaCodes, double[] areaRates, int selectedCode,
bool isRealCode, double callRate, double calculatedCost, int callTime)
{
for (int x = 0; x < areaCodes.Length; ++x)
{
if (selectedCode == areaCodes[x])
{
isRealCode = true;
callRate = areaRates[x];
calculatedCost = callRate * callTime;
x = areaCodes.Length;
return;
}
}
}
}
Pastebin for syntax highlighting.
http://pastebin.com/Vvb7CWeS
Appreciate any help that can be given. I'm still trying to understand methods more.
Reply
Answers (
2
)
interface
how to send an email to outlook to approve, reject, onhold