How to calculate Cell phone bill in java, calculation issues -
my task calculate monthly cell phone bill in java, new java , still learning. can't seem understand calculations , how there. have named variables , constants , initiliazed them, need make calculations find cost of total bill. output i'm trying reach : run: minutes used = 675 text messages sent/recieved = 1031 data used = 605 megabytes. have data play? (y/n) : y have text message plan (y/n)?: y monthly cell phone charges : monthly plan = 39.99 text message plan = 15.00 data plan = 20.00 addidtional minutes charged = 79.85 charge data overage = 40.00
public static void main(string[] args) { // todo code application logic here scanner user_input = new scanner(system.in); final double monthly_plan_voice; final int allowable_minutes; final double additional_minute_rate; final double text_msg_plan; final double text_msg_rate; final double data_plan_rate; final int allowable_data_incr; monthly_plan_voice = 39.99; //double cause of decimal allowable_minutes = 500; // int because no need decimal additional_minute_rate = 0.45; // has decimal text_msg_plan = 15.00; // rate depending on user input(double) text_msg_rate = 0.12; // rate depending on user input (double) data_plan_rate = 20.00; // double cause of decimal allowable_data_incr = 300; //int cause increment string minutes; system.out.print("enter number of minutes used month: "); minutes = user_input.next(); string texts; system.out.print("do have text message plan? (y/n): "); texts = user_input.next(); system.out.print("enter number of sent , recieved texts: "); texts = user_input.next(); string data; system.out.print("do have data plan? (y/n): "); data = user_input.next(); system.out.print("enter amount of data used: "); data = user_input.next(); if (allowable_data_incr < 300) { system.out.print("the cost of data 20.00$ "); } else { system.out.println("the cost of data 40.00$" ); } if (allowable_minutes > 500) { system.out.print(""); }
first personal option keep code clean , initiate , assign in 1 step:
final double monthly_plan_voice = 39.99; //double cause of decimal
secondly dollar sign "$" should used prefix number. ex $5.99 print statement reads:
system.out.print("the cost of data 20.00$ ");
i recommend changing each of little more professional.
system.out.print("the cost of data $20.00.");
now answer question. find total create new variable of type double. :
double totalmonthlybill = 0;
now in each of if else statements add cost per each part of bill total variable.
for example:
if (allowable_data_incr < 300) { system.out.print("the cost of data 20.00$ "); totalmonthlybill += 20; } else { system.out.println("the cost of data 40.00$" ); totalmonthlybill += 40; }
the += add or subtract if negative value of (totalmonthlybill) whatever passed. can use += 2 variable or passing int or double ext.
assuming variable1 , variable2 initialized , both have been given value of not null
variable1 += variable2; variable1 += 2;
this same as:
variable1 = variable1 + variable2; variable1 = variable1 + 2;
but in case since have running total , defining cost based of each step can achived defining total variable , using += in each if/else statement add value total can call later final price
system.out.println("total monthly bill: " + totalmonthlybill);
hope solves problem.
Comments
Post a Comment