基本要求和功能(手机拍的 模糊):
均在practice包中:
Manager和Books类:
package practice;
public class Manager {
private String hao;
private String password;
private String mname;
public Manager(String hao, String password, String mname) {
this.hao = hao;
this.password = password;
this.mname = mname;
}
public String getHao() {
return hao;
}
public void setHao(String hao) {
this.hao = hao;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getMname() {
return mname;
}
public void setMname(String mname) {
this.mname = mname;
}
}
package practice;
public class Books {
private int bianhao;
private String name;
private double price;
public Books() {
//super();
}
public Books(int bianhao, String name, double price) {
///super();
this.bianhao = bianhao;
this.name = name;
this.price = price;
}
public int getBianhao() {
return bianhao;
}
public void setBianhao(int bianhao) {
this.bianhao = bianhao;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
定义一个数据库类,准备内容初始化※
package practice;
public class sql {
private Manager m1=new Manager("a0001","abc123","李白");
private Manager m2=new Manager("a0002","abc456","芳华");
private Manager m3=new Manager("a0003","abc789","杜甫");
private Manager[] ms={m1,m2,m3};
private Books b1=new Books(123,"三国演义",2.5);
private Books b2=new Books(124,"水浒传",12.5);
private Books b3=new Books(125,"红楼梦",11.2);
private Books[] bs={b1,b2,b3};
public Manager[] getMs() {
return ms;
}
public void setMs(Manager[] ms) {
this.ms = ms;
}
public Books[] getBs() {
return bs;
}
public void setBs(Books[] bs) {
this.bs = bs;
}
}
interface声明:
package practice;
public interface Service {
Manager login();//登录
void showAllBooks();
int isUsed(int id);
void addBookk();
void changePrice();
}
Service实现类
package practice;
import java.util.Scanner;
public class ServiceMain implements Service {
sql s1=new sql();
Scanner sc=new Scanner(System.in);
@Override
public Manager login() {
System.out.println("请输入账号");
String hao=sc.next();
System.out.println("请输入密码");
String password=sc.next();
for(int i=0;i<s1.getMs().length;i++){
if(s1.getMs()[i].getPassword().equals(password)&&s1.getMs()[i].getHao().equals(hao)){
System.out.println("欢迎管理员:"+s1.getMs()[i].getMname());
return s1.getMs()[i];
}
}
System.out.println("账号或密码错误!");
return null;
}
@Override
public void showAllBooks() {
for(int i=0;i<s1.getBs().length;i++){
System.out.println(s1.getBs()[i].getBianhao()+" "+s1.getBs()[i].getName()+" "+s1.getBs()[i].getPrice());
}
}
@Override
public int isUsed(int id) {
for(int i=0;i<s1.getBs().length;i++){
if(id==s1.getBs()[i].getBianhao()){
return i;
}
}
return -1;
}
@Override
public void addBookk() {
System.out.println("请输入新书的编号");
int h=sc.nextInt();
int x=isUsed(h);
if(x==-1){
System.out.println("请输入新书的名称");
String n=sc.next();
System.out.println("请输入新书的价格");
double p=sc.nextDouble();
Books b=new Books(h,n,p);
Books[] newb=new Books[s1.getBs().length+1];
for(int i=0;i<s1.getBs().length;i++){
newb[i]=s1.getBs()[i];
}
newb[newb.length-1]=b;
s1.setBs(newb);
System.out.println("新增成功");
}else{
System.out.println("已有该编号!请重新输入");
}
}
@Override
public void changePrice() {
System.out.println("请输入新书的编号");
int h=sc.nextInt();
int x=isUsed(h);
if(x!=-1){
System.out.println("请输入此书的新价格");
double p=sc.nextDouble();
s1.getBs()[x].setPrice(p);
System.out.println("价格修改成功");
}else{
System.out.println("查询不到该编号");
}
}
}
主函数work
package practice;
import java.sql.SQLOutput;
import java.util.*;
public class work1 {
public static void main(String[] args) {
ServiceMain s=new ServiceMain();
sql s1=new sql();
Scanner sc=new Scanner(System.in);
System.out.println("————欢迎使用图书管理系统————");
while (true){
System.out.println("请输入 1-登录 0-退出");
int in=sc.nextInt();
if(in==1){
Manager m=s.login();
if(m==null){
}else {
while (true){
System.out.println("请使用菜单 1-查询所有书 2-新增图书 3-修改价格 0-退出登录");
int b=sc.nextInt();
if(b==1){
s.showAllBooks();
}else if(b==2){
s.addBookk();
}else if(b==3){
s.changePrice();
}else if(b==0){
System.out.println("已登出!");
break;
}else {
System.out.println("非法输入!");
}
}
}
}else if(in==0){
System.out.println("欢迎下次使用!");
System.exit(0);
}else {
System.out.println("非法输入");
}
}
}
}