Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. //* INVENTORY *//
  2.  
  3. public class Inventory
  4. {
  5. // Initializes the inventory for storing abstraction objects
  6. public final void init()
  7. {
  8. _count = 0;
  9. }
  10.  
  11. // Returns the number of currently stored abstraction objects
  12. //C++ TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
  13. //ORIGINAL LINE: size_t get_count()
  14. public final int get_count()
  15. {
  16. return _count;
  17. }
  18.  
  19. // Returns stored abstraction object by its index or default if index is invalid
  20. //C++ TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
  21. //ORIGINAL LINE: Bicycle get_item(size_t i)
  22. public final Bicycle get_item(int i)
  23. {
  24. return (i < _count) ? _items[i] : Bicycle;
  25. {
  26. };
  27. }
  28.  
  29. // From passed property values, creates and adds new abstraction object in an array _items
  30. public final void add_item(int id, String type, double price)
  31. {
  32. if (_count < Inventory.MAX_SIZE)
  33. {
  34. Bicycle new_item = new Bicycle();
  35. new_item.init(id, type, price);
  36. _items[_count] = new_item;
  37. _count++;
  38. }
  39. }
  40.  
  41. // Looks for a matching abstraction object and returns the first found or default object
  42. public final Bicycle find_item(Bicycle query)
  43. {
  44. //C++ TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
  45. //ORIGINAL LINE: for (size_t i = 0; i < _count; i++)
  46. for (int i = 0; i < _count; i++)
  47. {
  48. // for integer type property
  49. if (query.get_id() != 0 && query.get_id() != _items[i].get_id())
  50. continue;
  51.  
  52. // for string type property
  53. if (!query.get_type().equals("") && !query.get_type().equals(_items[i].get_type()))
  54. continue;
  55. return _items[i];
  56. }
  57.  
  58. return new Bicycle({}); // return the 'default' object (if you use Java - return null value)
  59. }
  60.  
  61. // The maximum number of abstraction objects that can be stored
  62. //C++ TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
  63. //ORIGINAL LINE: static const size_t MAX_SIZE({ 10 });
  64. private static final int MAX_SIZE = 10;
  65.  
  66. // An array for storing abstraction objects
  67. private Bicycle[] _items = new Bicycle[Inventory.MAX_SIZE];
  68.  
  69. // The number of currently stored abstraction objects in the array _items
  70. //C++ TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
  71. //ORIGINAL LINE: size_t _count;
  72. private int _count;
  73. }
  74.  
  75. //* INVENTORY *//
  76.  
  77. //* BICYCLE *//
  78.  
  79. public class Bicycle
  80. {
  81. public final int get_id()
  82. {
  83. return _id;
  84. }
  85. public final String get_type()
  86. {
  87. return _type;
  88. }
  89. public final double get_price()
  90. {
  91. return _price;
  92. }
  93. public final void init(int id, String type, double price)
  94. {
  95. _id = id;
  96. _type = type;
  97. _price = price;
  98. }
  99. private int _id;
  100. private String _type;
  101. private double _price;
  102. }
  103.  
  104. //* BICYCLE *//
  105.  
  106. //* MAIN *//
  107.  
  108. public class test_final
  109. {
  110.  
  111. // output abstraction object properties to the console window
  112. public static void show(Bicycle item)
  113. {
  114. System.out.print(item.get_id());
  115. System.out.print(" ");
  116. System.out.print(item.get_type());
  117. System.out.print(" ");
  118. System.out.print(item.get_price());
  119. System.out.print(" ");
  120. System.out.print("\n");
  121. }
  122.  
  123. // solution entry function
  124. public static int Main()
  125. {
  126. Inventory inv = new Inventory();
  127.  
  128. inv.init();
  129.  
  130. // specify and add a least two different abstraction objects
  131. inv.add_item(1, "mountain", 1500.00);
  132. inv.add_item(2, "sports", 1500.50);
  133.  
  134. Bicycle qry = new Bicycle();
  135. // provide some querying values (some can be default (e.g., "", 0) to denote unset criteria)
  136. qry.init(1, "mountain", 1507.00);
  137.  
  138. //C++ TO JAVA CONVERTER WARNING: The following line was determined to be a copy constructor call - this should be verified and a copy constructor should be created if it does not yet exist:
  139. //ORIGINAL LINE: show(inv.find_item(qry));
  140. show(inv.find_item(new Bicycle(qry)));
  141.  
  142. // test with another query values
  143. qry.init(2, "sports", 1500.50);
  144.  
  145. //C++ TO JAVA CONVERTER WARNING: The following line was determined to be a copy constructor call - this should be verified and a copy constructor should be created if it does not yet exist:
  146. //ORIGINAL LINE: show(inv.find_item(qry));
  147. show(inv.find_item(new Bicycle(qry)));
  148.  
  149. //C++ TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
  150. ///#if ! NDEBUG
  151. System.in.read(); // wait until Enter key is pressed
  152. ///#endif
  153.  
  154. return 0;
  155. }
  156. }
  157.  
  158. //* MAIN *//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement