import java.util.Map; import java.util.TreeMap; import java.sql.Connection; import java.sql.Statement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.DriverManager; class MultiSet { // Constructor and most methods not shown private Map counts; public MultiSet( ) { counts = new TreeMap( ); } // Return size of the MultiSet // If counts = [ hello=2, world=1, zebra=1 ] // This routine returns 4 public int size( ) { int total = 0; for( Integer occurs : counts.values( ) ) total += occurs; return total; } // Return true if x is in this Multiset public boolean contains( String x ) { return counts.get( x ) != null; } // Add a new item public void add( String x ) { Integer occurs = counts.get( x ); if( occurs == null ) counts.put( x, 1 ); else counts.put( x, occurs + 1 ); } // Remove an item. // If x is not found, return false. // If x is present with count 1, remove it from the map. // If x is present with count > 1, lower the count by 1. // If x was present return true. public boolean remove( String x ) { Integer occurs = counts.get( x ); if( occurs == null ) return false; if( occurs == 1 ) counts.remove( x ); else counts.put( x, occurs - 1 ); return true; } public String toString( ) { return counts.toString( ); } } class Sample5 { // Note: I used Schedule.txt instead of Schedule for // ease of testing with csv format public static void closedSections( Connection c, String name, String number ) throws SQLException { Statement s = c.createStatement( ); ResultSet rs = s.executeQuery( "SELECT Section FROM Schedule.txt WHERE " + "(Name=" + "'" + name + "')" + " AND " + "(Number=" + number + ")" + " AND " + "(Available=0)" ); while( rs.next( ) ) System.out.println( rs.getString( "Section" ) ); } public static void main( String [ ] args ) throws SQLException { MultiSet ms = new MultiSet( ); ms.add( "hello" ); ms.add( "world" ); ms.add( "zebra" ); ms.add( "hello" ); System.out.println( ms + " " + ms.size( ) ); ms.remove( "hello" ); System.out.println( ms + " " + ms.size( ) ); ms.remove( "hello" ); System.out.println( ms + " " + ms.size( ) ); Connection c = DriverManager.getConnection( "jdbc:odbc:Schedule" ); closedSections( c, "COP", "2250" ); } }