[問題] Java Atm 伺服器端問題

作者: HarryWen (Harry)   2017-01-05 02:32:04
請問我要怎麼做server端 在server端處理存錢、提錢 然後在寫入檔案




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;
}
}
作者: swpoker (swpoker)   2017-01-05 19:12:00
作業?
作者: p23j8a4b9z (我是小牙籤~)   2017-01-05 22:20:00
啊之前不是跟你講要用sql 寫作業也不給詳細一點是怎麼幫你看 如果你不用sql要寫入檔案或是變數也不講而且你存成檔案的話 每人應該要有不同檔案 每次登入去找你的檔案是哪個 在從裡面撈就好
作者: swpoker (swpoker)   2017-01-06 10:44:00
他應該在想:SQL甚麼的我不懂啦,我是問怎麼寫ATM拉
作者: ssccg (23)   2017-01-06 10:47:00
也可以用NoSQL啊,重點是需求規格要說清楚吧不過作業不清楚怎麼做應該去問老師或助教吧
作者: Jaymainal (發呆中)   2017-01-09 17:41:00
一定不是妹 是妹的話學長就會教你了
作者: kigohcy (風櫃店庄小孩念國中)   2017-01-11 10:30:00
用檔案會咬死噢,尤其又用win

Links booklink

Contact Us: admin [ a t ] ucptt.com