c# - Program not calculating correctly -
i have 2 calculations correct, parts , tax, service , labor , total wrong. insight overlooked appreciated.
code:
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; namespace windowsformsapplication10 { public partial class form1 : form { public form1() { initializecomponent(); } //method calculating oil , lube charges private int oillubecharges() { int total = 0; //if oil checked, add 28 total if (chkoilchange.checked) { total += 26; } //if lube checked, add 18 total if (chklube.checked) { total += 18; return total; } //if nothing checked return 0 else { return total; } } //method calculating flushes private int flushcharges() { //define local variable int total = 0; //if radiator checked, add 30 total if (chkradiator.checked) { total += 30; //if transmission checked, add 80 total if (chktransmission.checked) { total += 80; return total; } //if nothing checked return 0 else { return total; } } //if nothing checked return 0 else { return total; } } private int misccharges() { //define local variable int total = 0; //if inspection checked, add total if (chkinspection.checked) { total += 15; } //if replace muffler checked, add 100 total if (chkmuffler.checked) { total += 100; } //if tire rotation checked, add 20 total if (chktire.checked) { total += 20; return total; } //if nothing checked return 0 else { return total; } } //calculate total other charges private int othercharges() { int total = 0; int labor; decimal parts; if (int.tryparse(txtlabor.text, out labor)) { total = labor; } if (decimal.tryparse(txtparts.text, out parts)) { txtpartstotal.text = parts.tostring("c"); return total; } else { return total; } } private decimal taxcharges() { decimal addtax; string parts = txtparts.text; addtax = convert.todecimal(parts) * 0.06m; txttax.text = addtax.tostring("c"); return addtax; } private decimal totalcharges() { decimal total; decimal serviceandlabor; total = othercharges() + misccharges() + oillubecharges() + flushcharges() + taxcharges(); txttotal.text = total.tostring("c"); serviceandlabor = misccharges() + oillubecharges() + flushcharges() + othercharges(); txtserviceandlabor.text = serviceandlabor.tostring("c"); return total; } private void btncalculate_click(object sender, eventargs e) { oillubecharges(); flushcharges(); misccharges(); othercharges(); taxcharges(); totalcharges(); } private void exitbutton_click(object sender, eventargs e) { this.close(); } } }
i found problem. in old code had if statement nested 1 when shouldn't have been. on top of returned in middle of method, never reaching 1 statement.
//if radiator checked, add 30 total if (chkradiator.checked) { total += 30; //if transmission checked, add 80 total if (chktransmission.checked) { total += 80; return total; } //if nothing checked return 0 else { return total; } } //if nothing checked return 0 else { return total; }
Comments
Post a Comment