Advertisement
lgfjj

Program Assignment 7

Nov 30th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. Tyheir Brooks
  2. package programAssignment5;
  3. import java.io.*;
  4. import java.util.Scanner;
  5. public class CookieSale
  6. {
  7. private String [] teachers;
  8. private int [] sales;
  9. private int classes=0,newSales=0;
  10.  
  11. public CookieSale()
  12. {
  13. teachers = new String [25];
  14. sales = new int [25];
  15. }
  16.  
  17.  
  18. public void readInfo() throws IOException
  19. {
  20. int i;
  21. Scanner input = new Scanner (new FileReader("cookies.txt"));
  22. classes = input.nextInt();
  23. for (i=0;i<classes;i++)
  24. {
  25. teachers[i] = input.next();
  26. sales[i] = input.nextInt();
  27. }
  28. }
  29.  
  30. public void updateInfo() throws IOException
  31. {
  32. int subscript,newClasses,i=0;
  33. String teacherName ="";
  34. Scanner input = new Scanner (new FileReader("update.txt"));
  35. newClasses = input.nextInt();
  36. for (i =0;i<newClasses;i++)
  37. {
  38. teacherName = input.next();
  39. newSales = input.nextInt();
  40.  
  41. subscript = search(teacherName);
  42.  
  43. if (subscript>=0)
  44. sales[subscript] = (sales[subscript] + newSales);
  45. }
  46. }
  47.  
  48. public void swap(int first, int second)
  49. {
  50. String hello;
  51. hello = teachers[first];
  52. teachers[first] = teachers[second];
  53. teachers[second] = hello;
  54.  
  55. int hi;
  56. hi = sales[first];
  57. sales[first] = sales[second];
  58. sales[second] = hi;
  59.  
  60. }
  61.  
  62. public void sortByTitle()
  63. {
  64. int i, j, minSubscript;
  65.  
  66. for (i =0;i<classes-1;i++)
  67. {
  68. minSubscript = i;
  69. for (j = i+1;j<classes;j++)
  70. {
  71. if (teachers[j].compareTo(teachers[minSubscript])<0)
  72. minSubscript = j;
  73. }
  74. swap(i,minSubscript);
  75. }
  76. }
  77.  
  78. public void sortIntegers()
  79. {
  80. int i, j, minSubscript;
  81.  
  82. for (i =0;i<classes-1;i++)
  83. {
  84. minSubscript = i;
  85. for (j = i+1;j<classes;j++)
  86. {
  87. if (teachers[j].compareTo(teachers[minSubscript])<0)
  88. minSubscript = j;
  89. }
  90. swap(i,minSubscript);
  91. }
  92. }
  93.  
  94. public int search (String key)
  95. {
  96. int i=0;
  97. boolean found;
  98. found = false;
  99. while (!found&&i<classes)
  100. {
  101. if (teachers[i].equals(key))
  102. found=true;
  103. else
  104. i=i+1;
  105. }
  106. if (found==true)
  107. return i;
  108. else
  109. return -1;
  110. }
  111.  
  112. public void printInfo()
  113. {
  114. System.out.println("Teachers \t" + "Sales");
  115. for (int i =0;i<classes;i++)
  116. {
  117. System.out.print(teachers[i] + " \t");
  118. System.out.println(sales[i]);
  119. }
  120. }
  121.  
  122. public void theWinner()
  123. {
  124. int largest = 0;
  125. for (int i=0;i<classes;i++)
  126. if (sales[i]>sales[largest])
  127. largest = i ;
  128. System.out.println("The class with the largest sales is " + teachers[largest] + "with " + sales[largest]+ " boxes!");
  129. }
  130.  
  131. }
  132.  
  133. package programAssignment5;
  134. import java.io.*;
  135.  
  136. public class CookieSaleDemo
  137. {
  138. public static void main(String []Args) throws IOException
  139. {
  140. CookieSale christmas = new CookieSale();
  141. christmas.readInfo();
  142. christmas.sortByTitle();
  143. christmas.printInfo();
  144. System.out.println();
  145. System.out.println("UPDATED SALES");
  146. christmas.updateInfo();
  147. christmas.printInfo();
  148. christmas.theWinner();
  149.  
  150. }
  151. }
  152.  
  153. Teachers Sales
  154. Aliberti 6
  155. Alvarez 10
  156. Amendola 8
  157. Byrnes 9
  158. Chiara 12
  159. Conyers 5
  160. Courtney 5
  161. Davis 4
  162. DiLonardo 12
  163. DiPaoli 10
  164. Donofrio 15
  165. Harris 5
  166. Henselder 9
  167. Heyer 8
  168. Kumuyi 7
  169. Malerba 7
  170. Maloney 5
  171. Quesada 8
  172. Sgherza 6
  173. Smith 3
  174. Stix 5
  175. Tuohy 8
  176. Wynne 5
  177. Yousef 19
  178. Zambrano 18
  179.  
  180. UPDATED SALES
  181. Teachers Sales
  182. Aliberti 6
  183. Alvarez 10
  184. Amendola 8
  185. Byrnes 9
  186. Chiara 12
  187. Conyers 5
  188. Courtney 37
  189. Davis 4
  190. DiLonardo 12
  191. DiPaoli 10
  192. Donofrio 15
  193. Harris 5
  194. Henselder 9
  195. Heyer 8
  196. Kumuyi 7
  197. Malerba 7
  198. Maloney 5
  199. Quesada 8
  200. Sgherza 6
  201. Smith 35
  202. Stix 27
  203. Tuohy 39
  204. Wynne 5
  205. Yousef 19
  206. Zambrano 18
  207. The class with the largest sales is Tuohywith 39 boxes!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement