Advertisement
Guest User

kontakt

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