鱼C论坛

 找回密码
 立即注册
查看: 4237|回复: 2

[学习笔记] 银行系统

[复制链接]
发表于 2021-10-25 13:19:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. package bank;

  2. import java.util.InputMismatchException;
  3. import java.util.Scanner;
  4. class OutException extends Exception

  5. {

  6.         OutException(){

  7.         super("您的余额不足!");

  8.         }

  9. }
  10. class OutException2 extends Exception

  11. {

  12.         OutException2(){

  13.         super("请输入正确的序号!");

  14.         }

  15. }
  16. class Bank
  17. {
  18.         private String Name;
  19.         private double Money;
  20.         Bank() {
  21.         }
  22.         Bank(String Name,double Money) {
  23.                 this.Name = Name;
  24.                 this.Money = Money;
  25.         }

  26.         public void printY() {
  27.                 System.out.println(Name+"当期账户余额为:"+this.Money);
  28.         }
  29.         public void printX(int i) {
  30.        
  31.                 System.out.println((i+1)+"."+Name);
  32.         }
  33.         public void Cun(double Money)
  34.         {
  35.                 this.Money+=Money;
  36.         }
  37.         public void Qu(double Money) throws OutException
  38.         {
  39.                 if (this.Money<Money)
  40.                         throw new OutException();
  41.                 this.Money-=Money;
  42.        
  43.         }
  44. }

  45. public class Test {

  46.         public static void main(String[] args) {
  47.                 // TODO Auto-generated method stub
  48.                 Scanner sc = new Scanner(System.in);
  49.                
  50.                 System.out.println("请输入创建的账户数量:\n");
  51.                 int m = sc.nextInt();
  52.        
  53.                 sc.nextLine();
  54.                 Bank [] p = new Bank[m];
  55.                 for(int i = 0;i<m;i++)
  56.                 {
  57.                         System.out.println("请输入账户"+(i+1)+"的名称和金额:");
  58.                         String x = sc.nextLine();
  59.                         Double y =sc.nextDouble();
  60.                         sc.nextLine();
  61.                         p[i] = new Bank(x,y);
  62.                 }
  63.         while(true) {
  64.         try {
  65.                
  66.                 System.out.println("请选择你的功能:");
  67.                 System.out.println("0.退出程序\t1.存钱\n2.取钱\t3.查询账户\n");
  68.                 int n = sc.nextInt();
  69.                 sc.nextLine();       
  70.                 if(n<0||n>3)
  71.                 {
  72.                         throw new OutException2();
  73.                 }
  74.                
  75.                 switch(n) {
  76.                 case 1:
  77.                         System.out.println("请选择存钱账户:");
  78.                         for(int i=0;i<m;i++)
  79.                         {
  80.                                 p[i].printX(i);
  81.                         }
  82.                         int a = sc.nextInt();
  83.                         sc.nextLine();
  84.                         if(a<1||a>m)
  85.                         {
  86.                                 throw new OutException2();
  87.                         }
  88.                                
  89.                         System.out.println("请输入存钱的金额:");
  90.                         int b = sc.nextInt();
  91.                         sc.nextLine();
  92.                         p[a-1].Cun(b);
  93.                         p[a-1].printY();
  94.                         break;
  95.                 case 2:
  96.                         System.out.println("请选择取钱账户:");
  97.                         for(int i=0;i<m;i++)
  98.                         {
  99.                                 p[i].printX(i);
  100.                         }
  101.                         int a1 = sc.nextInt();
  102.                         sc.nextLine();
  103.                         if(a1<1||a1>m)
  104.                         {
  105.                                 throw new OutException2();
  106.                         }
  107.                         System.out.println("请输入取钱的金额:");
  108.                         int b1 = sc.nextInt();
  109.                         sc.nextLine();
  110.                         p[a1-1].Qu(b1);
  111.                        
  112.                         p[a1-1].printY();
  113.                         break;
  114.                 case 3:
  115.                         System.out.println("请选择要查询的账户:");
  116.                         for(int i=0;i<m;i++)
  117.                         {
  118.                                 p[i].printX(i);
  119.                         }
  120.                         int a2 = sc.nextInt();
  121.                         sc.nextLine();
  122.                         if(a2<1||a2>m)
  123.                         {
  124.                                 throw new OutException2();
  125.                         }
  126.                         p[a2-1].printY();
  127.                         break;
  128.                 case 0:
  129.                         System.out.println("程序已退出!");
  130.                         System.exit(0);
  131.                 }
  132.                 //break;
  133.                 }
  134.        
  135.         catch (InputMismatchException e1) {

  136.                 System.out.println("输入类型错误");
  137.                 sc.next();
  138.         }
  139.         catch(OutException e1)
  140.         {
  141.                         System.out.println(e1.getMessage());
  142.         } catch (OutException2 e) {
  143.                 // TODO Auto-generated catch block
  144.                 System.out.println(e.getMessage());
  145.         }
  146.         }
  147.         }

  148. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-7-3 11:06:35 | 显示全部楼层
good
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-2-2 14:00:00 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-15 00:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表