Advertisement
SashkoKlincharov

[Java][НП] - Мој ДДВ 1

Aug 27th, 2021
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.65 KB | None | 0 0
  1.  
  2.  
  3. Да се имплементира класа MojDDV која што од влезен тек ќе чита информации за скенирани фискални сметки од страна на еден корисник на истоимената апликација. Податоците за фискалните сметки се во следниот формат:
  4.  
  5. ID item_price1 item_tax_type1 item_price2 item_tax_type2 … item_price-n item_tax_type-n
  6.  
  7. На пример: 12334 1789 А 1238 B 1222 V 111 V
  8.  
  9. Постојат три типа на данок на додадена вредност и тоа:
  10.  
  11. А (18% од вредноста)
  12. B (5% од вредноста)
  13. V (0% од вредноста)
  14.  
  15. Повратокот на ДДВ изнесува 15% од данокот на додадената вредност за артикалот.
  16.  
  17. Да се имплементираат методите:
  18.  
  19. void readRecords (InputStream inputStream) - метод којшто ги чита од влезен тек податоците за фискалните сметки. Доколку е скенирана фискална сметка со износ поголем од 30000 денари потребно е да се фрли исклучок од тип AmountNotAllowedException. Дефинирајте каде ќе се фрла исклучокот, и каде ќе биде фатен, на начин што оваа функција, ќе може да ги прочита сите фискални коишто се скенирани. Исклучокот треба да испечати порака “Receipt with amount [сума на сите артикли] is not allowed to be scanned”.
  20. void printTaxReturns (OutputStream outputStream) - метод којшто на излезен тек ги печати сите скенирани фискални сметки во формат “ID SUM_OF_AMOUNTS TAX_RETURN”, каде што SUM_OF_AMOUNTS e збирот на сите артикли во фискалната сметка, а TAX_RETURN е пресметаниот повраток на ДДВ за таа фискална сметка.
  21.  
  22.  
  23. import java.io.*;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26.  
  27. class AmmountNotAllowedException extends Exception {
  28. public AmmountNotAllowedException(int val) {
  29. super("Receipt with amount " + val +" is not allowed to be scanned");
  30. }
  31. }
  32.  
  33. abstract class Itemm {
  34. private int price;
  35.  
  36. public Itemm(int price){
  37. this.price = price;
  38. }
  39.  
  40. public static Itemm createItemByType(int price, String type) {
  41. switch(type){
  42. case "A":
  43. return new ItemA(price);
  44. case "B":
  45. return new ItemB(price);
  46. case "V":
  47. return new ItemV(price);
  48. default:
  49. return null;
  50. }
  51. }
  52.  
  53. public int getPrice() {
  54. return price;
  55. }
  56.  
  57. public void setPrice(int price) {
  58. this.price = price;
  59. }
  60.  
  61. abstract double getDanokByType();
  62.  
  63. double getDDV(){
  64. return getDanokByType()*0.15;
  65. }
  66. }
  67.  
  68. class ItemA extends Itemm {
  69.  
  70. public ItemA(int price) {
  71. super(price);
  72. }
  73.  
  74. @Override
  75. double getDanokByType() {
  76. return this.getPrice()*0.18;
  77. }
  78.  
  79. @Override
  80. public String toString() {
  81. return super.toString();
  82. }
  83. }
  84.  
  85. class ItemB extends Itemm {
  86.  
  87. public ItemB(int price) {
  88. super(price);
  89. }
  90.  
  91. @Override
  92. double getDanokByType() {
  93. return this.getPrice()*0.05;
  94. }
  95.  
  96. @Override
  97. public String toString() {
  98. return super.toString();
  99. }
  100. }
  101.  
  102. class ItemV extends Itemm {
  103.  
  104. public ItemV(int price) {
  105. super(price);
  106. }
  107.  
  108. @Override
  109. double getDanokByType() {
  110. return 0;
  111. }
  112.  
  113. @Override
  114. public String toString() {
  115. return super.toString();
  116. }
  117. }
  118.  
  119. class Receipt {
  120. private int ID;
  121. private List<Itemm> itemList;
  122.  
  123. public Receipt(int ID, List<Itemm> itemList) {
  124. this.ID = ID;
  125. this.itemList = itemList;
  126. }
  127.  
  128. public int getID() {
  129. return ID;
  130. }
  131.  
  132. public int SUM_OF_ALL_AMOUNTS(){
  133. return itemList.stream().mapToInt(Itemm::getPrice).sum();
  134. }
  135.  
  136. public double GET_TAX_RETURN(){
  137. return itemList.stream().mapToDouble(Itemm::getDDV).sum();
  138. }
  139.  
  140. public static Receipt createNewReceipt(String line) throws AmmountNotAllowedException {
  141. String [] parts = line.split("\\s+");
  142. int ID = Integer.parseInt(parts[0]);
  143. List<Itemm> pom = new ArrayList<>();
  144. for (int i = 1; i < parts.length; i+=2) {
  145. Itemm item = Itemm.createItemByType(Integer.parseInt(parts[i]),parts[i+1]);
  146. pom.add(item);
  147. }
  148. Receipt receipt = new Receipt(ID,pom);
  149. if(receipt.SUM_OF_ALL_AMOUNTS()>30000)
  150. throw new AmmountNotAllowedException(receipt.SUM_OF_ALL_AMOUNTS());
  151. return receipt;
  152. }
  153. }
  154.  
  155. class MojDDV {
  156. private List<Receipt> receipts;
  157.  
  158. public MojDDV(){
  159. receipts = new ArrayList<>();
  160. }
  161.  
  162. void readRecords (InputStream inputStream) throws IOException {
  163. BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
  164. String line = null;
  165. while((line = br.readLine())!=null){
  166. try {
  167. receipts.add(Receipt.createNewReceipt(line));
  168. }catch (AmmountNotAllowedException exx){
  169. System.out.println(exx.getMessage());
  170. }
  171. }
  172. }
  173.  
  174. void printTaxReturns(OutputStream outputStream){
  175. PrintWriter pw = new PrintWriter(new OutputStreamWriter(outputStream),true);
  176. for(Receipt r : receipts){
  177. pw.println(String.format("%d %d %.2f",r.getID(),r.SUM_OF_ALL_AMOUNTS(),r.GET_TAX_RETURN()));
  178. }
  179. }
  180. }
  181.  
  182. public class MojDDVTest {
  183.  
  184. public static void main(String[] args) throws IOException {
  185.  
  186. MojDDV mojDDV = new MojDDV();
  187.  
  188. System.out.println("===READING RECORDS FROM INPUT STREAM===");
  189. mojDDV.readRecords(System.in);
  190.  
  191. System.out.println("===PRINTING TAX RETURNS RECORDS TO OUTPUT STREAM ===");
  192. mojDDV.printTaxReturns(System.out);
  193.  
  194. }
  195. }
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203. Sample input
  204.  
  205. 70876 1585 B 1585 V 247 V 1391 B 1391 V 1649 B 1649 V 548 B 548 V 640 B 640 V 1309 B 1309 V 1486 V 2093 V 106 V 2001 V 361 V
  206. 198710 1306 A 1306 B 1306 V 432 V 1222 V 1851 V 390 V 111 A 111 B 111 V 991 V 1611 A 1611 B 1611 V
  207. 140819 709 A 709 B 709 V 1628 A 1628 B 1628 V 680 V
  208. 584886 411 A 411 B 411 V 699 B 699 V 1571 V 1307 B 1307 V
  209. 588253 1528 B 1528 V 1744 B 1744 V 1033 V 412 B 412 V 1221 A 1221 B 1221 V 328 A 328 B 328 V 1301 A 1301 B 1301 V 1778 A 1778 B 1778 V 1651 A 1651 B 1651 V 1937 V 1521 V 2068 B 2068 V
  210. 970452 1703 V 1796 B 1796 V 1221 V 885 A 885 B 885 V 183 V 788 B 788 V 1753 B 1753 V 456 V 926 V 1898 V 410 B 410 V 824 B 824 V
  211. 51307 2002 B 2002 V 1776 V 2097 B 2097 V 1128 A 1128 B 1128 V 151 A 151 B 151 V
  212. 570505 1090 A 1090 B 1090 V 1121 B 1121 V 971 B 971 V 1260 A 1260 B 1260 V 443 V
  213. 987793 1436 V 1648 B 1648 V 1197 V 992 B 992 V 528 A 528 B 528 V 739 A 739 B 739 V 750 B 750 V
  214. 752690 1626 B 1626 V 1785 A 1785 B 1785 V 1938 V 1733 V 1137 B 1137 V 1832 A 1832 B 1832 V
  215.  
  216. Sample output
  217.  
  218. ===READING RECORDS FROM INPUT STREAM===
  219. Receipt with amount 34832 is not allowed to be scanned
  220. ===PRINTING TAX RETURNS RECORDS TO OUTPUT STREAM ===
  221. 70876 20538 53.42
  222. 198710 13970 104.47
  223. 140819 7691 80.63
  224. 584886 6816 29.22
  225. 970452 20184 72.32
  226. 51307 13811 74.87
  227. 570505 11677 96.76
  228. 987793 13214 69.14
  229. 752690 20048 145.51
  230.  
  231.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement