请问我要怎么做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;
}
}