Skip to content

Commit

Permalink
Balance smaller than 0 not allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
Offbeatlop committed Dec 4, 2017
1 parent 48f0f33 commit 9ef7e5e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
7 changes: 4 additions & 3 deletions BankClient/src/es/upm/dit/cnvr/model/ClientDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public HashMap<String, BankClient> getClientDB() {

public ServiceStatus createClient(BankClient client) {
ServiceStatus stat = ServiceStatus.INFORMATION_MISSED;
if (clientDB.containsKey(client.getAccount())) {
if (clientDB.containsKey(client.getAccount()) && client.getBalance() >= 0) {
// It already exists: We inform about it
stat = ServiceStatus.EXISTING_CLIENT;
}else {
Expand All @@ -46,7 +46,7 @@ public ServiceStatus createClient(BankClient client) {
return stat;
}


//XXX: No se utiliza
public BankClient readAccount(String clientAccount) {
BankClient client = null;
if (clientDB.containsKey(clientAccount))
Expand All @@ -57,7 +57,7 @@ public BankClient readAccount(String clientAccount) {

public ServiceStatus update (String accountId, double balance) {
ServiceStatus stat = ServiceStatus.INFORMATION_MISSED;
if (balance > 0 && clientDB.containsKey(accountId)){
if (balance >= 0 && clientDB.containsKey(accountId)){
BankClient client = clientDB.get(accountId);
client.setBalance(balance);
clientDB.put(client.getAccount(), client);
Expand All @@ -77,6 +77,7 @@ public ServiceStatus deleteClient(String accountId, String clientName) {
} else {
stat = ServiceStatus.INFORMATION_INVALID;
}
System.out.println("DB: " + clientDB.toString());
return stat;
}

Expand Down
14 changes: 11 additions & 3 deletions BankClient/src/es/upm/dit/cnvr/server/Barrier.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,27 @@ boolean leave() throws KeeperException, InterruptedException{
Stat stat = zk.exists("/boperationleave", false);
if (list.size() > 0) {
synchronized (mutexBarrier) {
mutexBarrier.wait(1000);
if(stat == null){
try{
zk.create("/boperationleave", new byte[0],
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} catch (Exception e1){
System.out.println("Node boperationleave created yet");
System.out.println("Node boperationleave already created");

}
}
}
mutexBarrier.wait(1000);
}
} else {
System.out.println("Leave");
if(stat != null){
try{
zk.delete("/boperationleave",0);
} catch (Exception e1){
System.out.println("Node boperationleave already created");

}
}
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion BankClient/src/es/upm/dit/cnvr/server/TCPServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static void main(String[] args) throws Exception {
zkobject.configure();
Operate operate = zkobject.getOperate();

ServerSocket welcomeSocket = new ServerSocket(6789);
ServerSocket welcomeSocket = new ServerSocket(6786);


while (true) {
Expand Down
2 changes: 1 addition & 1 deletion BankClient/src/es/upm/dit/cnvr/server/ZookeeperObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static Integer getMutexBarrier() {
public void configure() {
System.out.println("START CONFIGURE");
// This is static. A list of zookeeper can be provided for decide where to connect
String[] hosts = {"localhost:2181", "localhost:2182"};
String[] hosts = {"138.4.31.99:2181", "138.4.31.117:2182"};

// Select a random zookeeper server
Random rand = new Random();
Expand Down

0 comments on commit 9ef7e5e

Please sign in to comment.