944,103 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 771
  • Java RSS
Nov 2nd, 2009
0

how to output the result in jframe

Expand Post »
This code outputs the result in console. I want to implement a Swing JFrame, producing output readable by user. Do I have to change all my code if I want to use JFrame? Can anyone help? I am not familiar with JFrame at all. Thanks : )

Java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2.  
  3. /**
  4.  * Process payroll.
  5.  
  6.  */
  7. public class payroll
  8. {
  9. /**
  10.   * Creates an array of employees (currently hard coded), and asks
  11.   * for the hours of each, then prints paystubs for each and total
  12.   * amount paid at the end.
  13.   */
  14.  
  15. public static void main(String[] args)
  16. {
  17.  
  18.  
  19.  
  20.  
  21. Employee[] emp = new Employee[5];
  22. emp[0] = new HourlyEmployee("Glenn, James", "123456789", 6.00);
  23. emp[1] = new HourlyEmployee("Eastman, Roger", "222222222", 8.00);
  24. emp[2] = new SalariedEmployee("Haddad, David", "823712468", 150000);
  25. emp[3] = new SalariedEmployee("Buckley, James", "284618461", 125000);
  26. emp[4] = new SalariedEmployee("Thomas, Amanda", "184649714", 110000);
  27.  
  28.  
  29. // do this for two weeks
  30.  
  31. for (int wk = 0; wk < 2; wk++)
  32. {
  33. double totalPaid = 0.0;
  34.  
  35. // go over all employees
  36.  
  37. for (int e = 0; e < emp.length; e++)
  38. {
  39. double hrs = Double.parseDouble(JOptionPane.showInputDialog("Enter hours worked during week " + (wk + 1) + " for " + emp[e]));
  40.  
  41. totalPaid += emp[e].calcWeeklyPay(hrs);
  42. System.out.println(emp[e].makePaystub(hrs));
  43.  
  44. emp[e].updateRecord(hrs);
  45.  
  46.  
  47. }
  48.  
  49. System.out.println("Total paid for week = " + totalPaid);
  50. }
  51.  
  52. System.exit(0);
  53. }
  54. }
  55.  
  56. public class Employee
  57. {
  58.  
  59. private String ssn;
  60. private String name;
  61. private double ytdPay;
  62.  
  63. public Employee(String n, String s)
  64. {
  65. name = n;
  66. ssn = s;
  67. ytdPay = 0;
  68. }
  69. public double getYTDPay()
  70. {
  71. return ytdPay;
  72. }
  73. public double calcWeeklyPay(double hrs)
  74. {
  75. return 0.0;
  76. }
  77.  
  78. public void updateRecord(double hrs)
  79. {
  80. ytdPay += calcWeeklyPay(hrs);
  81. }
  82.  
  83. public String toString()
  84. {
  85. return (ssn.substring(0, 3) + "-"
  86. + ssn.substring(3, 5) + "-"
  87. + ssn.substring(5, 9) + " "
  88. + name);
  89. }
  90. public String makePaystub(double hrs)
  91. {
  92. return "";
  93. }
  94. }
  95.  
  96. public class HourlyEmployee extends Employee
  97. {
  98. private double wage;
  99.  
  100. public HourlyEmployee(String n, String s, double w)
  101. {
  102. super(n, s);
  103.  
  104. wage = w;
  105. }
  106.  
  107. public double calcWeeklyPay(double hrs)
  108. {
  109. return wage * hrs;
  110. }
  111. public String makePaystub(double hrs)
  112. {
  113. return (toString() + " " + hrs + " 0 " + wage + " "
  114. + calcWeeklyPay(hrs) + " "
  115. + (getYTDPay() + calcWeeklyPay(hrs)));
  116. }
  117. }
  118.  
  119. public class SalariedEmployee extends Employee
  120. {
  121. private double salary;
  122. private double leave;
  123.  
  124. public SalariedEmployee(String n, String s, double sal)
  125. {
  126. super(n, s);
  127.  
  128. salary = sal;
  129. leave = 0.0;
  130. }
  131.  
  132. public double calcWeeklyPay(double hrs)
  133. {
  134. double weeklySalary = salary / (365.0 / 7);
  135.  
  136. return weeklySalary * Math.min(40.0, hrs + calcLeaveUsed(hrs)) / 40.0;
  137. }
  138.  
  139. public double calcLeaveUsed(double hrs)
  140. {
  141. if (hrs > 40.0)
  142. return 0; // worked full week; no leave used
  143. else
  144. return Math.min(leave, 40.0 - hrs);
  145. }
  146.  
  147. public void updateRecord(double hrs)
  148. {
  149. // update year to date pay
  150.  
  151. super.updateRecord(hrs);
  152.  
  153. // take out any leave used
  154.  
  155. double leaveUsed = calcLeaveUsed(hrs);
  156. leave -= leaveUsed;
  157.  
  158. // put back in leave earned (leave earned if paid for >= 40 hrs)
  159.  
  160. if (hrs + leaveUsed >= 40.0)
  161. leave += 4;
  162. }
  163. public String makePaystub(double hrs)
  164. {
  165. return (toString() + " " + hrs + " " + calcLeaveUsed(hrs) + " "
  166. + salary + " " + calcWeeklyPay(hrs) + " "
  167. + (getYTDPay() + calcWeeklyPay(hrs)));
  168. }
  169. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
YingKang is offline Offline
30 posts
since Mar 2009
Nov 3rd, 2009
0
Re: how to output the result in jframe
So use a JTextArea, then, instead of using System.out.print or println, you use the JTextAreas append.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Can you use Strings as 'values' in a hashtable?
Next Thread in Java Forum Timeline: WTF!!! Vigenère Ciphering





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC