-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItem.java
More file actions
89 lines (72 loc) · 1.97 KB
/
Copy pathItem.java
File metadata and controls
89 lines (72 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.ashishp.dc.assignment.cl.entity;
import com.google.common.base.MoreObjects;
import java.io.Serializable;
import java.util.Objects;
/**
*
* @author <a href="http://www.linkedin.com/in/aashishpanchal">Aashish</a>
*
*/
public final class Item implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Current amount of money at the bank
*/
private int balance;
/**
* Temp variable for holding withdraw money amount with possibility to restore
* holds under assumption that only one money transfer is done at a time
*/
private int withdrawAmount;
public Item(int balance) {
this.balance = balance;
}
public int getBalance() {
return balance;
}
public void incrementBalance(int amount) {
balance += amount;
}
public void restoreBalance() {
balance += withdrawAmount;
withdrawAmount = 0;
}
/**
* Checks if current balance is over or equal the amount to be deducted
* if it is -> deducts the money, if not -> balance stay untouched
*
* @param amount to be deducted
* @return whether operation succeed or not
*/
public boolean decrementBalance(int amount) {
if (balance >= amount) {
balance -= amount;
withdrawAmount = amount;
return true;
}
return false;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (o instanceof Item) {
Item object = (Item) o;
return Objects.equals(balance, object.balance);
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(balance);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("balance", balance)
.toString();
}
}