java DevF Batch 5封装练习示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java DevF Batch 5封装练习示例相关的知识,希望对你有一定的参考价值。

// File: devf/Sensei.java

package devf;

import java.util.HashMap;

public class Sensei{
	private String type, firstName, lastName;
	private static HashMap<String, Double> types;
	private static double taxPercentage = 0.16;
	private double hours;

	static{
		types = new HashMap<String, Double>();
		types.put("blanca", new Double(50));
		types.put("roja", new Double(10000));
		types.put("negra", new Double(0));
	}

	public Sensei(String firstName, String lastName, double hours){
		this.firstName = firstName;
		this.lastName = lastName;
		this.hours = hours;
	}

	public void setHours(double hours){
		this.hours = hours;
	}

	public String getFullName(){
		return String.format("%s %s", this.firstName, this.lastName);
	}

	public double getSalary(){
		Double pricePerHour = (Double) types.get(this.type);

		return pricePerHour * this.hours;
	}

	public boolean setType(String type){
		if( !this.types.containsKey(type) ){
			return false;
		}
		
		this.type = type;
		return true;
	}

	public double getTaxes(){
		return this.getSalary() * this.taxPercentage;
	}

	public double getNetSalary(){
		return this.getSalary() - this.getTaxes();
	}

}

// File: Main.java

import devf.Sensei;

public class Main{
	public static void main(String[] args) {
		Sensei sergio = new Sensei("Sergio", "Malvaez", 10);
		Sensei rafa = new Sensei("Rafa", "Miranda", 40);
		if(sergio.setType("striper")){
			System.out.printf("Sensei: %s \n\tSalario bruto: %.2f \n\t -Impuestos: %.2f \n\t Salario neto: %.2f",
								sergio.getFullName(),
								sergio.getSalary(),
								sergio.getTaxes(),
								sergio.getNetSalary());
		}else{
			System.err.println("Invalid Sensei Type");
		}

		if(rafa.setType("roja")){
			System.out.printf("Sensei: %s \n\tSalario bruto: %.2f \n\t -Impuestos: %.2f \n\t Salario neto: %.2f",
								rafa.getFullName(),
								rafa.getSalary(),
								rafa.getTaxes(),
								rafa.getNetSalary());
		}else{
			System.err.println("Invalid Sensei Type");
		}

	}
}

以上是关于java DevF Batch 5封装练习示例的主要内容,如果未能解决你的问题,请参考以下文章

mail,at,batch,sleep小练习

运行基本 Spring Batch 示例时出错 - 原因:java.lang.ClassNotFoundException: org.hsqldb.jdbcDriver

java 封装练习题2

Java数据库练习02--管家婆项目

java从入门到精通OOP

java从入门到精通OOP