vault backup: 2024-03-08 10:55:57

This commit is contained in:
2024-03-08 10:55:57 +00:00
parent 28ea6a0a9b
commit 68fa81d02f
18 changed files with 662 additions and 81 deletions

View File

@@ -42,8 +42,19 @@
"file": "Semester 2/Database Systems/Booklet of Lecture Slides for Semester Two (2).pdf" "file": "Semester 2/Database Systems/Booklet of Lecture Slides for Semester Two (2).pdf"
} }
} }
},
{
"id": "bde983126ec4558f",
"type": "leaf",
"state": {
"type": "pdf",
"state": {
"file": "Semester 2/Database Systems/Exercise Booklet.pdf"
}
}
} }
] ],
"currentTab": 1
} }
], ],
"direction": "vertical" "direction": "vertical"
@@ -192,6 +203,15 @@
}, },
"active": "e96e468fb0a709e5", "active": "e96e468fb0a709e5",
"lastOpenFiles": [ "lastOpenFiles": [
"images/Pasted image 20240308103937.png",
"images/Pasted image 20240308103720.png",
"images/Pasted image 20240308102031.png",
"images/Pasted image 20240308101739.png",
"Semester 2/Programming 2/Project/Part 2/Library.java#",
"Semester 2/Database Systems/Exercise Booklet.pdf",
"Semester 2/Database Systems/Week 8/Week 8 Database Systems.md",
"Semester 2/Database Systems/Week 7/Week 7 Database Systems.md",
"Semester 2/Database Systems/Booklet of Lecture Slides for Semester Two (2).pdf",
"images/Pasted image 20240305135229.png", "images/Pasted image 20240305135229.png",
"images/Pasted image 20240305135043.png", "images/Pasted image 20240305135043.png",
"images/Pasted image 20240305134944.png", "images/Pasted image 20240305134944.png",
@@ -199,8 +219,6 @@
"images/Pasted image 20240305134835.png", "images/Pasted image 20240305134835.png",
"images/Pasted image 20240305134734.png", "images/Pasted image 20240305134734.png",
"images/Pasted image 20240305134506.png", "images/Pasted image 20240305134506.png",
"Semester 2/Database Systems/Booklet of Lecture Slides for Semester Two (2).pdf",
"Semester 2/Database Systems/Week 8/Week 8 Database Systems.md",
"Semester 2/Programming 2/Project/Part 2/Book.java#", "Semester 2/Programming 2/Project/Part 2/Book.java#",
"Semester 2/Programming 2/Project/Part 2/Periodical.java#", "Semester 2/Programming 2/Project/Part 2/Periodical.java#",
"Semester 2/Programming 2/Project/Part 2/LibraryItem.java#", "Semester 2/Programming 2/Project/Part 2/LibraryItem.java#",
@@ -208,13 +226,6 @@
"Semester 2/Programming 2/Project/Part 2/CD.java#", "Semester 2/Programming 2/Project/Part 2/CD.java#",
"Semester 2/Programming 2/Project/Part 2/DVD.java#", "Semester 2/Programming 2/Project/Part 2/DVD.java#",
"Semester 2/Programming 2/Project/Part 2/AudioVisual.java#", "Semester 2/Programming 2/Project/Part 2/AudioVisual.java#",
"Semester 2/Programming 2/Project/Part 2/PrintedItem.java#",
"Semester 2/Programming 2/Project/Part 2/Library.java#",
"images/Pasted image 20240305131110.png",
"images/Pasted image 20240305130824.png",
"Semester 2/Database Systems/Week 7/Week 7 Database Systems.md",
"images/Pasted image 20240227171728.png",
"images/Pasted image 20240227171700.png",
"Semester 2/Database Systems/Week 6/Week 6 Database Systems.md", "Semester 2/Database Systems/Week 6/Week 6 Database Systems.md",
"Semester 2/Computer Systems Internals & Linux/Week 6/Week 6 Computer Systems Internals.md", "Semester 2/Computer Systems Internals & Linux/Week 6/Week 6 Computer Systems Internals.md",
"Semester 2/Programming 2/Assessment 3 Revision/Parameter Passing WS3.md", "Semester 2/Programming 2/Assessment 3 Revision/Parameter Passing WS3.md",

View File

@@ -55,7 +55,7 @@ Definition: Checkpoints are points of sync between database and log file. All bu
- Checkpoint record to log file which contains identities of transactions active at time of checkpoint - Checkpoint record to log file which contains identities of transactions active at time of checkpoint
2. Resume processing. 2. Resume processing.
![](Pasted%20image%2020240305134734.png) - ![](Pasted%20image%2020240305134734.png)
### Example 1 ( Immediate ) ### Example 1 ( Immediate )
@@ -88,3 +88,55 @@ Definition: Checkpoints are points of sync between database and log file. All bu
## Example of Log Use ( Immediate ) ## Example of Log Use ( Immediate )
![](Pasted%20image%2020240305135229.png) ![](Pasted%20image%2020240305135229.png)
# Shadow Paging
Alternative technique for providing atomicity and durability.
DBMS maintains two page tables during the life of a transaction.
1. Current Page
2. Shadow Page Table
**Transaction Start**: Two pages are the same
- Shadow page table is never changed, only used to restore database.
**Transaction Execution**: current page table records all updates to database
**Transaction End**: Current page table becomes shadow page table and all modified pages from database buffers written to secondary storage.
**Transaction Failure**: new pages ignored, shadow page table becomes current page table.
# Tutorial
1. State whether each of the following is true or false. If it is false then briefly explain why.
1. True
2. True
3. True
4. False: REDO / NO UNDO
5. True
2. The DBMS maintains a log file containing transaction records that identify the start and end of transactions and the before and after images of the write operations. Consider the log files shown in Tables 32.1 and 32.2. For each one, explain what the DBMS recovery system would do, and why, if there was a failure at the time shown below each table and the DBMS was using:
1. Deferred update protocol
1. 32.1 Fail @10:24
- Redo T1 as commit before failure. Ignore T2 and 3, active at point of failure. T4 not active at point of failure, Ignore.
2. 32.2 Fail @9.21
- Redo T2 and 4, since no commit occurs. T3 commits after the checkpoint, and anything before has no effect.
1. Immediate update protocol
1. 32.1 Fail @10.24
- Undo T2 and 3, since they are active, and do not commit. T1 must be redone, and T4 is ignored, as it never started.
2. 32.2 Fail @9:21
- Undo T2 and 4, since they are active and do not commit. Redo T3, as it is active after the checkpoint.
# Workshop
1. ![](Pasted%20image%2020240308101739.png)
2. ![](Pasted%20image%2020240308102031.png)
3. ![](Pasted%20image%2020240308103720.png)
```
SELECT SUM([UnitPrice]) AS TotalPrice
FROM [Products]
WHERE [SupplierID] = (
SELECT [SupplierID]
FROM [Suppliers]
WHERE [Country] = 'UK'
);
```
4. ![](Pasted%20image%2020240308103937.png)
1.

Binary file not shown.

View File

@@ -0,0 +1,26 @@
#BlueJ class context
comment0.params=date
comment0.target=java.lang.String\ convertDateToLongString(java.util.Date)
comment0.text=\n\ Converts\ a\ Date\ object\ to\ a\ corresponding\ String\ in\n\ the\ long\ date\ pattern\ style\ "Saturday,\ 25\ March\ 2023".\n\n\ @param\ \ \ \ \ date\ \ a\ Date\ object\n\n\ @return\ \ \ \ a\ String,\ containing\ a\ long\ date\ pattern\n
comment1.params=date
comment1.target=java.lang.String\ convertDateToShortString(java.util.Date)
comment1.text=\n\ Converts\ a\ Date\ object\ to\ a\ corresponding\ String\ in\n\ the\ short\ date\ pattern\ style\ "25-03-2023".\n\n\ @param\ \ \ \ \ date\ \ a\ Date\ object\n\n\ @return\ \ \ \ a\ String,\ containing\ a\ short\ date\ pattern\n
comment2.params=dateString
comment2.target=java.util.Date\ convertStringToDate(java.lang.String)
comment2.text=\n\ Converts\ a\ string\ in\ the\ short\ date\ pattern\ style\ "25-03-2023"\n\ to\ a\ corresponding\ Date\ object.\n\n\ Any\ leading\ or\ trailing\ spaces\ are\ first\ removed\ from\ the\ date\ string.\n\ The\ String\ parameter\ that\ represent\ a\ date\ as\ a\ string\ must\ be\ in\ the\n\ format\ dd-mm-yyy\ (e.g.\ 25-03-2023\ or\ 25-3-2023)\ where\ dd\ represents\n\ one\ or\ two\ digits\ representing\ the\ day\ in\ the\ month,\ similarly\ for\n\ mm\ representing\ the\ month\ in\ the\ year\ and\ yyyy\ represents\ the\ four\n\ digits\ for\ the\ year.\n\n\ A\ RuntimeException\ is\ thrown\ if\ the\ date\ string\ is\ not\ recognised\ as\n\ a\ valid\ date.\ Such\ exceptions\ do\ not\ need\ to\ be\ caught\ or\ thrown\ as\ \n\ they\ are\ unchecked\ exceptions,\ but\ can\ be\ caught\ if\ necessary.\n\n\ @param\ \ \ \ \ dateString\ \ a\ Date\ object\n\n\ @return\ \ \ \ the\ Date\ object\n
comment3.params=startDate\ endDate
comment3.target=int\ daysBetween(java.util.Date,\ java.util.Date)
comment3.text=\n\ Calculates\ the\ number\ of\ days\ between\ two\ given\ dates,\ startDate\ and\ endDate.\n\n\ If\ startDate\ is\ after\ endDate\ then\ the\ number\ of\ days\ returned\ will\ be\ negative.\n\n\ @param\ \ \ \ \ startDate\ \ \ a\ Date\ object\n\ @param\ \ \ \ \ endDate\ \ \ \ \ a\ Date\ object\n\n\ @return\ \ \ \ an\ int,\ number\ of\ days\ between\ the\ dates\n
comment4.params=date\ noOfDays
comment4.target=java.util.Date\ incrementDate(java.util.Date,\ int)
comment4.text=\n\ Given\ date,\ a\ Date\ object,\ and\ noOfDays,\ an\ int,\ the\ method\ returns\n\ a\ Date\ object\ corresponding\ to\ noOfDays\ later\ than\ date.\n\n\ If\ noOfDays\ is\ negative,\ the\ resulting\ Date\ object\ will\ be\ before\ date.\n\n\ @param\ \ \ \ \ date\ \ \ \ \ \ a\ Date\ object\n\ @param\ \ \ \ \ noOfDays\ \ an\ int\n\n\ @return\ \ \ \ a\ Date\n
comment5.params=year
comment5.target=boolean\ isLeapYear(int)
comment5.text=\n\ Given\ year,\ an\ int,\ the\ method\ checks\ to\ see\ if\ the\ year\n\ is\ a\ leap\ year.\n\n\ @param\ \ \ \ \ year,\ \ an\ int\n\n\ @return\ \ \ \ a\ boolean,\ true\ only\ if\ the\ year\ is\ a\ leap\ year.\n
comment6.params=dateString
comment6.target=boolean\ isValidDateString(java.lang.String)
comment6.text=\n\ Given\ dateString,\ a\ String,\ the\ method\ checks\ to\ see\ if\ string\n\ corresponds\ to\ a\ valid\ shortDatePattern.\ \n\n\ @param\ \ \ \ \ dateString,\ a\ String\n\n\ @return\ \ \ \ a\ boolean,\ true\ only\ if\ the\ dateString\ is\ a\ valid\ pattern\n
comment7.params=date
comment7.target=java.util.Date\ nextDate(java.util.Date)
comment7.text=\n\ Given\ date,\ a\ Date\ object,\ the\ method\ returns\n\ a\ Date\ object\ corresponding\ to\ the\ next\ day.\n\n\ @param\ \ \ \ \ noOfDays\ \ an\ int\n\n\ @return\ \ \ \ a\ Date\n
numComments=8

View File

@@ -0,0 +1,263 @@
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
/**
*
* A class DateUtil with the following methods for dealing with dates.
*
* public static String convertDateToLongString(Date date)
* public static String convertDateToShortString(Date date)
* public static Date convertStringToDate(String dateString)
* public static int daysBetween(Date startDate, Date endDate)
* public static Date incrementDate(Date date, int noOfDays)
* public static boolean isLeapYear(int year)
* public static boolean isValidDateString(String dateString)
* public static Date nextDate(Date date)
*
* @author D E Newton
*
*/
public class DateUtil
{
public static SimpleDateFormat dateFormatter;
private static String longDatePattern;
private static String shortDatePattern;
/**
* initializer block -- useful for
* initializing static fields
*/
static
{
shortDatePattern = "dd-MM-yyyy"; // dd = day, MM = month, yyyy = year (as 2, 2 and 4 digits respectively)
longDatePattern = "EEEE, d MMMM yyyy"; // e.g. Saturday, 25 March 2023
// EEEE (4 or more) = day in week, in full e.g.Saturday
// MMMMM (4 or more) = month name as text in full e.g. March
// d = day as 1 or 2 digits e.g. 25th of month -> 25, 23rd -> 23
//
// some alternatives below to help understanding (see documentation for SimpleDateFormat class)
// EE (3 or less) -> Tue
// MMMM = month name as text in short e.g. Oct
// M (or MM) = month as digits e.g. 1 (or 01) would correspond to January
dateFormatter = new SimpleDateFormat(shortDatePattern); // default pattern set
dateFormatter.setLenient(false); // default is true and impossible dates such
// as 31-09-2023 would be accepted but interpreted as 01-10-2023
}
/**
* Converts a Date object to a corresponding String in
* the long date pattern style "Saturday, 25 March 2023".
*
* @param date a Date object
*
* @return a String, containing a long date pattern
*/
public static String convertDateToLongString(Date date)
{
dateFormatter.applyPattern(longDatePattern);
String dateString = dateFormatter.format(date);
dateFormatter.applyPattern(shortDatePattern); // reset pattern
return dateString;
}
/**
* Converts a Date object to a corresponding String in
* the short date pattern style "25-03-2023".
*
* @param date a Date object
*
* @return a String, containing a short date pattern
*/
public static String convertDateToShortString(Date date)
{
return dateFormatter.format(date);
}
/**
* Converts a string in the short date pattern style "25-03-2023"
* to a corresponding Date object.
*
* Any leading or trailing spaces are first removed from the date string.
* The String parameter that represent a date as a string must be in the
* format dd-mm-yyy (e.g. 25-03-2023 or 25-3-2023) where dd represents
* one or two digits representing the day in the month, similarly for
* mm representing the month in the year and yyyy represents the four
* digits for the year.
*
* A RuntimeException is thrown if the date string is not recognised as
* a valid date. Such exceptions do not need to be caught or thrown as
* they are unchecked exceptions, but can be caught if necessary.
*
* @param dateString a Date object
*
* @return the Date object
*/
public static Date convertStringToDate(String dateString)
{
dateString = dateString.trim();
ParsePosition posn = new ParsePosition(0); // initialise posn to the beginning of the string to be parsed
Date date = dateFormatter.parse(dateString, posn);
int endIndex = posn.getIndex(); // posn after parsing
String message = "Date string <" + dateString + "> not recognised";
if( date==null )
{
// parsing failed because string not recognised
message += ".";
throw new RuntimeException(message);
}
else if( endIndex!=dateString.length() )
{
// parsing failed because parsing did not "consume" all the characters
// in the string, indicating the complete string was not recognised
message += " because it contains extra characters after a date.";
throw new RuntimeException(message);
}
else
return date;
}
/**
* Calculates the number of days between two given dates, startDate and endDate.
*
* If startDate is after endDate then the number of days returned will be negative.
*
* @param startDate a Date object
* @param endDate a Date object
*
* @return an int, number of days between the dates
*/
public static int daysBetween(Date startDate, Date endDate)
{
// first check that startDate is not after endDate
boolean outOfOrder;
Date temp;
if( startDate.compareTo(endDate)<=0 )
{
outOfOrder = false;
}
else
{
outOfOrder = true;
temp = startDate;
startDate = endDate;
endDate = temp;
}
int daysBetween = 0;
Calendar calendar = new GregorianCalendar();
calendar.setTime(startDate); // initialised at start date
Calendar calendarEndDate = new GregorianCalendar();
calendarEndDate.setTime(endDate);
// First advance calendar a year at a time without advancing past the end date.
// This is much quicker, for dates that are years apart, than simply relying on the
// later "day at a time" loop. Advancing "a month at a time" before the "day at a
// time" loop would be even better but complex because of the varying month lengths.
Calendar prevYearCalendar = (Calendar) calendar.clone();
calendar.add(Calendar.YEAR, 1);
// advance calendar a year at a time until end date reached
while( !calendar.getTime().after(endDate) )
{
if( isLeapYear(prevYearCalendar.get(Calendar.YEAR)) &&
prevYearCalendar.get(Calendar.MONTH)<Calendar.MARCH )
// advancing past a leap year day
daysBetween += 366;
else
daysBetween += 365;
prevYearCalendar = (Calendar) calendar.clone();
calendar.add(Calendar.YEAR, 1);
}
calendar = prevYearCalendar; // calendar always advances too far, so need to correct
// now advance calendar a day at a time until end date reached
while( calendar.getTime().before(endDate) )
{
calendar.add(Calendar.DAY_OF_MONTH, 1);
daysBetween++;
}
if( outOfOrder )
daysBetween = -daysBetween;
return daysBetween;
}
/**
* Given date, a Date object, and noOfDays, an int, the method returns
* a Date object corresponding to noOfDays later than date.
*
* If noOfDays is negative, the resulting Date object will be before date.
*
* @param date a Date object
* @param noOfDays an int
*
* @return a Date
*/
public static Date incrementDate(Date date, int noOfDays)
{
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, noOfDays);
return calendar.getTime();
}
/**
* Given year, an int, the method checks to see if the year
* is a leap year.
*
* @param year, an int
*
* @return a boolean, true only if the year is a leap year.
*/
public static boolean isLeapYear(int year)
{
GregorianCalendar calendar = new GregorianCalendar();
return calendar.isLeapYear(year);
}
/**
* Given dateString, a String, the method checks to see if string
* corresponds to a valid shortDatePattern.
*
* @param dateString, a String
*
* @return a boolean, true only if the dateString is a valid pattern
*/
public static boolean isValidDateString(String dateString)
{
try
{
Date d = DateUtil.convertStringToDate(dateString);
return true;
}
catch(RuntimeException ex)
{
return false;
}
}
/**
* Given date, a Date object, the method returns
* a Date object corresponding to the next day.
*
* @param noOfDays an int
*
* @return a Date
*/
public static Date nextDate(Date date)
{
return incrementDate(date, 1);
}
}

View File

@@ -4,20 +4,23 @@ comment0.target=Library()
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ Library\n comment0.text=\n\ Constructor\ for\ objects\ of\ class\ Library\n
comment1.params=item comment1.params=item
comment1.target=void\ storeItem(LibraryItem) comment1.target=void\ storeItem(LibraryItem)
comment1.text=\n\ Appends\ a\ LibraryItem\ to\ the\ itemList.\n comment1.text=\n\ Inserts\ object\ value\ item\ alongside\ key\ of\ @itemCode\ into\ @itemsMap.\n
comment2.params=user comment2.params=user
comment2.target=void\ storeUser(LibraryUser) comment2.target=void\ storeUser(LibraryUser)
comment2.text=\n\ Appends\ a\ LibraryUser\ to\ the\ userList.\n comment2.text=\n\ Inserts\ object\ value\ user\ alongside\ key\ of\ @userID\ into\ @customerMap.\n\ If\ the\ userID\ is\ set\ to\ unknown,\ it\ will\ call\ @generateUserID.\n
comment3.params=prefix\ length comment3.params=reservation
comment3.target=java.lang.String\ generateUserID(java.lang.String,\ int) comment3.target=void\ storeLibraryReservation(LibraryReservation)
comment3.text=\n\ Returns\ a\ random\ unique\ user\ ID\ by\ specifying\ \n\ @prefix\ -\ arbitrary\ alphanumeric\ prefix\n\ @length\ -\ length\ of\ numeric\ ID\n\ and\ returning\ a\ unique\ user\ id\n\ @uuid\ -\ a\ unique\ string\ starting\ with\ @prefix,\ and\ concat.\ with\ a\ random\ number\n\ \n\ Example\:\ length\ \=\ 6,\ expected\ result\ should\ be\ under\ 999,999\ and\ above\ 99,999.\n\ If\ we\ just\ use\ 10^(length),\ this\ would\ generate\ any\ number\ under\ 1,000,000.\n\ This\ is\ an\ issue\ since\ any\ integers\ below\ 100,000\ can\ be\ used,\ which\ would\ be\ incorrect.\n\ By\ using\ the\ offset\ of\ a\ factor\ of\ 10\ below\ the\ desired\ length\ we\ can\ generate\ between\ 0\ and\ 899,999.\n\ After\ this,\ we\ can\ add\ 100,000\ back\ to\ the\ number\ to\ ensure\ a\ baseline\ length\ is\ maintained.\n\ \n\ Note\:\ I\ am\ aware\ that\ this\ is\ overengineered,\ and\ that\ several\ random\ integers\ of\ amount\ @length\ could\ be\ used,\ \n\ but\ this\ is\ considerably\ more\ efficient\ since\ there\ is\ no\ iteration\ involved\ in\ the\ creation\ of\ the\ ID.\ O(1)\n comment3.text=\n\ Inserts\ object\ value\ reservation\ alongside\ key\ of\ @reservationNo\ into\ @libraryReservationMap.\n
comment4.params= comment4.params=prefix\ length
comment4.target=void\ writeUserData() comment4.target=java.lang.String\ generateUserID(java.lang.String,\ int)
comment4.text=\n\ A\ method\ to\ output\ all\ user\ data\ to\ a\ file\ using\ a\ fileDialog\ to\ specify\ file\ location.\n\ \n\ Note\:\ Potentially\ could\ implement\ the\ HashSet\ made\ for\ GenerateUserID\ to\ avoid\ unnecessary\ recursion.\n comment4.text=\n\ Returns\ a\ random\ unique\ user\ ID\ by\ specifying\ \n\ @prefix\ -\ arbitrary\ alphanumeric\ prefix\n\ @length\ -\ length\ of\ numeric\ ID\n\ and\ returning\ a\ unique\ user\ id\n\ @uuid\ -\ a\ unique\ string\ starting\ with\ @prefix,\ and\ concat.\ with\ a\ random\ number\n\ \n\ Example\:\ length\ \=\ 6,\ expected\ result\ should\ be\ under\ 999,999\ and\ above\ 99,999.\n\ If\ we\ just\ use\ 10^(length),\ this\ would\ generate\ any\ number\ under\ 1,000,000.\n\ This\ is\ an\ issue\ since\ any\ integers\ below\ 100,000\ can\ be\ used,\ which\ would\ be\ incorrect.\n\ By\ using\ the\ offset\ of\ a\ factor\ of\ 10\ below\ the\ desired\ length\ we\ can\ generate\ between\ 0\ and\ 899,999.\n\ After\ this,\ we\ can\ add\ 100,000\ back\ to\ the\ number\ to\ ensure\ a\ baseline\ length\ is\ maintained.\n\ \n\ Note\:\ I\ am\ aware\ that\ this\ is\ overengineered,\ and\ that\ several\ random\ integers\ of\ amount\ @length\ could\ be\ used,\ \n\ but\ this\ is\ considerably\ more\ efficient\ since\ there\ is\ no\ iteration\ involved\ in\ the\ creation\ of\ the\ ID.\ O(1)\n
comment5.params= comment5.params=
comment5.target=void\ printAll() comment5.target=void\ writeUserData()
comment5.text=\n\ Prints\ to\ the\ terminal,\ in\ a\ human-readable\ format,\ all\ items\ in\ the\ itemList\n\ \n\ Contains\ a\ marker\ at\ the\ start\ and\ end\ to\ visualise\ in\ terminal\ output.\n comment5.text=\n\ A\ method\ to\ output\ all\ user\ data\ to\ a\ file\ using\ a\ fileDialog\ to\ specify\ file\ location.\n\ \n\ Note\:\ Potentially\ could\ implement\ the\ HashSet\ made\ for\ GenerateUserID\ to\ avoid\ unnecessary\ recursion.\n
comment6.params= comment6.params=
comment6.target=void\ readData() comment6.target=void\ printAll()
comment6.text=\n\ A\ method\ to\ read\ all\ data\ from\ files\ using\ a\ fileDialog\ to\ specify\ file\ location.\n\ This\ will\ create\ the\ corresponding\ objects\ depending\ on\ flags\ contained\ in\ the\ file\n\ and\ populate\ it's\ fields.\n\ \n\ Default\ flag\ value\:\ "book",\ to\ support\ legacy\ files.\ This\ will\ not\ interfere\ with\ \n\ files\ of\ different\ flag\ orders.\n comment6.text=\n\ Prints\ to\ the\ terminal,\ in\ a\ human-readable\ format,\ all\ items\ in\ the\ itemList\n\ \n\ Contains\ a\ marker\ at\ the\ start\ and\ end\ to\ visualise\ in\ terminal\ output.\n
numComments=7 comment7.params=
comment7.target=void\ readData()
comment7.text=\n\ A\ method\ to\ read\ all\ data\ from\ files\ using\ a\ fileDialog\ to\ specify\ file\ location.\n\ This\ will\ create\ the\ corresponding\ objects\ depending\ on\ flags\ contained\ in\ the\ file\n\ and\ populate\ it's\ fields.\n\ \n\ Default\ flag\ value\:\ "book",\ to\ support\ legacy\ files.\ This\ will\ not\ interfere\ with\ \n\ files\ of\ different\ flag\ orders.\n
numComments=8

View File

@@ -7,10 +7,13 @@
*/ */
// Import all required libraries. Not using .* as it is not good practice due to potential conflicts. // Import all required libraries. Not using .* as it is not good practice due to potential conflicts.
import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.Scanner; import java.util.Scanner;
import java.util.Random; import java.util.Random;
import java.util.Map;
import java.util.HashMap;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.awt.FileDialog; import java.awt.FileDialog;
@@ -19,40 +22,65 @@ import java.io.PrintWriter;
public class Library public class Library
{ {
private ArrayList<LibraryItem> itemList; // Initialise an ArrayList of name itemList to store Library Items // private List<LibraryItem> itemList; // Initialise an ArrayList of name itemList to store Library Items
private ArrayList<LibraryUser> userList; // Initialise an ArrayList of name userList to store Library Users // private List<LibraryUser> userList; // Initialise an ArrayList of name userList to store Library Users
private HashSet<String> uuidSet; // Initialise a Hash Set of name uuidSet ( unique user identifier ) to store unique user IDs for efficient O(1) searching private HashSet<String> uuidSet; // Initialise a Hash Set of name uuidSet ( unique user identifier ) to store unique user IDs for efficient O(1) searching
private Map<String, LibraryUser> customerMap;
private Map<String, LibraryItem> itemsMap;
private Map<String, LibraryReservation> libraryReservationMap;
/* /*
* Constructor for objects of class Library * Constructor for objects of class Library
*/ */
public Library() public Library()
{ {
itemList = new ArrayList<LibraryItem>(); // itemList = new ArrayList<LibraryItem>();
userList = new ArrayList<LibraryUser>(); // userList = new ArrayList<LibraryUser>();
uuidSet = new HashSet<String>(); uuidSet = new HashSet<String>();
customerMap = new HashMap<String, LibraryUser>();
itemsMap = new HashMap<String, LibraryItem>();
libraryReservationMap = new HashMap<String, LibraryReservation>();
} }
/* /*
* Appends a LibraryItem to the itemList. * Inserts object value item alongside key of @itemCode into @itemsMap.
*/ */
public void storeItem( LibraryItem item ) public void storeItem( LibraryItem item )
{ {
itemList.add( item ); // itemList.add( item );
itemsMap.put( item.getItemCode(), item );
} }
/* /*
* Appends a LibraryUser to the userList. * Inserts object value user alongside key of @userID into @customerMap.
* If the userID is set to unknown, it will call @generateUserID.
*/ */
public void storeUser( LibraryUser user ) public void storeUser( LibraryUser user )
{ {
userList.add( user ); // userList.add( user );
System.out.println( "User Storing: " + user.getFirstName() );
if ( user.getUserID().equals( "unknown" ) ) if ( user.getUserID().equals( "unknown" ) )
{ {
user.setUserID( generateUserID( "AB-", 6 ) ); user.setUserID( generateUserID( "AB-", 6 ) );
} }
customerMap.put( user.getUserID(), user );
} }
/*
* Inserts object value reservation alongside key of @reservationNo into @libraryReservationMap.
*/
public void storeLibraryReservation( LibraryReservation reservation )
{
libraryReservationMap.put( reservation.getReservationNo(), reservation );
}
public void generateReservationNo()
{
if( libraryReservationMap.values() = null )
return "000001";
}
/* /*
* Returns a random unique user ID by specifying * Returns a random unique user ID by specifying
* @prefix - arbitrary alphanumeric prefix * @prefix - arbitrary alphanumeric prefix
@@ -111,7 +139,8 @@ public class Library
FileDialog fileBox = new FileDialog( frame, "Save", FileDialog.SAVE ); // Initialise file dialog box to save written data. FileDialog fileBox = new FileDialog( frame, "Save", FileDialog.SAVE ); // Initialise file dialog box to save written data.
fileBox.setVisible( true ); fileBox.setVisible( true );
PrintWriter writer = new PrintWriter( new File( fileBox.getDirectory() + fileBox.getFile() ) ); PrintWriter writer = new PrintWriter( new File( fileBox.getDirectory() + fileBox.getFile() ) );
for ( LibraryUser user : userList ){ // for ( LibraryUser user : userList ){
for ( LibraryUser user : customerMap.values() ) {
user.writeData( writer ); user.writeData( writer );
} }
writer.close(); writer.close();
@@ -129,7 +158,7 @@ public class Library
public void printAll() public void printAll()
{ {
System.out.println("\n\nStart Detail Print"); System.out.println("\n\nStart Detail Print");
for( LibraryItem item : itemList ) for( LibraryItem item : itemsMap.values() )
{ {
System.out.println("---------------"); System.out.println("---------------");
item.printDetails(); item.printDetails();
@@ -215,6 +244,7 @@ public class Library
} }
else if ( typeFlag.equals( "user" ) ) { else if ( typeFlag.equals( "user" ) ) {
//Process User Data //Process User Data
System.out.println( "User: " + lineItem );
Scanner detailScanner = new Scanner ( lineItem ).useDelimiter(","); Scanner detailScanner = new Scanner ( lineItem ).useDelimiter(",");
LibraryUser user = new LibraryUser(); LibraryUser user = new LibraryUser();
user.readData( detailScanner ); user.readData( detailScanner );

View File

@@ -0,0 +1,35 @@
#BlueJ class context
comment0.params=reservationNo\ itemCode\ userID\ startDate\ noOfDays
comment0.target=LibraryReservation(java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String,\ int)
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ LibraryReservation\n
comment1.params=
comment1.target=java.lang.String\ getReservationNo()
comment1.text=\n\ Return\ value\ of\ @reservationNo\n
comment10.params=noOfDays
comment10.target=void\ setNoOfDays(int)
comment10.text=\n\ Set\ @noOfDays\ to\ a\ new\ value\n
comment2.params=
comment2.target=java.lang.String\ getItemCode()
comment2.text=\n\ Return\ value\ of\ @itemCode\n
comment3.params=
comment3.target=java.lang.String\ getUserID()
comment3.text=\n\ Return\ value\ of\ @userID\n
comment4.params=
comment4.target=java.util.Date\ getStartDate()
comment4.text=\n\ Return\ value\ of\ @startDate\n
comment5.params=
comment5.target=int\ getNoOfDays()
comment5.text=\n\ Return\ value\ of\ @noOfDays\n
comment6.params=reservationNo
comment6.target=void\ setReservationNo(java.lang.String)
comment6.text=\n\ Set\ @reservationNo\ to\ a\ new\ value\n
comment7.params=itemCode
comment7.target=void\ setItemCode(java.lang.String)
comment7.text=\n\ Set\ @itemCode\ to\ a\ new\ value\n
comment8.params=userID
comment8.target=void\ setUserID(java.lang.String)
comment8.text=\n\ Set\ @userID\ to\ a\ new\ value\n
comment9.params=startDate
comment9.target=void\ setStartDate(java.lang.String)
comment9.text=\n\ Set\ @startDate\ to\ a\ new\ value\n
numComments=11

View File

@@ -0,0 +1,125 @@
/**
* A class to track and allow for reservations in a Library
*
* @George Wilkinson
* @1.0
*/
import java.util.Date;
public class LibraryReservation
{
// instance variables
private String reservationNo;
private String itemCode;
private String userID;
private Date startDate;
private int noOfDays;
/**
* Constructor for objects of class LibraryReservation
*/
public LibraryReservation( String reservationNo, String itemCode, String userID, String startDate, int noOfDays )
{
this.reservationNo = reservationNo;
this.itemCode = itemCode;
this.userID = userID;
this.startDate = DateUtil.convertStringToDate( startDate );
this.noOfDays = noOfDays;
}
/*
* Start Accessor
*/
/*
* Return value of @reservationNo
*/
public String getReservationNo()
{
return reservationNo;
}
/*
* Return value of @itemCode
*/
public String getItemCode()
{
return itemCode;
}
/*
* Return value of @userID
*/
public String getUserID()
{
return userID;
}
/*
* Return value of @startDate
*/
public Date getStartDate()
{
return startDate;
}
/*
* Return value of @noOfDays
*/
public int getNoOfDays()
{
return noOfDays;
}
/*
* End Accessor
*
* Start Mutator
*/
/*
* Set @reservationNo to a new value
*/
public void setReservationNo( String reservationNo )
{
this.reservationNo = reservationNo;
}
/*
* Set @itemCode to a new value
*/
public void setItemCode( String itemCode )
{
this.itemCode = itemCode;
}
/*
* Set @userID to a new value
*/
public void setUserID( String userID )
{
this.userID = userID;
}
/*
* Set @startDate to a new value
*/
public void setStartDate( String startDate )
{
this.startDate = DateUtil.convertStringToDate( startDate );
}
/*
* Set @noOfDays to a new value
*/
public void setNoOfDays( int noOfDays )
{
this.noOfDays = noOfDays;
}
/*
* End Mutator
*/
}

View File

@@ -1,4 +1,4 @@
unknown, Newton, David, E, Dr AB-906182, Smith, Sara, C, Ms
unknown, Gregson, Brian, R T, Mr AB-241496, Evans, David, , Dr
unknown, Evans, David, , Dr AB-769390, Newton, David, E, Dr
unknown, Smith, Sara, C, Ms AB-396038, Gregson, Brian, R T, Mr

View File

@@ -0,0 +1,4 @@
AB-906182, Smith, Sara, C, Ms
AB-241496, Evans, David, , Dr
AB-769390, Newton, David, E, Dr
AB-396038, Gregson, Brian, R T, Mr

View File

@@ -17,14 +17,20 @@ dependency5.type=UsesDependency
dependency6.from=Library dependency6.from=Library
dependency6.to=LibraryUser dependency6.to=LibraryUser
dependency6.type=UsesDependency dependency6.type=UsesDependency
dependency7.from=LibraryReservation
dependency7.to=DateUtil
dependency7.type=UsesDependency
dependency8.from=Library
dependency8.to=LibraryReservation
dependency8.type=UsesDependency
objectbench.height=76 objectbench.height=76
objectbench.width=940 objectbench.width=940
package.editor.height=874 package.editor.height=874
package.editor.width=814 package.editor.width=814
package.editor.x=0 package.editor.x=0
package.editor.y=31 package.editor.y=31
package.numDependencies=6 package.numDependencies=8
package.numTargets=9 package.numTargets=11
package.showExtends=true package.showExtends=true
package.showUses=true package.showUses=true
project.charset=UTF-8 project.charset=UTF-8
@@ -39,8 +45,34 @@ target1.showInterface=false
target1.type=AbstractTarget target1.type=AbstractTarget
target1.typeParameters= target1.typeParameters=
target1.width=110 target1.width=110
target1.x=350 target1.x=130
target1.y=220 target1.y=240
target10.editor.height=700
target10.editor.width=900
target10.editor.x=960
target10.editor.y=146
target10.height=50
target10.name=AudioVisual
target10.naviview.expanded=true
target10.showInterface=false
target10.type=AbstractTarget
target10.typeParameters=
target10.width=110
target10.x=380
target10.y=240
target11.editor.height=700
target11.editor.width=900
target11.editor.x=40
target11.editor.y=51
target11.height=50
target11.name=LibraryReservation
target11.naviview.expanded=true
target11.showInterface=false
target11.type=ClassTarget
target11.typeParameters=
target11.width=160
target11.x=230
target11.y=440
target2.editor.height=786 target2.editor.height=786
target2.editor.width=960 target2.editor.width=960
target2.editor.x=34 target2.editor.x=34
@@ -52,8 +84,8 @@ target2.showInterface=false
target2.type=ClassTarget target2.type=ClassTarget
target2.typeParameters= target2.typeParameters=
target2.width=80 target2.width=80
target2.x=580 target2.x=360
target2.y=320 target2.y=340
target3.editor.height=700 target3.editor.height=700
target3.editor.width=900 target3.editor.width=900
target3.editor.x=557 target3.editor.x=557
@@ -65,8 +97,8 @@ target3.showInterface=false
target3.type=AbstractTarget target3.type=AbstractTarget
target3.typeParameters= target3.typeParameters=
target3.width=120 target3.width=120
target3.x=470 target3.x=250
target3.y=90 target3.y=110
target4.editor.height=1049 target4.editor.height=1049
target4.editor.width=1920 target4.editor.width=1920
target4.editor.x=0 target4.editor.x=0
@@ -78,34 +110,34 @@ target4.showInterface=false
target4.type=ClassTarget target4.type=ClassTarget
target4.typeParameters= target4.typeParameters=
target4.width=80 target4.width=80
target4.x=690 target4.x=470
target4.y=360 target4.y=380
target5.editor.height=700 target5.editor.height=700
target5.editor.width=1619 target5.editor.width=900
target5.editor.x=192 target5.editor.x=55
target5.editor.y=267 target5.editor.y=274
target5.height=50 target5.height=50
target5.name=Periodical target5.name=LibraryUser
target5.naviview.expanded=true target5.naviview.expanded=true
target5.showInterface=false target5.showInterface=false
target5.type=ClassTarget target5.type=ClassTarget
target5.typeParameters= target5.typeParameters=
target5.width=90 target5.width=110
target5.x=280 target5.x=100
target5.y=360 target5.y=170
target6.editor.height=700 target6.editor.height=700
target6.editor.width=900 target6.editor.width=1619
target6.editor.x=55 target6.editor.x=192
target6.editor.y=274 target6.editor.y=267
target6.height=50 target6.height=50
target6.name=LibraryUser target6.name=Periodical
target6.naviview.expanded=true target6.naviview.expanded=true
target6.showInterface=false target6.showInterface=false
target6.type=ClassTarget target6.type=ClassTarget
target6.typeParameters= target6.typeParameters=
target6.width=110 target6.width=90
target6.x=320 target6.x=60
target6.y=150 target6.y=380
target7.editor.height=1049 target7.editor.height=1049
target7.editor.width=1920 target7.editor.width=1920
target7.editor.x=0 target7.editor.x=0
@@ -117,31 +149,31 @@ target7.showInterface=false
target7.type=ClassTarget target7.type=ClassTarget
target7.typeParameters= target7.typeParameters=
target7.width=80 target7.width=80
target7.x=400 target7.x=180
target7.y=320 target7.y=340
target8.editor.height=1049 target8.editor.height=700
target8.editor.width=1920 target8.editor.width=900
target8.editor.x=0 target8.editor.x=40
target8.editor.y=31 target8.editor.y=51
target8.height=50 target8.height=50
target8.name=Library target8.name=DateUtil
target8.naviview.expanded=true target8.naviview.expanded=true
target8.showInterface=false target8.showInterface=false
target8.type=ClassTarget target8.type=ClassTarget
target8.typeParameters= target8.typeParameters=
target8.width=100 target8.width=80
target8.x=480 target8.x=70
target8.y=220 target8.y=10
target9.editor.height=700 target9.editor.height=1049
target9.editor.width=900 target9.editor.width=960
target9.editor.x=960 target9.editor.x=0
target9.editor.y=146 target9.editor.y=31
target9.height=50 target9.height=50
target9.name=AudioVisual target9.name=Library
target9.naviview.expanded=true target9.naviview.expanded=true
target9.showInterface=false target9.showInterface=false
target9.type=AbstractTarget target9.type=ClassTarget
target9.typeParameters= target9.typeParameters=
target9.width=110 target9.width=100
target9.x=600 target9.x=260
target9.y=220 target9.y=240

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB