Java基础知识综合练习_简单的银行账户操作系统

使用java实现一个简单的银行账户类,

其中包括:
账户信息:
账号、
姓名、
开户时间、
身份证号码、
账户上的金额
等成员。
有:
存款方法、
取款方法、
显示开户时间的方法、
获得账上的金额的方法
等。
并编写测试类。

账户类:

 1 public class TestDay12_2_Account {
 2 
 3     String id;//帐号
 4     int password;//密码
 5     String name;//姓名
 6     String personId;//身份证号
 7     double balance = 0.0;
 8     String time;
 9 
10     public TestDay12_2_Account() {
11         super();
12     }
13 
14     public TestDay12_2_Account(String id, int password, String name, String personId, String time) {
15         super();
16         this.id = id;
17         this.password = password;
18         this.name = name;
19         this.personId = personId;
20         this.time = time;
21     }
22 
23     // 存款方法
24     public void deposit(double balance) {
25         this.balance += balance;
26         System.out.println("成功存入" + balance + "元");
27         System.out.println("当前总余额为:" + this.balance + "元");
28     }
29 
30     // 取款方法
31     public void withdraw(double balance) {
32         this.balance -= balance;
33         System.out.println("成功取出" + balance + "元");
34         System.out.println("当前总余额为:" + this.balance + "元");
35     }
36 
37     // 输出信息
38     public String test() {
39         return "id:" + id + "\r姓名:" + name + "\r身份证号:" + personId + "\r当前余额:" + balance + "\r开户时间:" + time;
40     }
41 }

账户操作类:

  1 public class TestDay12_2_AccountTool {
  2     
  3     // 起始界面
  4     public static int begin() {
  5         System.out.println("请选择您要执行的操作(输入数字):");
  6         System.out.println("1.登录");
  7         System.out.println("2.注册");
  8         System.out.println("0.退出");
  9         int operation = TestDay12_2_AccountTool.getInt("===================");
 10         return operation;
 11     }
 12 
 13     // 注册方法
 14     public static TestDay12_2_Account register() {
 15         TestDay12_2_Account a = new TestDay12_2_Account();
 16 
 17         a.id = TestDay12_2_AccountTool.getString("请设置用户名:");
 18         a.password = TestDay12_2_AccountTool.getInt("请设置密码(数字):");// 密码
 19         a.name = TestDay12_2_AccountTool.getString("请输入您的姓名:");// 姓名
 20         a.personId = TestDay12_2_AccountTool.getString("请输入您的身份证号:");// 身份证号
 21         a.balance = 0.0;
 22         // 创建日期对象
 23         Date d = new Date();
 24         // 给定模式
 25         SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
 26         String s = sdf.format(d);
 27         a.time = s;
 28         return a;
 29     }
 30 
 31     // 登录密码错误执行
 32     public static int loginerror() {
 33         int x = 0;
 34         System.out.println("用户名或密码错误,请选择您要执行的操作(输入数字):");
 35         System.out.println("1.重试");
 36         System.out.println("2.退出");
 37         int operationerror = TestDay12_2_AccountTool.getInt("===================");
 38         switch (operationerror) {
 39         case 1:
 40             break;
 41         case 2:
 42             x = 1;
 43             break;
 44         default:
 45             System.out.println("请输入指定的数字");
 46             break;
 47         }
 48         return x;
 49     }
 50 
 51     // 登录成功执行
 52     public static void loginyes(TestDay12_2_Account a) {
 53         System.out.println("======登录成功======");
 54         while (true) {
 55             System.out.println("请选择您要执行的操作(输入数字):");
 56             System.out.println("1.查询");
 57             System.out.println("2.存款");
 58             System.out.println("3.取款");
 59             System.out.println("0.退出");
 60 
 61             // 获取操作数字
 62             int operationnum = TestDay12_2_AccountTool.getInt("===================");
 63             // 判断是否退出
 64             if (operationnum == 0) {
 65                 break;
 66             }
 67 
 68             // 执行操作数
 69             TestDay12_2_AccountTool.operationnum(a, operationnum);
 70         }
 71     }
 72 
 73     // 具体操作方法
 74     public static void operationnum(TestDay12_2_Account a, int operationnum) {
 75         // 具体操作执行
 76         switch (operationnum) {
 77         case 1:
 78             // 输出信息
 79             System.out.println(a.test());
 80             break;
 81         case 2:
 82             // 存款操作
 83             System.out.println("请输入存入金额");
 84             double deposit = Double.parseDouble(TestDay12_2_AccountTool.getmoney("==================="));
 85             a.deposit(deposit);
 86             break;
 87         case 3:
 88             // 取款操作
 89             System.out.println("请输入取出金额");
 90             double withdraw = Double.parseDouble(TestDay12_2_AccountTool.getmoney("==================="));
 91             a.withdraw(withdraw);
 92             break;
 93         default:
 94             System.out.println("请输入指定的数字");
 95             break;
 96         }
 97     }
 98 
 99     // 获取文本数据
100     public static String getString(String i) {
101         // 创建键盘对象
102         @SuppressWarnings("resource")
103         Scanner sc = new Scanner(System.in);
104 
105         // 输入提示
106         System.out.println(i);
107         // 赋值数据
108         String x = sc.next();
109         return x;
110     }
111 
112     // 获取整数数据
113     public static int getInt(String i) {
114         // 创建键盘对象
115         @SuppressWarnings("resource")
116         Scanner sc = new Scanner(System.in);
117 
118         // 输入提示
119         System.out.println(i);
120         while (true) {
121             if (sc.hasNextInt()) {
122                 // 赋值数据
123                 int x = sc.nextInt();
124                 return x;
125             } else {
126                 System.out.println("请输入整数数字");
127                 sc = new Scanner(System.in);
128             }
129         }
130     }
131 
132     // 获取金额数据(整数/小数)
133     public static String getmoney(String i) {
134         // 创建键盘对象
135         @SuppressWarnings("resource")
136         Scanner sc = new Scanner(System.in);
137 
138         // 输入提示
139         System.out.println(i);
140         while (true) {
141             if (sc.hasNextDouble()) {
142                 String x = Double.toString(sc.nextDouble());
143                 return x;
144             } else {
145                 System.out.println("请输入数字");
146                 sc = new Scanner(System.in);
147             }
148         }
149     }
150 }

