Java基于對(duì)象流實(shí)現(xiàn)銀行系統(tǒng)_第1頁
Java基于對(duì)象流實(shí)現(xiàn)銀行系統(tǒng)_第2頁
Java基于對(duì)象流實(shí)現(xiàn)銀行系統(tǒng)_第3頁
Java基于對(duì)象流實(shí)現(xiàn)銀行系統(tǒng)_第4頁
Java基于對(duì)象流實(shí)現(xiàn)銀行系統(tǒng)_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第Java基于對(duì)象流實(shí)現(xiàn)銀行系統(tǒng)系統(tǒng)特點(diǎn):

數(shù)據(jù)持久化到文件中,系統(tǒng)啟動(dòng)后,加載文件中數(shù)據(jù)到集合中,相當(dāng)于做了一個(gè)緩存。文件讀寫使用的是對(duì)象流(ObjectInputStream和ObjectOutputStream),用戶是看不懂那些文件的,一定程度上保證了密碼和余額的安全性。采用了MVC分層設(shè)計(jì)思想,雖然沒有C控制層和V視圖層,但是有完備的M模型層,數(shù)據(jù)處理Service,數(shù)據(jù)讀寫Dao,數(shù)據(jù)存儲(chǔ)POJO(每一層只負(fù)責(zé)處理自己的事情,層次內(nèi)部的改動(dòng)不會(huì)影響其他層次)。雖然有些細(xì)節(jié)問題由于時(shí)間關(guān)系沒有處理好,比如沒有處理輸入類型不匹配的異常,但是絕大多數(shù)功能都合理得實(shí)現(xiàn)了。

pojo:

