Sultan t of Computer Science COMP2002Fall 2016 Due Date 1711
Solution
package com.temp;
import java.util.Scanner;
public class BDA {
//This variable is used for counting No. of Donations.
public static int noofdonations = 0;
//This variable indicates the deposit amount.
public static int amount = 0;
//This variable is used for counting No. of successful requests.
public static int acceptedRequests = 0;
/**
* This variable is used for maintaining requests whether they should be successful.
* A request is successful if 2 donations are made consecutively without a request in between them.
*/
public static int successfulRequest = 0;
public static void performOperation(String s){
String[] t = s.split(\" \");
//This switch is used for performing the respective operation which user has entered.
switch (t[0]) {
case \"D\":
case \"d\":
noofdonations++;
/**
* Setting the successful request count.
*/
if(successfulRequest > 0){
successfulRequest--;
}
//Adding the donation amount to deposit.
try{
amount+=Integer.parseInt(t[1]);
}catch(NumberFormatException e){
e.printStackTrace();
}
System.out.println(\"Total Amount : \"+amount);
break;
case \"R\":
case \"r\":
int request = 0;
try{
request = Integer.parseInt(t[1]);
}catch(NumberFormatException e){
e.printStackTrace();
}
//Checking if the deposit is greater than 25, request amount is greater than deposit and Two or more consecutive donations are made.
if((request < amount) &&(amount >= 25 || successfulRequest == 0)){
successfulRequest = 2;
acceptedRequests++;
amount-= request;
System.out.println(\"Request Accepted.\");
} else {
System.out.println(\"Request Rejected.\");
}
System.out.println(\"Total Amount : \"+amount);
break;
case \"I\":
case \"i\":
//Displaying information
System.out.println(\"Total Amount : \"+amount);
System.out.println(\"No. of Donations : \"+noofdonations);
System.out.println(\"Accepted Requests : \"+acceptedRequests);
break;
default:
System.out.println(\"Invalid choice\");
break;
}
//Displaying alert if deposit goes below 15 liters.
if(amount <= 15)
System.out.println(\"Alert deposit below limit.\ \ \");
}
public static void main(String arg[]){
String a = \"\";
do{
//Display menu.
System.out.println(\"To Donate enter D and amount in liters\");
System.out.println(\"To Request enter R and amount in liters\");
System.out.println(\"To get information enter I\");
System.out.println(\"To exit enter E\");
System.out.println(\"\ Enter your selection\");
//Get input from user.
Scanner s = new Scanner(System.in);
a = s.nextLine();
//Do the respective action.
performOperation(a);
}while(!a.equalsIgnoreCase(\"E\"));
}
}

