Advertisement
Nikki12345671

Staples

Apr 5th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. //Nikki Forehand
  2.  
  3. public class Program {
  4.  
  5. public static void main(String[] args) {
  6. //Code to test your stapler. Do not edit.
  7. Stapler redStapler = new Stapler(), blueStapler = new Stapler(), whiteStapler = new Stapler();
  8. System.out.println(redStapler.getStaples() == 0 ? ":) New stapler is empty." : ":( New stapler should be empty.");
  9. System.out.println(redStapler.staple() == false ? ":) Empty stapler can't staple." : ":( Empty stapler shouldn't staple.");
  10. redStapler.open(); redStapler.loadStaples(1); System.out.println(redStapler.staple() == false ? ":) Open stapler can't staple." : ":( Open stapler shouldn't staple.");
  11. redStapler.close(); redStapler.open(); redStapler.loadStaples(-50); redStapler.close(); System.out.println(redStapler.getStaples() == 1 ? ":) loadStaples() ignores negative staples." : ":( loadStaples() should ignore negative staples.");
  12. blueStapler.open(); blueStapler.loadStaples(525); blueStapler.close(); whiteStapler.open(); whiteStapler.loadStaples(251); whiteStapler.loadStaples(251); whiteStapler.close(); System.out.println(blueStapler.getStaples() == 500 && whiteStapler.getStaples() == 500 ? ":) Stapler honors 500 staple capacity." : ":( Stapler may hold no more than 500 staples");
  13. while(blueStapler.staple());
  14. blueStapler.open(); blueStapler.loadStaples(1); blueStapler.close(); boolean then, now; then = blueStapler.staple(); now = blueStapler.staple(); System.out.println(then == true && now == false && blueStapler.getStaples() == 0 ? ":) Staplers with staples return true. Empty ones return false." : ":( Staplers with staples return true. Empty ones return false.");
  15. System.out.println("Thanks for using the Stapler tester :)");
  16. }
  17. }
  18.  
  19. //Edit only code below this line in Eclipse
  20.  
  21. class Stapler{
  22. //Data
  23. int staples;
  24. boolean isOpen;
  25.  
  26. //Methods (Behaviors)
  27. public Stapler(){
  28. staples = 0;
  29. isOpen = false;
  30. }
  31.  
  32. public void loadStaples(int s){
  33. //Update staples variable to be s more, but no more than 500 total.
  34. //If s is not positive, ignore the value
  35.  
  36. if(s > 0) {
  37. staples= staples + s ;
  38. if(staples > 500) {
  39. staples = 500 ;
  40. }
  41.  
  42. }
  43.  
  44.  
  45. }
  46.  
  47. public boolean staple(){
  48. //1. If the stapler is closed and not out of staples, staple!
  49. //2. Reduce the number of staples by 1 & return true.
  50. //If there was a problem stapling (not closed, out of staples) return false;
  51. if(isOpen == true) {
  52. return false;
  53. }
  54. else if (staples < 1) {
  55. return false;
  56. }
  57. else {
  58. staples = staples - 1;
  59. return true;
  60. }
  61. }
  62.  
  63. public void open(){
  64. //Open the stapler
  65. isOpen = true;
  66. }
  67.  
  68. public void close(){
  69. //Close the stapler\
  70. isOpen = false;
  71. }
  72.  
  73. public int getStaples(){
  74. return staples;
  75. }
  76.  
  77. public boolean isClosed(){
  78. return !isOpen;
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement