无目录
用户class
public class User {
private String cardNo;
private String identity;
private String userName;
private String password;
private String phone;
private double balance;
public User() {
}
public User(String cardNo, String identity, String userName, String password, String phone, double balance) {
this.cardNo = cardNo;
this.identity = identity;
this.userName = userName;
this.password = password;
this.phone = phone;
this.balance = balance;
}
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
public String getIdentity() {
return identity;
}
public void setIdentity(String identity) {
this.identity = identity;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
@Override
public String toString() {
return "User{" +
"cardNo='" + cardNo + '\'' +
", identity='" + identity + '\'' +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
", phone='" + phone + '\'' +
", balance=" + balance +
'}';
}
银行class
import java.util.Objects;
import java.util.Scanner;
public class Bank {
// static boolean flag = false;
private final User[] users = new User[5];
User user = new User();
private static final Scanner glo_sc = new Scanner(System.in);
public void initial (){
User user1 = new User("12345","98765","Henry","1234","02198765678",20000.0);
User user2 = new User("23456","87654","Harry","1234","02187654567",30000.0);
User user3 = new User("34567","76543","Hudson","1234","02176543456",40000.0);
User user4 = new User("45678","65432","Leon","1234","02165432345",50000.0);
User user5 = new User("56789","54321","Levis","1234","02154321234",60000.0);
users[0] = user1;
users[1] = user2;
users[2] = user3;
users[3] = user4;
users[4] = user5;
}
public static void main(String[] args) {
Bank bank = new Bank();
bank.initial();
while (true) {
System.out.println("=====TXY=Bank=AYM=System=====");
System.out.println("Press Y for continue. Press 0 for leave:");
String command = glo_sc.next();
if (Objects.equals(command, "y")){
bank.login();
}else if (Objects.equals(command, "0")){
break;
}else {
System.out.println("Input incorrect, try again please.");
}
}
}
private void login(){
while (true) {
System.out.println("====Customer==Login====");
System.out.println("Please enter your CardNo:");
String num = glo_sc.next();
System.out.println("Please enter your Password:");
String pw = glo_sc.next();
for (User value : users) {
if (num.equals(value.getCardNo()) && pw.equals(value.getPassword())) {
System.out.println("Welcome back, Our friend dear " + value.getUserName() + "!");
user = value;
// flag = true;
// System.out.println(flag);
showMenu();
return;
}
}
// System.out.println(flag);
System.out.println("Sorry, something must got wrong!");
while (true) {
System.out.println("Press Y to continue. Press 0 to back:");
String command = glo_sc.next();
if ("y".equals(command)){
break;
}else if ("0".equals(command)){
return;
}else {
System.out.println("chose your operation by Y or 0");
}
}
}
}
private void showMenu() {
System.out.println("=====Customer==Service==Center=====");
label:do {
System.out.println("=====1.Save===2.Withdraw===3.Trans===4.Query Balance===5.Modify Password===0.Exit======");
System.out.println("Chose what you gonna do:");
int choice = glo_sc.nextInt();
switch (choice){
case 1:
save();
break ;
case 2:
withDraw();
break ;
case 3:
trans();
break ;
case 4:
queryBalance();
break ;
case 5:
modifyPassword();
break ;
case 0:
System.out.println("Welcome to your next visit!");
break label;
default:
System.out.println("Incorrect Input, try again please.");
break ;
}
}while (true);
}
private void withDraw() {
System.out.println("======Customer==Withdraw==Service=======");
System.out.println("Enter the amount you gonna withdraw:");
double wdr = glo_sc.nextDouble();
if (wdr <= user.getBalance()) {
if (wdr % 100 == 0.0) {
double balance = user.getBalance() - wdr;
user.setBalance(balance);
System.out.println("Withdraw succeed, your balance update to: " + user.getBalance());
} else {
System.out.println("Sorry, the ATM doesn't support amount less than 100.0 ");
}
} else {
System.out.println("Sorry, your balance isn't enough.");
}
}
private void save() {
System.out.println("======Customer==Deposit==Service=======");
System.out.println("Enter the amount of you deposit:");
double deposit = glo_sc.nextDouble();
if (deposit > 0) {
deposit = user.getBalance() + deposit;
user.setBalance(deposit);
System.out.println("Deposit succeed. Your balance update to " + user.getBalance());
} else {
System.out.println("The amount you input is incorrect, deposit failed.");
}
}
private void trans() {
System.out.println("======Customer==Transfer==Service=======");
System.out.println("Enter account which you gonna transfer to:");
String toAcc = glo_sc.next();
System.out.println("Enter how many you gonna transfer:");
double amount = glo_sc.nextDouble();
if (amount <= user.getBalance()){
for (User value : users) {
String existAcc = value.getCardNo();
if (toAcc.equals(existAcc)) {
double myBalance = user.getBalance() - amount;
user.setBalance(myBalance);
System.out.println("Transferring for you~~~~");
double hisBalance = value.getBalance();
value.setBalance(hisBalance + amount);
System.out.println("Transferring succeed. Your balance change to: " + user.getBalance());
return;
}
}
System.out.println("The target account is incorrect.");
}else {
System.out.println("Your balance isn't enough.");
}
}
private void queryBalance() {
System.out.println("Your current balance is: "+user.getBalance());
while (true) {
System.out.println("Press 0 back to previous:");
int press = glo_sc.nextInt();
if (press == 0) {
return;
} else {
System.out.println("Input illegal.");
}
}
}
private void modifyPassword() {
System.out.println("Enter your new password:");
String newPw = glo_sc.next();
System.out.println("Enter new password one more time:");
String newPw1 = glo_sc.next();
if (newPw.equals(newPw1)){
user.setPassword(newPw);
System.out.println("Modify succeed. Take care your new password carefully.");
}else {
System.out.println("The password you input twice are not the same, operation stop!");
}
}
}