已知有如下Buy_car接口,请编写其客户端子类People类为委托类,以及代理类ProxySale类,具体要求如下。 已有的接口定义如下: interface Buy_car{ public void buy_mycar(); } 需要编写Buy_car接口的子类People: 子类People实现接口Buy_car: (1)添加3个私有属性:现金 cash(int), 是否贵宾 vip(boolean),用户名username(String); (2)添加三参构造,初始化三个属性。 (3)覆写buy_mycar()方法,输出信息“。。。要买车”。例如:Tom要买车 (4)添加三个属性的getter方法。 需要编写代理类ProxySale: 设计一个代理类ProxySale,实现接口Buy_car: (1)添加1个私有属性People对象。 (2)添加一参构造,初始化People对象。 (3)覆写buy_mycar()方法,方法里面要判断用户是否为VIP,是就可以直接买车;如果不是VIP则判断现金是否够50000元以上(包含50000),够就可以直接买车;如果不够就不能买车。 已有的Main类定义: /* 请在这里填写答案 */ public class Main { public static void main(String[] args) { Buy_car buy1=new ProxySale(new People(0,true,"Tom")); Buy_car buy2=new ProxySale(new People(60000,false,"Jack")); Buy_car buy3=new ProxySale(new People(40000,false,"Rose")); buy1.buy_mycar(); buy2.buy_mycar(); buy3.buy_mycar(); } } 输入样例: 输出样例: Tom是vip,可以直接购买新车! Jack买了新车,交易结束! Rose钱不够,不能购买新车!
时间: 2023-06-18 19:07:46 浏览: 159
代码如下:
```
interface Buy_car{
public void buy_mycar();
}
class People implements Buy_car {
private int cash;
private boolean vip;
private String username;
public People(int cash, boolean vip, String username) {
this.cash = cash;
this.vip = vip;
this.username = username;
}
@Override
public void buy_mycar() {
System.out.println(username + "要买车");
}
public int getCash() {
return cash;
}
public boolean isVip() {
return vip;
}
public String getUsername() {
return username;
}
}
class ProxySale implements Buy_car {
private People people;
public ProxySale(People people) {
this.people = people;
}
@Override
public void buy_mycar() {
if (people.isVip()) {
System.out.println(people.getUsername() + "是vip,可以直接购买新车!");
} else if (people.getCash() >= 50000) {
System.out.println(people.getUsername() + "买了新车,交易结束!");
} else {
System.out.println(people.getUsername() + "钱不够,不能购买新车!");
}
}
}
public class Main {
public static void main(String[] args) {
Buy_car buy1=new ProxySale(new People(0,true,"Tom"));
Buy_car buy2=new ProxySale(new People(60000,false,"Jack"));
Buy_car buy3=new ProxySale(new People(40000,false,"Rose"));
buy1.buy_mycar();
buy2.buy_mycar();
buy3.buy_mycar();
}
}
```
阅读全文