本例中View和Controller并未分开,只是对控制器的方法进行了封装与视图分开,可以先从main函数开始逐一寻找并理解调用的方法
Model(类):
public class Account {
private String accountName;
private String accountPwd;
private double accountMoney;
public Account(String name,String pwd,double money){
this.accountName = name;
this.accountPwd = pwd;
this.accountMoney = money;
}
public Account(String name,String pwd){
this.accountName = name;
this.accountPwd = pwd;
this.accountMoney = 0;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public String getAccountPwd() {
return accountPwd;
}
public void setAccountPwd(String accountPwd) {
this.accountPwd = accountPwd;
}
public double getAccountMoney() {
return accountMoney;
}
public void setAccountMoney(double accountMoney) {
this.accountMoney = accountMoney;
}
}
View和Controller:
import java.util.ArrayList;
import java.util.Scanner;
public class AccountBank {
public static String LogWelcomeStr = "######## Welcome to ########";
public static String LogChoice = "Plase choice the Operation: 1.EstablishAccount 2.Logon 3.Exit";
public static String WelcomeStr = "Welcome to bijienetBank!";
public static String ExitStr = "Thank you for your visit,exit the system success!";
public static String ChoiceOperation = "plase choice your operation: 1.SaveMoney 2.TakeMoney 3.Transfer 4.See Balance 5.Exit";
public static String ChoiceError = "Choice Error! Plase check you choice!";
public static String InputAccountName = "Plase input the account name";
public static String InputAccountPwd = "Plase input the account password";
public static String EstablishSuccess = "Congratulations the account establish success!";
public static String EstablishSuccessFollow = "Continue establish account-> 1 Return to the upper level-> 2";
public static String EstablishFailure = "Sorry the account establish failure, plase check you input!";
public static String SaveMoneyStr = "Plase input you amount of money,at least one hundred and just the hundred multiple";
public static String InputLogonAccountName = "Plase input your account";
public static String InputLogonAccountPwd = "Plase input your password";
public static String InputErrorNotNull = "Plase check your input,the account and pwd must not null!";
public static String InputError = "Plase check your input,the account or password have error!";
public static String LogonSuccess = "Logon success!";
public static String InputMoneyException = "Plase check your money,just the hundred multiple is allow";
public static String SaveMoneySuccess = "SaveMoney success!Deposit amount is: ";
public static String TakeMoneyStr = "Plase input you amount of money,at least one hundred and just the hundred multiple";
public static String TakeMoneyException = "Operation false! Your money is not enough";
public static String TakeMoneySuccess = "TakeMoney success!Take amount is: ";
public static String TransferStr = "Plase input the target account";
public static String TransferMoney = "Plase input the transfer amount";
public static String TransferMoneyException = "Operation false! Your money is not enough";
public static String TransfertargetException = "Plase check your input, the target account is not exist";
public static String TransferSuccess = "Transfer success,the transfer amount is: ";
public static String QueryBbalance = "Your balabce is: ";
public static ArrayList<Account> accountList = new ArrayList<Account>();
/**
*
* @param account
* @param money
* @return
*/
public static boolean saveMoney(Account account,double money){
boolean flag = false;
if(account != null && money > 0 && money%100==0){
account.setAccountMoney(account.getAccountMoney() + money);
flag = true;
}
return flag;
}
public static boolean takeMoney(Account account,double money){
boolean flag = false;
if(account != null && money > 0 && money%100==0){
account.setAccountMoney(account.getAccountMoney() - money);
flag = true;
}
return flag;
}
/**
*
* @param flag
*/
public static void handleResult(Boolean flag){
}
/**
* 创建用户
* @param name
* @param pwd
* @return
*/
public static Account EstablishAccount(String name,String pwd){
Account account = null;
if(!name.equals("") && !pwd.equals("")){
account = new Account(name,pwd);
}
return account;
}
/**
* 处理添加过程的方法
*/
public static void HandleEstablishAccountProcess() throws InterruptedException{
Account account = null;
Boolean establishFlag = true;
while(establishFlag){
System.out.println(InputAccountName);
String accountName = new Scanner(System.in).next();
System.out.println(InputAccountPwd);
String accountPwd = new Scanner(System.in).next();
if(accountName!=null && accountPwd !=null){
account = EstablishAccount(accountName,accountPwd);
}
if(account != null){
System.out.println(EstablishSuccess);
accountList.add(account);
boolean choiceEstablishSuccessFollowflag = true;
while(choiceEstablishSuccessFollowflag){
System.out.println(EstablishSuccessFollow);
char choiceEstablishSuccessFollow;
choiceEstablishSuccessFollow = new Scanner(System.in).next().charAt(0);
if(choiceEstablishSuccessFollow == '1'){
establishFlag = true;
choiceEstablishSuccessFollowflag = false;
}else if(choiceEstablishSuccessFollow == '2'){
establishFlag = false;
choiceEstablishSuccessFollowflag = false;
}else{
System.out.println(ChoiceError);
choiceEstablishSuccessFollowflag = true;
}
}
}else{
System.out.println(EstablishFailure);
}
}
}
/**
* Save
* @param account
*/
public static void storage(Account account){
System.out.println(SaveMoneyStr);
double amount = new Scanner(System.in).nextDouble();
if(amount>0 && amount%100==0){
boolean saveResult = saveMoney(account,amount);
if(saveResult){
System.out.println(SaveMoneySuccess + amount);
}else{
System.out.println(InputMoneyException);
}
}else{
System.out.println(InputMoneyException);
}
}
/**
* Take
* @param account
*/
public static void takeMoney(Account account){
System.out.println(TakeMoneyStr);
double takeAmount = new Scanner(System.in).nextDouble();
if(takeAmount < account.getAccountMoney()){
if(takeAmount>0 && takeAmount%100==0){
boolean takeResult = takeMoney(account,takeAmount);
if(takeResult){
System.out.println(TakeMoneySuccess + takeAmount);
}else{
System.out.println(InputMoneyException);
}
}else{
System.out.println(InputMoneyException);
}
}else{
System.out.println(TakeMoneyException);
}
}
/**
* Transfer
* @param account
*/
public static void transferMoney(Account account){
boolean transferFlag = true;
System.out.println(TransferStr);
String targetAccount = new Scanner(System.in).next();
System.out.println(TransferMoney);
double transferMoney = new Scanner(System.in).nextDouble();
if(transferMoney>0 && transferMoney%100==0){
if(transferMoney < account.getAccountMoney()){
for(Account transferAccount:accountList){
if(transferAccount.getAccountName().equals(targetAccount)){
transferAccount.setAccountMoney(transferAccount.getAccountMoney() + transferMoney);
account.setAccountMoney(account.getAccountMoney() - transferMoney);
System.out.println(TransferSuccess + transferMoney);
transferFlag = false;
break;
}
}
if(transferFlag){
System.out.println(TransfertargetException);
}
}else{
System.out.println(TransferMoneyException);
}
}else{
System.out.println(InputMoneyException);
}
}
/**
* CheckBalance
*/
public static void checkBalance(Account account){
System.out.println(QueryBbalance+account.getAccountMoney());
}
/**
* OperationProcess
*/
public static void OperationProcess(){
System.out.println(InputLogonAccountName);
String accountName = new Scanner(System.in).next();
System.out.println(InputLogonAccountPwd);
String accountPwd = new Scanner(System.in).next();
if(!accountName.equals("") && !accountPwd.equals("")){
for(Account account : accountList){
if(account.getAccountName().equals(accountName) && account.getAccountPwd().equals(accountPwd)){
System.out.println(LogonSuccess);
boolean flag = true;
while(flag){
System.out.println(WelcomeStr);
System.out.println(ChoiceOperation);
char choice = new Scanner(System.in).next().charAt(0);
switch(choice){
case '1':
storage(account);
break;
case '2':
takeMoney(account);
break;
case '3':
transferMoney(account);
break;
case '4':
checkBalance(account);
break;
case '5':
System.out.println(ExitStr);
flag = false;
break;
default:
System.out.println(ChoiceError);
break;
}
}
break;
}else{
System.out.println(InputError);
}
}
}else{
System.out.println(InputErrorNotNull);
}
}
public static void main(String[] args){
boolean logFlag = true;
while(logFlag){
System.out.println(LogWelcomeStr);
System.out.println(LogChoice);
char logChoice = new Scanner(System.in).next().charAt(0);
switch(logChoice){
case '1':
try {
HandleEstablishAccountProcess();
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
case '2':
OperationProcess();
break;
case '3':
System.out.println(ExitStr);
logFlag = false;
break;
default:
System.out.println(ChoiceError);
break;
}
}
}
}