java 板


LINE

請問我要怎麼做server端 在server端處理存錢、提錢 然後在寫入檔案 http://i.imgur.com/ILHCuQV.jpg http://i.imgur.com/6YxOdhF.jpg public class ATMMachine { public static Scanner sc=new Scanner(System.in); private String name = "Harris"; private double password = 123; public static void checkBalance() throws IOException { // 餘額查詢 char data[] = new char[128]; // 建立可容納128個字元的陣列 FileReader fr = new FileReader("count.txt"); // 建立物件fr int num = fr.read(data); // 將資料讀入字元陣列data內 String str = new String(data, 0, num); // 將字元陣列轉換成字串 fr.close(); System.out.println("\tYour current balance is " + str); } public static void withdrawMoney() {// 提金錢 if (BalanceInquiry.balance == 0) { System.out.println("\tYour current balance is zero."); System.out.println("\tYou cannot withdraw!"); System.out.println("\tYou need to deposit money first."); } else if (BalanceInquiry.balance <= 500) { System.out .println("\tYou do not have sufficient money to withdraw"); System.out .println("\tChecked your balance to see your money in the bank."); } else if (Withdraw.withdraw > BalanceInquiry.balance) { System.out .println("\tThe amount you withdraw is greater than to your balance"); System.out.println("\tPlease check the amount you entered."); } else { BalanceInquiry.balance = BalanceInquiry.balance - Withdraw.withdraw; System.out.println("\n\tYou withdraw the amount of Php " + Withdraw.withdraw); } } public static void depositMoney() throws IOException {// 存錢 FileWriter fw=new FileWriter("count.txt"); String str="7000"; //fw.write(data); // 將字元陣列寫到檔案裡 fw.write(str); // 將字串寫到檔案裡 fw.close(); System.out.println("\tYou deposited the amount of "+ Deposit.getDeposit()); } public static void main(String[] args){// 主程式 Scanner read = new Scanner(System.in); int select = 0; int choice = 0; System.out .println("===================================================="); System.out.println("\tWelcome to this simple ATM machine"); System.out .println("===================================================="); System.out.println(); do { try { do { System.out.println("PLEASE INPUT YOUR ID:"); String s = sc.next(); if (s.equals("Harris")) { System.out.println("PLEASE INPUT YOUR PASSWORD:"); int ss = sc.nextInt(); if (ss == 123) { continue; } else { System.out.println("ERROR"); System.exit(0); } } else { System.out.println("ERROR"); System.exit(0); } System.out.println("\tPlease select ATM Transactions"); System.out.println("========================"); System.out.println("| [1] Deposit |"); System.out.println("| [2] Withdraw |"); System.out.println("| [3] Balance Inquiry |"); System.out.println("| [4] Exit |"); System.out.println("========================"); System.out.print("\n\tWhat would you like to do? "); select = read.nextInt(); if (select > 4) {// 大於4個選項 輸入錯誤Would you like to try another tra nsaction? System.out .println("\n\tPlease select correct transaction."); } else { switch (select) { case 1:// 存錢 System.out.print("\n\tEnter the amount of money to deposit: "); Deposit.deposit = read.nextDouble(); String str1 = "1:"+ String.valueOf(Deposit.deposit); Socket s0 = new Socket("localhost", 8787); BufferedReader in = new BufferedReader(new InputStreamReader(s0.getInpu tStream())); PrintWriter out = new PrintWriter(new OutputStreamWriter(s0.getOutputSt ream())); // 送出"Hello"字串 out.println(str1); out.flush(); char data[]=new char[128]; // 建立可容納128個字元的陣列 FileReader fr=new FileReader("record.txt"); // 建立物件fr int num=fr.read(data); // 將資料讀入字元陣列data內 String str=new String(data,0,num); // 將字元陣列轉換成字串 fr.close(); String[] aArray = str.split(":"); for (int i=0;i<aArray.length;i++) { System.out.println(i+" "+aArray[i]); } String str8=new String(data,0,num); // 將字元陣列轉換成字串 int deposit=2000; int total=0; String str0=""; total=deposit+Integer.parseInt(aArray[2]); for (int i=0;i<aArray.length-1;i++) str0=str0+aArray[i]+":"; str0=str0+String.valueOf(total); System.out.println(str0); FileWriter fw=new FileWriter("record.txt"); fw.write(str0); BalanceInquiry.balance = Deposit.deposit+ BalanceInquiry.balance; depositMoney(); break; case 2:// 提錢 System.out.print("\n\tTo withdraw, make sure that you have sufficient b alance in your account."); System.out.println(); System.out.print("\tEnter amount of money to withdraw: "); Withdraw.withdraw = read.nextDouble(); String str2 = "2:"+ String.valueOf(Withdraw.withdraw); Socket s1 = new Socket("localhost", 8787); BufferedReader in1 = new BufferedReader(new InputStreamReader(s1.getInp utStream())); PrintWriter out1 = new PrintWriter(new OutputStreamWriter(s1.getOutputS tream())); // 送出"Hello"字串 out1.println(str2); out1.flush(); String[] aArray1 = str2.split(":"); for (int i=0;i<aArray1.length;i++) { System.out.println(i+" "+aArray1[i]); } withdrawMoney(); break; case 3:// 查詢餘額 checkBalance(); break; default:// 離開 System.out.print("\n\tTransaction exited."); break; } } } while (select > 4); do { try { System.out.println("\n\tWould you like to try another transaction?"); System.out.println("\n\tPress [1] Yes \n\tPress [2] No"); System.out.print("\tEnter choice: "); choice = read.nextInt(); if (choice > 2) {// 大於二輸入錯誤 System.out .print("\n\tPlease select correct choice."); } } catch (Exception e) {// 例外 System.out .println("\tError Input! Please enter a number only."); read = new Scanner(System.in); System.out.println("\tEnter yout choice:"); choice = read.nextInt(); } } while (choice > 2); } catch (Exception e) {// 例外 System.out .println("\tError Input! Please enter a number only."); read = new Scanner(System.in); System.out.println("\tEnter yout choice:"); select = read.nextInt(); } } while (choice <= 1); System.out.println("\n\tThank you for using this simple ATM Machine."); } private static void land() { // TODO Auto-generated method stub } } class Deposit extends ATMMachine { static double deposit; public void setDeposit(double d) { deposit = d; } public static double getDeposit() { return deposit; } } class Withdraw extends ATMMachine { static double withdraw = 0; public void setWithdraw(double w) { withdraw = w; } public static double getWithdraw() { return withdraw; } } class BalanceInquiry extends ATMMachine { static double balance = 0; public void setBalance(double b) { balance = b; } public static double getBalance() { return balance; } } --



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.141.237.170
※ 文章網址: https://webptt.com/m.aspx?n=bbs/java/M.1483554726.A.BB0.html
1F:→ swpoker: 作業? 01/05 19:12
2F:→ p23j8a4b9z: 啊之前不是跟你講要用sql 寫作業也不給詳細一點是怎麼 01/05 22:20
3F:→ p23j8a4b9z: 幫你看 如果你不用sql要寫入檔案或是變數也不講 01/05 22:20
4F:→ p23j8a4b9z: 而且你存成檔案的話 每人應該要有不同檔案 每次登入去 01/05 22:22
5F:→ p23j8a4b9z: 找你的檔案是哪個 在從裡面撈就好 01/05 22:22
6F:→ swpoker: 他應該在想:SQL甚麼的我不懂啦,我是問怎麼寫ATM拉 01/06 10:44
7F:→ ssccg: 也可以用NoSQL啊,重點是需求規格要說清楚吧 01/06 10:47
8F:→ ssccg: 不過作業不清楚怎麼做應該去問老師或助教吧 01/06 10:48
9F:→ Jaymainal: 一定不是妹 是妹的話學長就會教你了 01/09 17:41
10F:推 kigohcy: 用檔案會咬死噢,尤其又用win 01/11 10:30







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:BuyTogether站內搜尋

TOP