一個書店管理系統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<Customer> arrayList=new ArrayList<Customer>();//存儲客戶信息的集合
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<arrayList.size();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<Customer>) 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<BookTwo> 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<BookTwo> list){
Iterator<BookTwo> 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<BookTwo> list,String bnum,int bcount){
for(int i=0;i<list.size();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<BookTwo> list,String bnum,int bcount,int date){
for(int i=0;i<list.size();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<list.size();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<BookTwo> list,String bnum,int bcount){
for(int i=0;i<list.size();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<BookTwo> list=new ArrayList<BookTwo>();//圖書集合
//ArrayList<Customer> arrayList2=new ArrayList<Customer>();//新加入的客戶信息集合
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<signin.arrayList.size();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);//循環到登陸界面
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM