随笔 - 322  文章 - 0  评论 - 4  阅读 - 77146

Java 数组对象 小测试

复制代码
 1 package com.bytezero.bank;
 2 /**
 3  * 
 4  * @Description 
 5  * @author  Bytezero·zhenglei!      Email:420498246@qq.com
 6  * @version
 7  * @date 2021年9月15日下午11:53:31
 8  * @
 9  *
10  */
11 public class BankTest
12 {
13     public static void main(String[] args) 
14     {
15         Bank bank = new Bank();
16         
17         
18         bank.addCustomer("Byte", "zero");
19         
20         
21         //连续操作
22         bank.getCunstomer(0).setAccount(new Account(2000));
23         
24         bank.getCunstomer(0).getAccount().withdraw(500);
25         
26         double balance    = bank.getCunstomer(0).getAccount().getBalance();
27         
28         System.out.println("客户"+bank.getCunstomer(0).getFirstName()+"的账户余额为:"+balance);
29         
30         System.out.println("*******************************");
31         
32         bank.addCustomer("Byte2", "zero2");
33         
34         bank.getCunstomer(1).setAccount(new Account(2000));
35         
36         bank.getCunstomer(1).getAccount().withdraw(500);
37         
38         double balance1    = bank.getCunstomer(1).getAccount().getBalance();
39         
40         System.out.println("客户"+bank.getCunstomer(1).getFirstName()+"的账户余额为:"+balance);
41     
42     
43         System.out.println("银行客户的个数为:"+bank.getNumberOfCustomers());
44     
45     }
46 }
复制代码
复制代码
 1 package com.bytezero.bank;
 2 
 3 public class Bank {
 4 
 5     private Customer[] customers; //存放多个客户的数组
 6     
 7     //private Customer[] customers=10; //1:
 8     private int numberOfCustomers; //记录客户的个数
 9     
10     public Bank()
11     {
12         customers = new Customer[10];  //1:
13     }
14     
15     //添加客户
16     public void addCustomer(String f,String l)
17     {
18         Customer cust = new Customer(f,l);
19 //        customers[numberOfCustomers] = cust;
20 //        numberOfCustomers++;
21         customers[numberOfCustomers++] = cust;
22         
23         
24     }
25     
26     
27     //获取客户的个数
28     public int getNumberOfCustomers()
29     {
30         return numberOfCustomers;
31     }
32     
33     //取指定位置的客户
34     public Customer getCunstomer(int index)
35     {
36 //        return customers[index];   //可能报异常
37         if(index >= 0 && index < numberOfCustomers)
38         {
39             return customers[index];
40         }
41         
42         return null;
43     }
44     
45 }
复制代码
复制代码
 1 package com.bytezero.bank;
 2 
 3 public class Account
 4 {
 5     private double balance;
 6     
 7     
 8     public Account(double init_balance)
 9     {
10         this.balance = init_balance;
11     }
12     
13     public double getBalance()
14     {
15         return balance;
16     }
17     
18     //存钱操作
19     public void deposit(double amt)
20     {
21         if(amt > 0)
22         {
23             balance += amt;
24             System.out.println("存钱成功!");
25         }
26     }
27     
28     //取钱操作
29     public void withdraw(double amt)
30     {
31         if(balance >= amt)
32         {
33             balance -= amt;
34             System.out.println("取钱成功");
35             
36             
37         }
38         else
39         {
40             System.out.println("余额不足");
41         }
42     }
43     
44 }
复制代码
复制代码
 1 package com.bytezero.bank;
 2 
 3 public class Customer
 4 {
 5     private String firstName;
 6     private String lastName;
 7     private Account account;
 8 
 9     
10     public Customer(String f, String l)
11     {
12         
13         this.firstName = f;
14         this.lastName = l;
15     }
16 
17 
18     public Account getAccount() {
19         return account;
20     }
21 
22 
23     public void setAccount(Account account) {
24         this.account = account;
25     }
26 
27 
28     public String getFirstName() {
29         return firstName;
30     }
31 
32 
33     public String getLastName() {
34         return lastName;
35     }
36     
37     
38     
39     
40 }
复制代码

 

posted on   Bytezero!  阅读(47)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示