Advertisement
Filip_Markoski

[NP] 1.2 Систем за банкарско работење (Solved)

Oct 15th, 2017
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.95 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3. import java.util.Arrays;
  4.  
  5. public class BankTester {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner jin = new Scanner(System.in);
  9.         String test_type = jin.nextLine();
  10.         switch (test_type) {
  11.             case "typical_usage":
  12.                 testTypicalUsage(jin);
  13.                 break;
  14.             case "equals":
  15.                 testEquals();
  16.                 break;
  17.         }
  18.         jin.close();
  19.     }
  20.  
  21.     private static void testEquals() {
  22.         Account a1 = new Account("Andrej", "20.00$");
  23.         Account a2 = new Account("Andrej", "20.00$");
  24.         Account a3 = new Account("Andrej", "30.00$");
  25.         Account a4 = new Account("Gajduk", "20.00$");
  26.         List<Account> all = Arrays.asList(a1, a2, a3, a4);
  27.         if (!(a1.equals(a1) && !a1.equals(a2) && !a2.equals(a1) && !a3.equals(a1) && !a4.equals(a1)
  28.                 && !a1.equals(null))) {
  29.             System.out.println("Your account equals method does not work properly.");
  30.             return;
  31.         }
  32.         Set<Long> ids = all.stream().map(Account::getId).collect(Collectors.toSet());
  33.         if (ids.size() != all.size()) {
  34.             System.out.println("Different accounts have the same IDS. This is not allowed");
  35.             return;
  36.         }
  37.         FlatAmountProvisionTransaction fa1 = new FlatAmountProvisionTransaction(10, 20, "20.00$", "10.00$");
  38.         FlatAmountProvisionTransaction fa2 = new FlatAmountProvisionTransaction(20, 20, "20.00$", "10.00$");
  39.         FlatAmountProvisionTransaction fa3 = new FlatAmountProvisionTransaction(20, 10, "20.00$", "10.00$");
  40.         FlatAmountProvisionTransaction fa4 = new FlatAmountProvisionTransaction(10, 20, "50.00$", "50.00$");
  41.         FlatAmountProvisionTransaction fa5 = new FlatAmountProvisionTransaction(30, 40, "20.00$", "10.00$");
  42.         FlatPercentProvisionTransaction fp1 = new FlatPercentProvisionTransaction(10, 20, "20.00$", 10);
  43.         FlatPercentProvisionTransaction fp2 = new FlatPercentProvisionTransaction(10, 20, "20.00$", 10);
  44.         FlatPercentProvisionTransaction fp3 = new FlatPercentProvisionTransaction(10, 10, "20.00$", 10);
  45.         FlatPercentProvisionTransaction fp4 = new FlatPercentProvisionTransaction(10, 20, "50.00$", 10);
  46.         FlatPercentProvisionTransaction fp5 = new FlatPercentProvisionTransaction(10, 20, "20.00$", 30);
  47.         FlatPercentProvisionTransaction fp6 = new FlatPercentProvisionTransaction(30, 40, "20.00$", 10);
  48.         if (fa1.equals(fa1) &&
  49.                 !fa2.equals(null) &&
  50.                 fa2.equals(fa1) &&
  51.                 fa1.equals(fa2) &&
  52.                 fa1.equals(fa3) &&
  53.                 !fa1.equals(fa4) &&
  54.                 !fa1.equals(fa5) &&
  55.                 !fa1.equals(fp1) &&
  56.                 fp1.equals(fp1) &&
  57.                 !fp2.equals(null) &&
  58.                 fp2.equals(fp1) &&
  59.                 fp1.equals(fp2) &&
  60.                 fp1.equals(fp3) &&
  61.                 !fp1.equals(fp4) &&
  62.                 !fp1.equals(fp5) &&
  63.                 !fp1.equals(fp6)) {
  64.             System.out.println("Your transactions equals methods do not work properly.");
  65.             return;
  66.         }
  67.         Account accounts[] = new Account[]{a1, a2, a3, a4};
  68.         Account accounts1[] = new Account[]{a2, a1, a3, a4};
  69.         Account accounts2[] = new Account[]{a1, a2, a3};
  70.         Account accounts3[] = new Account[]{a1, a2, a3, a4};
  71.  
  72.         Bank b1 = new Bank("Test", accounts);
  73.         Bank b2 = new Bank("Test", accounts1);
  74.         Bank b3 = new Bank("Test", accounts2);
  75.         Bank b4 = new Bank("Sample", accounts);
  76.         Bank b5 = new Bank("Test", accounts3);
  77.  
  78.         if (!(b1.equals(b1) &&
  79.                 !b1.equals(null) &&
  80.                 !b1.equals(b2) &&
  81.                 !b2.equals(b1) &&
  82.                 !b1.equals(b3) &&
  83.                 !b3.equals(b1) &&
  84.                 !b1.equals(b4) &&
  85.                 b1.equals(b5))) {
  86.             System.out.println("Your bank equals method do not work properly.");
  87.             return;
  88.         }
  89.         accounts[2] = a1;
  90.         if (!b1.equals(b5)) {
  91.             System.out.println("Your bank equals method do not work properly.");
  92.             return;
  93.         }
  94.         long from_id = a2.getId();
  95.         long to_id = a3.getId();
  96.         Transaction t = new FlatAmountProvisionTransaction(from_id, to_id, "3.00$", "3.00$");
  97.         b1.makeTransaction(t);
  98.         if (b1.equals(b5)) {
  99.             System.out.println("Your bank equals method do not work properly.");
  100.             return;
  101.         }
  102.         b5.makeTransaction(t);
  103.         if (!b1.equals(b5)) {
  104.             System.out.println("Your bank equals method do not work properly.");
  105.             return;
  106.         }
  107.         System.out.println("All your equals methods work properly.");
  108.     }
  109.  
  110.     private static void testTypicalUsage(Scanner jin) {
  111.         String bank_name = jin.nextLine();
  112.         int num_accounts = jin.nextInt();
  113.         jin.nextLine();
  114.         Account accounts[] = new Account[num_accounts];
  115.         for (int i = 0; i < num_accounts; ++i)
  116.             accounts[i] = new Account(jin.nextLine(), jin.nextLine());
  117.         Bank bank = new Bank(bank_name, accounts);
  118.         while (true) {
  119.             String line = jin.nextLine();
  120.             switch (line) {
  121.                 case "stop":
  122.                     return;
  123.                 case "transaction":
  124.                     String descrption = jin.nextLine();
  125.                     String amount = jin.nextLine();
  126.                     String parameter = jin.nextLine();
  127.                     int from_idx = jin.nextInt();
  128.                     int to_idx = jin.nextInt();
  129.                     jin.nextLine();
  130.                     Transaction t = getTransaction(descrption, from_idx, to_idx, amount, parameter, bank);
  131.                     System.out.println("Transaction amount: " + t.getAmount());
  132.                     System.out.println("Transaction description: " + t.getDescription());
  133.                     System.out.println("Transaction successful? " + bank.makeTransaction(t));
  134.                     break;
  135.                 case "print":
  136.                     System.out.println(bank.toString());
  137.                     System.out.println("Total provisions: " + bank.totalProvision());
  138.                     System.out.println("Total transfers: " + bank.totalTransfers());
  139.                     System.out.println();
  140.                     break;
  141.             }
  142.         }
  143.     }
  144.  
  145.     private static Transaction getTransaction(String description, int from_idx, int to_idx, String amount, String o, Bank bank) {
  146.         switch (description) {
  147.             case "FlatAmount":
  148.                 return new FlatAmountProvisionTransaction(bank.getAccounts()[from_idx].getId(),
  149.                         bank.getAccounts()[to_idx].getId(), amount, o);
  150.             case "FlatPercent":
  151.                 return new FlatPercentProvisionTransaction(bank.getAccounts()[from_idx].getId(),
  152.                         bank.getAccounts()[to_idx].getId(), amount, Integer.parseInt(o));
  153.         }
  154.         return null;
  155.     }
  156. }
  157.  
  158. class Account {
  159.     private String name;
  160.     private String balance;
  161.     private long id;
  162.  
  163.     Account(String n, String b) {
  164.         Random rand = new Random();
  165.         this.name = n;
  166.         this.balance = b;
  167.         this.id = rand.nextLong();
  168.     }
  169.  
  170.     public String getName() {
  171.         return name;
  172.     }
  173.  
  174.     public void setName(String name) {
  175.         this.name = name;
  176.     }
  177.  
  178.     public String getBalance() {
  179.         return balance;
  180.     }
  181.  
  182.     public void setBalance(String balance) {
  183.         this.balance = balance;
  184.     }
  185.  
  186.     public long getId() {
  187.         return id;
  188.     }
  189.  
  190.     public void setId(long id) {
  191.         this.id = id;
  192.     }
  193.  
  194.     @Override
  195.     public String toString() {
  196.         // TODO Auto-generated method stub
  197.         return "Name: " + name + "\nBalance: " + balance + "\n";
  198.     }
  199.  
  200.     @Override
  201.     public int hashCode() {
  202.         final int prime = 31;
  203.         int result = 1;
  204.         result = prime * result + (name != null ? name.hashCode() : 0);
  205.         result = prime * result + (balance != null ? balance.hashCode() : 0);
  206.         result = prime * result + (int) (id ^ (id >>> 32));
  207.         return result;
  208.     }
  209.  
  210.     @Override
  211.     public boolean equals(Object obj) {
  212.         if (this == obj) {
  213.             return true;
  214.         }
  215.         if (obj == null) {
  216.             return false;
  217.         }
  218.         if (this.getClass() != obj.getClass()) {
  219.             return false;
  220.         }
  221.  
  222.         Account other = (Account) obj;
  223.         if (!name.equals(other.name)) {
  224.             return false;
  225.         }
  226.         if (!balance.equals(other.balance)) {
  227.             return false;
  228.         }
  229.         if (id != other.id) {
  230.             return false;
  231.         }
  232.         return true;
  233.     }
  234. }
  235.  
  236. abstract class Transaction {
  237.     protected final long fromId, toId;
  238.     protected final String description;
  239.     protected final String amount;
  240.  
  241.     public Transaction(long fromId, long toId, String description, String amount) {
  242.         this.fromId = fromId;
  243.         this.toId = toId;
  244.         this.description = description;
  245.         this.amount = amount;
  246.     }
  247.  
  248.     public String getAmount() {
  249.         return amount;
  250.     }
  251.  
  252.     public long getFromId() {
  253.         return fromId;
  254.     }
  255.  
  256.     public long getToId() {
  257.         return toId;
  258.     }
  259.  
  260.     public String getDescription() {
  261.         return description;
  262.     }
  263.  
  264.     public abstract double getProvision();
  265.  
  266.     public int hashcode() {
  267.         final int prime = 31;
  268.         int result = 1;
  269.         result = prime * result + (int) (fromId ^ (fromId >>> 32));
  270.         result = prime * result + (int) (toId ^ (toId >>> 32));
  271.         result = prime * result + (description != null ? description.hashCode() : 0);
  272.         result = prime * result + (amount != null ? amount.hashCode() : 0);
  273.         return result;
  274.     }
  275.  
  276.     @Override
  277.     public boolean equals(Object obj) {
  278.         if (this == obj) {
  279.             return true;
  280.         }
  281.         if (obj == null) {
  282.             return false;
  283.         }
  284.         if (getClass() != obj.getClass()) {
  285.             return false;
  286.         }
  287.  
  288.         Transaction other = (Transaction) obj;
  289.         if (fromId != other.fromId) {
  290.             return false;
  291.         }
  292.         if (toId != other.toId) {
  293.             return false;
  294.         }
  295.         if (!description.equals(other.description)) {
  296.             return false;
  297.         }
  298.         if (!amount.equals(other.amount)) {
  299.             return false;
  300.         }
  301.         return true;
  302.     }
  303.  
  304. }
  305.  
  306. class FlatAmountProvisionTransaction extends Transaction {
  307.     private String flatAmount;
  308.  
  309.     FlatAmountProvisionTransaction(long fromId, long toId, String amount, String flatProvision) {
  310.         super(fromId, toId, "FlatAmount", amount);
  311.         this.flatAmount = flatProvision;
  312.     }
  313.  
  314.     public String getFlatAmount() {
  315.         return amount;
  316.     }
  317.  
  318.     public String getFlatProvision() {
  319.         return flatAmount;
  320.     }
  321.  
  322.     public double getProvision() {
  323.         return Bank.getValue(flatAmount);
  324.     }
  325.  
  326.     @Override
  327.     public int hashCode() {
  328.         final int prime = 31;
  329.         int result = super.hashCode();
  330.         result = prime * result + (flatAmount != null ? flatAmount.hashCode() : 0);
  331.         return result;
  332.     }
  333.  
  334.     @Override
  335.     public boolean equals(Object obj) {
  336.         if (this == obj) {
  337.             return true;
  338.         }
  339.         if (obj == null) {
  340.             return false;
  341.         }
  342.         if (getClass() != obj.getClass()) {
  343.             return false;
  344.         }
  345.         if (!super.equals(obj)) {
  346.             return false;
  347.         }
  348.  
  349.         FlatAmountProvisionTransaction other = (FlatAmountProvisionTransaction) obj;
  350.         if (!flatAmount.equals(other.flatAmount)) {
  351.             return false;
  352.         }
  353.         return true;
  354.     }
  355. }
  356.  
  357. class FlatPercentProvisionTransaction extends Transaction {
  358.     private int centsPerDolar;
  359.  
  360.     FlatPercentProvisionTransaction(long fromId, long toId, String amount, int centsPerDolar) {
  361.         super(fromId, toId, "FlatPercent", amount);
  362.         this.centsPerDolar = centsPerDolar;
  363.     }
  364.  
  365.     public int getCentsPerDolar() {
  366.         return centsPerDolar;
  367.     }
  368.  
  369.     public double getProvision() {
  370.         double procent = (double) centsPerDolar / 100;
  371.         int cel = (int) Bank.getValue(super.getAmount());
  372.         return procent * cel;
  373.     }
  374.  
  375.     @Override
  376.     public int hashCode() {
  377.         final int prime = 31;
  378.         int result = super.hashCode();
  379.         result = prime * result + (int) centsPerDolar;
  380.         return result;
  381.     }
  382.  
  383.     @Override
  384.     public boolean equals(Object obj) {
  385.         if (this == obj) {
  386.             return true;
  387.         }
  388.         if (obj == null) {
  389.             return false;
  390.         }
  391.         if (getClass() != obj.getClass()) {
  392.             return false;
  393.         }
  394.         if (!super.equals(obj)) {
  395.             return false;
  396.         }
  397.  
  398.         FlatPercentProvisionTransaction other = (FlatPercentProvisionTransaction) obj;
  399.         if (centsPerDolar != other.centsPerDolar) {
  400.             return false;
  401.         }
  402.         return true;
  403.     }
  404.  
  405. }
  406.  
  407. class Bank {
  408.     private String name;
  409.     private Account accounts[];
  410.     private String totalTransfers;
  411.     private String totalProvision;
  412.  
  413.     public Bank(String name, Account[] accounts) {
  414.         super();
  415.         totalTransfers = "0.00$";
  416.         totalProvision = "0.00$";
  417.         this.name = name;
  418.         this.accounts = Arrays.copyOf(accounts, accounts.length);
  419.     }
  420.  
  421.     public String getName() {
  422.         return name;
  423.     }
  424.  
  425.     public String totalProvision() {
  426.         return totalProvision;
  427.     }
  428.  
  429.     public String totalTransfers() {
  430.         return totalTransfers;
  431.     }
  432.  
  433.     public Account[] getAccounts() {
  434.         return accounts;
  435.     }
  436.  
  437.     public static int toNumber(char c) {
  438.         return c - '0';
  439.     }
  440.  
  441.     public static double getValue(String str) {
  442.         String temp = str.substring(0, str.length() - 1);
  443.         double d = Double.parseDouble(temp);
  444.         return d;
  445.     }
  446.  
  447.     public static String getString(double value) {
  448.         String temp = String.format("%.2f", value);
  449.         temp += "$";
  450.         return temp;
  451.     }
  452.  
  453.     public boolean isInBank(long id) {
  454.         for (int i = 0; i < accounts.length; i++) {
  455.             if (accounts[i].getId() == id) {
  456.                 return true;
  457.             }
  458.         }
  459.         return false;
  460.     }
  461.  
  462.     public int getIndex(long id) {
  463.         for (int i = 0; i < accounts.length; i++) {
  464.             if (accounts[i].getId() == id) {
  465.                 return i;
  466.             }
  467.         }
  468.         return -1;
  469.     }
  470.  
  471.     public boolean makeTransaction(Transaction t) {
  472.         if (isInBank(t.getFromId()) && isInBank(t.getToId())) {
  473.             /* get the indexes of the two accounts in the bank */
  474.             int tempFrom = getIndex(t.getFromId());
  475.             int tempTo = getIndex(t.getToId());
  476.             double balanceUserFrom = getValue(accounts[tempFrom].getBalance());
  477.             double balanceUserTo = getValue(accounts[tempTo].getBalance());
  478.             double balanceTransaction = getValue(t.getAmount());
  479.             if (tempFrom == tempTo) {
  480.                 balanceUserFrom -= t.getProvision();
  481.                 accounts[tempFrom].setBalance(getString(balanceUserFrom));
  482.                 setTotalTransfers(balanceTransaction);
  483.                 setTotalProvision(t.getProvision());
  484.                 return true;
  485.             }
  486.             if (balanceUserFrom >= balanceTransaction + t.getProvision()) {
  487.                 double toPay = balanceTransaction + +t.getProvision();
  488.                 balanceUserFrom -= toPay;
  489.                 accounts[tempFrom].setBalance(getString(balanceUserFrom));
  490.                 balanceUserTo += balanceTransaction;
  491.                 accounts[tempTo].setBalance(getString(balanceUserTo));
  492.                 setTotalTransfers(balanceTransaction);
  493.                 setTotalProvision(t.getProvision());
  494.                 return true;
  495.             }
  496.         }
  497.         return false;
  498.     }
  499.  
  500.     private void setTotalProvision(double provision) {
  501.         double t = getValue(totalProvision);
  502.         t += provision;
  503.         totalProvision = getString(t);
  504.     }
  505.  
  506.     private void setTotalTransfers(double balanceTransaction) {
  507.         double t = getValue(totalTransfers);
  508.         t += balanceTransaction;
  509.         totalTransfers = getString(t);
  510.     }
  511.  
  512.     @Override
  513.     public String toString() {
  514.         StringBuffer sb = new StringBuffer("");
  515.         sb.append("Name: " + name + "\n\n");
  516.         for (Account account : accounts) {
  517.             sb.append(account.toString());
  518.         }
  519.         return sb.toString();
  520.     }
  521.  
  522.     @Override
  523.     public int hashCode() {
  524.         final int prime = 31;
  525.         final int[] result = {1};
  526.         result[0] = prime * result[0] + (name != null ? name.hashCode() : 0);
  527.         result[0] = prime * result[0] + (int) getValue(totalProvision);
  528.         result[0] = prime * result[0] + (int) getValue(totalTransfers);
  529.         Arrays.stream(accounts).forEach(account -> result[0] = prime * result[0] + (account != null ? account.hashCode() : 0));
  530.         return result[0];
  531.     }
  532.  
  533.     @Override
  534.     public boolean equals(Object obj) {
  535.         if (this == obj) {
  536.             return true;
  537.         }
  538.         if (obj == null) {
  539.             return false;
  540.         }
  541.         if (getClass() != obj.getClass()) {
  542.             return false;
  543.         }
  544.  
  545.         Bank other = (Bank) obj;
  546.  
  547.         if (name != other.name) {
  548.             return false;
  549.         }
  550.         if (accounts.length != other.accounts.length) {
  551.             return false;
  552.         }
  553.         for (int i = 0; i < accounts.length; i++) {
  554.             if (!accounts[i].equals(other.accounts[i])) {
  555.                 return false;
  556.             }
  557.         }
  558.         if (!totalTransfers.equals(other.totalTransfers)) {
  559.             return false;
  560.         }
  561.         if (!totalProvision.equals(other.totalProvision)) {
  562.             return false;
  563.         }
  564.         return true;
  565.     }
  566.  
  567. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement