银行余额存取小例子
定义账户
package atguigu.com;
public class Account {
private int id;
private double balance;
private double annualInterestRate;
public Account(int id,double balance,double annualInterestRate){
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
//在提款方法withDraw中,需要判断用户余额是否能够满足提款数额的要求,如果不能,应给出提示。
public void withDraw(double amount){//取钱
if(balance < amount){
System.out.println("余额不足,取钱失败");
return;
}
balance -= amount;
System.out.println("成功取出: " + amount);
}
public void deposit(double amount){//存钱
if(amount > 0){
System.out.println("成功存入: " + amount);
}
}
}
定义用户
package atguigu.com;
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 Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
测试程序
package atguigu.com;
/*
* 写一个测试程序
* (1)创建一个Customer,名字叫Jane Smith,他有一个账号为1000,余额为2000元,年利率为1.23
* (2)对Jane Smith操作
* 存入100元,再取出960元,再取出2000元
* 打印出Jane Smith的基本信息
*/
public class CustomerTest {
public static void main(String[] args){
Customer cust = new Customer("Jane", "Smith");
Account acct = new Account(1000,2000,0.0123);
cust.setAccount(acct);
cust.getAccount().deposit(100);
cust.getAccount().withDraw(960);
cust.getAccount().withDraw(2000);
System.out.println("Customer[" + cust.getLastName() + "," +
cust.getFirstName() + "]has a account: id is " + acct.getId() + "\r" + ",annualInterestRate is " + acct.getAnnualInterestRate()*100 + "%,balance is " + acct.getBalance());
}
}4.10关键字package,import的使用
package atguigu2.com;
import java.util.*;
import atguigu.com.Account;
/*
* 一、package关键字的使用
* 1.为了更好的实现项目中类的管理、提供包的概念
* 2.使用package声明类或接口所属的包,声明在源文件的首行
* 3.包,属于标识符,遵循标识符的命名规则、规范(xxxxyyyzzz)、"见名知意"
* 4.每"."一次,就代表一层文件目录。
*
*
* 补充:同一个包下,不能命名同名的接口、类
* 不同包下,可以命名同名的接口、类
*
* 二、import关键字的使用
* import:导入
* 1.为了源文件中显式的使用import结构导入指定包下的类、接口
* 2.声明在包的声明和类的声明之间
* 3.如果需要导入多个结构,则并列写出即可
* 4.可以使用"XXX.*"的方式,表示可以导入XXX包下的所有结构。
* 5.如果使用的类或接口是java.lang包下定义的,则可以省略import结构
* 6.如果使用的类或接口是本包下定义的,则可以省略import结构
* 7.如果在源文件中使用了不同包下的同名类,则必须至少有一个类需要以全类名的方式显式。
* 8.如果使用"XXX.*"方式表明可以调用XXX包下的所有结构。但是如果使用的是XXXX子包下的结
* 构,则仍需要显示导入
* 9.import static:导入指定类或接口中的静态结构:属性或方法。
*/
public class PackageImportTest {
String info = Arrays.toString(new int[]{1,2,3});
ArrayList list = new ArrayList();
HashMap map = new HashMap();
Account acct = new Account(1000);
}

京公网安备 11010502036488号