Java学习配套实验

1 篇文章 0 订阅

实验一:

写一个名为Account的类模拟账户。该类的属性和方法如下图所示。该类包括的属性: 账号 id,余额 balance ,年利率 annualInterestRate。包含的方法:访问器( getter和 setter方法 ),取款方法withdraw(),存款方法deposit()。

提示: 在提款方法 在提款方法withdraw中,需要判断用户余额是否能够满足提款数额的要求。如果不能,应给出提示。

 2. 创建 Customer 类 

3. 写一个测试程序
(1) 创建一个Customer ,名字叫Jane Smith, 他有一个账号为1000,余额为 2000元, 年利率为1.23% 的账户。
(2) 对 Jane Smith操作。

存入100元,再取出960元。再取出 2000元。
        打印出 Jane Smith 的基本信息。
        成功存入 :100.0
        成功取出:960.0
        余额不足,取款失败

        Customer [Smith, Jane] has a account: id is 1000, annualInterestRate is 1.231.23 %, balance is 1140.0
         

Account

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;
	};
	public void withdraw(double ammount) {
		if (this.balance >= ammount) {
			System.out.println("成功取出:"+ammount);
			this.balance = this.balance-ammount;
		}else {
			System.out.println("余额不足,取款失败");
		}
	}
	public void deposit(double money) {
		this.balance += money;
		System.out.println("成功存入:"+money);
	}
	
}

Customer

public class Customer {
	private String firstName;
	private String lastName;
	private Account account;
	public Customer(String firstName, String lastName) {
		super();
		this.firstName = firstName;
		this.lastName = lastName;
	}
	public String getFirstName() {
		return firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public Account getAccount() {
		return account;
	}
	public void setAccount(Account account) {
		this.account = account;
	}
}

Test

public class Customer {
	private String firstName;
	private String lastName;
	private Account account;
	public Customer(String firstName, String lastName) {
		super();
		this.firstName = firstName;
		this.lastName = lastName;
	}
	public String getFirstName() {
		return firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public Account getAccount() {
		return account;
	}
	public void setAccount(Account account) {
		this.account = account;
	}
}

实验二

按照如下的UML 类图,创建相应的类,提供必要的结构

在提款方法 withdraw() 中, 需要判断用户余额是否能够满足提款数额的要求,如果不能, 应给出提示 。deposit()方法表示存款。

public class Account {
	private double balance;
	public Account(double init_balance) {
		this.balance = init_balance;
	}
	public double getBalance() {
		return balance;
	}
	public void deposit(double amt) {
		this.balance += amt;
		System.out.println("存入:"+ amt);
	}
	public void withdraw(double amt) {
		if(this.balance >= amt) {
			this.balance -= amt;
			System.out.println("支出:"+amt);
			
		}else {
			System.out.println("余额不足");
		}
	}
}

按照如下的UML 类图,创建相应的类,提供必要结构

 

public class Customer {
	private String firstName;
	private String lastName;
	private Account account;
	public Customer(String firstName, String lastName) {
		super();
		this.firstName = firstName;
		this.lastName = lastName;
	}
	public String getFirstName() { 
		return firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public Account getAccount() {
		return account;
	}
	public void setAccount(Account account) {
		this.account = account;
	}
}

按照如下的UML 类图,创建相应的类,提供必要结构

addCustomer方法必须依照参数(姓,名)构造一个新的Customer对象,然后把它放到 customer 数组中。还必须把数组中numberOfCustomer 属性的值加1
getNumOfCustomers方法返回numberOfCustomer属性值。 
getCustomerget方法返回与给出的index参数相关的客户

public class Bank {
	private Customer[] customers;
	private int numberOfCustomer;
	public Bank() {
		customers = new Customer[10];
	}
	public void addCustomers(String firstName,String lastName) {
		Customer cust = new Customer(firstName, lastName);
		customers[numberOfCustomer++] = cust;
	}
	//获取客户个数
	public int getNumOfCustomers() {
		return numberOfCustomer;
	}
	public Customer getCustomer(int index) {
		if(index >=0 && index < numberOfCustomer) {
			return customers[index];
		}
		return null;
	}
	
}

4. 创建 BankTest类进行测试 

public class Test {
	public static void main(String[] args) {
		Bank bank = new Bank();
		bank.addCustomers("Join", "Smith");
		bank.getCustomer(0).setAccount(new Account(2000));
		bank.getCustomer(0).getAccount().deposit(100);
		bank.getCustomer(0).getAccount().withdraw(690);
		bank.addCustomers("Join", "Smith");
		System.out.println(bank.getNumOfCustomers());		
	}
}

实验三 类的继承,super

1、写一个名为 Account的类模拟账户。该类的属性和方法如下图所示。该类包括的属性:账号id,余额balance,年利率annualInterestRate;包含的方法:访问器方法( getter和setter方法),返回月利率的方法getMonthlyInterest(),取款方法 withdraw(),存款方法deposit()。

 写一个用户程序测试Account 类。在用户程序中,创建一个账号为1122、余额为20000、年利率4.5% 的Account 对象。使用withdraw方法提款30000元,并打印余额
再使用withdraw 方法提款2500 元,使用 deposit 方法存款 3000 元,然后打印余额和月利率。

public class Account {
	private int id;
	private double balance;
	private double annuallnterestRate;
	public Account(int id, double balance, double annuallnterestRate) {
		super();
		this.id = id;
		this.balance = balance;
		this.annuallnterestRate = annuallnterestRate;
	}
	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 getAnnuallnterestRate() {
		return annuallnterestRate;
	}
	public void setAnnuallnterestRate(double annuallnterestRate) {
		this.annuallnterestRate = annuallnterestRate;
	}
	public double getMonthlyInterest() {
		return annuallnterestRate/12;
	}
	public void withdraw(double amount) {
		if (balance >= amount) {
			balance = balance - amount;
			return;
		}
		System.out.println("余额不足!");		
	}
	public void deposit(double amount) {
		balance += amount;
	}
	
	public static void main(String[] args) {
		Account acc = new Account(1122, 20000, 0.045);
		acc.withdraw(30000);
		System.out.println("您的余额账户为:"+acc.getBalance());
		acc.withdraw(2500);
		System.out.println("您的余额账户为:"+acc.getBalance());
		acc.deposit(3000);
		System.out.println("您的余额账户为:"+acc.getBalance());
		System.out.println("月利率为:"+(acc.getMonthlyInterest())*100+"%");
	}
}

2、创建Account类的一个子类CheckAccount代表可透支的账户,该账户中定义一个属性overdraft代表可透支限额。在CheckAccount类中重写withdraw方法,其算法如下:

如果(取款金额<账户余额),
    可直接取款
如果(取款金额> 账户余额),
    计算需要透支的额度 
    判断可透支额overdraft是否足够支付本次透支需要,如果可以 
    将账户余额修改为 0,冲减可透支金额    
    如果不可以 
    提示用户超过可透支额的限额

 写一个用户程序测试CheckAccount 类。在用户程序中,创建一个账号为1122、余额为20000、年利率4.5% 、可透支限额为5000的Account 对象。使用withdraw方法提款5000元,并打印账户余额和可透支额。再使用withdraw方法提款18000元,并打印账户余额和可透支额。再使用withdraw方法提款3000元,并打印账户余额和可透支额。
 

package com.atguigu.exercise;

public class CheckAccount extends Account{
	private double overdraft;
	public CheckAccount(int id, double balance, double annuallnterestRate,double overdraft) {
		super(id,balance,annuallnterestRate);
 		this.overdraft = overdraft;
	}
	@Override
	public void withdraw(double amount) {	
		if(this.getBalance() >= amount) {
		//方法一
		//	this.setBalance(this.getBalance() - amount);
		//方法二
			super.withdraw(amount);
		//方式三:修改balance的权限
		}else if(overdraft > (amount - this.getBalance())){
			overdraft -= (amount - this.getBalance());
			this.setBalance(0);
		}else {
			System.out.println("超过可透支额的限额");
		}
	}
	public double getOverdraft() {
		return overdraft;
	}
	public void setOverdraft(double overdraft) {
		this.overdraft = overdraft;
	}
	public static void main(String[] args) {
		CheckAccount cacc = new CheckAccount(1122, 20000, 0.045, 5000);
		cacc.withdraw(5000);
		System.out.println("账户余额为:"+cacc.getBalance());
		System.out.println("可透支额为:"+cacc.getOverdraft());
		cacc.withdraw(18000);
		System.out.println("账户余额为:"+cacc.getBalance());
		System.out.println("可透支额为:"+cacc.getOverdraft());
		cacc.withdraw(3000);
	}
}

实验三

编写工资系统,实现不同类型员工 (多态 )的按月发放工资 。如果当月出现某个Employee对象的生日 ,则将该雇员的工资增加 100 元。
实验说明

Employee

package com.sxt.p3;

public abstract class Employee {
	private String name;
	private int number;
	private MyDate birthday;
	public Employee() {
		super();
	}
	public Employee(String name, int number, MyDate birthday) {
		super();
		this.name = name;
		this.number = number;
		this.birthday = birthday;
	}
	@Override
	public String toString() {
		return "name=" + name + ", number=" + number + ", birthday=" + birthday.toDateString() ;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	public MyDate getBirthday() {
		return birthday;
	}
	public void setBirthday(MyDate birthday) {
		this.birthday = birthday;
	}
	
	public abstract int earnings();
	
}

MyDate

package com.sxt.p3;

public class MyDate {
	private int year;
	private int month;
	private int day;
	
	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getDay() {
		return day;
	}

	public void setDay(int day) {
		this.day = day;
	}

	public MyDate(int year, int month, int day) {
		super();
		this.year = year;
		this.month = month;
		this.day = day;
	}

	public String toDateString() {
		return year+"年"+month+"月"+day+"日";
	}
	
}

SalariedEmployee

package com.sxt.p3;

public class SalariedEmployee extends Employee {
	private int monthSalary;
	
	public SalariedEmployee() {
		super();
	}

	

	public SalariedEmployee(String name, int number, MyDate birthday) {
		super(name, number, birthday);
	}
	

	public SalariedEmployee(String name, int number, MyDate birthday, int monthSalary) {
		super(name, number, birthday);
		this.monthSalary = monthSalary;
	}



	public int getMonthSalary() {
		return monthSalary;
	}


	public void setMonthSalary(int monthSalary) {
		this.monthSalary = monthSalary;
	}


	@Override
	public int earnings() {
		return monthSalary;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "SalariedEmployee["+super.toString()+"]";
	}

	


}

HourlyEmployee

package com.sxt.p3;

public class HourlyEmployee extends Employee{
	private int wage;
	private int hour;
	
	public HourlyEmployee() {
		super();
	}

	public HourlyEmployee(String name, int number, MyDate birthday, int wage, int hour) {
		super(name, number, birthday);
		this.wage = wage;
		this.hour = hour;
	}

	@Override
	public int earnings() {
		// TODO Auto-generated method stub
		return wage*hour;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "HourlyEmployee["+super.toString()+"]";
	}
	
	
}

 PayrollSystem

package com.sxt.p3;
import java.util.*;
public class PayrollSystem {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int month = scan.nextInt();
		Employee[] emps = new Employee[4];//此处只是开辟了空间,没有生成对象
		
		emps[0] = new SalariedEmployee("mas", 1002, new MyDate(1992, 1, 23),1200);
		emps[1] = new HourlyEmployee("ma", 1003, new MyDate(1993, 4, 3),5,2000);
		emps[2] = new SalariedEmployee("maf", 1004, new MyDate(1994, 2, 13),5000);
		emps[3] = new HourlyEmployee("gd", 1005, new MyDate(1995, 2, 30),4,500);
		for (int i = 0;i<emps.length;i++) {
			System.out.println(emps[i]);
			System.out.println("月工资为"+emps[i].earnings());
			
			if (month == emps[i].getBirthday().getMonth()) {
				System.out.println("生日快乐");
			}
		}
	}
		
}

 

 

 

 

 

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值