Banker.java
import java.util.LinkedList;public class Banker {private int[] available; // 擁有各類資源的數目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];}}/* * 初步檢查客戶請求資源是否能給予滿足,是return true * * @param available 可用資源量 * * @param request 客戶請求資源量 * * @param need 客戶達到最大值所需要的資源量 */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;}/* * 檢查銀行家的available是否大於等於客戶的need,是return true * * @param available 可用資源量 * * @param need 客戶達到最大值所需要的資源量 */public boolean checkAvailable(int[] available, int[] need) {for (int i = 0; i < need.length; i++) {if (available[i] < need[i]) {return false;}}return true;}/* * * @ 系統安全性檢查演算法 嘗試給系統分配資源 每次獲得鏈表中的第一個元素, 符合條件則進行修改,並將該元素放到隊尾,然後將flag置為0; * 否則將該元素放到隊尾,並將flag++ */public boolean safeCheck(LinkedList<Customer> customers, Banker banker) {int count = customers.size();int[] needTemp;int[] available = banker.getAvailable();int[] work = available;int type = available.length;int flag = 0;Customer cusTemp;System.out.println("進程名" + "\t" + " work" + "\t\t" + " need" + " \t"+ " Allocation" + "\t" + " Work+Allocation" + "\t "+ " finish");while (flag < count) {cusTemp = customers.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] + " ");}customers.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.addLast(cusTemp);flag++;}}for (int i = 0; i < count; i++) {if (customers.get(i).isFinish() == false) {return false;}}return true;}/* * 客戶請求之後,經過初步判斷通過之後,假設給予某個客戶所要求的量,然後進行判斷 * * @param customers 客戶鏈表 * * @param banker 銀行家 * * @param request 請求量 * * @param name 客戶名 * * @return 通過的話返回true */public boolean checkSafe(LinkedList<Customer> customers, 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, customers.get(k).getNeed())) {return false;}int[] alloc = customers.get(k).getAllocation();for (int c = 0; c < alloc.length; c++) {alloc[c] += request[c];}customers.get(k).setAllocation(alloc);}}return safeCheck(customers, 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表示該請求已經被完成private int type; //需求類型的種類private int[] need; //尚需的給類資源數private int[] max; //最大需求private int[] allocation; //已經分得的給類資源數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.LinkedList;import java.util.Scanner;public class MyOS {static LinkedList<Customer> customers = new LinkedList<Customer>();static Banker 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;Scanner input = new Scanner(System.in);System.out.println("請輸入資源種類 :");type = input.nextInt();max = new int[type];allocation = new int[type];available = new int[type];System.out.println("請輸入客戶個數:");cno = input.nextInt();for(int k=0; k<type; k++){System.out.println("請輸入銀行家的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("請輸入第"+i+"位客戶的max");max[c] = input.nextInt();}for(int j=0; j<type; j++){System.out.println("請輸入第"+i+"位客戶的allocation");allocation[j] = input.nextInt();}name = i+1+"號";Customer cus = new Customer(name, type, max, allocation);customers.add(i, cus);}showCustomer(customers);System.out.println("輸入1檢查系統的安全性,輸入2向系統發出請求,其他退出");myCase = input.nextInt();switch(myCase){case 1://檢查系統的安全性if(banker.safeCheck(customers, 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(LinkedList<Customer> customers){int[] n;int[] al;System.out.println("進程名"+"\t\t"+" need"+" \t"+" Allocation"+"\t "+" finish");for(int i=0; i<customers.size(); i++){Customer cus = customers.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(cus.isFinish()+" ");}}}
輸入的格式控制不好看,有什麼辦法可以改善一下嗎?