Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- class Account{
- private String name;
- private long id;
- private String balance;
- public Account(String name, String balance){
- this.name = name;
- this.balance = balance;
- Random r = new Random();
- this.id = r.nextLong();
- }
- public Account(){
- this.name = "";
- this.balance = "0.00$";
- Random r = new Random();
- this.id = r.nextLong();
- }
- public Account(Account a){
- this.name = a.getName();
- this.id = a.getId();
- this.balance = a.getBalance();
- }
- public String getBalance() {
- return this.balance;
- }
- public String getName() {
- return this.name;
- }
- public long getId() {
- return this.id;
- }
- public void setBalance(String balance) {
- this.balance = balance;
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append("Name: ").append(this.name).append(System.lineSeparator());
- sb.append("Balance: ").append(this.balance).append(System.lineSeparator());
- return sb.toString();
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((balance == null) ? 0 : balance.hashCode());
- result = prime * result + (int) (id ^ (id >>> 32));
- result = prime * result + ((name == null) ? 0 : name.hashCode());
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- Account other = (Account) obj;
- if (balance == null) {
- if (other.balance != null)
- return false;
- } else if (!balance.equals(other.balance))
- return false;
- if (id != other.id)
- return false;
- if (name == null) {
- if (other.name != null)
- return false;
- } else if (!name.equals(other.name))
- return false;
- return true;
- }
- }
- abstract class Transaction{
- private long fromId;
- private long toId;
- private String description;
- private String amount;
- public Transaction(long fromId, long toId, String desc, String amount){
- this.fromId = fromId;
- this.toId = toId;
- this.description = desc;
- this.amount = amount;
- }
- public Transaction(){
- this.fromId = 0;
- this.toId = 0;
- this.description = null;
- this.amount = null;
- }
- public String getAmount() {
- return this.amount;
- }
- public long getFromId() {
- return this.fromId;
- }
- public long getToId() {
- return this.toId;
- }
- public abstract double getProvision();
- public String getDescription() {
- return this.description;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((amount == null) ? 0 : amount.hashCode());
- result = prime * result + ((description == null) ? 0 : description.hashCode());
- result = prime * result + (int) (fromId ^ (fromId >>> 32));
- result = prime * result + (int) (toId ^ (toId >>> 32));
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- Transaction other = (Transaction) obj;
- if (amount == null) {
- if (other.amount != null)
- return false;
- } else if (!amount.equals(other.amount))
- return false;
- if (description == null) {
- if (other.description != null)
- return false;
- } else if (!description.equals(other.description))
- return false;
- if (fromId != other.fromId)
- return false;
- if (toId != other.toId)
- return false;
- return true;
- }
- }
- class FlatAmountProvisionTransaction extends Transaction{
- private String flatProvision;
- public FlatAmountProvisionTransaction(long fromId, long toId, String amount, String flatProvision) {
- // TODO Auto-generated constructor stub
- super(fromId, toId, "FlatAmount", amount);
- this.flatProvision = flatProvision;
- }
- public String getFlatAmount() {
- return this.flatProvision;
- }
- @Override
- public double getProvision() {
- return Bank.pretvoriVoDouble(this.flatProvision);
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = super.hashCode();
- result = prime * result + ((flatProvision == null) ? 0 : flatProvision.hashCode());
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (!super.equals(obj))
- return false;
- if (getClass() != obj.getClass())
- return false;
- FlatAmountProvisionTransaction other = (FlatAmountProvisionTransaction) obj;
- if (flatProvision == null) {
- if (other.flatProvision != null)
- return false;
- } else if (!flatProvision.equals(other.flatProvision))
- return false;
- return true;
- }
- }
- class FlatPercentProvisionTransaction extends Transaction{
- private int centsPerDollar;
- public FlatPercentProvisionTransaction(long fromId, long toId, String amount, int centsPerDollar) {
- // TODO Auto-generated constructor stub
- super(fromId, toId, "FlatPercent", amount);
- this.centsPerDollar = centsPerDollar;
- }
- public int getPercent() {
- return this.centsPerDollar;
- }
- @Override
- public double getProvision() {
- return (int)Bank.pretvoriVoDouble(this.getAmount()) * this.getPercent() / 100.0;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = super.hashCode();
- result = prime * result + centsPerDollar;
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (!super.equals(obj))
- return false;
- if (getClass() != obj.getClass())
- return false;
- FlatPercentProvisionTransaction other = (FlatPercentProvisionTransaction) obj;
- if (centsPerDollar != other.centsPerDollar)
- return false;
- return true;
- }
- }
- class Bank{
- private Account [] accounts;
- private String bankName;
- private String totalTransfers;
- private String totalProvision;
- public Bank(String bankName, Account accounts[]){
- this.bankName = bankName;
- this.accounts = new Account [accounts.length];
- for(int i = 0; i < accounts.length; ++i) {
- this.accounts[i] = new Account(accounts[i]);
- }
- this.totalProvision = "0.00$";
- this.totalTransfers = "0.00$";
- }
- public boolean makeTransaction(Transaction t) {
- if (this.daliPostojat(t.getFromId(), t.getToId()) && this.dovolnoSredstva(t)) {
- double amount = Bank.pretvoriVoDouble(t.getAmount());
- double provision = t.getProvision();
- double vkupno = amount + provision;
- Account fromAccount = this.getAccount(t.getFromId());
- Account toAccount = this.getAccount(t.getToId());
- fromAccount.setBalance(Bank.pretvoriVoString(Bank.pretvoriVoDouble(fromAccount.getBalance()) - vkupno ));
- toAccount.setBalance(Bank.pretvoriVoString(Bank.pretvoriVoDouble(toAccount.getBalance()) + amount));
- this.totalProvision = Bank.pretvoriVoString(Bank.pretvoriVoDouble(this.totalProvision) + provision);
- this.totalTransfers = Bank.pretvoriVoString(Bank.pretvoriVoDouble(this.totalTransfers) + amount);
- return true;
- }
- return false;
- }
- public boolean daliPostojat(long fromId, long toId) {
- return this.getAccount(fromId) != null && this.getAccount(toId) != null;
- }
- public boolean dovolnoSredstva(Transaction t) {
- double amount = Bank.pretvoriVoDouble(t.getAmount());
- double provizija = t.getProvision();
- double vkupno = amount + provizija;
- Account fromAccount = this.getAccount(t.getFromId());
- if(Bank.pretvoriVoDouble(fromAccount.getBalance()) >= vkupno)
- return true;
- else
- return false;
- }
- public Account [] getAccounts() {
- return this.accounts;
- }
- public Account getAccount(long id) {
- for(int i = 0; i < accounts.length; ++i) {
- if(accounts[i].getId() == id)
- return accounts[i];
- }
- return null;
- }
- public static String pretvoriVoString(double amount) {
- String str = String.format("%.2f", amount);
- str += "$";
- return str;
- }
- public static double pretvoriVoDouble(String amount) {
- return Double.parseDouble(amount.substring(0, amount.length()-1));
- }
- public String totalTransfers() {
- return this.totalTransfers;
- }
- public String totalProvision() {
- return this.totalProvision;
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append("Name: ").append(this.bankName).append(System.lineSeparator()).append(System.lineSeparator());
- for(int i = 0; i < accounts.length; ++i) {
- sb.append(accounts[i].toString());
- }
- return sb.toString();
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + Arrays.hashCode(accounts);
- result = prime * result + ((bankName == null) ? 0 : bankName.hashCode());
- result = prime * result + ((totalProvision == null) ? 0 : totalProvision.hashCode());
- result = prime * result + ((totalTransfers == null) ? 0 : totalTransfers.hashCode());
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- Bank other = (Bank) obj;
- if (!Arrays.equals(accounts, other.accounts))
- return false;
- if (bankName == null) {
- if (other.bankName != null)
- return false;
- } else if (!bankName.equals(other.bankName))
- return false;
- if (totalProvision == null) {
- if (other.totalProvision != null)
- return false;
- } else if (!totalProvision.equals(other.totalProvision))
- return false;
- if (totalTransfers == null) {
- if (other.totalTransfers != null)
- return false;
- } else if (!totalTransfers.equals(other.totalTransfers))
- return false;
- return true;
- }
- }
- public class BankTester {
- public static void main(String[] args) {
- Scanner jin = new Scanner(System.in);
- String test_type = jin.nextLine();
- switch (test_type) {
- case "typical_usage":
- testTypicalUsage(jin);
- break;
- case "equals":
- testEquals();
- break;
- }
- jin.close();
- }
- private static void testEquals() {
- Account a1 = new Account("Andrej", "20.00$");
- Account a2 = new Account("Andrej", "20.00$");
- Account a3 = new Account("Andrej", "30.00$");
- Account a4 = new Account("Gajduk", "20.00$");
- List<Account> all = Arrays.asList(a1, a2, a3, a4);
- if (!(a1.equals(a1)&&!a1.equals(a2)&&!a2.equals(a1) && !a3.equals(a1)
- && !a4.equals(a1)
- && !a1.equals(null))) {
- System.out.println("Your account equals method does not work properly.");
- return;
- }
- Set<Long> ids = all.stream().map(Account::getId).collect(Collectors.toSet());
- if (ids.size() != all.size()) {
- System.out.println("Different accounts have the same IDS. This is not allowed");
- return;
- }
- FlatAmountProvisionTransaction fa1 = new FlatAmountProvisionTransaction(10, 20, "20.00$", "10.00$");
- FlatAmountProvisionTransaction fa2 = new FlatAmountProvisionTransaction(20, 20, "20.00$", "10.00$");
- FlatAmountProvisionTransaction fa3 = new FlatAmountProvisionTransaction(20, 10, "20.00$", "10.00$");
- FlatAmountProvisionTransaction fa4 = new FlatAmountProvisionTransaction(10, 20, "50.00$", "50.00$");
- FlatAmountProvisionTransaction fa5 = new FlatAmountProvisionTransaction(30, 40, "20.00$", "10.00$");
- FlatPercentProvisionTransaction fp1 = new FlatPercentProvisionTransaction(10, 20, "20.00$", 10);
- FlatPercentProvisionTransaction fp2 = new FlatPercentProvisionTransaction(10, 20, "20.00$", 10);
- FlatPercentProvisionTransaction fp3 = new FlatPercentProvisionTransaction(10, 10, "20.00$", 10);
- FlatPercentProvisionTransaction fp4 = new FlatPercentProvisionTransaction(10, 20, "50.00$", 10);
- FlatPercentProvisionTransaction fp5 = new FlatPercentProvisionTransaction(10, 20, "20.00$", 30);
- FlatPercentProvisionTransaction fp6 = new FlatPercentProvisionTransaction(30, 40, "20.00$", 10);
- if (fa1.equals(fa1) &&
- !fa2.equals(null) &&
- fa2.equals(fa1) &&
- fa1.equals(fa2) &&
- fa1.equals(fa3) &&
- !fa1.equals(fa4) &&
- !fa1.equals(fa5) &&
- !fa1.equals(fp1) &&
- fp1.equals(fp1) &&
- !fp2.equals(null) &&
- fp2.equals(fp1) &&
- fp1.equals(fp2) &&
- fp1.equals(fp3) &&
- !fp1.equals(fp4) &&
- !fp1.equals(fp5) &&
- !fp1.equals(fp6)) {
- System.out.println("Your transactions equals methods do not work properly.");
- return;
- }
- Account accounts[] = new Account[]{a1, a2, a3, a4};
- Account accounts1[] = new Account[]{a2, a1, a3, a4};
- Account accounts2[] = new Account[]{a1, a2, a3};
- Account accounts3[] = new Account[]{a1, a2, a3, a4};
- Bank b1 = new Bank("Test", accounts);
- Bank b2 = new Bank("Test", accounts1);
- Bank b3 = new Bank("Test", accounts2);
- Bank b4 = new Bank("Sample", accounts);
- Bank b5 = new Bank("Test", accounts3);
- if (!(b1.equals(b1) &&
- !b1.equals(null) &&
- !b1.equals(b2) &&
- !b2.equals(b1) &&
- !b1.equals(b3) &&
- !b3.equals(b1) &&
- !b1.equals(b4) &&
- b1.equals(b5))) {
- System.out.println("Your bank equals method do not work properly.");
- return;
- }
- accounts[2] = a1;
- if (!b1.equals(b5)) {
- System.out.println("Your bank equals method do not work properly.");
- return;
- }
- long from_id = a2.getId();
- long to_id = a3.getId();
- Transaction t = new FlatAmountProvisionTransaction(from_id, to_id, "3.00$", "3.00$");
- b1.makeTransaction(t);
- if (b1.equals(b5)) {
- System.out.println("Your bank equals method do not work properly.");
- return;
- }
- b5.makeTransaction(t);
- if (!b1.equals(b5)) {
- System.out.println("Your bank equals method do not work properly.");
- return;
- }
- System.out.println("All your equals methods work properly.");
- }
- private static void testTypicalUsage(Scanner jin) {
- String bank_name = jin.nextLine();
- int num_accounts = jin.nextInt();
- jin.nextLine();
- Account accounts[] = new Account[num_accounts];
- for (int i = 0; i < num_accounts; ++i)
- accounts[i] = new Account(jin.nextLine(), jin.nextLine());
- Bank bank = new Bank(bank_name, accounts);
- while (true) {
- String line = jin.nextLine();
- switch (line) {
- case "stop":
- return;
- case "transaction":
- String descrption = jin.nextLine();
- String amount = jin.nextLine();
- String parameter = jin.nextLine();
- int from_idx = jin.nextInt();
- int to_idx = jin.nextInt();
- jin.nextLine();
- Transaction t = getTransaction(descrption, from_idx, to_idx, amount, parameter, bank);
- System.out.println("Transaction amount: " + t.getAmount());
- System.out.println("Transaction description: " + t.getDescription());
- System.out.println("Transaction successful? " + bank.makeTransaction(t));
- break;
- case "print":
- System.out.println(bank.toString());
- System.out.println("Total provisions: " + bank.totalProvision());
- System.out.println("Total transfers: " + bank.totalTransfers());
- System.out.println();
- break;
- }
- }
- }
- private static Transaction getTransaction(String description, int from_idx, int to_idx, String amount, String o, Bank bank) {
- switch (description) {
- case "FlatAmount":
- return new FlatAmountProvisionTransaction(bank.getAccounts()[from_idx].getId(),
- bank.getAccounts()[to_idx].getId(), amount, o);
- case "FlatPercent":
- return new FlatPercentProvisionTransaction(bank.getAccounts()[from_idx].getId(),
- bank.getAccounts()[to_idx].getId(), amount, Integer.parseInt(o));
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement