用java基础写一个银行系统
首先创建一个用户类,将用户的所有属性放进去
package com.hniu.Bank;
public class User {
//卡号
private String cardNo;
//密码
private String password;
//用户名
private String userName;
//余额
private double balance;
//构造方法 (无参)
public User() {
}
//构造方法(有参)
public User(String cardNo, String password, String userName, double balance) {
this.cardNo = cardNo;
this.password = password;
this.userName = userName;
this.balance = balance;
}
//get/set方法
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
然后创建一个Bank银行类
package com.hniu.Bank;
import java.util.Scanner;
public class Bank {
private final User[] users=new User[5];
private int size;
//初始化方法
public void initial(){
//实例化一个有参构造函数,创建第一个用户
User user1 = new User("6222003040001234","123456","韩信",100000.00);
//实例化一个无参构造函数,创建第二个用户
User user2 = new User();
user2.setCardNo("6222003040001245");
user2.setPassword("123456");
user2.setUserName("橘右京");
user2.setBalance(10.00);
//将用户放入数组中
users[0]=user1;
users[1]=user2;
size=2;
System.out.println("==========初始化成功。。。请登陆==========");
}
//登陆
public void login(){
Scanner input = new Scanner(System.in);
System.out.println("请输入卡号:");
String cardNo=input.next();
System.out.println("请输入密码:");
String pwd=input.next();
User u=null;
for (int i = 0; i <size ; i++) {
if (users[i].getCardNo().equals(cardNo)&&users[i].getPassword().equals(pwd)){
u=users[i];
break;
}
}
if (u!=null){
menu(u);
}else {
System.out.println("用户名或密码错误,请重新输入。。。");
}
}
//菜单
public void menu(User u){
System.out.println("==========欢迎进入xxx银行系统==用户名:"+u.getUserName()+"==========");
do {
System.out.println("1.存款\n2.取款\n3.转账\n4.查询余额\n5.修改密码\n0.退出");
System.out.println("请输入您的操作:");
Scanner input = new Scanner(System.in);
int choice=input.nextInt();
switch (choice){
case 1:
draw(u);
break;
case 2:
save(u);
break;
case 3:
transfer(u);
break;
case 4:
balance(u);
break;
case 5:
pwd(u);
break;
case 0:
return;
}
}while (true);
}
//取钱
public void draw(User u) {
System.out.println("请输入取款金额:");
Scanner input = new Scanner(System.in);
double money=input.nextDouble();
if (money>0){
u.setBalance(u.getBalance()+money);
System.out.println("余额:"+u.getBalance()+"");
}else {
System.out.println("输入有误,请重新输入。。。");
}
}
//存钱
public void save(User u) {
System.out.println("请输入存款金额:");
Scanner input = new Scanner(System.in);
double money=input.nextDouble();
if (money>0){
u.setBalance(u.getBalance()-money);
System.out.println("余额:"+u.getBalance()+"");
}else {
System.out.println("输入有误,请重新输入。。。");
}
}
//转账
public void transfer(User u){
System.out.println("请输入对方卡号:");
Scanner input = new Scanner(System.in);
String cardNo=input.next();
User toUser=null;
for (int i = 0; i <size ; i++) {
if (cardNo.equals(users[i].getCardNo())){
toUser=users[i];
break;
}
}
if (toUser!=null){
System.out.println("请输入转账金额");
double money=input.nextDouble();
toUser.setBalance(toUser.getBalance()+money);
u.setBalance(u.getBalance()-money);
System.out.println("转账成功。。。");
System.out.println("余额为:"+u.getBalance()+"");
}else {
System.out.println("对方账号不存在!!!!");
}
}
//查询余额
public void balance(User u){
System.out.println("余额为:"+u.getBalance()+"");
}
//修改密码
public void pwd(User u){
System.out.println("请输入旧密码:");
Scanner input = new Scanner(System.in);
String oldPwd=input.next();
if (oldPwd.equals(u.getPassword())){
System.out.println("请输入新密码:");
String newPwd=input.next();
u.setPassword(newPwd);
}else {
System.out.println("旧密码错误,请重新输入。。。");
}
}
}
最后一个测试类用来运行程序
package com.hniu.Bank;
public class TestBank {
public static void main(String[] args) {
Bank bank = new Bank();
bank.initial();
bank.login();
}
}