import java.util.Scanner; import java.util.NoSuchElementException; /** * Superclass of items / assets stored in a Library. * * @author George Wilkinson * @version 4.1 */ public abstract class LibraryItem { // instance variables private String title; private String itemCode; private int cost; private int timesBorrowed; private boolean onLoan; /* * Field Accessor Start */ public String getTitle() { return title; } public String getItemCode() { return itemCode; } public int getCost() { return cost; } public int getTimesBorrowed() { return timesBorrowed; } public boolean getOnLoan() { return onLoan; } /* * Field Accessor End * * Field Mutator Start */ public void setTitle( String title ) { this.title = title; } public void setItemCode( String itemCode ) { this.itemCode = itemCode; } public void setCost( int cost ) { this.cost = cost; } public void setTimesBorrowed( int timesBorrowed ) { this.timesBorrowed = timesBorrowed; } public void setOnLoan( boolean onLoan ) { this.onLoan = onLoan; } /* * Field Mutator End */ /** * Forms field variables in a desired format, prints * to the PrintWriter, and flush to the file. * Takes Parameter * PrintWriter @writer */ public void printDetails() { System.out.println("Item Code: " + itemCode + "\nTitle: " + title + "\nCost: £" + ( ( float )( cost ) )/100 + // Convert cost in pence to £pounds.pence to be more readable. "\nBorrowed " + getTimesBorrowed() + " times." ); if( getOnLoan() ) System.out.println( "On Loan"); else System.out.println( "Available to Loan"); } /** * Populate the fields with details from the scanner * Takes Parameters * Scanner @detailScanner */ public void readItemData( Scanner detailScanner ) { if ( detailScanner != null ) { this.title = detailScanner.next().trim(); this.itemCode = detailScanner.next().trim(); this.cost = Integer.parseInt( detailScanner.next().trim() ); this.timesBorrowed = Integer.parseInt( detailScanner.next().trim() ); this.onLoan = Boolean.parseBoolean( detailScanner.next().trim() ); } } }