WR

WR (http://forums.webrats.com/index.php)
-   Computers and Programming (http://forums.webrats.com/forumdisplay.php?f=67)
-   -   Hey guys looking for some Java help (http://forums.webrats.com/showthread.php?t=144105)

shocking4life 07-08-2008 03:39 AM

Hey guys looking for some Java help
 
Im coding this application and I cant figure out how to make the last bit to work. It asks for user input in a loop until they do not want to input any more entries, it should then use the entered data to calculate a total and averages.

From what i have it just uses the last entry from the loop and not the entire loop. Any help would be appreciated, thanks!

To sum up the program will:
* Ask whether the consultant is a partner (y/n)
* Asks for employee hours
* Calculate the total cost for the employee
* Output the Employee type, Employee Hours and Employee Cost
* Repeat if desired
Then it will: (heres where im having trouble)
*Tabulate the total number of employees
* Tabulate the total project cost
* Calulate the average hours per employee
* Calculate the average project hourly billing rate
and output it to the console.


Code:
import java.text.NumberFormat; import java.util.Scanner; public class BillingAppp { public static void main(String[] args) { // welcome the user to the program System.out.println("Weclome to the Billing Application"); System.out.println(); // print a blank line final double PARTNER_RATE = 300.00; final double REGULAR_RATE = 150.00; double employeeRate = 0; int employeeCount = 0; double payRate = 0.0; double totalCost = 0.0; double workHours = 0.0; double averageBilling = 0.0; double averageHours = 0.0; String employeeType = "employeeType"; // create a Scanner object named sc Scanner sc = new Scanner(System.in); String choice = "y"; while (!choice.equalsIgnoreCase("n")) { // get the input from the user declaring which consultant and assigns string and rate System.out.print("Is the consultant a partner?: (y/n) "); choice = sc.next(); employeeCount = employeeCount + 1; if (choice.equals("y") || choice.equals("Y")) payRate = PARTNER_RATE; if (choice.equals("y") || choice.equals("Y")) employeeType = "Partner Consultant"; if (choice.equals("n") || choice.equals("N")) payRate = REGULAR_RATE; if (choice.equals("n") || choice.equals("N")) employeeType = "Regular Consultant"; System.out.print("Enter Employee Hours: "); workHours = sc.nextDouble(); System.out.println(); //calculate total cost averageHours = workHours / employeeCount; averageBilling = totalCost / employeeCount; totalCost = (payRate * workHours); NumberFormat currency = NumberFormat.getCurrencyInstance(); System.out.println( "Employee Type: " + employeeType + "\n" + "Employee Hours: " + (workHours) + "\n" + "Total Employee Cost: " + currency.format(totalCost) + "\n"); System.out.println(); // see if the user wants to add another employee System.out.print("Do you want to add another employee? (y/n): "); choice = sc.next(); System.out.println(); } // calculate and display total number of employees, total project cost, avg hours worked, avg billing rate NumberFormat currency = NumberFormat.getCurrencyInstance(); String message = "Total number of Employees: " + employeeCount + "\n" + "Total Project Cost: " + currency.format(totalCost) + "\n" + "Average Hours Worked : " + (averageHours) + "\n" + "Average Billing Rate: " + currency.format(averageBilling) + "\n"; System.out.println(message); } }

shocking4life 07-08-2008 02:50 PM

Re: Hey guys looking for some Java help
 
anyone? The problem seems to be with my variables. I need to have a set of variables that are used for each individual employee, and then a set of overall variables that track all the various totals. But I cant figure out how to set that part up.

toxicjn 07-12-2008 03:36 PM

Re: Hey guys looking for some Java help
 
Ok, I'm a C guy, so correct any syntax errors. I'm sure this is what you ment to do. I created an additional variable called employeeCost which is added to totalCost at the end of each while loop. And I moved averageHours and averageBilling out of the loop.

Like I said, I did not compile this, good luck.


Code:
import java.text.NumberFormat; import java.util.Scanner; public class BillingAppp { public static void main(String[] args) { // welcome the user to the program System.out.println("Weclome to the Billing Application"); System.out.println(); // print a blank line final double PARTNER_RATE = 300.00; final double REGULAR_RATE = 150.00; double employeeRate = 0; int employeeCount = 0; double payRate = 0.0; double employeeCost = 0.0; double totalCost = 0.0; double workHours = 0.0; double averageBilling = 0.0; double averageHours = 0.0; String employeeType = "employeeType"; // create a Scanner object named sc Scanner sc = new Scanner(System.in); String choice = "y"; while (!choice.equalsIgnoreCase("n")) { // get the input from the user declaring which consultant and assigns string and rate System.out.print("Is the consultant a partner?: (y/n) "); choice = sc.next(); employeeCount = employeeCount + 1; if (choice.equalsIgnoreCase("y")) { payRate = PARTNER_RATE; employeeType = "Partner Consultant"; } else if (choice.equalsIgnoreCase("n")) { payRate = REGULAR_RATE; employeeType = "Regular Consultant"; } System.out.print("Enter Employee Hours: "); workHours = sc.nextDouble(); System.out.println(); //calculate the employees cost, and print employeeCost = (payRate * workHours); NumberFormat currency = NumberFormat.getCurrencyInstance(); System.out.println( "Employee Type: " + employeeType + "\n" + "Employee Hours: " + (workHours) + "\n" + "Total Employee Cost: " + currency.format(employeeCost) + "\n"); System.out.println(); totalCost = totalCost + employeeCost; // see if the user wants to add another employee System.out.print("Do you want to add another employee? (y/n): "); choice = sc.next(); System.out.println(); } //end while // calculate and display total number of employees, total project cost, avg hours worked, avg billing rate averageHours = workHours / employeeCount; averageBilling = totalCost / employeeCount; NumberFormat currency = NumberFormat.getCurrencyInstance(); String message = "Total number of Employees: " + employeeCount + "\n" + "Total Project Cost: " + currency.format(totalCost) + "\n" + "Average Hours Worked : " + (averageHours) + "\n" + "Average Billing Rate: " + currency.format(averageBilling) + "\n"; System.out.println(message); } //end main } //end class

shocking4life 07-15-2008 02:40 PM

Re: Hey guys looking for some Java help
 
toxicjn thanks for your reply, this is exactly what had to be fixed,
i actually did finally figure this with the help of a buddy before i had returned to check this post lol.

but again thanks!


All times are GMT -5. The time now is 04:25 AM.
click on one of our sponsors! OR REMOVE ADS

Powered by: vBulletin Version 3.0.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.