java mysql 书店系统_一个书店管理系统java

自己的第一个小程序

ps:书是在集合里面后面文件处理的有一点小问题,希望有人会给点意见

//客户类

import java.io.Serializable;

public class Customer implements Serializable{

//客户的属性

private String name ;//客户姓名

private String passWord;//客户密码

//客户属性的get,set方法

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPassWord() {

return passWord;

}

public void setPassWord(String passWord) {

this.passWord = passWord;

}

//带参构造方法

public Customer(String name, String passWord) {

super();

this.name = name;

this.passWord = passWord;

}

public Customer() {

super();

}

}

//客户登陆类

public class Signin {

ArrayList arrayList=new ArrayList();//存储客户信息的集合

Customer customer=new Customer();//构造客户对象

//将集合加入客户信息

public void setInformation(){

arrayList.add(new Customer("张3","123"));

arrayList.add(new Customer("李4","1234"));

arrayList.add(new Customer("王5","12345"));

arrayList.add(new Customer("赵6","123456"));

}

//遍历客户集合

public int bianLi(String name,String passWord ){

for(int j=0;j

customer=arrayList.get(j);

//customer=it.next();

//如果与传入信息相同就返回1

if(customer.getName().equals(name)&&customer.getPassWord().equals(passWord)){

break;

}else if(j!=arrayList.size()-1){//控制for循环循环到集合中的每一个

continue;

}else{

return 0;//没有找到返回0

}

}

return 1;//找到返回1

}

//将客户集合写入文件

public void writeInformation(){

ObjectOutputStream oos=null;

try {

oos=new ObjectOutputStream( new FileOutputStream("d:\\pro2.docx"));//在文件后面追加不把信息覆盖

oos.writeObject(arrayList);

} catch (IOException e) {

e.printStackTrace();

}finally{

try {

oos.flush();

oos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

//将文件的信息读出

@SuppressWarnings("unchecked")

public void readInformation(){

ObjectInputStream ois=null;

try {

ois=new ObjectInputStream(new FileInputStream("d:\\pro2.docx"));

arrayList=(ArrayList) ois.readObject();//读入对象

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (ClassNotFoundException e) {

e.printStackTrace();

}finally{

try {

ois.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

//书的类

public class BookTwo {

//书的属性

private String bname;//图书名字

private String btype;//图书种类

private double bprice;//图书价格

private String bnum;//图书编号

private int bleft;//图书库存

//书的属性的get,set方法

public String getBname() {

return bname;

}

public void setBname(String bname) {

this.bname = bname;

}

public String getBtype() {

return btype;

}

public void setBtype(String btype) {

this.btype = btype;

}

public double getBprice() {

return bprice;

}

public void setBprice(double bprice) {

this.bprice = bprice;

}

public String getBnum() {

return bnum;

}

public void setBnum(String bnum) {

this.bnum = bnum;

}

public int getBleft() {

return bleft;

}

public void setBleft(int bleft) {

this.bleft = bleft;

}

//带参的构造函数

public BookTwo( String bnum,String bname, String btype, double bprice,

int bleft) {

super();

this.bname = bname;

this.btype = btype;

this.bprice = bprice;

this.bnum = bnum;

//this.bcount = bcount;

this.bleft = bleft;

}

//不带参的构造函数

public BookTwo() {

super();

}

}

//书的方法类

public class BookTwoMenthod {

int count;

BookTwo book=new BookTwo();

//客户成功登陆后的界面

public void show(){

System.out.println("*********欢迎使用书店系统**********");

System.out.println("1.查看");

System.out.println("2.还书");

System.out.println("3.退出");

System.out.print("请输入要选择的编号:");

}

//将传入的list加入图书内容

public void show1(ArrayList list) {

list.add(new BookTwo("1001","《淘气包马小跳》","搞笑",23,100));

list.add(new BookTwo("1002","《水煮大神》","搞笑",23,100));

list.add(new BookTwo("1003","《爆笑小白》","搞笑",23,100));

list.add(new BookTwo("1004","《然后爱情随遇而安》","温馨",23,100));

list.add(new BookTwo("1005","《心有不甘》","虐心",23,100));

list.add(new BookTwo("1006","《神仙肉》","搞笑+虐心",23,100));

}

//遍历传入list并输出所有信息

public void show2(ArrayList list){

Iterator ti=list.iterator();//迭代器迭代输出

System.out.println("编号\t名称\t\t种类\t价格\t库存");

while(ti.hasNext()){

BookTwo e=(BookTwo) ti.next();

System.out.println(e.getBnum()+"\t"

+e.getBname()+"\t"+e.getBtype()+"\t\t"

+e.getBprice()+"\t"+e.getBleft());

}

}

//购买的方法

public void buyBook(ArrayList list,String bnum,int bcount){

for(int i=0;i

book=list.get(i);

if(bcount>=book.getBleft()){//比较库存与所买书数量

System.out.println("书库没有足够库存!");

break;

}else if(book.getBnum().equals(bnum)){//通过bnum来找到该书

System.out.println("您要购买的书是:"+book.getBname());

System.out.println("您需要支付金额是:"+bcount*book.getBprice());

//传递修改数据,通过set方法修改集合中的信息

book=new BookTwo(bnum, book.getBname(), book.getBtype(), book.getBprice(), book.getBleft()-bcount);

list.set(i, book);

break;

}else if(i==list.size()-1){//控制for循环循环到集合中的每一个

System.out.println("没有您要购买的书");

}else{

continue;

}

}

}

//租书的方法

public void rentBook(ArrayList list,String bnum,int bcount,int date){

for(int i=0;i

book=list.get(i);

if(bcount>=book.getBleft()){//比较库存与所买书数量

System.out.println("书库没有足够库存!");

break;

}else if(book.getBnum().equals(bnum)){

switch (date){

case 1://租书一周所需价钱

System.out.println("您要租的书是:"+book.getBname());

System.out.println("您要支付的价格是:"+7*bcount*5);

break;

case 2://租书一月所需价钱

System.out.println("您要的书是:"+book.getBname());

System.out.println("您要支付的价格是:"+15*bcount*2);

break;

case 3://租书半年所需价钱

System.out.println("您要购买的书是:"+book.getBname());

System.out.println("您要支付的价格是:"+183*bcount*1);

break;

default:

System.out.println("请输入数字(1-3)!");

break;

}

}else if(i!=list.size()-1){//控制for循环循环到集合中的每一个

continue;

}else{

System.out.println("没有该本书!");

break;

}

break;

}

//数据更新

for(int i=0;i

book=list.get(i);

if(book.getBnum().equals(bnum)){

book=new BookTwo(bnum, book.getBname(), book.getBtype(), book.getBprice(), book.getBleft()-bcount);

list.set(i, book);

}else{

continue;

}

}

}

//还书的方法

public int returnBook(ArrayList list,String bnum,int bcount){

for(int i=0;i

book=list.get(i);

if(book.getBnum().equals(bnum)){

if(book.getBleft()<100){

//更新数据

book=new BookTwo(bnum, book.getBname(), book.getBtype(), book.getBprice(), book.getBleft()+bcount);

list.set(i, book);

System.out.println("还书成功!");

return 1;//有该书时返回1

}else{

System.out.println("您没有借这个书请确定书编号后,在还书");

return 1;

}

}else if(i!=list.size()-1){//控制for循环循环到集合中的每一个

continue;

}else{

System.out.println("没有该书,请确定书的编号,重新登陆");

break;

}

}

return 0;//没有该书时返回0

}

}

//测试类

public class BookTwoTest {

public static void main(String[] args) {

Signin signin=new Signin();//登陆的对象

Customer customer=new Customer();

ArrayList list=new ArrayList();//图书集合

//ArrayList arrayList2=new ArrayList();//新加入的客户信息集合

BookTwoMenthod fiction=new BookTwoMenthod();//图书方法对象

Scanner input=new Scanner(System.in);

signin.setInformation();//调用Signin类里面的客户信息写入集合

signin.writeInformation();//调用Signin类里面的写入文件的方法

fiction.show1(list);//调用BookTwoMenthod类的将图书信息写入集合

do{

//客户登陆界面

System.out.println("**********欢迎使用登陆界面***********");

System.out.println("请输入您的姓名:");

String name =input.next();

System.out.println("请输入您的密码:");

String passWord=input.next();

if(signin.bianLi(name, passWord)==1){//调用Signin遍历方法若返回为1则登陆成功

System.out.println("登陆成功!");

for(int j=0;;j++){

fiction.show();//显示客户成功登陆后的界面

String i=input.next();//客户选择的功能

switch(i){

case "1"://显示图书

fiction.show2(list);

System.out.println("您要买书(1)还是租书(2)还是退出(3)?");

String k=input.next();

if(k.equals("1")){//购买图书

System.out.println("请输入您要购买的书的编号,数量:");

String bnum=input.next();

int bcount =input.nextInt();

fiction.buyBook(list,bnum, bcount);

continue;//跳到选择主界面

}else if(k.equals("2")){//租图书

System.out.println("请输入您要购租的书的编号,数量,日期:");

String bnum=input.next();

int bcount =input.nextInt();

System.out.println("您要租的时长:1.一个周;2.一个月;3.半年");

try{

int date=input.nextInt();

fiction.rentBook(list,bnum, bcount, date);

}catch(InputMismatchException e){

System.out.println("请输入数字");

}

continue;//跳到选择主界面

}else if(k.equals("3")){//退出到登陆界面

break;//跳到登陆界面

}else{

System.out.println("请输入规定数字!");

continue;//跳到选择主界面

}

case "2"://还书

do{

System.out.println("请输入要还书的编号,数量");

String bnum=input.next();

int bcount=input.nextInt();

if(fiction.returnBook(list,bnum, bcount)==0){//调用还书方法

continue;//跳到选择还书界面

}else{

break;//跳到选择大界面

}

}while(true);

continue;

case "3"://退回登陆界面

break;

default://输入有错误

System.out.println("请输入数字(1-3)!");

continue;//退回登陆界面

}

break;

}

}else{

//调用Signin遍历方法若返回不为1则需要注册

System.out.println("密码或用户名错误!");

System.out.println("您是否要注册?y or n");

String s=input.next();

if(s.equals("y")){

//需要注册时

System.out.println("请输入您的姓名:");

String name1 =input.next();//得到姓名

System.out.println("请输入您的密码:");

String passWord1=input.next();//得到密码

//遍历客户集合判断姓名,密码是否存在文件中

for(int j=0;j

customer=signin.arrayList.get(j);

//已存在

if(customer.getName().equals(name)&&customer.getPassWord().equals(passWord)){

System.out.println("用户已存在!");

break;//跳到登陆界面

//不存在

}else{

signin.arrayList.add(new Customer(name1, passWord1));//在客户集合中加入新成员

signin.writeInformation();//调用写的方法将新成员写入

System.out.println("注册成功!");

signin.readInformation();//调用读的方法读入内存

signin.bianLi(name1, passWord1);//可有可无

break;//跳到登陆界面

}

}

continue;//退回登陆界面

}else{

System.out.println("用户名或密码错误!否则不注册您不能访问该系");

continue;//退回登陆界面

}

}

}while(true);//循环到登陆界面

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值