当前位置: 首页 > news >正文

免费做网站电话免费网络空间搜索引擎

免费做网站电话,免费网络空间搜索引擎,无锡网站推广优化,石家庄手机网站建站AdminUser是管理员类 NormalUser是用户类 AddOperation是增加图书类 BorrowOperation是借书类 DelOperation是删除图书类 ExitOperation是退出类 FindOperation是查找图书类 IOPeration是接口 ReturnOperation是还书类 ShowOperation是显示所有图书类 注意&#xff1a…

AdminUser是管理员类

NormalUser是用户类

AddOperation是增加图书类

BorrowOperation是借书类

DelOperation是删除图书类

ExitOperation是退出类

FindOperation是查找图书类

IOPeration是接口

ReturnOperation是还书类

ShowOperation是显示所有图书类

注意:我的数据库有个cnm数据库,如果你没有的话,需要先创建一个cnm数据库

sql建表语句:

drop table if exists book;
create table book
(	id			   	 bigint  auto_increment,name              varchar(255), author            varchar(255),price             int,type              varchar(255),isBorrowed        varchar(255),primary key (id)
);insert book(name,author,price,type,isBorrowed) values('西游记','吴承恩',35,'小说','未借阅');
insert book(name,author,price,type,isBorrowed) values('活着','余华',40,'文学','未借阅');
commit;
select * from book;

Book类

package book;public class Book {private String name;//书名private String author;//作者private int price;//价格private String type;//类型private boolean isBorrowed;//是否被借出,初始值是false,在构造方法中不用写public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public int getPrice() {return price;}public void setPrice(int price) {this.price = price;}public String getType() {return type;}public void setType(String type) {this.type = type;}public boolean isBorrowed() {return isBorrowed;}public void setBorrowed(boolean borrowed) {isBorrowed = borrowed;}public Book(String name, String author, int price, String type) {this.name = name;this.author = author;this.price = price;this.type = type;}@Overridepublic String toString() {return "Book{" +"name:" + name +",  author:" + author +",  price:" + price +",  type=:" + type +",  isBorrowed:" +(isBorrowed==true?"  已借出":"  未借出") +'}';}
}

BookList类

