Advertisement
darko13

java pcerija

Nov 1st, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.60 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. class InvalidPizzaTypeException extends Exception {
  6. public InvalidPizzaTypeException() {
  7. super("InvalidPizzaTypeException");
  8.  
  9. }
  10.  
  11. }
  12.  
  13. class InvalidExtraTypeException extends Exception {
  14. public InvalidExtraTypeException() {
  15. super("InvalidExtraTypeException");
  16.  
  17. }
  18. }
  19.  
  20. class ItemOutOfStockException extends Exception {
  21. Item item;
  22.  
  23. public ItemOutOfStockException(Item item) {
  24. this.item = item;
  25. }
  26. }
  27.  
  28. class ArrayIndexOutOfBоundsException extends Exception {
  29. int idx;
  30.  
  31. public ArrayIndexOutOfBоundsException(int idx) {
  32. this.idx = idx;
  33. }
  34. }
  35.  
  36. class Item {
  37. protected int cena;
  38. protected String type;
  39.  
  40. public int getPrice() {
  41. return cena;
  42. }
  43.  
  44. }
  45.  
  46. class ExtraItem extends Item {
  47.  
  48. ExtraItem(String type) throws InvalidExtraTypeException {
  49. if (type.equals("Coke") || type.equals("Ketchup")) {
  50. if (type.equals("Coke"))
  51. cena = 5;
  52. else
  53. cena = 3;
  54. this.type = type;
  55.  
  56. } else
  57. throw new InvalidExtraTypeException();
  58. }
  59.  
  60. }
  61.  
  62. class PizzaItem extends Item {
  63. public PizzaItem(String type) throws Exception {
  64. if (type.equals("Standard") || type.equals("Pepperoni")
  65. || type.equals("Vegetarian")) {
  66. this.type = type;
  67. if (type.equals("Standard"))
  68. cena = 10;
  69. else if (type.equals("Pepperoni"))
  70. cena = 12;
  71. else if (type.equals("Vegetarian"))
  72. cena = 8;
  73. } else
  74. throw new InvalidPizzaTypeException();
  75. }
  76.  
  77. }
  78.  
  79. class Order {
  80. List<Item> Nitem;
  81. List<Integer> counts;
  82.  
  83. public Order() {
  84. Nitem = new ArrayList<Item>();
  85. counts = new ArrayList<Integer>();
  86. }
  87.  
  88. void addItem(Item item, int count) throws ItemOutOfStockException {
  89. if (count <= 10) {
  90. int index = 0;
  91. boolean flag = false;
  92. for (int i = 0; i < Nitem.size(); i++) {
  93. if (Nitem.get(i).type.equals(item.type) == true) {
  94. flag = true;
  95. index = i;
  96. }
  97. }
  98. if (flag == true) {
  99. Nitem.set(index, item);
  100. counts.set(index, count);
  101. } else {
  102. Nitem.add(item);
  103. counts.add(count);
  104. }
  105. } else
  106. throw new ItemOutOfStockException(item);
  107.  
  108. }
  109.  
  110. public int getPrice() {
  111. int sum = 0;
  112. for (int i = 0; i < Nitem.size(); i++) {
  113. if (Nitem.get(i).type.equals("Coke"))
  114. sum += Nitem.get(i).getPrice() * counts.get(i);
  115. else if (Nitem.get(i).type.equals("Pepperoni"))
  116. sum += Nitem.get(i).getPrice() * counts.get(i);
  117. else if (Nitem.get(i).type.equals("Vegetarian"))
  118. sum += Nitem.get(i).getPrice() * counts.get(i);
  119. else if (Nitem.get(i).type.equals("Standard"))
  120. sum += Nitem.get(i).getPrice() * counts.get(i);
  121. else if (Nitem.get(i).type.equals("Ketchup"))
  122. sum += Nitem.get(i).getPrice() * counts.get(i);
  123.  
  124. }
  125. return sum;
  126. }
  127.  
  128. public void displayOrder() {
  129. int x = 1;
  130. for (int i = 0; i < Nitem.size(); i++) {
  131. System.out.format("%d.%-15sx%-3d%d$\n", x, Nitem.get(i).type,
  132. counts.get(i), (Nitem.get(i).cena * counts.get(i)));
  133. x++;
  134. }
  135. String str="Total";
  136. System.out.format("%-20s%d$\n",str, getPrice());
  137.  
  138. }
  139.  
  140. public void removeItem(int idx) throws Exception {
  141. if (idx >= 0&&idx < Nitem.size()) {
  142. Nitem.remove(idx);
  143. counts.remove(idx);
  144.  
  145. } else
  146. throw new ArrayIndexOutOfBоundsException(idx);
  147.  
  148. }
  149.  
  150. public void lock() {
  151. System.out.println("Treba da se implementira");
  152. }
  153. }
  154.  
  155.  
  156. public class PizzaOrderTest {
  157.  
  158. public static void main(String[] args) {
  159. Scanner jin = new Scanner(System.in);
  160. int k = jin.nextInt();
  161. if ( k == 0 ) { //test Item
  162. try {
  163. String type = jin.next();
  164. String name = jin.next();
  165. Item item = null;
  166. if ( type.equals("Pizza") ) item = new PizzaItem(name);
  167. else item = new ExtraItem(name);
  168. System.out.println(item.getPrice());
  169. } catch (Exception e) {
  170. System.out.println(e.getClass().getSimpleName());
  171. }
  172. }
  173. if ( k == 1 ) { // test simple order
  174. Order order = new Order();
  175. while ( true ) {
  176. try {
  177. String type = jin.next();
  178. String name = jin.next();
  179. Item item = null;
  180. if ( type.equals("Pizza") ) item = new PizzaItem(name);
  181. else item = new ExtraItem(name);
  182. if ( !jin.hasNextInt() ) break;
  183. order.addItem(item, jin.nextInt());
  184. } catch (Exception e) {
  185. System.out.println(e.getClass().getSimpleName());
  186. }
  187. }
  188. jin.next();
  189. System.out.println(order.getPrice());
  190. order.displayOrder();
  191. while ( true ) {
  192. try {
  193. String type = jin.next();
  194. String name = jin.next();
  195. Item item = null;
  196. if ( type.equals("Pizza") ) item = new PizzaItem(name);
  197. else item = new ExtraItem(name);
  198. if ( ! jin.hasNextInt() ) break;
  199. order.addItem(item, jin.nextInt());
  200. } catch (Exception e) {
  201. System.out.println(e.getClass().getSimpleName());
  202. }
  203. }
  204. System.out.println(order.getPrice());
  205. order.displayOrder();
  206. }
  207. if ( k == 2 ) { // test order with removing
  208. Order order = new Order();
  209. while ( true ) {
  210.  
  211. try {
  212. String type = jin.next();
  213. String name = jin.next();
  214. Item item = null;
  215. if ( type.equals("Pizza") ) item = new PizzaItem(name);
  216. else item = new ExtraItem(name);
  217. if ( !jin.hasNextInt() ) break;
  218. order.addItem(item, jin.nextInt());
  219. } catch (Exception e) {
  220. System.out.println(e.getClass().getSimpleName());
  221. }
  222. }
  223. jin.next();
  224. System.out.println(order.getPrice());
  225. order.displayOrder();
  226. while ( jin.hasNextInt() ) {
  227. try {
  228. int idx = jin.nextInt();
  229. order.removeItem(idx);
  230. } catch (Exception e) {
  231. System.out.println(e.getClass().getSimpleName());
  232. }
  233. }
  234. System.out.println(order.getPrice());
  235. order.displayOrder();
  236. }
  237. if ( k == 3 ) { //test locking & exceptions
  238. Order order = new Order();
  239. try {
  240. order.lock();
  241. } catch (Exception e) {
  242. System.out.println(e.getClass().getSimpleName());
  243. }
  244. try {
  245. order.addItem(new ExtraItem("Coke"), 1);
  246. } catch (Exception e) {
  247. System.out.println(e.getClass().getSimpleName());
  248. }
  249. try {
  250. order.lock();
  251. } catch (Exception e) {
  252. System.out.println(e.getClass().getSimpleName());
  253. }
  254. try {
  255. order.addItem(new PizzaItem("Standard"), 1);
  256. } catch (Exception e) {
  257. System.out.println(e.getClass().getSimpleName());
  258. }
  259. try {
  260. order.lock();
  261. } catch (Exception e) {
  262. System.out.println(e.getClass().getSimpleName());
  263. }
  264. try {
  265. order.addItem(new PizzaItem("Standard"), 1);
  266. } catch (Exception e) {
  267. System.out.println(e.getClass().getSimpleName());
  268. }
  269. try {
  270. order.removeItem(0);
  271. } catch (Exception e) {
  272. System.out.println(e.getClass().getSimpleName());
  273. }
  274. }
  275. }
  276.  
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement