In: Computer Science
I have created a method in java in my menu list and I have to put all the work into the method but I cannot get it to print and am not sure where I am going wrong. this is my code:
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
String fName = "";
String lName = "";
double hoursWorked;
double hourlyRate;
int optionOne = 1;
int optionTwo = 2;
int optionThree = 3;
int optionFour = 4;
int choice;
System.out.println("Calculator Menu");
System.out.println("1) Wage Calculator");
System.out.println("2) Coupon Calculator");
System.out.println("3) Roman Number Converter");
System.out.println("4) Exit");
System.out.println("");
System.out.println("Enter choice: ");
choice = in.nextInt();
switch(choice) {
case 1:
System.out.println("Enter first name: ");
fName = in.next();
System.out.println("Enter last name: ");
lName = in.next();
System.out.println("Enter hourly rate: ");
hourlyRate = in.nextDouble();
System.out.println("Enter hours worked: ");
hoursWorked = in.nextDouble();
System.out.printf("Name: " + fName + "," + lName);
System.out.println("");
wageCalculator();
break;
case 2:
double purchaseAmount;
int purchaseType;
final double autoP = 1;
final double frag = 2;
final double acc = 3;
System.out.println("Enter purchase amount: ");
purchaseAmount = in.nextDouble(); {
System.out.println("Choose purchase type: ");
System.out.println("1) Auto Parts");
System.out.println("2) Fragrances");
System.out.println("3) Accessories");
purchaseType = in.nextInt();
} if (purchaseType == autoP) {
System.out.println("Your coupon is: $" + purchaseAmount*.10 + "(Auto Parts)");
} if (purchaseType == frag) {
System.out.println("Your coupon is: $" + purchaseAmount*.15 + "(Fragrances)");
} if (purchaseType == acc) {
System.out.println("Your coupon is: $" + purchaseAmount*.20 + "(Accessories)");
}
break;
case 3:
double calc_wages;
System.out.println("No option right now");
break;
case 4:
System.out.println("Thank you for using our program. Have a great day!");
}
// public static int convertRomanNumber(String str). {
// }
}
private static void wageCalculator() {
// TODO Auto-generated method stub
double hoursWorked = 0;
double hourlyRate = 0;
double overtimeHours;
double overtimePay = 0;
double regularPay = 0;
if (hoursWorked > 40) {
overtimeHours = hoursWorked - 40;
System.out.println("Overtime hours: " + overtimeHours);
overtimePay = overtimeHours*(1.5*hourlyRate);
System.out.println("Overtime Pay: " + overtimePay);
hoursWorked = 40;
}
regularPay = hourlyRate*hoursWorked;
System.out.println("Regular hours worked: " + hoursWorked);
System.out.println("Regular Pay: $" + regularPay);
System.out.printf("Total Pay $%.2f", regularPay + overtimePay);
}
}
Solution:
----------------------
You were getting wrong output because you didn't pass hourlyRate,hoursWorked value to wageCalculator() method. You have to passed these values explicitly as these values are used in diiferent function( main() and wageCalculator()). To get Proper Output use wageCalculator(hourlyRate,hoursWorked); inside case 1
And Declare method as
private static void wageCalculator(double hourlyRate,double
hoursWorked) {
// TODO Auto-generated method
stub
double overtimeHours;
double overtimePay = 0;
double regularPay = 0;
if (hoursWorked > 40) {
overtimeHours = hoursWorked -
40;
System.out.println("Overtime hours:
" + overtimeHours);
overtimePay =
overtimeHours*(1.5*hourlyRate);
System.out.println("Overtime Pay: "
+ overtimePay);
hoursWorked = 40;
}
regularPay =
hourlyRate*hoursWorked;
System.out.println("Regular hours
worked: " + hoursWorked);
System.out.println("Regular Pay: $"
+ regularPay);
System.out.printf("Total Pay
$%.2f", regularPay + overtimePay);
}
Full Code is Given Below:
=============================
import java.util.Scanner;
public class Menu {
public static void main(String[] args) {
// TODO Auto-generated method
stub
Scanner in = new
Scanner(System.in);
String fName = "";
String lName = "";
double hoursWorked;
double hourlyRate;
int optionOne = 1;
int optionTwo = 2;
int optionThree = 3;
int optionFour = 4;
int choice;
System.out.println("Calculator
Menu");
System.out.println("1) Wage
Calculator");
System.out.println("2) Coupon
Calculator");
System.out.println("3) Roman Number
Converter");
System.out.println("4)
Exit");
System.out.println("");
System.out.println("Enter choice:
");
choice = in.nextInt();
switch(choice) {
case 1:
System.out.println("Enter first
name: ");
fName = in.next();
System.out.println("Enter last
name: ");
lName = in.next();
System.out.println("Enter hourly
rate: ");
hourlyRate = in.nextDouble();
System.out.println("Enter hours
worked: ");
hoursWorked =
in.nextDouble();
System.out.printf("Name: " + fName
+ "," + lName);
System.out.println("");
//passing values to function
wageCalculator(hourlyRate,hoursWorked);
break;
case 2:
double purchaseAmount;
int purchaseType;
final double autoP = 1;
final double frag = 2;
final double acc = 3;
System.out.println("Enter purchase
amount: ");
purchaseAmount = in.nextDouble();
{
System.out.println("Choose purchase
type: ");
System.out.println("1) Auto
Parts");
System.out.println("2)
Fragrances");
System.out.println("3)
Accessories");
purchaseType = in.nextInt();
} if (purchaseType == autoP)
{
System.out.println("Your coupon is:
$" + purchaseAmount*.10 + "(Auto Parts)");
} if (purchaseType == frag) {
System.out.println("Your coupon is:
$" + purchaseAmount*.15 + "(Fragrances)");
} if (purchaseType == acc) {
System.out.println("Your coupon is:
$" + purchaseAmount*.20 + "(Accessories)");
}
break;
case 3:
double calc_wages;
System.out.println("No option right
now");
break;
case 4:
System.out.println("Thank you for
using our program. Have a great day!");
}
// public static int
convertRomanNumber(String str). {
// }
}
private static void
wageCalculator(double hourlyRate,double hoursWorked) {
// TODO Auto-generated method
stub
double overtimeHours;
double overtimePay = 0;
double regularPay = 0;
if (hoursWorked > 40) {
overtimeHours = hoursWorked -
40;
System.out.println("Overtime hours:
" + overtimeHours);
overtimePay =
overtimeHours*(1.5*hourlyRate);
System.out.println("Overtime Pay: "
+ overtimePay);
hoursWorked = 40;
}
regularPay =
hourlyRate*hoursWorked;
System.out.println("Regular hours
worked: " + hoursWorked);
System.out.println("Regular Pay: $"
+ regularPay);
System.out.printf("Total Pay
$%.2f", regularPay + overtimePay);
}
}
Output:
================
Code Snapshot:
====================