属性:账号、密码、余额,银行名称。
方法:注册账号,存款、取款、查询余额。 创建该类对象并测试
定义
import java.util.Scanner;
public class Bank {
int Zhanghao;
int mima;
int yue = 1000;
String bankname;
Scanner c = new Scanner(System.in);
public void Chuangjian() //创建账号
{
System.out.println("输入创建的账号:");
Zhanghao = c.nextInt();
System.out.println("请设置密码");
mima = c.nextInt();
System.out.println("创建成功");
}
public int cun()//存
{
System.out.println("输入存的金额:");
int a = c.nextInt();
yue = yue + a;
System.out.println("存款成功");
return yue;
}
public int qu()//取
{
System.out.println("输入取的金额:");
int a = c.nextInt();
if (a > yue) {
System.out.println("余额不足");
}
else {
System.out.println("取款成功!");
yue = yue - a;
}
return yue;
}
public void chaxun()//查询
{
System.out.println("余额为:" + yue);
}
}
测试
class testBank {
public static void main(String[] args) {
Bank a1 = new Bank();
a1.Chuangjian();
System.out.println("请输入账号");
Scanner t = new Scanner(System.in);
int b1 = t.nextInt();
System.out.println("请输入密码");
int b2 = t.nextInt();
if (b1 == a1.Zhanghao & b2 == a1.mima) {
boolean x = true;
do {
System.out.println("输入操作:1.取款 2.存款 3.查询 4.退出");
int c = t.nextInt();
switch (c) {
case 1: {
a1.qu();
System.out.println();
continue;
}
case 2: {
a1.cun();
System.out.println();
continue;
}
case 3: {
a1.chaxun();
System.out.println();
continue;
}
case 4: {
x = false;
break;
}
}
} while (x == true);
} else {
System.out.println("账号或密码错误");
}
}
}
运行结果