主类:

 1 import java.util.ArrayList;
 2 
 3 public class TestDay12_2 {
 4     public static void main(String[] args) {
 5         ArrayList<TestDay12_2_Account> l = new ArrayList<TestDay12_2_Account>();
 6         // 创建对象
 7         TestDay12_2_Account a = new TestDay12_2_Account("yang", 123456, "杨金儒", "21102219*********", "[email protected]");
 8         l.add(a);
 9         System.out.println("======欢迎光临======");
10         // 账户操作循环
11         while (true) {
12             int operation = TestDay12_2_AccountTool.begin();
13             if (operation == 1) {
14                 System.out.println("======请登录======");
15                 // 登录循环
16                 while (true) {
17                     // String id = DiyToolsaaa.DiyOperation.getString(i);
18                     String id = TestDay12_2_AccountTool.getString("请输入用户名:");
19                     int password = Integer.parseInt(TestDay12_2_AccountTool.getString("请输入密码:"));
20                     //登录成功标记
21                     boolean access = false;
22                     for (TestDay12_2_Account user : l) {
23                         if (id.equals(user.id) && password == user.password) {
24                             access = true;
25                             TestDay12_2_AccountTool.loginyes(user);
26                             break;
27                         }
28                     }
29                     if (!access) {
30                         // 登录密码错误执行
31                         int loginerrorreturn = TestDay12_2_AccountTool.loginerror();
32                         if (loginerrorreturn == 1) {
33                             break;
34                         }
35                     }
36                 }
37             } else if (operation == 2) {
38                 l.add(TestDay12_2_AccountTool.register());
39                 System.out.println("======注册成功======");
40             } else if (operation == 0) {
41                 break;
42             } else {
43                 System.out.println("请输入指定的数字");
44             }
45         }
46         System.out.println("---------------");
47         System.out.println("程序已结束");
48     }
49 }

猜你喜欢

转载自www.cnblogs.com/youlixin/p/10245776.html
今日推荐