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/cn.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灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP