请采用采用简单工厂设计模式,为某个汽车销售店设计汽车销售系统,接口car至少有方法print(),三个汽车类:宝马、奥迪、大众 (属性:品牌,价格),在测试类中根据客户要求购买的汽车品牌,通过接口car为客户提供相应的汽车对象。

 

package pro4;

public class Car01 {
	public static void main(String[] args) { 
		Car c1=null;
		c1=Factory.getInstance("Audi");
		if(c1!=null){
			c1.print();
		}
	}
}
interface Car{
	public void print();
}
class Factory{
	public static Car getInstance(String name){
		Car c1=null;
		if("BMW".equals(name)){
			c1=new BMW("BMW 740Li",1200000);
		}
		if("Audi".equals(name)){
			c1=new Audi("Audi A8",1000000);
		}
		if("VW".equals(name)){
			c1=new VW("Volkswagen Pharton",1888888);
		}
		return c1;
	}
}
class BMW implements Car{
	private String name;
	private double money;
	public BMW(String name,double money){
		this.setName(name);
		this.setMoney(money);
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
	}
	public void print() {
		System.out.println("BMW--"+name+"的价格为"+money+"¥");
	}	
}
class Audi implements Car{
	private String name;
	private double money;
	public Audi(String name,double money){
		this.setName(name);
		this.setMoney(money);
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
	}
	public void print() {
		System.out.println("Audi--"+name+"的价格为"+money+"¥");
	}	
}
class VW implements Car{
	private String name;
	public VW(String name,double money){
		this.setName(name);
		this.setMoney(money);
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
	}
	private double money;
	public void print() {
		System.out.println("VW--"+name+"的价格为"+money+"¥");
	}	
}

  

posted on 2017-10-11 11:06  煮咖啡的猪!  阅读(230)  评论(0编辑  收藏  举报