Skip to content

Instantly share code, notes, and snippets.

Created April 9, 2011 14:21
Show Gist options
  • Save anonymous/911420 to your computer and use it in GitHub Desktop.
Save anonymous/911420 to your computer and use it in GitHub Desktop.
package database;
import java.sql.*;
public class BookShelf {
static Connection link;
static Statement statement;
static ResultSet results;
public static void main(String[] args)
{
try{
// Step 1
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Step 2
link = DriverManager.getConnection("jdbc:odbc:Books","","");
// Step 3
statement = link.createStatement();
}
catch(ClassNotFoundException e){
System.out.println("Unable to load driver");
System.exit(1);
}
catch(SQLException e){
System.out.println("Cannot connect to the database");
System.exit(1);
}
new BookShelf();
} //end main
public BookShelf()
{
/* another step is to create a method called addBook that adds Book objects to the ArrayList **/
public void addBook(Book theBook)
{
books.add(theBook);
}
/* now we create a method called sizeOfBookshelf that returns the size of the ArrayList in other words,
* it returns the number of books hold in array list
*/
public int sizeOfBookshelf()
{
return books.size();
}
/* now we create a method called costOfBookshelf that returns the cost of all the books in the ArrayList
* to do it we use a for loop that goes through all books and adds their cost one by one and gives us total
*/
public double costOfBookshelf()
{ double totalCost = 0;
for( Book book : books)
{totalCost = book.getCost() + totalCost ;}
return totalCost;
}
//create a method called highestPricePAid that will return the cost of the most expensive book in the ArrayList
public double highestPricePaid()
{
double max = 0;
double temp = 0 ;
for ( Book book : books)
{{ temp = book.getCost(); }
if (temp > max)
max = temp; }
return max ;
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment