1.使用数组保存书,CD,磁带等信息,并能实现插入,删除,查找功能。插入,删除时,要显示操作正确与否的信息;查找时按关键字进行查找,显示查找结果。
2.重新设计第一题。设计一个媒体类,其中包含:书,CD及磁带三个子类。按照类的设计模式,完成它们的插入,删除及查找功能。

解决方案 »

  1.   

    使用数组保存书
    是用ArrayList吗?自己构建一个类,包装数组[],动态分配数组长度(自伸展,自收缩).Object[10];我以前好像做过这个题目,哈哈class Book{
    属性 get & set
    }
      

  2.   

    使用数组保存书 
    是用ArrayList吗? 自己构建一个类,包装数组[],动态分配数组长度(自伸展,自收缩). Object[10]; 我以前好像做过这个题目,哈哈 class Book{ 
    属性 get & set 
    }
      

  3.   

    使用数组保存书 
    是用ArrayList吗? 自己构建一个类,包装数组[],动态分配数组长度(自伸展,自收缩). Object[10]; 我以前好像做过这个题目,哈哈 class Book{ 
    属性 get & set 
    }
      

  4.   

    查找频繁的话,用ArrayList;
    插入、删除频繁的话,用LinkedList,
      

  5.   

    建立仓库类,要实现查找用HashMap好了。就是放一个HashMap成员。
    具体就是Map中Key放String 而Value中放多媒体这个类型。
    然后书,CD及磁带三个子类继承多媒体。
    就可以实现了。
    代码自己写。。
      

  6.   

    我忍不住要说下   shit
      

  7.   

    package com.lyb.csdn;import java.util.ArrayList;
    import java.util.Iterator;public class SaveInfo { /**
     * @author lybjust
     */
    ArrayList al = new ArrayList(); public void save(Book book) {
    al.add(book);
    System.out.println(book.getBookId() + "插入成功!");
    } public void del(Book book) {
    al.remove(book);
    System.out.println("删除成功   " + book.getBookId() + " "
    + book.getBookName());
    } public Book search(int id) {
    Book b = null;
    Iterator iter = al.iterator();
    while (iter.hasNext()) {
    b = (Book) iter.next();
    if (b.getBookId() == id) {
    break;
    }
    }
    return b;
    } public static void main(String[] args) {
    // TODO Auto-generated method stub
    Book b1 = new Book(1001, "CSDN_1");
    Book b2 = new Book(1002, "CSDN_2");
    Book b3 = new Book(1003, "CSDN_3");
    SaveInfo sa = new SaveInfo();
    sa.save(b1);
    sa.save(b2);
    sa.save(b3);
    sa.del(b2);
    Book bs = (Book) sa.search(1001);
    System.out.println("查询结果   " + bs.getBookId() + " " + bs.getBookName());
    }
    }/*class CD {
    private int cdId;
    private String cdName; public CD(int cdId, String cdName) {
    this.cdId = cdId;
    this.cdName = cdName;
    } public int getCdId() {
    return cdId;
    } public void setCdId(int cdId) {
    this.cdId = cdId;
    } public String getCdName() {
    return cdName;
    } public void setCdName(String cdName) {
    this.cdName = cdName;
    }
    }*/class Book {
    private int bookId;
    private String bookName; public Book(int bookId, String bookName) {
    this.bookId = bookId;
    this.bookName = bookName;
    } public int getBookId() {
    return bookId;
    } public void setBookId(int bookId) {
    this.bookId = bookId;
    } public String getBookName() {
    return bookName;
    } public void setBookName(String bookName) {
    this.bookName = bookName;
    }
    }
      

  8.   

    package com.failthshopping.daos;import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Vector;
    import com.failthshopping.beans.UserInfoBean;/**
     * @date 2008-8-4
     * @author East(张栋芳)
     * @UserInfoDao(用户信息表)的数据库操作
     */
    public class UserInfoDao
        extends BaseDao {
      public UserInfoDao() {
      }
      /****
       * 增加用户信息
       */
      public boolean insertUserInfo(UserInfoBean user) {
        Connection con = null;
        PreparedStatement pstmt = null;
        int result = 0;
        String strSql = "insert into userInfo(userName,password,sex,age,card,qq,email,telephone,address,buyPoint,sellPoint,headImg,isLogin,isVip,pwdQuestion,pwdAnswer,re)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
          con = super.createConnection();
          pstmt = con.prepareStatement(strSql);
          pstmt.setString(1, user.getUserName());
          pstmt.setString(2, user.getPassword());
          pstmt.setString(3, user.getSex());
          pstmt.setInt(4, user.getAge());
          pstmt.setString(5, user.getCard());
          pstmt.setString(6, user.getQq());
          pstmt.setString(7, user.getEmail());
          pstmt.setString(8, user.getTelephone());
          pstmt.setString(9, user.getAddress());
          pstmt.setInt(10, user.getBuyPoint());
          pstmt.setInt(11, user.getSellPoint());
          pstmt.setString(12, user.getHeadImg());
          pstmt.setInt(13, user.getIsLogin());
          pstmt.setInt(14, user.getIsVip());
          pstmt.setString(15, user.getPwdQuestion());
          pstmt.setString(16, user.getPwdAnswer());
          pstmt.setString(17, user.getRe());
          result = pstmt.executeUpdate();
        }
        catch (SQLException se) {
          se.printStackTrace();
        }
        finally {
          super.closeAll(con, pstmt);
          return result > 0 ? true : false;
        }
      }
      /****
       * 通过用户名修改用户基本信息
       */
      public boolean updateUserInfoByUserName(UserInfoBean user) {
        Connection con = null;
        PreparedStatement pstmt = null;
        int result = 0;
        String strSql = "update userInfo set password=?,sex=?,age=?,card=?,qq=?,email=?,telephone=? where userName=?";
        try {
          con = super.createConnection();
          pstmt = con.prepareStatement(strSql);
          pstmt.setString(1, user.getPassword());
          pstmt.setString(2, user.getSex());
          pstmt.setInt(3, user.getAge());
          pstmt.setString(4, user.getCard());
          pstmt.setString(5, user.getQq());
          pstmt.setString(6, user.getEmail());
          pstmt.setString(7, user.getTelephone());
          pstmt.setString(8, user.getUserName());
          result = pstmt.executeUpdate();
        }
        catch (SQLException se) {
          se.printStackTrace();
        }
        finally {
          super.closeAll(con, pstmt);
          return result > 0 ? true : false;
        }
      }
      /****
       * 通过用户名修改用户密码
       */
      public boolean updateUserPwdByUserName(UserInfoBean user) {
        Connection con = null;
        PreparedStatement pstmt = null;
        int result = 0;
        String strSql = "update userInfo set password=? where userName=?";
        try {
          con = super.createConnection();
          pstmt = con.prepareStatement(strSql);
          pstmt.setString(1, user.getPassword());
          pstmt.setString(2, user.getUserName());
          result = pstmt.executeUpdate();
        }
        catch (SQLException se) {
          se.printStackTrace();
        }
        finally {
          super.closeAll(con, pstmt);
          return result > 0 ? true : false;
        }
      }  /****
       * 通过用户名删除用户信息
       */
      public boolean deleteUserInfoByUserName(UserInfoBean user) {
        Connection con = null;
        PreparedStatement pstmt = null;
        int result = 0;
        String strSql = "delete userInfo where userName=?";
        try {
          con = super.createConnection();
          pstmt = con.prepareStatement(strSql);
          pstmt.setString(1, user.getUserName());
          result = pstmt.executeUpdate();
        }
        catch (SQLException se) {
          se.printStackTrace();
        }
        finally {
          super.closeAll(con, pstmt);
          return result > 0 ? true : false;
        }
      }
      /***
       * 通过用户名修改是否登录
       */
      public boolean updateByUserNameIsLogin(UserInfoBean user) {
        Connection con = null;
        PreparedStatement pstmt = null;
        String strSql = "update userInfo set isLogin = ? while userName=?";
        int result = 0;
        try {
          con = super.createConnection();
          pstmt = con.prepareStatement(strSql);
          pstmt.setInt(1, user.getIsLogin());
          pstmt.setString(2, user.getUserName());
          result = pstmt.executeUpdate();
        }
        catch (SQLException se) {
          se.printStackTrace();
        }
        finally {
          super.closeAll(con, pstmt);
          return result > 0 ? true : false;
        }
      }
      /***
       * 通过用户名修改是否是会员
       */
      public boolean updateByUserNameIsVip(UserInfoBean user) {
        Connection con = null;
        PreparedStatement pstmt = null;
        String strSql = "update userInfo set isVip = ? where userName=?";
        int result = 0;
        try {
          con = super.createConnection();
          pstmt = con.prepareStatement(strSql);
          pstmt.setInt(1, user.getIsVip());
          pstmt.setString(2, user.getUserName());
          result = pstmt.executeUpdate();
        }
        catch (SQLException se) {
          se.printStackTrace();
        }
        finally {
          super.closeAll(con, pstmt);
          return result > 0 ? true : false;
        }
      }
      /****
       * 查询是否存在用户名和密码
       */
      public boolean isExistsUserName(UserInfoBean user) {
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String strSql = "select * from userInfo where userName=? and password = ?";
        int result = 0;
        try {
          con = super.createConnection();
          pstmt = con.prepareStatement(strSql);      pstmt.setString(1, user.getUserName());
          pstmt.setString(2, user.getPassword());
          rs = pstmt.executeQuery();
          if (rs.next()) {
            result++;
          }
          else {
            return false;
          }
        }
        catch (SQLException se) {
          se.printStackTrace();
        }
        finally {
          super.closeAll(con, pstmt, rs);
          return result > 0 ? true : false;
        }
      }  /***
       * 通过用户名修改用户的密码问题和密码答案
       */
      public boolean updatePwdQuestion(UserInfoBean user) {
        int result = 0;
        Connection con = null;
        PreparedStatement pstmt = null;
        String strSql =
            "update userInfo set pwdQuestion =?,pwdAnswer=? where userName=?";
        try {
          con = super.createConnection();
          pstmt = con.prepareStatement(strSql);
          pstmt.setString(1, user.getPwdQuestion());
          pstmt.setString(2, user.getPwdAnswer());
          pstmt.setString(3, user.getUserName());
          result = pstmt.executeUpdate();
        }
        catch (SQLException se) {
          se.printStackTrace();
        }
        finally {
          super.closeAll(con, pstmt);
          return result > 0 ? true : false;
        }
      }
      }