Need some help with my c# program ...
-
20. srpna 2012 11:43
Hello guys. I need some of your time to help me...
What I've been told to do :
[quote]1. Create a menu that contains the following options: Get a persons details (p) Get the Salary (s) Calculate and display (d) Exit (x)
2. Display the menu and wait for the users response.
3. Process the options entered until the user enters 'x' to quit.
Using a switch statement organise the code to suit the options.[/quote]I need some help on looping and repetition because I'm expected to put appropriate effort into allowing the user to re-enter data that has been detected as invalid
I only can use for Loop, while Loop do while Loop and nested loops.
What I've done so far...
string full_name = ""; string street_number = ""; string address = ""; string state = ""; string post_code = ""; string phone_number = ""; float hours_worked = 0; float gross_pay = 0; float tax = 0; float net_pay = 0; float annual_salary = 0; float tax_rate = 0; float pay_rate = 0; string vtemp = ""; char response = 'x'; char cont = 'y'; while (cont == 'y') { Console.WriteLine("Enter personal details (p)"); Console.WriteLine(); Console.WriteLine("Enter salary details (s)"); Console.WriteLine(); Console.WriteLine("Calculate and display (d)"); Console.WriteLine(); Console.WriteLine("Exit (x)"); Console.WriteLine(""); Console.Write("Enter p,s,d or x to proceed > "); vtemp = Console.ReadLine(); char.TryParse(vtemp, out response); if (response == 'p') { Console.Clear(); Console.Write("Get a persons details :"); vtemp = Console.ReadLine(); { Console.Write("Enter Full Name > "); vtemp = Console.ReadLine(); full_name = vtemp; } if (string.IsNullOrEmpty(full_name)) { Console.WriteLine("Please enter correct data"); return; } Console.Write("Enter Street Number >"); vtemp = Console.ReadLine(); street_number = vtemp; if (string.IsNullOrEmpty(street_number)) { Console.WriteLine("Please enter correct data"); return; } Console.Write("Enter adress >"); vtemp = Console.ReadLine(); address = vtemp; if (string.IsNullOrEmpty(address)) { Console.WriteLine("Please enter correct data"); return; } Console.Write("Enter State >"); vtemp = Console.ReadLine(); state = vtemp; if (string.IsNullOrEmpty(state)) { Console.WriteLine("Please enter correct data"); return; } else if (state.Length != 3) { Console.WriteLine("Please use only the first 3 letters of your state"); } Console.Write("Enter Post Code >"); vtemp = Console.ReadLine(); post_code = vtemp; if (post_code.StartsWith("1")) { Console.WriteLine("Please enter correct data"); return; } else if (post_code.StartsWith("8")) { Console.WriteLine("Please enter correct data"); return; } else if (post_code.StartsWith("9")) { Console.WriteLine("Please enter correct data"); return; } if (post_code == null || post_code.Length != 4) { Console.WriteLine("Please enter correct data"); return; } Console.Write("Enter Phone Number >"); vtemp = Console.ReadLine(); phone_number = vtemp; if (string.IsNullOrEmpty(phone_number)) { Console.WriteLine("Please enter correct data"); return; } else if (phone_number.Length != 8) { Console.WriteLine("Please enter correct data"); return; } do { Console.Write("Enter how much hours you worked >"); vtemp = Console.ReadLine(); float.TryParse(vtemp, out hours_worked); if (hours_worked <= -0.000001) Console.WriteLine("Please enter valid data"); } while (hours_worked <= -0.000001); Console.Write("Please press y to go back to main menu> "); vtemp = Console.ReadLine(); char.TryParse(vtemp, out cont); Console.Clear(); } else if (response == 's') { Console.Clear(); Console.Write("Enter Salary >"); vtemp = Console.ReadLine(); float.TryParse(vtemp, out annual_salary); if (annual_salary <= 0.000001) Console.WriteLine("Please enter Valid Salary"); } while (annual_salary <= 0.000001) ; } //Determine pay rate and tax rates based on entered data if (annual_salary >= 0 && annual_salary <= 16500) { tax_rate = 11.32F; pay_rate = 8.68F; } else if (annual_salary <= 19500) { tax_rate = 15.14F; pay_rate = 10.26F; } else if (annual_salary <= 29500) { tax_rate = 22.65F; pay_rate = 15.54F; } else if (annual_salary <= 33500) { tax_rate = 27.1F; pay_rate = 17.63F; } else if (annual_salary <= 39500) { tax_rate = 30.92F; pay_rate = 20.79F; } else if (annual_salary <= 58500) { tax_rate = 35.72F; pay_rate = 31.31F; } else if (annual_salary <= 89500) { tax_rate = 40.72F; pay_rate = 47.12F; } else if (annual_salary > 89500) { tax_rate = 50.52F; pay_rate = 55.67F; } Console.Write("Please press y to go back to main menu> "); vtemp = Console.ReadLine(); char.TryParse(vtemp, out cont); Console.Clear(); // Calculate gross pay gross_pay = hours_worked * pay_rate; // Calculate total tax tax = gross_pay * (tax_rate / 100); // Calculate net pay net_pay = gross_pay - tax; //Output data if (response == 'd') { Console.Clear(); Console.Write("(d)Calculate and display"); vtemp = Console.ReadLine(); } Console.WriteLine("Persons Details : {0}, {1} {2}, {3},{4},{5}", full_name, street_number, address, state, post_code, phone_number); Console.WriteLine("Hours Worked : {0} hours", hours_worked); Console.WriteLine("Annual Salary : ${0:0,0}", annual_salary); Console.WriteLine("Pay Rate : ${0}", pay_rate.ToString("F2")); Console.WriteLine("Tax Rate : {0}%", tax_rate.ToString("F2")); Console.WriteLine("Gross Pay : ${0}", gross_pay.ToString("F2")); Console.WriteLine("Tax : ${0}", tax.ToString("F2")); Console.WriteLine("Net Pay : ${0}\n", net_pay.ToString("F2")); } } }
Všechny reakce
-
20. srpna 2012 13:00
Hi,
I understood, that you need some assistance on loops for a menu structure.
You could seperate that task in subtasks. So you could do something like this:
bool continue = true;
do
{
DisplayMenu();
switch(GetKey())
{
case '1':
DoSomethingFor1();
break;
// More cases ....
case 'x':
continue = false;
Console.WriteLine("Good Bye!");
break;
default:
Console.WriteLine("Sorry, invalid selection!");
}
} while (continue)So you have the core switch statement. And I just introduced a few methods that you need to implement. So DisplayMenu should simply show the possible selections. And GetKey should read a key from the keyboard. Feel free to rename parts or just replace an introduced Method with a buildin Method (so maybe you directly want to call a method of the Console class directly).
I hope I was able to help you with the missing part. And I hope that you saw a way to split the code into parts through methods. That way you do not need to write all the code together which quickly gets quite ugly and hard to read.
Also be aware: I didn't test the code in any way - so there might be typos or so.
If you are not allowed to use methods, then you have to put the whole code of the method at the call of the method which makes the code really hard to read. But that part shouldn't be to hard to do and I would still start more abstract and define more and more ... So when starting development, you should define what is happening e.g. start with a main loop:
bool continue = true;
do
{
// Logic of my program
} while (continue)and then replace it more and more ... e.g.
do
{
// Display menu
char key;
// Get Key into key variable
switch(key)
{
case '1':
// Do menu item 1
break;
case '2':
// Do stuff for menu item 2
break;
and so on ...That way you hopefully keep the overview!
What could also help in such a development area:
Visual Studio supports regions. You can define blocks of code and give it names. That way you can collapse parts of your code - which can be usefull if you cannot keep parts small through methods. e.g.
case '1':
#region Code to do something
// Here comes your code
#endregionNow the Editor should give you a [+] on the left side which you can click to enlarge / reduce that part of the code.
I hope I was able to help you a little bit.
With kind kind regards,
Konrad
- Navržen jako odpověď NorkkMicrosoft Community Contributor 20. srpna 2012 13:36
- Označen jako odpověď lol124567 20. srpna 2012 15:21