Sultan t of Computer Science COMP2002Fall 2016 Due Date 1711

Sultan t of Computer Science COMP2002-Fall 2016 Due Date 17-11-2016 Homework 2 23:55 Hours In blood bank h blood samples related to that type. The system starts with certain amount of type \"O\" called \"deposit\" that may increase or decrease according to Requests\" and \"Donations\". Then the users interacting with the system should follow the following conditions To donate, enter \"D\" and the amount of blood in liters. The output will display the new deposit amount and how many \"donations till the current donation. To request blood, enter \"R\" and the amount of blood. The output will be either accept or reject and display the new amount of deposit and how many ACCEPTED \"requests\" till the current request. . . To get information, enter \"\" and the system will print the deposit amount, how many \"donations\", and how many accepted \"requests\" To accept a request the following conditions must be satisfied . Request is accept only After two consecutive \"donations\" with no \"request\" in between them. \"but the user may ask for information between them\" In case the deposit >=25. o OR .Request is accepted if the requested amount within the ability of the deposit. Request is rejected during the \"emergency donation\" status. If the user received the following message from the system Alert $SS5 deposit below limit\" this means the deposit below 15 liters, and the system must accept \"emergency donations\" that will make the deposit not less than 15 liters \" this emergency donation will be done through more than one danation process\" before handling any new \"request\" Your program must show menu REPEATEDLY, that handles the system \"as the sample attached\" until the user input \"E Sample-Input?Output:

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\"));
   }
}

 Sultan t of Computer Science COMP2002-Fall 2016 Due Date 17-11-2016 Homework 2 23:55 Hours In blood bank h blood samples related to that type. The system start
 Sultan t of Computer Science COMP2002-Fall 2016 Due Date 17-11-2016 Homework 2 23:55 Hours In blood bank h blood samples related to that type. The system start

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site