while trying to do some exercises to learn java i wrote a simple program to calculate the count of worked hours and give the deserved salary.
package horulycalc;
import java.util.Arrays;
import java.util.Scanner;
public class HorulyCalc {
public static void main(String[] args) {
Scanner param = new Scanner(System.in);
String [] titles = {"Developer","Designer","Data Entry","Manager","CEO"};
System.out.println("Hello and welcome to Job Counter");
System.out.println("Please enter you Job Title");
String title = param.nextLine();
while(!Arrays.asList(titles).contains(title)){
System.out.println("Please enter valid Job Title");
title = param.nextLine();
if(Arrays.asList(titles).contains(title)){
System.out.println("Please enter your Hours for this week :");
String count = param.nextLine();
System.out.printf("Your Salary is : $ %f",HoursMath(HourRate(title),Integer.parseInt(count)));
break ;
}
}
}
public static int HourRate(String jobTitle){
int rate = 0;
switch(jobTitle){
case "Developer":
rate = 10 ;
break;
case "Designer":
rate = 8 ;
break ;
case "Data Entry":
rate = 6;
break ;
case "Manager":
rate = 15 ;
break;
case "CEO":
rate = 36;
break ;
}
return rate ;
}
public static float HoursMath(int rate ,int count){
float total ;
total = rate * count ;
return total ;
}
}
the program run fine if i added a wrong Job title for the first time i mean an input which is not included within the job titles array.
when i enter a valid Job Title in the first time for example "CEO" the program break and netbeans how it as its finished
ifstatement outside the loop and simplify the code. i.e. you want to calculate the hours after you have a valid title, I assume, so put that after it.