publicclassUserimplementsSerializable{

privatestaticfinallongserialVersionUID=-7019656536540026883L;

privateStringusername;//用戶名

privateStringpassword;//密碼

privateDoublebalance;//余額

publicUser(Stringusername,Stringpassword,Doublebalance){

this.username=username;

this.password=password;

this.balance=balance;

}

publicUser(){

}

publicStringgetUsername(){

returnusername;

}

publicvoidsetUsername(Stringusername){

this.username=username;

}

publicStringgetPassword(){

returnpassword;

}

publicvoidsetPassword(Stringpassword){

this.password=password;

}

publicDoublegetBalance(){

returnbalance;

}

publicvoidsetBalance(Doublebalance){

this.balance=balance;

}

publicStringtoString(){

StringBuilderbuilder=newStringBuilder("{");

builder.append(this.username).append(",").append(this.password).append(",").append(this.balance).append("}");

returnbuilder.toString();

}

dao層:

publicclassATMDao{

privateStringuserFilePath="src\\userMsg.txt";//相對(duì)路徑

privateHashMapString,User

userMsg;

{

userMsg=this.loadFile();//讀取文件信息

}

publicHashMapString,UsergetUserMsg(){

returnuserMsg;

}

//查詢用戶

publicUserselectUser(Stringusername){

returnuserMsg.get(username);

}

//更新用戶信息

publicvoidupdateUser(Useruser){

this.userMsg.replace(user.getUsername(),user);

this.updateFile(userMsg);

}

//增加用戶

publicvoidaddUser(Useruser){

this.userMsg.put(user.getUsername(),user);

this.updateFile(userMsg);

}

//刪除用戶

publicvoiddeleteUser(Useruser){

this.userMsg.remove(user.getUsername());

this.updateFile(userMsg);

}

//只在初始化時(shí)時(shí)調(diào)用一次

/*

publicvoidinitFile(){

Useruser1=newUser("張三","123",800.0);

Useruser2=newUser("李四","666",70.0);

Useruser3=newUser("王五","888",555.0);

userMsg.put(user1.getUsername(),user1);

userMsg.put(user2.getUsername(),user2);

userMsg.put(user3.getUsername(),user3);

this.updateFile(userMsg);

}*/

privateHashMapString,UserloadFile(){

Filefile=newFile(userFilePath);

FileInputStreamfis=null;

ObjectInputStreamois=null;

HashMapString,Userbox=null;

try{

fis=newFileInputStream(file);

ois=newObjectInputStream(fis);

try{

box=(HashMapString,User)ois.readObject();

}catch(ClassNotFoundExceptione){

e.printStackTrace();

}

}catch(IOExceptione){

e.printStackTrace();

}finally{

try{

if(fis!=null)

fis.close();

}catch(IOExceptione){

e.printStackTrace();

}

try{

if(ois!=null)

ois.close();

}catch(IOExceptione){

e.printStackTrace();

}

}

returnbox;

}

privatevoidupdateFile(HashMapString,UseruserMsg){

Filefile=newFile(userFilePath);

FileOutputStreamfos=null;

ObjectOutputStreamoos=null;

try{

fos=newFileOutputStream(file);

oos=newObjectOutputStream(fos);

oos.writeObject(userMsg);

oos.flush();

}catch(IOExceptione){

e.printStackTrace();

}finally{

try{

if(fos!=null)

fos.close();

}catch(IOExceptione){

e.printStackTrace();

}

try{

if(oos!=null)

oos.close();

}catch(IOExceptione){

e.printStackTrace();

}

}

}

}

service層:

publicclassATMService{

ATMDaodao=newATMDao();

publicbooleanlogin(Stringusername,Stringpassword){

Useruser=dao.selectUser(username);

if(user!=nulluser.getPassword().equals(password))

returntrue;

else

returnfalse;

}

//查詢余額

publicDoublequeryBalance(Stringname){

Useruser=dao.selectUser(name);

returnuser.getBalance();

}

//存款

publicvoiddeposit(Stringname,Doubleaccount){

Useruser=dao.selectUser(name);

user.setBalance(user.getBalance()+account);

dao.updateUser(user);

}

//取款

publicvoidwithdrawal(Stringname,Doubleaccount){

Useruser=dao.selectUser(name);

if(user.getBalance()=account){

user.setBalance(user.getBalance()-account);

dao.updateUser(user);

}else{

System.err.println("對(duì)不起,"+user.getUsername()+",您的余額不足");

}

}

//轉(zhuǎn)賬

publicvoidtransfer(StringoldName,StringnewName,Doubleaccount){

UseroldUser=dao.selectUser(oldName);

UsernewUser=dao.selectUser(newName);

if(newUser!=null){

if(oldUser.getBalance()=account){

oldUser.setBalance(oldUser.getBalance()-account);

newUser.setBalance(newUser.getBalance()+account);

dao.updateUser(oldUser);

dao.updateUser(newUser);

}else{

System.err.println("對(duì)不起,"+oldUser.getUsername()+",您的余額不足");

}

}else{

System.err.println("對(duì)不起,您輸入的賬號(hào)不存在");

}

}

//銷戶

publicvoidcloseAccount(Stringname){

Useru=dao.selectUser(name);

dao.deleteUser(u);

}

//注冊(cè)

publicvoidregister(Stringname,Stringpassword){

Useruser=dao.selectUser(name);

if(user!=null){

System.err.println("對(duì)不起,您輸入的賬號(hào)已存在");

}else{

user=newUser(name,password,0.0);

dao.addUser(user);

}

}

}

測(cè)試:

publicclassTest{

publicstaticvoidmain(String[]args){

ATMServiceservice=newATMService();

Scannerinput=newScanner(System.in);

System.out.println("歡迎您使用銀行自助服務(wù)系統(tǒng)\n請(qǐng)輸入您的賬號(hào):");

Stringusername=input.nextLine();

System.out.println("請(qǐng)輸入您的密碼:");

Stringpassword=input.nextLine();

if(service.login(username,password)){

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

while(true){

System.out.println("請(qǐng)選擇您的操作:\n0.注冊(cè)\n1.查詢余額\n2.存款\n3.取款\n4.轉(zhuǎn)賬\n5.銷戶\n6.退出");

Stringoption=input.nextLine();

switch(option){

case"0":

System.out.println("請(qǐng)輸入您的用戶名:");

Stringnewname=input.nextLine();

System.out.println("請(qǐng)輸入您的密碼:");

StringnewPass=input.nextLine();

service.register(newname,newPass);

System.out.println("恭喜您,注冊(cè)成功!,您的用戶名為"+newname);

break;

case"1":

Doublebalance=service.queryBalance(username);

System.out.println("您的余額為"+balance);

break;

case"2":

System.out.println("請(qǐng)輸入存款金額:");

doubleamount=input.nextDouble();

input.nextLine();

service.deposit(username,amount);

System.out.println("恭喜您,存款成功");

break;

case"3":

System.out.println("請(qǐng)輸入取款金額:");

doubled=input.nextDouble();

input.nextLine();

service.withdrawal(username,d);

System.out.println("恭喜您,取款成功");

break;

case"4":

System.out.println("請(qǐng)輸入對(duì)方賬戶:");

Stringanothername=input.nextLine();

System.out.println("請(qǐng)輸入轉(zhuǎn)賬金額:");

doublet=input.nextDo

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論