Encapsulation, inheritance, and polymorphism of Java's------Class (II)

Source: Internet
Author: User

issue: Statement of bank account class, member variables include account number, depositor name, opening time, identity card numbers, deposit balance and other accounts information, member methods include account opening, deposit, withdrawal, inquiry (balance, details), sales and other operations.

The main members are described below

public class Accounts {                        //account class    private String name;                   Depositor name    private double balance;                  Account balance Public Accounts    (String name)             //constructor method, parameter specify depositor name public    String getName ()                //Return account name    public Double balance ()                 //View account balance public    Boolean put (double value)         //deposit operation, parameter for deposit amount public    double get ( Double value)           //withdrawal operation, parameter for withdrawal amount, return actual fetch amount}



Import java.util.scanner;import java.math.*;class account{string name;static int count=0;static String[] Opea=new String[10000];static double[] val=new double[1000];d ouble balance;static String accountid= "6215000000000000000"; String id;public Account (string name,double balance,string id) {this.set (name,balance,id);} public void Set (String name,double balance,string id) {this.name=name;this.balance=balance;if (Id.length () >18| | Id.length () <1) {System.out.println ("Input ID Error!"); return;} This.id=id;accountid=add (AccountID);} public void Inquire () {System.out.println ("-------Balance and trad information-------"); System.out.println ("Balance:" +this.balance); for (int i=0;i<count;i++) {System.out.println (opea[i]+ "" +val[i]);}} Public String GetName () {return "Accoutname:" +this.name+ "\naccountid" +this.accountid+ "\nid:" +id.substring (0,6) + "* * "+id.substring (14,id.length ());} public double balance () {return this.balance;} Public Boolean put (double value) {this.balance+=value;if (value==0){return false;} Opea[count]= "put"; Val[count++]=value;return true;} Public double get (double value) {if (value>this.balance) {System.out.println ("lack of balance!!!"); return 0;} Opea[count]= "Get"; Val[count++]=value;this.balance-=value;return value;} public Boolean exit () {return true;} public static string Add (String str) {char[] c=new char[19];int leng=19;int sign=0,i;for (i=0;i<19;i++) {leng=leng-1; int Sum=0;sum+=str.charat (Leng)-' 0 '; if (i==0) {sum+=1;} Sum+=sign;c[leng]= (char) (sum+48); if (sum>=10) {sign=1;c[leng]= (char) (sum%10+48);} else{sign=0;}} return string.valueof (c);}} Class Main{public static void Main (string[] args) {account p=new account ("Tom", 1000, "340822123409231190"); System.out.println ("-------accoutn information-------"); System.out.printf (P.getname ()); System.out.println (); System.out.println ("Balance:" +p.balance ());p. Put (123213);p. Inquire (); System.out.println (); Account p2=new Account ("Sex", 100000, "340822123434533454"); System.out.println ("-------accoutn information-------"); System.out.printf (P2.getname ()); System.out.println (); System.out.println ("Balance:" +p2.balance ());p 2.get (12); System.out.println ("Balance:" +p2.balance ());p 2.get (12000000); System.out.println ("Balance:" +p2.balance ());p 2.inquire (); System.out.println ();}}


Encapsulation, inheritance, and polymorphism of Java's------Class (II)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.