Class encapsulation, inheritance and polymorphism

package bank;
import java.text.SimpleDateFormat;//Common date manipulation tool class
import java.util.Calendar;//Get a calendar using the default time zone and locale
import java.util.Date;
public class yinhang {
  private String acount; //Account
  private String name;//Name
  private String time;//Time
  private String id;//Identity
  private double num;//Amount
  public String getAcount() {
   return acount;
  }
  public void setAcount(String a) {
   acount = a;
  }
  public String getName() {
   return name;
  }
  public void setName(String n) {
   name = n;
  }
  public String getTime() {
   return time;
  }
  public void setTime(String t) {
   time = t;
  }
  public String getId() {
   return id;
  }
  public void setId(String ID) {
   id = ID;
  }
  public double getNum() {
   return num;
  }
  public void setNum( double m) {
   num = m;
  }
  public void cunkuan(double m){
   num=num+m;
  }
  //withdrawal method
  public void qukuan(double m){
   if(m>num){
    System.out.println(" Insufficient balance, can't withdraw money");
   }else{
    num=num-m;
   }
  }
  //Method to display account opening time
  public String kaiHuTime(){
   Date currDate=Calendar.getInstance().getTime();
   SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
   this.time=sdf.format(currDate);
   return time;
  }
  //Method to get the amount on the account
  public double GetAcountNum(){
   return num;
  }
 }

package bank;
import java.util.Scanner;//input source program
public class Main {
 public static void main(String[] args) {
  yinhang bank=new yinhang();
  bank.setAcount("6666");
  bank. setId("123456789");
  bank.setName("by");
  bank.setNum(10000);
  Scanner sc=new Scanner(System.in);
  System.out.println("Please enter your account number");
  String acount=sc.nextLine();
  System.out.println("Please enter your password");
  String pwd=sc.nextLine();
  if(pwd.equals("123456")&& acount.equals(bank. getAcount())){//The following operations can only be performed if the password is correct
    while(true){
     System.out.println("The current amount of your account is: "+bank.getNum()+"\nPlease enter the following operations: ");
     System.out.println("1: Deposit");
     System.out.println("2: Withdrawal");
     System.out.println("3: Display the time of opening the account");
     System.out.println("4: Get the amount of the account");
     System.out.println("5: Exit the current account");
    Scanner scan= new Scanner(System.in);//There may be influence between the same variable sc, you can renew a
    String option=scan.nextLine();
    
    switch(option){//Select service
    case "1" :
     System.out .println("Please enter the money you want to deposit");
     double num=scan.nextDouble();
     bank.cunkuan(num);
     System.out.println("The current amount is"+bank.getNum());
     break ;
    case "2":
     System.out.println("Please enter the money you want to withdraw");
     double num1=scan.nextDouble();
     bank.qukuan(num1);
     System.out.println("The current balance is" +bank.getNum());
     break;
    case "3":
     System.out.println("The account opening time is: ");
     System.out.println(bank.kaiHuTime());
     break;
    
    case "4":
     System.out.println("The current account amount is: "+bank. getNum());
     break;
     
    case "5":
     System.out.println("Exit the system");
     System.exit(0);
     break;
     default : System.out.println("Sorry, your input is wrong" );
     break;
    }
     
   }
  }else{
   System.out.println("The password or account is incorrect, please re-enter");
   
  }
  
 }
 }

 Result: Please enter your account number
6666
Please enter your password
123456
The current amount of your account is: 10000.0
Please enter the following operations:
1: Deposit
2: Withdraw money
3: Display the time of account opening
4: Obtain the amount of the account
5: Exit the current Account
1
, please enter the money you want to deposit
100
The current amount is 10100.0
The current amount of your account is: 10100.0
Please enter the following operations:
1: Deposit
2: Withdraw money
3: Display the time of opening the account
4: Obtain the amount of the account
5: Exit the current account
2
Please enter the money you want to withdraw
400
The current balance is 9700.0
The current amount of your account is: 9700.0
Please enter the following operations:
1: Deposit
2: Withdrawal
3: Display the time of opening the account
4: Obtain the amount of the account
5: Exit the current account
3 The
account opening time is:
2018-05-01
The current amount of your account is: 9700.0
Please enter the following operations:
1: Deposit
2: Withdrawal
3: Display the time of account opening
4: Get the amount of the account
5: Log out of the current account
4
The current account amount is: 9700.0
The current amount of your account is: 9700.0
Please enter the following operations:
1: Deposit
2: Withdraw money
3: Display the time of opening the account
4: Get the amount of the account
5 : Exit the current account
5
Exit the system

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325103007&siteId=291194637