您好,登錄后才能下訂單哦!
這篇文章主要講解了“Java如何實(shí)現(xiàn)零錢通”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Java如何實(shí)現(xiàn)零錢通”吧!
如圖:
(1)可以把收益入賬和消費(fèi),保存到數(shù)組中(但目前學(xué)到的數(shù)組是定長的)
(2)可以使用對象
(3)可以使用String拼接
完成收益入賬,完成功能驅(qū)動程序員增加新的變化的代碼
(1)要接收收益入賬的金額,并更新余額
(2)拼接好字符串
(3)找到時間如何輸出,并且修改其輸出格式
我用的字符串拼接
創(chuàng)建一個包 com.project.money.oop
然后創(chuàng)建兩個類 SmallChangeSys和SmallChangeSysOOP
package com.project.money.oop; public class SmallChangeSys { public static void main(String[] args) { SmallChangeSysOOP p=new SmallChangeSysOOP(); p.menu(); } }
SmallChangeSysOOP用來實(shí)現(xiàn)具體功能
代碼如下:
package com.project.money.oop; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class SmallChangeSysOOP { private double in_money=0;//當(dāng)前流動的錢 private double balance=0;//余額 private String detailed="------------零錢通明細(xì)--------------"; private Date date = null; //可以用于日期格式化的 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); public void menu(){}; //1 零錢通明細(xì) public void detailed_(){}; //2 收益入賬" public void input(){}; //3消費(fèi) public void output(){}; }
public void menu(){//菜單 boolean loop=true; Scanner scanner = new Scanner(System.in); while(loop){//循環(huán)展示菜單 System.out.println("\n============oop零錢通菜單=============="); System.out.println("\t\t1 零錢通明細(xì)"); System.out.println("\t\t2 收益入賬"); System.out.println("\t\t3 消費(fèi)"); System.out.println("\t\t4 退" + "\t" + "出"); System.out.print("請選擇1-4:");int key= scanner.nextInt();//選擇; switch (key){ case 1:detailed_();break; case 2:input();break; case 3:output();break; case 4:System.out.println("\t\t程序已經(jīng)退出");loop=false;break; } } }
public void detailed_(){ System.out.println(detailed);//直接輸出字符 }
public void input(){//收益入賬 Scanner scanner = new Scanner(System.in); System.out.println("請輸入金額:"); this.in_money=scanner.nextDouble(); while(in_money<0){ System.out.println("======輸入有誤======"); System.out.println("是否重新輸入?Y(是)或 N(否) ");//可能點(diǎn)錯了不是輸入 String in=scanner.next();//是否重新輸入 if(in.equals("n")||in.equals("N")){ System.out.println("返回主頁面中...."); /*****************************************/ try { Thread.sleep( 1000 );//暫停1秒提升用戶感受 } catch (Exception e){} /*******************************************/ return;//退出輸入 } System.out.println("請輸入金額:"); this.in_money=scanner.nextDouble(); } this.balance+=this.in_money; String beizhu=""; date = new Date(); System.out.println("請輸入備注:"); beizhu=scanner.next(); this.detailed+="\n收益入帳\t+" + this.in_money + "\t" + sdf.format(date) + "\t余額:" + this.balance+ " 備注:" +beizhu; }
public void output(){ System.out.println("請輸入金額:"); Scanner scanner = new Scanner(System.in); in_money=scanner.nextDouble(); if(balance<in_money){ System.out.println("余額不足,請充值"); return; } while(in_money<0){ System.out.println("輸入錯誤,請重輸"); in_money=scanner.nextDouble(); if(balance<in_money){ System.out.println("余額不足,請充值"); return; } } this.balance-=in_money; System.out.println("請輸入備注:"); String beizhu=scanner.next(); this.detailed+="\n余額支出\t-" + this.in_money + "\t" + sdf.format(date) + "\t余額:" + this.balance+ " 備注:" +beizhu; }
package com.project.money.oop; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class SmallChangeSysOOP { private double in_money=0;//當(dāng)前流動的錢 private double balance=0;//余額 private String detailed="------------零錢通明細(xì)--------------"; private Date date = null; //可以用于日期格式化的 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); public void menu(){//菜單 boolean loop=true; Scanner scanner = new Scanner(System.in); while(loop){//循環(huán)展示菜單 System.out.println("\n============oop零錢通菜單=============="); System.out.println("\t\t1 零錢通明細(xì)"); System.out.println("\t\t2 收益入賬"); System.out.println("\t\t3 消費(fèi)"); System.out.println("\t\t4 退" + "\t" + "出"); System.out.print("請選擇1-4:");int key= scanner.nextInt();//選擇; switch (key){ case 1:detailed_();break; case 2:input();break; case 3:output();break; case 4:System.out.println("\t\t程序已經(jīng)退出");loop=false;break; } } } //1 零錢通明細(xì) public void detailed_(){ System.out.println(detailed); } //2 收益入賬" public void input(){//收益入賬 Scanner scanner = new Scanner(System.in); System.out.println("請輸入金額:"); this.in_money=scanner.nextDouble(); while(in_money<0){ System.out.println("======輸入有誤======"); System.out.println("是否重新輸入?Y(是)或 N(否) ");//可能點(diǎn)錯了不是輸入 String in=scanner.next();//是否重新輸入 if(in.equals("n")||in.equals("N")){ System.out.println("返回主頁面中...."); /*****************************************/ try { Thread.sleep( 1000 );//暫停1秒提升用戶感受 } catch (Exception e){} /*******************************************/ return;//退出輸入 } System.out.println("請輸入金額:"); this.in_money=scanner.nextDouble(); } this.balance+=this.in_money; String beizhu=""; date = new Date(); System.out.println("請輸入備注:"); beizhu=scanner.next(); this.detailed+="\n收益入帳\t+" + this.in_money + "\t" + sdf.format(date) + "\t余額:" + this.balance+ " 備注:" +beizhu; } //3消費(fèi) public void output(){ System.out.println("請輸入金額:"); Scanner scanner = new Scanner(System.in); in_money=scanner.nextDouble(); if(balance<in_money){ System.out.println("余額不足,請充值"); return; } while(in_money<0){ System.out.println("輸入錯誤,請重輸"); in_money=scanner.nextDouble(); if(balance<in_money){ System.out.println("余額不足,請充值"); return; } } this.balance-=in_money; System.out.println("請輸入備注:"); String beizhu=scanner.next(); this.detailed+="\n余額支出\t-" + this.in_money + "\t" + sdf.format(date) + "\t余額:" + this.balance+ " 備注:" +beizhu; } }
感謝各位的閱讀,以上就是“Java如何實(shí)現(xiàn)零錢通”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Java如何實(shí)現(xiàn)零錢通這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。