Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- import java.util.Arrays;
- 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;
- }
- }
- class Account {
- private String name;
- private String balance;
- private long id;
- Account(String n, String b) {
- Random rand = new Random();
- this.name = n;
- this.balance = b;
- this.id = rand.nextLong();
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getBalance() {
- return balance;
- }
- public void setBalance(String balance) {
- this.balance = balance;
- }
- public long getId() {
- return id;
- }
- public void setId(long id) {
- this.id = id;
- }
- @Override
- public String toString() {
- // TODO Auto-generated method stub
- return "Name: " + name + "\nBalance: " + balance + "\n";
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + (name != null ? name.hashCode() : 0);
- result = prime * result + (balance != null ? balance.hashCode() : 0);
- result = prime * result + (int) (id ^ (id >>> 32));
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (this.getClass() != obj.getClass()) {
- return false;
- }
- Account other = (Account) obj;
- if (!name.equals(other.name)) {
- return false;
- }
- if (!balance.equals(other.balance)) {
- return false;
- }
- if (id != other.id) {
- return false;
- }
- return true;
- }
- }
- abstract class Transaction {
- protected final long fromId, toId;
- protected final String description;
- protected final String amount;
- public Transaction(long fromId, long toId, String description, String amount) {
- this.fromId = fromId;
- this.toId = toId;
- this.description = description;
- this.amount = amount;
- }
- public String getAmount() {
- return amount;
- }
- public long getFromId() {
- return fromId;
- }
- public long getToId() {
- return toId;
- }
- public String getDescription() {
- return description;
- }
- public abstract double getProvision();
- public int hashcode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + (int) (fromId ^ (fromId >>> 32));
- result = prime * result + (int) (toId ^ (toId >>> 32));
- result = prime * result + (description != null ? description.hashCode() : 0);
- result = prime * result + (amount != null ? amount.hashCode() : 0);
- 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 (fromId != other.fromId) {
- return false;
- }
- if (toId != other.toId) {
- return false;
- }
- if (!description.equals(other.description)) {
- return false;
- }
- if (!amount.equals(other.amount)) {
- return false;
- }
- return true;
- }
- }
- class FlatAmountProvisionTransaction extends Transaction {
- private String flatAmount;
- FlatAmountProvisionTransaction(long fromId, long toId, String amount, String flatProvision) {
- super(fromId, toId, "FlatAmount", amount);
- this.flatAmount = flatProvision;
- }
- public String getFlatAmount() {
- return amount;
- }
- public String getFlatProvision() {
- return flatAmount;
- }
- public double getProvision() {
- return Bank.getValue(flatAmount);
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = super.hashCode();
- result = prime * result + (flatAmount != null ? flatAmount.hashCode() : 0);
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- if (!super.equals(obj)) {
- return false;
- }
- FlatAmountProvisionTransaction other = (FlatAmountProvisionTransaction) obj;
- if (!flatAmount.equals(other.flatAmount)) {
- return false;
- }
- return true;
- }
- }
- class FlatPercentProvisionTransaction extends Transaction {
- private int centsPerDolar;
- FlatPercentProvisionTransaction(long fromId, long toId, String amount, int centsPerDolar) {
- super(fromId, toId, "FlatPercent", amount);
- this.centsPerDolar = centsPerDolar;
- }
- public int getCentsPerDolar() {
- return centsPerDolar;
- }
- public double getProvision() {
- double procent = (double) centsPerDolar / 100;
- int cel = (int) Bank.getValue(super.getAmount());
- return procent * cel;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = super.hashCode();
- result = prime * result + (int) centsPerDolar;
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- if (!super.equals(obj)) {
- return false;
- }
- FlatPercentProvisionTransaction other = (FlatPercentProvisionTransaction) obj;
- if (centsPerDolar != other.centsPerDolar) {
- return false;
- }
- return true;
- }
- }
- class Bank {
- private String name;
- private Account accounts[];
- private String totalTransfers;
- private String totalProvision;
- public Bank(String name, Account[] accounts) {
- super();
- totalTransfers = "0.00$";
- totalProvision = "0.00$";
- this.name = name;
- this.accounts = Arrays.copyOf(accounts, accounts.length);
- }
- public String getName() {
- return name;
- }
- public String totalProvision() {
- return totalProvision;
- }
- public String totalTransfers() {
- return totalTransfers;
- }
- public Account[] getAccounts() {
- return accounts;
- }
- public static int toNumber(char c) {
- return c - '0';
- }
- public static double getValue(String str) {
- String temp = str.substring(0, str.length() - 1);
- double d = Double.parseDouble(temp);
- return d;
- }
- public static String getString(double value) {
- String temp = String.format("%.2f", value);
- temp += "$";
- return temp;
- }
- public boolean isInBank(long id) {
- for (int i = 0; i < accounts.length; i++) {
- if (accounts[i].getId() == id) {
- return true;
- }
- }
- return false;
- }
- public int getIndex(long id) {
- for (int i = 0; i < accounts.length; i++) {
- if (accounts[i].getId() == id) {
- return i;
- }
- }
- return -1;
- }
- public boolean makeTransaction(Transaction t) {
- if (isInBank(t.getFromId()) && isInBank(t.getToId())) {
- /* get the indexes of the two accounts in the bank */
- int tempFrom = getIndex(t.getFromId());
- int tempTo = getIndex(t.getToId());
- double balanceUserFrom = getValue(accounts[tempFrom].getBalance());
- double balanceUserTo = getValue(accounts[tempTo].getBalance());
- double balanceTransaction = getValue(t.getAmount());
- if (tempFrom == tempTo) {
- balanceUserFrom -= t.getProvision();
- accounts[tempFrom].setBalance(getString(balanceUserFrom));
- setTotalTransfers(balanceTransaction);
- setTotalProvision(t.getProvision());
- return true;
- }
- if (balanceUserFrom >= balanceTransaction + t.getProvision()) {
- double toPay = balanceTransaction + +t.getProvision();
- balanceUserFrom -= toPay;
- accounts[tempFrom].setBalance(getString(balanceUserFrom));
- balanceUserTo += balanceTransaction;
- accounts[tempTo].setBalance(getString(balanceUserTo));
- setTotalTransfers(balanceTransaction);
- setTotalProvision(t.getProvision());
- return true;
- }
- }
- return false;
- }
- private void setTotalProvision(double provision) {
- double t = getValue(totalProvision);
- t += provision;
- totalProvision = getString(t);
- }
- private void setTotalTransfers(double balanceTransaction) {
- double t = getValue(totalTransfers);
- t += balanceTransaction;
- totalTransfers = getString(t);
- }
- @Override
- public String toString() {
- StringBuffer sb = new StringBuffer("");
- sb.append("Name: " + name + "\n\n");
- for (Account account : accounts) {
- sb.append(account.toString());
- }
- return sb.toString();
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- final int[] result = {1};
- result[0] = prime * result[0] + (name != null ? name.hashCode() : 0);
- result[0] = prime * result[0] + (int) getValue(totalProvision);
- result[0] = prime * result[0] + (int) getValue(totalTransfers);
- Arrays.stream(accounts).forEach(account -> result[0] = prime * result[0] + (account != null ? account.hashCode() : 0));
- return result[0];
- }
- @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 (name != other.name) {
- return false;
- }
- if (accounts.length != other.accounts.length) {
- return false;
- }
- for (int i = 0; i < accounts.length; i++) {
- if (!accounts[i].equals(other.accounts[i])) {
- return false;
- }
- }
- if (!totalTransfers.equals(other.totalTransfers)) {
- return false;
- }
- if (!totalProvision.equals(other.totalProvision)) {
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement