Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 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.  
  30.  
  31. public static FOOD[] foods = null;
  32.  
  33. public static abstract class FOOD implements Comparable<FOOD> {
  34. public int size;
  35. public String name;
  36. public FOOD () {
  37. this.size = -1;
  38. }
  39. public FOOD (int size) {
  40. this.size = size;
  41. }
  42.  
  43. public int getSize() {
  44. return this.size;
  45. }
  46. public String getName() {
  47. return this.name;
  48. }
  49.  
  50.  
  51. //sort the food by their size and then alphabetically by type
  52. public int compareTo (FOOD other) {
  53. if (this.getSize() < other.getSize()) {
  54. return -1;
  55. }
  56. if (this.getSize() == other.getSize()) {
  57. if (this.getName().compareTo(other.getName()) < 0) {
  58. return -1;
  59. }
  60. if (this.getName().compareTo(other.getName()) > 0) {
  61. return 1;
  62. }
  63. return 0;
  64. }
  65. return 1;
  66. }
  67. }
  68.  
  69.  
  70. public static class Potato extends FOOD {
  71. public Potato () {
  72. this.name = "Potato";
  73. }
  74. public Potato (int size) {
  75. super(size);
  76. this.name = "Potato";
  77. }
  78.  
  79. public int getSize () {
  80. return this.size;
  81. }
  82. public String getName () {
  83. return this.name;
  84. }
  85. public String toString () {
  86. return this.getName() + " " + this.getSize();
  87. }
  88.  
  89. }
  90.  
  91. public static class Tomato extends FOOD {
  92. public Tomato () {
  93. this.size = 121;
  94. this.name = "Tomato";
  95. }
  96. public Tomato (int size) {
  97. super(size);
  98. this.name = "Tomato";
  99. }
  100. public int getSize () {
  101. return this.size;
  102. }
  103. public String getName () {
  104. return this.name;
  105. }
  106. public String toString () {
  107. return this.getName() + " " + this.getSize();
  108. }
  109.  
  110. }
  111.  
  112.  
  113. public static void print() {
  114. for (int x=0; x< foods.length; x++) {
  115. System.out.println(foods[x].toString());
  116. }
  117. }
  118.  
  119. public static void main(final String[] args) {
  120.  
  121.  
  122. foods = new FOOD[10];
  123.  
  124. foods[0] = new Tomato(11);
  125. foods[1] = new Tomato();
  126. foods[2] = new Potato(1);
  127. foods[3] = new Potato(42);
  128. foods[4] = new Potato(77);
  129. foods[5] = new Potato(55);
  130. foods[6] = new Potato(46);
  131. foods[7] = new Potato(12);
  132. foods[8] = new Potato(11);
  133. foods[9] = new Potato(9);
  134.  
  135.  
  136. Arrays.sort(foods);
  137. print();
  138. }
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement