Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. package mgminterview;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5.  
  6. //Your task is to:
  7. //- fix the code
  8. //- improve the poor quality of this code.
  9. //
  10. //(You can change anything you want
  11. //
  12. //The class should sort food instances and output the following lines:
  13. //
  14. //Potato 1
  15. //Potato 9
  16. //Potato 11
  17. //Tomato 11
  18. //Potato 12
  19. //Potato 42
  20. //Potato 46
  21. //Potato 55
  22. //Potato 77
  23. //Tomato 121
  24. //
  25.  
  26.  
  27. public class MgmInterviewFoodSort {
  28.  
  29. public FOOD[] foods;
  30.  
  31. public static abstract class FOOD implements Comparable<FOOD> {
  32. public int size;
  33. public String name;
  34. public FOOD () {
  35. this.size = -1;
  36. }
  37. public FOOD (int size) {
  38. this.size = size;
  39. }
  40.  
  41. public int getSize() {
  42. return this.size;
  43. }
  44. public String getName() {
  45. return this.name;
  46. }
  47.  
  48.  
  49. //sort the food by their size and then alphabetically by type
  50. public int compareTo (FOOD other) {
  51. if (this.getSize() < other.getSize()) {
  52. return -1;
  53. }
  54. if (this.getSize() == other.getSize()) {
  55. if (this.getName().compareTo(other.getName()) < 0) {
  56. return -1;
  57. }
  58. if (this.getName().compareTo(other.getName()) > 0) {
  59. return 1;
  60. }
  61. return 0;
  62. }
  63. return 1;
  64. }
  65. }
  66.  
  67.  
  68. public static class Potato extends FOOD {
  69. public Potato () {
  70. this.name = "Potato";
  71. }
  72. public Potato (int size) {
  73. super(size);
  74. this.name = "Potato";
  75. }
  76.  
  77. public int getSize () {
  78. return this.size;
  79. }
  80. public String getName () {
  81. return this.name;
  82. }
  83. public String toString () {
  84. return this.getName() + " " + this.getSize();
  85. }
  86.  
  87. }
  88.  
  89. public static class Tomato extends FOOD {
  90. public Tomato () {
  91. this.size = 121;
  92. this.name = "Tomato";
  93. }
  94. public Tomato (int size) {
  95. super(size);
  96. this.name = "Tomato";
  97. }
  98. public int getSize () {
  99. return this.size;
  100. }
  101. public String getName () {
  102. return this.name;
  103. }
  104. public String toString () {
  105. return this.getName() + " " + this.getSize();
  106. }
  107.  
  108. }
  109.  
  110.  
  111. public static void print(FOOD[] foods) {
  112. for (int x=0; x< foods.length; x++) {
  113. System.out.println(foods[x].toString());
  114. }
  115. }
  116.  
  117. public static void main(final String[] args) {
  118.  
  119.  
  120. FOOD[] foods = new FOOD[10];
  121.  
  122. foods[0] = new Tomato(11);
  123. foods[1] = new Tomato();
  124. foods[2] = new Potato(1);
  125. foods[3] = new Potato(42);
  126. foods[4] = new Potato(77);
  127. foods[5] = new Potato(55);
  128. foods[6] = new Potato(46);
  129. foods[7] = new Potato(12);
  130. foods[8] = new Potato(11);
  131. foods[9] = new Potato(9);
  132.  
  133.  
  134. Arrays.sort(foods);
  135. print(foods);
  136. }
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement