Home > Back-end >  What a great god can teach me this problem is how to write
What a great god can teach me this problem is how to write

Time:10-01

School speak too fast, not keep up with... Can only write the previous diu diu

CodePudding user response:

Turn to

CodePudding user response:

Actually not difficult, basic classes and their methods defined?
Print menu is needless to say, isn't it?
The other main is the use of the Scanner class!

The building Lord come on, believe you do!

CodePudding user response:

Give you write a simple example, there would be a kung fu

 import Java. Util. HashMap; 
import java.util.Map;
import java.util.Scanner;

The class BankCard {
Private long cardNum;
Private String customerName.
private String password;
Private double balance;
Private String idCardNum;

Public BankCard (String idCardNum, String customerName) {
Enclosing idCardNum=idCardNum;
Enclosing customerName=customerName;
CardNum=62208888;
Password="666666";
The balance=0.0;
}

Public long getCardNum () {
Return cardNum;
}

Public String getCustomerName () {
Return customerName.
}

Public String getIdCardNum () {
Return idCardNum;
}

Public double the getBalance () {
Return the balance.
}

Public String getCode () {
Return the password;
}

Public void changePwd (String PWD) {
Enclosing the password=PWD;
}

Public void dePosit (double money) {
The balance +=money;
}

Public String drawMoney (double money) {
If (money & gt; The balance) {
Return the String. Format (" your current balance is insufficient, the current balance for %, 2 f ", the balance).
}
The balance -=money;
Return the String. Format (". Successful withdrawal, the current balance for % 2 f ", the balance).
}
}

Public class TestBankCard {
String [] menu={
"Account",
"Window for user information,"
"Window for balance,"
"Change password",
"Deposit",
"Withdrawal,"
"Exit"
};

Private Map CARDS=new HashMap<> (a);

Public static void main (String [] args) {
TestBankCard TBC=new TestBankCard ();
Int choice=0;
While (true) {
Choice=TBC. ChoiceMenu ();
If (choice==7) {
System. Out.println (" thank you for your use. Goodbye!" );
break;
}
TBC. PerformtMenu (choice);
}
}

Public int choiceMenu () {
System. The out. Println (" * * * * "welcome to the bank management System).
For (int I=0; I & lt; Menu. Length; I++) {
System. The out. Printf (" % d % s \ n ", I + 1, menu [I]);
}
System. The out. Println (" * * * * * * * * * * * * * * * * * ");
Scanner sc=new Scanner(System.in);
Int choice=0;
While (true) {
System. The out. Printf (" please select: ");
Try {
Choice=Integer. The valueOf (sc) nextLine ());
If (choice & gt;=1 & amp; & Choice & lt; {
=7)break;
}
Throw new Exception (" input error!" );
} the catch (Exception e) {
System. Out.println (" option is invalid, please choose!" );
}
}
Return the choice;
}

Public void performtMenu (int menuId) {
Scanner sc=new Scanner(System.in);
BankCard BC=null;
If (menuId==1) {
System. The out. Println (" input id and name ");
System. The out. Printf (" please enter the id: ");
String idNum=sc. NextLine ();
System. The out. Printf (" please input your name: ");
String name=sc. NextLine ();
BC=new BankCard (idNum, name);
CARDS. The put (BC. GetCardNum (), BC);
} else {
BC=searchCard ();
If (BC!=null) {
The switch (menuId) {
Case 2:
System. The out. Printf (" \ t name card: % d: % s \ n password: % s \ t id: % s \ n ",
BC. GetCardNum (), BC. GetCustomerName (), BC, getCode (), BC. GetIdCardNum ());
break;
Case 3:
System. The out. Printf (" balance: % 2 f \ n ", BC. The getBalance ());
break;
Case 4:
String pwd;
While (true) {
System. The out. Printf (" please enter the original password: ");
The PWD=sc. NextLine ();
If (PWD) equals (BC, getCode ())) {
break;
}
System. The out. Println (" password is not correct, please retyping ");//can output in three card lock
}
System. The out. Printf (" please enter a new password: ");
The PWD=sc. NextLine ();
BC. ChangePwd (PWD);
break;
Case 5:
Case 6:
Double money=0.0;
String buf="deposit";
If (menuId==6)
Buf="withdrawal";
While (true) {
System. The out. Printf (" please enter the amount of % s: ", buf);
Try {
Money=Double. The valueOf (sc) nextLine ());
break;
} the catch (Exception e) {
System. The out. Println (" input amount is wrong, please heavy lose ");
}
}
If (menuId==5) {
BC. DePosit (money);
System. The out. Printf (" balance: % 2 f \ n ", BC. The getBalance ());
} else {
System. The out. Println (BC. DrawMoney (money));
}
break;
}
} else {
System. Out.println (" no user account, please open an account first!" );
}
}
}

Public BankCard searchCard () {
BankCard BC=null;
For (Map. Entry En: CARDS. EntrySet ()) {
nullnullnullnullnullnull
  • Related