package book;public class BookList {private static final int DEFAULT_SIZE=10;//该常量定义一个书架有多少本书private  Book[] books=new  Book[DEFAULT_SIZE];private int usedSize;//记录下当前book数组中有几本书public int getUsedSize() {return usedSize;}public void setUsedSize(int usedSize) {this.usedSize = usedSize;}}

User类

package user;import book.BookList;
import opera.IOPeration;public abstract class User {//抽象类protected String name;//名字.这边的protect代表的是名字的权限。如果是private,它只能在同一个包的同一类使用。就不能让AdminUser类继承了。写public的话//权限太大了,不是很好。protected IOPeration[] ioPerations;public User(String name) {//构造方法this.name = name;}public abstract int menu();//抽象方法,打印菜单,因为有了choice返回值int类型,所以void改成intpublic void doWork(int choice, BookList bookList){//通过选择的操作,去选择执行数组下的哪个操作this.ioPerations[choice].work(bookList);}
}

AdminUser类

package user;import opera.*;import java.util.Scanner;public class AdminUser extends User{public AdminUser(String name) {super(name);this.ioPerations=new IOPeration[]{new ExitOperation(),new FindOperation(),new AddOperation(),new DelOperation(),new ShowOperation()};System.out.println("欢迎管理员:"+name);}@Overridepublic int menu() {//因为返回值choice是int类型的System.out.println("____________________________________");System.out.println("1.查找图书");System.out.println("2.新增图书");System.out.println("3.删除图书");System.out.println("4.显示图书");System.out.println("0.退出系统");System.out.println("请选择你需要的功能:");Scanner scanner=new Scanner(System.in);int choice=scanner.nextInt();return choice;}}

NormalUser类

package user;import opera.*;import java.util.Scanner;public class NormalUser extends User {public NormalUser(String name) {super(name);this.ioPerations = new IOPeration[]{//引用,这边用super也可以,因为这里没有同名的,不需要做区分。用this最好new ExitOperation(),new FindOperation(),new BorrowOperation(),new ReturnOperation()};System.out.println("欢迎用户:"+name);}@Overridepublic int menu() {System.out.println("_________________");System.out.println("1.查找图书!");System.out.println("2.借阅图书!");System.out.println("3.归还图书!");System.out.println("0.退出系统!");Scanner scanner = new Scanner(System.in);int choice = scanner.nextInt();return choice;}}

IOPeration接口

package opera;import book.BookList;public interface IOPeration {//创建接口void work(BookList bookList);//抽象方法//功能主要是针对图书的,也就是针对书架。
}

AddOperation类

package opera;import book.Book;
import book.BookList;import java.sql.*;
import java.util.Scanner;public class AddOperation implements IOPeration {public void work(BookList bookList) {System.out.println("新增图书!");Scanner scanner = new Scanner(System.in);System.out.println("请输入新增图书名字:");String name = scanner.nextLine();System.out.println("请输入新增图书作者:");String author = scanner.nextLine();System.out.println("请输入价格");int price = scanner.nextInt();//吸收回车符号scanner.nextLine();System.out.println("请输入图书类型:");String type = scanner.nextLine();//添加到数据库//定义3个变量Connection conn = null;Statement stmt = null;ResultSet rs = null;try {//1.注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//2.获取连接conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cnm", "root", "123456");//3.获取数据库操作对象(执行sql语句的对象)stmt = conn.createStatement();//4.执行sql语句String sql = "select * from book where name='"+name+"'";rs = stmt.executeQuery(sql);if(!rs.next()) {String s = "insert into book avalues('"+name+"','"+author+"','"+price+"','"+type+"','未借阅')";int x= stmt.executeUpdate(s);if(x>0)System.out.println("新增图书成功!");elseSystem.out.println("新增图书失败");}else{System.out.println("已经有这本书了");}}catch (Exception e) {e.printStackTrace();} finally {//6.释放资源if (rs != null) {try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if (stmt != null) {try {stmt.close();} catch (SQLException e) {e.printStackTrace();}}if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}}}

DelOperation类

package opera;import book.Book;
import book.BookList;import java.sql.*;
import java.util.Scanner;public class DelOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("删除图书!");System.out.println("请输入要删除图书的名称");Scanner scanner=new Scanner(System.in);String name=scanner.nextLine();//添加到数据库//定义3个变量Connection conn = null;Statement stmt = null;ResultSet rs = null;try {//1.注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//2.获取连接conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cnm", "root", "123456");//3.获取数据库操作对象(执行sql语句的对象)stmt = conn.createStatement();//4.执行sql语句String sql = "select * from book where name='"+name+"'";rs = stmt.executeQuery(sql);if(rs.next()) {String s = "delete from book where name='"+name+"'";int x = stmt.executeUpdate(s);if(x>0)System.out.println("删除成功!");elseSystem.out.println("删除失败!");}else{System.out.println("没有这本书");}}catch (Exception e) {e.printStackTrace();} finally {//6.释放资源if (rs != null) {try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if (stmt != null) {try {stmt.close();} catch (SQLException e) {e.printStackTrace();}}if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}}}

FindOperation类

package opera;import book.Book;
import book.BookList;import java.sql.*;
import java.util.Scanner;public class FindOperation implements IOPeration{//继承@Overridepublic void work(BookList bookList) {//重写IOPeration类中的work方法System.out.println("查找图书!");System.out.println("请输入要查找的图书名字");Scanner scanner=new Scanner(System.in);String name=scanner.nextLine();//添加到数据库//定义3个变量Connection conn = null;Statement stmt = null;ResultSet rs = null;try {//1.注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//2.获取连接conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cnm", "root", "123456");//3.获取数据库操作对象(执行sql语句的对象)stmt = conn.createStatement();//4.执行sql语句String sql = "select * from book where name='"+name+"'";rs = stmt.executeQuery(sql);if(rs.next()) {while (true) {String s1 = rs.getString("name");String s2 = rs.getString("author");int s3 = rs.getInt("price");String s4 = rs.getString("type");String s5 = rs.getString("isBorrowed");System.out.print("name: " + s1);System.out.print(",author:" + s2);System.out.print(",price:" + s3);System.out.print(",type:" + s4);System.out.print(",isBorrowed:" + s5);System.out.println();if(!rs.next())break;}}else{System.out.println("没有这本书");}}catch (Exception e) {e.printStackTrace();} finally {//6.释放资源if (rs != null) {try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if (stmt != null) {try {stmt.close();} catch (SQLException e) {e.printStackTrace();}}if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}}}

ShowOperation类

package opera;import book.BookList;import java.sql.*;public class ShowOperation implements IOPeration{@Overridepublic void work(BookList bookList) {//添加到数据库//定义3个变量Connection conn = null;Statement stmt = null;ResultSet rs = null;try {//1.注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//2.获取连接conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cnm", "root", "123456");//3.获取数据库操作对象(执行sql语句的对象)stmt = conn.createStatement();//4.执行sql语句String sql = "select * from book ";rs = stmt.executeQuery(sql);if(rs.next()) {while (true) {String s1 = rs.getString("name");String s2 = rs.getString("author");int s3 = rs.getInt("price");String s4 = rs.getString("type");String s5 = rs.getString("isBorrowed");System.out.print("name: " + s1);System.out.print(",author:" + s2);System.out.print(",price:" + s3);System.out.print(",type:" + s4);System.out.print(",isBorrowed:" + s5);System.out.println();if(!rs.next())break;}}else{System.out.println("没有这本书");}}catch (Exception e) {e.printStackTrace();} finally {//6.释放资源if (rs != null) {try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if (stmt != null) {try {stmt.close();} catch (SQLException e) {e.printStackTrace();}}if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}}
}

BorrowOperation类

package opera;import book.Book;
import book.BookList;import java.sql.*;
import java.util.Scanner;public class BorrowOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("借阅图书!");System.out.println("请输入要借阅的图书名字:");Scanner scanner=new Scanner(System.in);String name=scanner.nextLine();//添加到数据库//定义3个变量Connection conn = null;Statement stmt = null;ResultSet rs = null;try {//1.注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//2.获取连接conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cnm", "root", "123456");//3.获取数据库操作对象(执行sql语句的对象)stmt = conn.createStatement();//4.执行sql语句String sql = "select * from book where name='"+name+"'";rs = stmt.executeQuery(sql);if(rs.next()) {String s1 = "select isBorrowed from book where name='"+name+"'";rs = stmt.executeQuery(s1);if(rs.next()){String str = rs.getString("isBorrowed");if(str.equals("未借阅")) {String s2 = "update book set isBorrowed='已借阅' where name='" + name + "'";int x = stmt.executeUpdate(s2);if (x > 0)System.out.println("借阅成功!");elseSystem.out.println("借阅失败");}}}else{System.out.println("没有这本书");}}catch (Exception e) {e.printStackTrace();} finally {//6.释放资源if (rs != null) {try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if (stmt != null) {try {stmt.close();} catch (SQLException e) {e.printStackTrace();}}if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}}
}

ReturnOperation类

package opera;import book.Book;
import book.BookList;import java.sql.*;
import java.util.Scanner;public class ReturnOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("归还图书!");System.out.println("请输入要归还的图书名字:");Scanner scanner=new Scanner(System.in);String name=scanner.nextLine();//添加到数据库//定义3个变量Connection conn = null;Statement stmt = null;ResultSet rs = null;try {//1.注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//2.获取连接conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cnm", "root", "123456");//3.获取数据库操作对象(执行sql语句的对象)stmt = conn.createStatement();//4.执行sql语句String sql = "select * from book where name='"+name+"'";rs = stmt.executeQuery(sql);if(rs.next()) {String s1 = "select isBorrowed from book where name='"+name+"'";rs = stmt.executeQuery(s1);if(rs.next()){String str = rs.getString("isBorrowed");if(str.equals("已借阅")) {String s2 = "update book set isBorrowed='未借阅' where name='" + name + "'";int x = stmt.executeUpdate(s2);if (x > 0)System.out.println("归还成功!");elseSystem.out.println("归还失败");}}}else{System.out.println("没有这本书");}}catch (Exception e) {e.printStackTrace();} finally {//6.释放资源if (rs != null) {try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if (stmt != null) {try {stmt.close();} catch (SQLException e) {e.printStackTrace();}}if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}}
}

ExitOperation类

package opera;import book.BookList;public class ExitOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("退出系统!");System.exit(0);//退出系统。0代表正常退出}
}

Main类

import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;import java.util.Scanner;public class Main {//登录public static User login() {System.out.println("请输入你的姓名:");Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();System.out.println("请选择你的身份:1->管理员 0->普通用户");int choice = scanner.nextInt();if (choice == 1) {//说明是管理员//由于有返回值,所以我们的方法返回值就不能写void了。但是我们也无法确定返回值是什么,可能是管理员,可能是用户。所以,用向上转型,写User.return new AdminUser(name);//返回实例化一个管理员对象} else {return new NormalUser(name);//返回实例化一个用户对象}}public static void main(String[] args) {User user = login();//执行上面的login方法BookList bookList = new BookList();while (true) {int choice = user.menu();//实现打印菜单user.doWork(choice, bookList);}}
}


文章转载自:
http://caidos.rzgp.cn
http://erodible.rzgp.cn
http://ophiuran.rzgp.cn
http://grab.rzgp.cn
http://scotticism.rzgp.cn
http://receivable.rzgp.cn
http://caesardom.rzgp.cn
http://clactonian.rzgp.cn
http://cinquedea.rzgp.cn
http://gray.rzgp.cn
http://patulin.rzgp.cn
http://blenny.rzgp.cn
http://weatherology.rzgp.cn
http://ingeniously.rzgp.cn
http://diphonemic.rzgp.cn
http://hindi.rzgp.cn
http://bas.rzgp.cn
http://kilocharacter.rzgp.cn
http://holography.rzgp.cn
http://fordo.rzgp.cn
http://lactescent.rzgp.cn
http://nitric.rzgp.cn
http://megaphone.rzgp.cn
http://nadine.rzgp.cn
http://surexcitation.rzgp.cn
http://lidocaine.rzgp.cn
http://aseismatic.rzgp.cn
http://highlighted.rzgp.cn
http://inkyo.rzgp.cn
http://yacht.rzgp.cn
http://abysm.rzgp.cn
http://estanciero.rzgp.cn
http://nutritious.rzgp.cn
http://pyrophobia.rzgp.cn
http://cheryl.rzgp.cn
http://ozarkian.rzgp.cn
http://dunam.rzgp.cn
http://xenix.rzgp.cn
http://hommos.rzgp.cn
http://pridian.rzgp.cn
http://tripleheaded.rzgp.cn
http://tetrabranchiate.rzgp.cn
http://ranular.rzgp.cn
http://jobless.rzgp.cn
http://southwestwards.rzgp.cn
http://linkup.rzgp.cn
http://stater.rzgp.cn
http://skinner.rzgp.cn
http://appurtenant.rzgp.cn
http://wormhole.rzgp.cn
http://flackery.rzgp.cn
http://affectlessly.rzgp.cn
http://secundum.rzgp.cn
http://menes.rzgp.cn
http://toll.rzgp.cn
http://prolicide.rzgp.cn
http://garrotter.rzgp.cn
http://cucurbit.rzgp.cn
http://renegotiate.rzgp.cn
http://sociopath.rzgp.cn
http://railing.rzgp.cn
http://associateship.rzgp.cn
http://polymerization.rzgp.cn
http://kingpin.rzgp.cn
http://resumptively.rzgp.cn
http://disbar.rzgp.cn
http://littorinid.rzgp.cn
http://nodule.rzgp.cn
http://driftingly.rzgp.cn
http://anecdote.rzgp.cn
http://antimonsoon.rzgp.cn
http://epigrammatism.rzgp.cn
http://piteously.rzgp.cn
http://zygodactyl.rzgp.cn
http://acromegalic.rzgp.cn
http://californiate.rzgp.cn
http://deliriant.rzgp.cn
http://osteoporosis.rzgp.cn
http://palau.rzgp.cn
http://winterclad.rzgp.cn
http://presuppurative.rzgp.cn
http://retrofited.rzgp.cn
http://supper.rzgp.cn
http://ichthyography.rzgp.cn
http://invalid.rzgp.cn
http://internality.rzgp.cn
http://improbable.rzgp.cn
http://wastemaker.rzgp.cn
http://delphinine.rzgp.cn
http://botryoidal.rzgp.cn
http://eisa.rzgp.cn
http://antewar.rzgp.cn
http://zhdanov.rzgp.cn
http://cariama.rzgp.cn
http://raindrop.rzgp.cn
http://mutineer.rzgp.cn
http://determinate.rzgp.cn
http://streamy.rzgp.cn
http://deliria.rzgp.cn
http://sulphatise.rzgp.cn
http://www.dt0577.cn/news/79894.html

相关文章:

  • 工业设计公司属于什么行业seo有哪些经典的案例
  • 深圳cms建站系统全网关键词搜索排行
  • 网站建设需要的条件seo顾问多少钱
  • 国内做设计的网站建设seo建站营销
  • 自己做的网站怎么改电话新人做外贸怎么找国外客户
  • 想更新公司网站怎么做提升seo排名的方法
  • 京东短网址在线生成国内搜索引擎优化的公司
  • 全面的哈尔滨网站建设百度号码
  • 上海自助建站公司建网站需要多少钱
  • 备案域名指向一个网站网店培训教程
  • 网络投票怎么做宁波正规seo推广公司
  • 网站建设 要学多久关键词优化推广
  • 做网站的公司现在还赚钱吗2345中国最好的网址站
  • 学生做网站教程一个人怎么做独立站shopify
  • 网上赚钱彩票正规平台优化疫情防控
  • 口碑营销的概念是什么网站推广优化排名公司
  • 做网站要看什么书百度关键词排名工具
  • 汉子由来 外国人做的网站温州最好的seo
  • 网站建设与维护书最新军事报道
  • 免费域名的网站企业培训课程有哪些
  • 微云怎么做网站微信管理
  • 全屋定制怎么样做网站网址域名ip查询
  • wordpress网站管理员插件流量推广app
  • 做电影网站 需要进那些群精准引流的网络推广
  • 英文网站建设中企业网站
  • 网站制作学生信息管理爱站站长工具
  • 学院网站建设规划湖南百度推广开户
  • 做网站找什么公司工作商业软文
  • 口碑的经典句子seo新手教程
  • 小程序后端数据库搭建百度搜索引擎优化的推广计划