PatPeter

Printer Homework for big daddy#2095 <@515027421729128460>

Sep 18th, 2019
104
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class WirelessPrinter {
  2. public static void main(String args[]) {
  3. WirelessPrinter printer = new WirelessPrinter(1.0, 500);
  4. // turn it on and check its state
  5. printer.turnOn();
  6. System.out.println("Power: true => " + printer.isOn()); // expected true
  7. System.out.println("Connected: true => " + printer.isConnected()); // expected true
  8. System.out.println("Paper Percent: 100 => " + printer.getPaperLevel()); // expected 100(%)
  9. System.out.println("Ink Level: 1.0 => " + printer.getInkLevel()); // expected 1.0
  10.  
  11. // try print
  12. System.out.println(" ");
  13. printer.print(50);
  14. System.out.println("Paper Level: 90 => " + printer.getPaperLevel()); // expected 90
  15. System.out.println("Ink Level: 0.95 => " + printer.getInkLevel()); // expected 0.95
  16. System.out.println("Pages Printed: 50 => " + printer.getTotalPagesPrinted()); //expected 50
  17. System.out.println("Paper Used: 50 => " + printer.getTotalPaperUsed()); // expected 50
  18.  
  19. // try print more pages than what is left in the tray
  20. System.out.println(" ");
  21. printer.print(500); // out of paper
  22. System.out.println("Paper Level: 0 => " + printer.getPaperLevel()); // expected 0
  23. System.out.println("Ink Level: 0.5 => " + printer.getInkLevel()); // expected 0.5
  24. System.out.println("Pages Printed: 500 => " + printer.getTotalPagesPrinted());//expected 500
  25. System.out.println("Pages Used: 500 => " + printer.getTotalPaperUsed()); // expected 500
  26.  
  27. // try loadPaper method
  28. System.out.println(" ");
  29. printer.loadPaper(1000);
  30. System.out.println("Paper Level Exact: 500 => " + printer.getPaperLevelExact()); // expected 500
  31.  
  32. // try replace the cartridge
  33. System.out.println(" ");
  34. printer.replaceCartridge();
  35. System.out.println("Ink Level: 1.0 => " + printer.getInkLevel()); // expected 1.0
  36.  
  37. // try disconnect method
  38. System.out.println(" ");
  39. printer.disconnect(); // network goes off
  40. printer.print(50);
  41. System.out.println("Paper Level Exact: 500 => " + printer.getPaperLevelExact()); // expected 500
  42. System.out.println("Ink Level: 1.0 => " + printer.getInkLevel()); // expected 1.0
  43. }
  44.  
  45. private boolean Connected;
  46. private boolean Status;
  47. private double inkLevel;
  48. private int paperlevel;
  49. private double canPrint;
  50. private double inkLevelNow;
  51. private int totalPages;
  52. private int currentPaper;
  53. private int blankPages;
  54.  
  55. public WirelessPrinter() {
  56. inkLevel = .5;
  57. paperlevel = 0;
  58. }
  59. public static final int PAGES_PER_CARTRIDGE = 1000;
  60. public static final int TRAY_CAPACITY = 500;
  61. public static final double NEW_CARTRIDGE_INK_LEVEL = 1.0;
  62.  
  63. public WirelessPrinter(double ink,int paper) {
  64. inkLevel = ink;
  65. paperlevel = paper;
  66. }
  67.  
  68. //working but does not print initial ink level
  69. public double getInkLevel() {
  70. return inkLevel;
  71. }
  72. //prints percentage
  73. public int getPaperLevel() {
  74. return (int) ((double) paperlevel * 100 /TRAY_CAPACITY);
  75. }
  76. // Prints out of 500
  77. public int getPaperLevelExact() {
  78. return paperlevel;
  79. }
  80.  
  81. public int getTotalPagesPrinted() {
  82. return (totalPages-blankPages);
  83. }
  84. public int getTotalPaperUsed() {
  85. return totalPages + blankPages;
  86. }
  87.  
  88. //working
  89. public boolean isConnected() {
  90. if (Connected == true) {
  91. return true;
  92. }
  93. else {
  94. return false;
  95. }
  96. }
  97.  
  98. //working
  99. public boolean isOn() {
  100. if(Status == true) {
  101. return true;
  102. }
  103. else {
  104. return false;
  105. }
  106. }
  107.  
  108. //working
  109. public void connect() {
  110. Connected = true;
  111. }
  112.  
  113. //working
  114. public void disconnect() {
  115. Connected = false;
  116. }
  117.  
  118. //working
  119. public void turnOn() {
  120. Status = true;
  121. Connected = true;
  122. }
  123.  
  124. //working
  125. public void turnOff() {
  126. Status = false;
  127. }
  128. //working
  129. public void replaceCartridge() {
  130. inkLevel = NEW_CARTRIDGE_INK_LEVEL;
  131. }
  132.  
  133. public void print(int pages) {
  134. if (Connected) {
  135. //paper code
  136.  
  137. //total pages Printed
  138. System.out.println(paperlevel);
  139. totalPages += Math.min(pages, paperlevel);
  140. System.out.println(totalPages);
  141. currentPaper = Math.min(pages, paperlevel);
  142. paperlevel = Math.max(paperlevel - pages, 0);
  143.  
  144. //ink code
  145.  
  146. canPrint = ((double) Math.min(currentPaper,pages)) / PAGES_PER_CARTRIDGE;
  147. System.out.println("canPrint: " + canPrint);
  148. //rounds the inklevel
  149. //ink i have
  150. inkLevelNow = Math.min(inkLevel, canPrint);
  151. System.out.println("inkLevel: " + inkLevel);
  152. System.out.println("inkLevelNow: " + inkLevelNow);
  153. //rounds the inklevel
  154. long inkLevelRounded = Math.round(inkLevel * 100);
  155. System.out.println("inkLevelRounded: " + inkLevelRounded);
  156. long newInkLevelRounded = Math.round(inkLevelNow * 100);
  157. System.out.println("newInkLevelRounded: " + newInkLevelRounded);
  158. long inkLevelDifference = inkLevelRounded - newInkLevelRounded;
  159. System.out.println("inkLevelDifference: " + inkLevelDifference);
  160. inkLevel = inkLevelDifference / 100d;
  161. System.out.println("inkLevel: " + inkLevel);
  162. //cannot go below 0 ink
  163. inkLevel = Math.max(inkLevel, 0);
  164. System.out.println(inkLevel);
  165.  
  166.  
  167. blankPages = (int) Math.round(Math.min(0,inkLevel - inkLevelNow)*100)/100;
  168. System.out.println(blankPages);
  169. blankPages += blankPages * PAGES_PER_CARTRIDGE;
  170. System.out.println(blankPages);
  171. }
  172. else {
  173. return;
  174. }
  175. }
  176. //working
  177. public void loadPaper(int pages) {
  178. paperlevel = Math.min(pages+ paperlevel, TRAY_CAPACITY);
  179. }
  180. }
RAW Paste Data