/** * 2023 frq 1. * AppointmentBook * * implementation and test code: Chris Thiel, OFMCap * */ public class AppointmentBook { private boolean[][] minutes; private static final boolean FREE = true; private static final boolean NOT_FREE = false; /** * part (a) */ public int findFreeBlock(int period, int duration) { return -11; // Replace with your code } /** * part (b) */ public boolean makeAppointment(int startPeriod, int endPeriod, int duration) { return false;// Replace with your code } /** * true is min in period available * Pre conditions: 1 <= per <=8, 0<= min <= 59 */ private boolean isMinuteFree(int period, int minute) { // my own implementation return minutes[period][minute] == FREE; } private void reserveBlock(int period, int start, int dur) { for (int i=0; i < dur; i++) minutes[period][start+i] = NOT_FREE; } public AppointmentBook() { minutes = new boolean[9][60]; for(int p=0; p<=8; p++) for(int m=0; m<60; m++) minutes[p][m] = FREE; reserveBlock(0, 0, 60); } public String toString() { String s=""; for(int p=2; p<=4; p++) { s += "Period "+p+"\n00: "; for(int i=0; i<60; i++) { if (minutes[p][i]==FREE) s+="o"; else s+="x"; if ( (i+1)%10 == 0 && (i+1)<60) s+="\n"+(i+1)+": "; } s +="\n"; } return s; } public static void main( String[] args ) { AppointmentBook a = new AppointmentBook(); // set to values in part (a) example; a.reserveBlock(2, 0, 10); a.reserveBlock(2, 15, 15); a.reserveBlock(2, 45, 5); //System.out.println(a); System.out.println("part a findFreeBlock(2,15) returns "+ a.findFreeBlock(2,15) + "(should be 30)"); System.out.println("part a findFreeBlock(2,9) returns "+ a.findFreeBlock(2,9) + "(should be 30)"); System.out.println("part a findFreeBlock(2,20) returns "+ a.findFreeBlock(2,20) + "(should be -1)"); AppointmentBook b = new AppointmentBook(); b.reserveBlock(2, 0, 25); b.reserveBlock(2, 30,30); b.reserveBlock(3, 15, 26); b.reserveBlock(4, 0, 5); b.reserveBlock(4, 30, 14); // System.out.println(b); System.out.println("\npart b.makeAppointment(2,4,22) returns "+ b.makeAppointment(2,4,22) + "(should be true)"); System.out.println("part b.makeAppointment(3,4,3) returns "+ b.makeAppointment(3,4,3) + "(should be true)"); System.out.println("part b.makeAppointment(2,4,30) returns "+ b.makeAppointment(2,4,30) + "(should be false)"); System.out.println(b); } }