Java基础实战_Bank项目01
目录结构
不要加01,这里是为了区分
【Account.java】类
package banking;
public class Account {
private double balance; //银行帐户的当前(或即时)余额
//公有构造器 ,这个参数为 balance 属性赋值
public Account(double init_balance) {
this.balance = init_balance;
}
//用于获取经常余额
public double getBalance() {
return balance;
}
//向当前余额增加金额
public void deposit(double amt){
balance+=amt;
}
//从当前余额中减去金额
public void withdraw(double amt){
balance-=amt;
}
}
【TestBanking】类
package banking;
import banking.*;
public class TestBanking {
public static void main(String[] args) {
Account account;
// Create an account that can has a 500.00 balance.
System.out.println("Creating an account with a 500.00 balance.");
//code
account = new Account(500.00);
System.out.println("Withdraw 150.00");
//code
account.withdraw(150.00);
System.out.println("Deposit 22.50");
//code
account.deposit(22.50);
System.out.println("Withdraw 47.62");
//code
account.withdraw(47.62);
// Print out the final account balance
System.out.println("The account has a balance of " + account.getBalance());
}
}
Java基础实战_Bank项目02
目录结构
自己做的话不需要加02,这里是为了区分
【Account.java】类
package banking;
public class Account {
private double balance; //银行帐户的当前(或即时)余额
//公有构造器 ,这个参数为 balance 属性赋值
public Account(double init_balance) {
this.balance = init_balance;
}
//用于获取经常余额
public double getBalance() {
return balance;
}
//向当前余额增加金额
public void deposit(double amt){
balance+=amt;
}
//从当前余额中减去金额
public void withdraw(double amt){
balance-=amt;
}
}
【Customer.java】类
package banking;
public class Customer {
private String firstName;
private String lastName;
private Account account;
public Customer(String f, String l) {
this.firstName = f;
this.lastName = l;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public Account getAccount() {
return account;
}
public void setAccount(Account acct) {
this.account = acct;
}
}
【TestBanking.java】类
package banking;/*
* This class creates the program to test the banking classes.
* It creates a new Bank, sets the Customer (with an initial balance),
* and performs a series of transactions with the Account object.
*/
import banking.*;
public class TestBanking {
public static void main(String[] args) {
Customer customer;
Account account;
// Create an account that can has a 500.00 balance.
System.out.println("Creating the customer Jane Smith.");
//code
customer = new Customer("Jane","Smith");
System.out.println("Creating her account with a 500.00 balance.");
//code
account = new Account(500.00);
customer.setAccount(account);
System.out.println("Withdraw 150.00");
//code
customer.getAccount().withdraw(150.00);
System.out.println("Deposit 22.50");
//code
customer.getAccount().deposit(22.50);
System.out.println("Withdraw 47.62");
//code
customer.getAccount().withdraw(47.62);
// Print out the final account balance
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName()
+ "] has a balance of " + account.getBalance());
}
}
Java基础实战_Bank项目03
目录结构
自己做的话不需要加03,这里是为了区分
【Account.java】类
package banking;
public class Account {
private double balance; //银行帐户的当前(或即时)余额
//公有构造器 ,这个参数为 balance 属性赋值
public Account(double init_balance) {
this.balance = init_balance;
}
//用于获取经常余额
public double getBalance() {
return balance;
}
/**
* 向当前余额增加金额
* @param amt 增加金额
* @return 返回 true(意味所有存款是成功的)
*/
public boolean deposit(double amt){
balance+=amt;
return true;
}
/**
* 从当前余额中减去金额
* @param amt 提款数目
* @return 如果 amt小于 balance, 则从余额中扣除提款数目并返回 true,否则余额不变返回 false。
*/
public boolean withdraw(double amt){
if (amt < balance){
balance-=amt;
return true;
}else{
return false;
}
}
}
【Customer.java】类
package banking;
public class Customer {
private String firstName;
private String lastName;
private Account account;
public Customer(String f, String l) {
this.firstName = f;
this.lastName = l;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public Account getAccount() {
return account;
}
public void setAccount(Account acct) {
this.account = acct;
}
}
【TestBanking.java】类
package banking;/*
* This class creates the program to test the banking classes.
* It creates a new Bank, sets the Customer (with an initial balance),
* and performs a series of transactions with the Account object.
*/
import banking.*;
public class TestBanking {
public static void main(String[] args) {
Customer customer;
Account account;
// Create an account that can has a 500.00 balance.
System.out.println("Creating the customer Jane Smith.");
//code
customer = new Customer("Jane","Smith");
System.out.println("Creating her account with a 500.00 balance.");
//code
account = new Account(500.00);
customer.setAccount(account);
// Perform some account transactions
System.out.println("Withdraw 150.00: " + account.withdraw(150.00));
System.out.println("Deposit 22.50: " + account.deposit(22.50));
System.out.println("Withdraw 47.62: " + account.withdraw(47.62));
System.out.println("Withdraw 400.00: " + account.withdraw(400.00));
// Print out the final account balance
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName()
+ "] has a balance of " + account.getBalance());
}
}
Java基础实战_Bank项目04
目录结构
【Account.java】类
package banking;
public class Account {
private double balance; //银行帐户的当前(或即时)余额
//公有构造器 ,这个参数为 balance 属性赋值
public Account(double init_balance) {
this.balance = init_balance;
}
//用于获取经常余额
public double getBalance() {
return balance;
}
/**
* 向当前余额增加金额
* @param amt 增加金额
* @return 返回 true(意味所有存款是成功的)
*/
public boolean deposit(double amt){
balance+=amt;
return true;
}
/**
* 从当前余额中减去金额
* @param amt 提款数目
* @return 如果 amt小于 balance, 则从余额中扣除提款数目并返回 true,否则余额不变返回 false。
*/
public boolean withdraw(double amt){
if (amt < balance){
balance-=amt;
return true;
}else{
return false;
}
}
}
【Customer.java】类
package banking;
public class Customer {
private String firstName;
private String lastName;
private Account account;
public Customer(String f, String l) {
this.firstName = f;
this.lastName = l;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public Account getAccount() {
return account;
}
public void setAccount(Account acct) {
this.account = acct;
}
}
【Bank.java】类
package banking;
/**
* 银行类
*/
public class Bank {
private Customer[] customers; //Customer对象的数组
private int numberOfCustomer; //整数,跟踪下一个 customers 数组索引
/**
* 公有构造器,以合适的最大尺寸(至少大于 5)初始化 customers 数组。
*/
public Bank() {
customers = new Customer[10];
}
/**
* 该方法必须依照参数(姓,名)构造一个新的 Customer 对象然后把它放到 customer 数组中。还必须把 numberOfCustomers 属性的值加 1。
* @param f 姓
* @param l 名
*/
public void addCustomer(String f,String l){
customers[numberOfCustomer++]=new Customer(f,l);
}
/**
* 通过下标索引获取 customer
* @param index 下标索引
* @return customer
*/
public Customer getCustomer(int index) {
return customers[index]