Martina312

[НП] - Пицерија

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