Banker. Java
Import Java. util. optional list; public class banker {private int [] available; // number of resources owned by private int [] work; Banker () {} banker (INT [] available, int type) {This. available = new int [type]; this. work = new int [type]; for (INT I = 0; I <type; I ++) {This. available [I] = available [I]; this. work [I] = available [I] ;}/ ** check whether the requested resources are satisfied, yes return true ** @ Param available resources ** @ Param request customer requested resources ** @ par Am need the amount of resources required to reach the maximum value */Public Boolean checkavailable (INT [] available, int [] request, int [] Need) {for (INT I = 0; I <request. length; I ++) {If (request [I]> available [I] | request [I]> need [I]) {return false ;}} return true ;} /** check whether the banker's available is greater than or equal to the customer's need, is return true ** @ Param available resources ** @ Param need the amount of resources required by the customer to reach the maximum value */Public Boolean checkavailable (INT [] available, int [] Need) {for (in T I = 0; I <need. length; I ++) {If (available [I] <need [I]) {return false ;}} return true ;} /*** @ the system security check algorithm tries to allocate resources to the system to obtain the first element in the linked list each time. If the conditions are met, modify the element and put it at the end of the team, then set the flag to 0; * Otherwise, put the element at the end of the team, and set the flag ++ */Public Boolean safecheck (using list <customer> MERs, banker) {int COUNT = MERs. size (); int [] needtemp; int [] available = banker. getavailable (); int [] Work = available; int type = available. length; int Flag = 0; customer custemp; system. out. println ("process name" + "\ t" + "work" + "\ t" + "need" + "\ t" + "Allocation" + "\ t" + "work + allocation" + "\ t" + "finish "); while (flag <count) {custemp = MERs. getfirst (); needtemp = custemp. getneed (); If (checkavailable (work, needtemp) & custemp. isfinish () = false) {int [] Ava; int [] work1 = AVA = banker. getavailable (); int [] n; int [] Al; n = custemp. getneed (); Al = custemp. getallocation (); system. out. print ("" + custemp. getname () + "\ t");/* Work = AVA */For (Int J = 0; j <Ava. length; j ++) {// work [J] + = al [J]; system. out. print (work1 [J] + "");} system. out. print ("\ t");/* need */For (Int J = 0; j <Ava. length; j ++) {system. out. print (N [J] + "");} system. out. print ("\ t");/* allocation */For (Int J = 0; j <Ava. length; j ++) {system. out. print (Al [J] + "");} custo MERs. remove (0); custemp. setfinish (true); customers. addlast (custemp); int [] alloct = custemp. getallocation (); For (int K = 0; k <type; k ++) {work [k] + = alloct [k];} banker. setwork (work); system. out. print ("\ t"); For (Int J = 0; j <Ava. length; j ++) {system. out. print (work1 [J] + "");} system. out. print ("\ t"); system. out. println (custemp. isfinish () + ""); flag = 0;} else {customers. removefirst (); customers. addla ST (custemp); flag ++ ;}} for (INT I = 0; I <count; I ++) {If (MERS MERs. get (I ). isfinish () = false) {return false;} return true;}/** after the customer request is approved by preliminary judgment, assuming that the amount required by a customer is given, then, Judge ** @ Param MERs customer linked list ** @ Param banker ** @ Param request Request volume ** @ Param name customer name ** @ return returns true */ public Boolean checksafe (partition list <customer> MERs, banker banker, int [] request, string name) {for (int K = 0; K <customers. Size (); k ++) {If (customers. Get (k). getname (). Equals (name) {If (! Checkavailable (available, request, MERS MERs. get (k ). getneed () {return false;} int [] alloc = MERs. get (k ). getallocation (); For (int c = 0; C <alloc. length; C ++) {alloc [c] + = request [c];} MERs. get (k ). setallocation (alloc) ;}} return safecheck (MERS MERs, banker);} public int [] getavailable () {return available;} public void setavailable (INT [] available) {This. available = available;} public int [] getwork () {return work;} public void setwork (INT [] Work) {This. work = work ;}}
Customer. Java
Public Class Customer {private string name; private Boolean finish; // true indicates that the request has been completed with private int type; // type of the requirement type private int [] need; // The number of class resources that need to be added. Private int [] Max; // The maximum number of private int [] allocation; // The number of allocated class resources. Customer () {} customer (string name, int type, int [] Max, int [] allocation) {This. name = Name; this. max = new int [type]; this. allocation = new int [type]; this. need = new int [type]; for (INT I = 0; I <type; I ++) {This. allocation [I] = allocation [I]; this. max [I] = MAX [I]; this. need [I] = MAX [I]-allocation [I] ;}} Public String getname () {return name;} public void setname (string name) {This. name = Name;} public Boolean isfinish () {return finish;} public void setfinish (Boolean finish) {This. finish = finish;} public int GetType () {return type;} public void settype (INT type) {This. type = type;} public int [] getneed () {return need;} public void setneed (INT [] Need) {This. need = need;} public int [] getmax () {return Max;} public void setmax (INT [] Max) {This. max = max;} public int [] getallocation () {return allocation;} public void setallocation (INT [] allocation) {This. allocation = allocation ;}}
Myos. Java
Import Java. util. using list; import Java. util. extends; public class myos {static users list <customer> MERs = new users list <customer> (); static banker = new banker (); public static void main (string [] ARGs) {string name = NULL; int mycase = 0; int type; int CNO; int [] Max; int [] allocation; int [] available; invalid input = new partition (system. in); system. out. println ("Enter the resource type:"); type = input. nextin T (); max = new int [type]; Allocation = new int [type]; available = new int [type]; system. out. println ("Enter the number of customers:"); CNO = input. nextint (); For (int K = 0; k <type; k ++) {system. out. println ("Enter the banker's available"); available [k] = input. nextint () ;}banker = new banker (available, type); For (INT I = 0; I <CNO; I ++) {for (INT C = 0; c <type; C ++) {system. out. println ("Enter the" + I + "customer's Max"); Max [c] = input. nextint () ;}for (Int J = 0; j <type; J ++) {system. out. println ("Enter the" + I + "Bid customer's allocation"); allocation [J] = input. nextint ();} name = I + 1 + "; customer Cus = new customer (name, type, Max, allocation); MERs. add (I, Cus);} showcustomer (MERS mers); system. out. println ("input 1 checks system security, input 2 sends a request to the system, and other exits"); mycase = input. nextint (); Switch (mycase) {Case 1: // check the system's security if (banker. safecheck (MERS MERs, banker) {system. out. println ("safe! ");} Else {system. Out. println (" not safe! ");} Break; Case 2: // system. out. println ("Please input a name:"); string pname = input. next (); system. out. println ("Please input the request (Press ENTER key after input a numer):"); int [] request = new int [type]; for (INT dd = 0; DD <type; dd ++) {request [DD] = input. nextint ();} If (banker. checksafe (customers, banker, request, pname) {system. out. println ("the request is available! ");} Break; default: system. Out. println (" Bye! "); Break ;}} public static void showcustomer (partition list <customer> MERs mers) {int [] n; int [] Al; system. out. println ("process name" + "\ t" + "need" + "\ t" + "Allocation" + "\ t" + "finish "); for (INT I = 0; I <MERs. size (); I ++) {customer Cus = MERs. get (I); n = Cus. getneed (); Al = Cus. getallocation (); system. out. print ("" + Cus. getname () + "\ t"); system. out. print ("\ t");/* need */For (Int J = 0; j <Al. length; j ++) {system. out. print (N [J] + "");} system. out. print ("\ t");/* allocation */For (Int J = 0; j <Al. length; j ++) {system. out. print (Al [J] + "");} system. out. print ("\ t"); system. out. println (cuz. isfinish () + "");}}}
It is difficult to control the input format. Is there any way to improve it?