how to output the result in jframe

Reply

Join Date: Mar 2009
Posts: 18
Reputation: YingKang is an unknown quantity at this point 
Solved Threads: 0
YingKang YingKang is offline Offline
Newbie Poster

how to output the result in jframe

 
0
  #1
25 Days Ago
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 : )

  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. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,358
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
0
  #2
25 Days Ago
So use a JTextArea, then, instead of using System.out.print or println, you use the JTextAreas append.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC