编程语言
首页 > 编程语言> > Java 练习(构造器,this练习; 测试取款程序)

Java 练习(构造器,this练习; 测试取款程序)

作者:互联网

构造器,this练习


Girl.java

package exer1;

public class Girl {
	
	private String name;
	private int age;
	
	public Girl() {
		
	}
	
	public Girl(String name, int age) {
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	
	public void marry(Boy boy) {
		System.out.println("我想嫁给" + boy.getName());
		boy.marry(this);
	}
	
	/**
	 * 
	 * @param girl
	 * @return 正数,当前对象大;负数,当前对象小;0:当前对象与形参对象想等
	 */
	
	public int compare(Girl girl) {
		return this.age - girl.age;
	}

}

Boy.java

package exer1;

public class Boy {
	private String name;
	private int age;
	
	public Boy(String name, int age) {
		this.name = name;
		this.age = age;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	public void marry(Girl girl) {
		System.out.println("我想娶" + girl.getName());
	}
	
	public void shout() {
		if(this.age >= 22) {
			System.out.println("你可以合法登记结婚了!");
		}else {
			System.out.println("先多谈谈恋爱~~");
		}
	}
	

}

BoyGirlTest.java

package exer1;

public class BoyGirlTest {
	public static void main(String[] args) {
		Boy boy = new Boy("罗密欧", 21);
		
		boy.shout();
		
		Girl girl = new Girl("朱丽叶", 18);
		girl.marry(boy);
		
		Girl girl1 = new Girl("祝英台", 19);
		int compare = girl.compare(girl1);
		if(compare > 0) {
			System.out.println(girl.getName() + "大");
		}else if(compare < 0) {
			System.out.println(girl1.getName() + "大");
		}else {
			System.out.println("一样大");
		}
	}
}

运行结果:

测试取款程序

写一个测试程序。
(1)创建一个 Customer,名字叫 Jane Smith,他有一个账号为 1000,余额为2000元,年利率为1.23
(2)对Jane Smith操作。存入 100 元,再取出 960元。再取出 2000元。打印出Jane Smith 的基本信息

Account.java

package com.klvchen.exer3;

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) {
			balance += amount;
			System.out.println("成功存入: " + amount);
		}
	}
	
	
}

Customer.java

package com.klvchen.exer3;

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;
	}
}

CustomerTest.java

package com.klvchen.exer3;

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 " + cust.getAccount().getId() + ", annualInterestRate is " 
				+ cust.getAccount().getAnnualInterestRate() * 100 + "% , balance is " +  cust.getAccount().getBalance());
	}

}

运行结果:

标签:Java,String,age,练习,void,return,public,取款,name
来源: https://www.cnblogs.com/klvchen/p/14362452.html