导航:首页 > 编程语言 > java声明银行类

java声明银行类

发布时间:2024-10-09 01:04:52

① 用java编程设计一个银行账户类,其中包括以下内容,并用字符界面模拟存款和取款过程。

import java.util.Scanner;

public class ZH {

private String zh;//账户
private String password;//密码
private String openTime;//开户时间
private String sfz;//身份证号
private double je;//存款金额
public String getZh() {
return zh;
}
public void setZh(String zh) {
this.zh = zh;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getOpenTime() {
return openTime;
}
public void setOpenTime(String openTime) {
this.openTime = openTime;
}
public String getSfz() {
return sfz;
}
public void setSfz(String sfz) {
this.sfz = sfz;
}
public double getJe() {
return je;
}
public void setJe(double je) {
this.je = je;
}

//存款方法
public void ck(double je){
this.je=this.je+je;//存入的金额加上原有的金额
}

//取款方法
public void qk(double je){
if(je>this.je){//取款金额大于余额
System.out.println("存款余额不足");
}else{
this.je=this.je-je;//原有的金额减去取出的金额
}
}
public static void main(String[] args) {
ZH zh = new ZH();//初始化一个账户信息
zh.setJe(10000.0);//向zh账户添加余额
zh.setOpenTime("2013.12.3");//向zh账户添加开发时间
zh.setPassword("123456");//向zh账户添加密
zh.setSfz("123456789");//向zh账户添加身份证
zh.setZh("zhangsan");//向zh账户添加账号
System.out.println("欢迎光临模拟银行");
Scanner scan = new Scanner(System.in);
int count=0;//记录输入错误的次数
while(1==1){//循环
System.out.println("请输入账号");
String zhm=scan.next();
System.out.println("请输入密码");
String mm=scan.next();
if(zhm.equals(zh.getZh()) && mm.equals(zh.getPassword())){//输入的信息与zh账户信息的密码和账号一致
while(1==1){
System.out.println("当前余额为:"+zh.getJe()+"元。请选择操作:1.存款;2.取款;3.退出(只能输入数字)");
String cz=scan.next();
switch (Integer.parseInt(cz)) {
case 1:
System.out.println("请输入存款金额(输入小数)");
double ckje=scan.nextDouble();
zh.ck(ckje);
System.out.println("实施存款:"+ckje+"元,当前余额为"+zh.getJe()+"元");
break;
case 2:
System.out.println("请输入取款金额(输入小数)");
double qkje=scan.nextDouble();
zh.qk(qkje);
System.out.println("实施取款:"+qkje+"元,当前余额为"+zh.getJe()+"元");
break;
case 3:
break;
default:
System.out.println("暂无此功能");//输入1或者2、3以外的操作
break;
}
if("3".equals(cz)){
break;
}
}
System.out.println("退出操作");
break;
}else{

if(count>=3){
System.out.println("已输入错误三次,账号被锁");
break;//结束循环
}else{
System.out.println("账号或密码错误,请重新输入");
count++;//错误一次count+1
continue;//进入下次循环
}
}
}

}
}

② 在java中,声明银行账户类,成员变量包括账号,储户姓名,开户时间,身份证号码,存款余额等账户信息,成

import java.util.Scanner; public class bank
{ int number,id; int year,month,day; float money; String name; void set(int y,int m,int d)
{ year=y; month=m; day=d; } public String toString()

{ return year+"年"+month+"月"+day+"日"; } public float money1(int n)
{ money=money+n; return money; } public float money2(int x)
{ if(money<x) return 0; money=money-x; return money; } public static void main(String args[])
{ bank b=new bank(); b.set(2009,8,6); b.money=0; b.number=1; b.id=20102; b.name="张三"; System.out.println("用户名:"+b.name);
System.out.println("身份证号:"+b.id); System.out.println("开户日期是:"+b.toString()); System.out.print("请输入存入的数额:"); Scanner sc = new Scanner(System.in); int i = sc.nextInt(); b.money1(i); System.out.print("账户余额:"+b.money); System.out.println(); System.out.print("请输入取款余额:"); Scanner cc = new Scanner(System.in); int j = cc.nextInt(); b.money2(j); System.out.print("账户余额:"+b.money); } }

③ 用JAVA语言编写程序,模拟银行账户功能。要有: 属性:账号、储户姓名、地址、存款余额、最小余额。

package com.cshr.test;

public class bank {
private String card;//账号
private String uname;//储户姓名
private String address;//地址
private double money;//存款余额
public static final double minMoney=0;//最小余额
public bank(){}

public String getCard() {
return card;
}
public void setCard(String card) {
this.card = card;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public static double getMinmoney() {
return minMoney;
}
}

package com.cshr.test;

import java.util.List;

import org.hibernate.Session;

import com.utils.HibernateSessionFactory;

public class bankDao {
//存款
public void addMoney(double money,double Sqlmoney,String card){
Session session=HibernateSessionFactory.getSession();
try{
session.beginTransaction();

String hql="update bank b set b.money+="+money+Sqlmoney+" where b.card='"+card+"'";
session.createQuery(hql).executeUpdate();
session.beginTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.beginTransaction().rollback();
}finally{
HibernateSessionFactory.closeSession();
}
}
//取款
public void delMoney(double money,double Sqlmoney,String card){
Session session=HibernateSessionFactory.getSession();
try{
session.beginTransaction();

String hql="update bank b set b.money+="+(Sqlmoney-money)+" where b.card='"+card+"'";
session.createQuery(hql).executeUpdate();
session.beginTransaction().commit();
}catch(Exception e){
e.printStackTrace();
System.out.println("取款失败");
session.beginTransaction().rollback();
}finally{
HibernateSessionFactory.closeSession();
}
}
//查询所有
public List<bank> selectfind(){
Session session=HibernateSessionFactory.getSession();
session.beginTransaction();
String hql="select b from bank b ";
List<bank> list=session.createQuery(hql).list();
return list;
}
}

④ java定义一个类实现银行账户的概念。

public class BankAccount {

String account ;
int account_num;

//构造函数
public BankAccount(String account,int account_num){
this.account=account;
this.account_num=account_num;
}
//存钱
public String addNum(int num){
account_num= account_num+num;
return "存钱成功";
}

//取钱
public String getNum(int num){
String result;
if(account_num>num){
account_num =account_num-num;
result="取钱成功";
}else{
result="账户余额不足,还剩"+account_num;
}
return result;
}

//显示余额
public String displayNum(){
return "账户余额:"+account_num;
}

//显示账号
public String displayAccount(){
return "账户:"+account;
}

public static void main(String[] args){
BankAccount lucy = new BankAccount("lucy",100);
BankAccount jack = new BankAccount("jack",50);
//显示他们各自的余额
System.out.println("===显示他们各自的余额===");
System.out.println("lucy:"+lucy.displayNum());
System.out.println("jack:"+jack.displayNum());
//存钱取钱
System.out.println("===存钱取钱===");
System.out.println("lucy取50:"+lucy.getNum(50));
System.out.println("jack存200:"+jack.addNum(200));
//显示账户 显示余额
System.out.println("===存钱取钱===");
System.out.println(lucy.displayAccount()+""+lucy.displayNum());
System.out.println(jack.displayAccount()+""+jack.displayNum());

}

}

运行结果:
===显示他们各自的余额===
lucy:账户余额:100
jack:账户余额:50
===存钱取钱===
lucy取50:取钱成功
jack存200:存钱成功
===存钱取钱===
账户:lucy账户余额:50
账户:jack账户余额:250

阅读全文

与java声明银行类相关的资料

热点内容
一个软件的服务器地址怎么看 浏览:700
干程序员可以干兼职吗 浏览:640
go中文pdf 浏览:530
电脑dns的服务器地址设置 浏览:530
编程出1000以内水仙花数 浏览:113
程序员多大转行 浏览:879
单片机灯 浏览:871
如何启动u8管理服务器 浏览:280
程序员消费多少 浏览:510
编译室半年总结怎么写范文 浏览:469
windowsapi编程入门教程 浏览:837
单片机51系列 浏览:58
python迭代器是什么 浏览:538
bat备份文件夹删除 浏览:928
推拉的文件夹 浏览:517
嵌入式linux前景 浏览:15
屏幕滚动命令 浏览:504
解压珠含毒素 浏览:772
美文诵读配乐用什么app 浏览:569
java代码设置字体 浏览:452