Advertisement
Dimoo23

[НП] Систем за банкарско работење 10/10 clean code

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