装修需要找土工,电工,木工,油漆工,四类工人,
土工负责砌墙,贴瓷砖,
电工负责埋电线和水管,
木工负责做家具,
油漆工负责粉刷,
房东需要寻找这四种工人完成各自的任务。
具体要求:四类工人继承工人类,工人和房东都继承people类,
people 类具有:身份证号ID,姓名name,,电话Tel,性别
工人worker有:price(时薪),certificate(职业证书),company(所属公司)
四种工人都能实现室内装修(Decorator)的接口,该接口有一方法:doWork()
四种工人实现该接口,并实现doWork方法.
房东(houseOwner)有自己的属性:要装修的房子addr(字符串), 装修的钱decorateFund(实数),
房东有自己的方法:装修decorate(),该方法带参数,可以是电工,土工,油漆工,木工,执行内容为调用这些工人的doWork方法。能根据不同的工种打印出
我是土工,负责砌墙,贴瓷砖
我是电工,负责埋电线和水管
我是木工,负责做家具
我是油漆工,负责粉刷
Java代码:
接口(Decorator):
package fourth_first;
public interface Decorator {
void doWork();
}
People类:
package fourth_first;
public class People {
char ID;
char name;
char Tel;
int sex;
}
Worker类:
package fourth_first;
public class Worker extends People{
double price;
char certificate;
char company;
}
HouseOwner类:
package fourth_first;
public class HouseOwner extends People{
char addr;
int decorateFund;
void decorate(Decorator d) {
d.doWork();
}
}
SoilWorker类:
package fourth_first;
public class SoilWorker extends Worker implements Decorator{
public void doWork() {
System.out.println("我是土工,负责砌墙,贴瓷砖");
}
}
ElectricWorker类:
package fourth_first;
public class ElectricWorker extends Worker implements Decorator{
public void doWork() {
System.out.println("我是电工,负责埋电线和水管");
}
}
BrushWorker类:
package fourth_first;
public class BrushWorker extends Worker implements Decorator{
public void doWork() {
System.out.println("我是油漆工,负责粉刷");
}
}
WoodWorker类:
package fourth_first;
public class WoodWorker extends Worker implements Decorator{
public void doWork() {
System.out.println("我是木工,负责做家具");
}
}
演示类:
package fourth_first;
public class Demo {
public static void main(String[] args) {
HouseOwner owner = new HouseOwner();
owner.decorate(new SoilWorker());
owner.decorate(new ElectricWorker());
owner.decorate(new WoodWorker());
owner.decorate(new BrushWorker());
}
}
思考设计以下相关的类
秘书和老板都能预订酒店,
老板告诉秘书
房间价位在500到1500之间都可以预定,
在500以下不预定,
在1500元以上需经过老板同意。老板再根据具体情况(情况是否紧急等、自己发挥)决定是否预定酒店。如秘书和老板的关系(可以直接约老板也可以通过秘书预约,通过秘书就不用烦老板了,秘书会回绝一些不满足条件的客人等)).
Java代码:
接口(Hotel):
package fourth_second_first;
public interface Hotel {
void book(int price);
}
秘书类:
package fourth_second_first;
public class Secretary implements Hotel{
public void book(int price) {
if(price < 500)
{
System.out.println("我是秘书,酒店不预订");
} else if(price >= 500 && price <=1500){
System.out.println("我是秘书,酒店预订");
} else {
Boss.judge(price);
}
}
}
老板类:
package fourth_second_first;
public class Boss implements Hotel{
static boolean status = true;
public void book(int price) {
if(price < 500) {
System.out.println("我是老板,酒店不预订");
} else if(price >=500 && price <=1500) {
System.out.println("我是老板,酒店预订");
}
else {
Boss.judge(price);
}
}
public static void judge(int price) {
if(status) {
System.out.println("我是老板,情况紧急,酒店预订");
} else {
System.out.println("我是老板,酒店不预订");
}
}
}
演示类:
package fourth_second_first;
public class Demo {
public static void main(String[] args) {
Secretary s = new Secretary();
s.book(490);
s.book(500);
s.book(1600);
System.out.println();
Boss b = new Boss();
b.book(480);
b.book(1400);
b.book(1800);
}
}
接口的代理模式(房产中介是房东的代理,它们都能实现出租和收租金,他们实现同一个接口。但有了中介,可以提前做些审核的工作,审核房东或求租者等一系列工作。以此类推,可以推广到所有相关的代理需求,
Java代码:
接口(Transaction):
package fourth_second_second;
public interface Transaction {
void lease(int price);
void collect(int price);
}
房东类:
package fourth_second_second;
public class HouseOwner implements Transaction{
static int rent;
static int minprice = 1000;
static int count = 0;
static boolean status = true;
public static void judge(int price) {
if(status) {
System.out.println("我是房东,买主提供租金:" + price + " 情况不紧急,不出租");
}
}
public void lease(int price) {
if(price < HouseOwner.minprice) {
HouseOwner.judge(price);
} else {
System.out.println("买主提供租金:" + price + "\n我是房东,房子出租");
collect(price);
}
}
public void collect(int rent) {
HouseOwner.rent += rent;
HouseOwner.count++;
System.out.println("租金累计:" + HouseOwner.rent + "\t累计收到:" + HouseOwner.count + "次");
}
}
中介类:
package fourth_second_second;
public class Intermediary implements Transaction{
public void lease(int price) {
if(price < HouseOwner.minprice) {
HouseOwner.judge(price);
} else {
System.out.println("买主提供租金:" + price + "\n我是中介,房子出租");
collect(price);
}
}
public void collect(int rent) {
HouseOwner.rent += rent;
HouseOwner.count++;
System.out.println("租金累计:" + HouseOwner.rent + "\t累计收到:" + HouseOwner.count + "次");
}
}
演示类:
package fourth_second_second;
public class Demo {
public static void main(String[] args) {
Intermediary a = new Intermediary();
a.lease(1000);
HouseOwner b = new HouseOwner();
b.lease(900);
b.lease(1500);
}
}
ATM机和柜台工作人员都能实现取钱和存钱的功能,设计储户存取钱的操作,有两个参数:账号(存折或银行卡)和存取钱的设备(ATM机或柜台),思考,该如何设计相应的类和接口(抽象类)来实现前述功能,要注意存取钱具体实现的合理性?
Java代码:
Manage接口
package fourth_third;
public interface Manage {
void save(TradeWay t, Manage s);
void take(TradeWay t, Manage s);
}
TradeWay接口
package fourth_third;
public interface TradeWay {//区分储户的交易方式(存折/银行卡号)
}
Card类
package fourth_third;
public class Card implements TradeWay{
People people;
String account;
static int money;
Card(){
}
Card(People people, int money, String account){
this.people = people;
this.money = money;
this.account = account;
System.out.println("账户的账号为:"+this.account);
}
}
BankBook类
package fourth_third;
public class BankBook implements TradeWay{
People people;
String account;
static int money;
BankBook(){
}
BankBook(People people, int money, String account){
this.people = people;
this.money = money;
this.account = account;
System.out.println("账户的账号为:"+this.account);
}
}
ATM类
package fourth_third;
import java.util.*;
public class ATM implements Manage {
Scanner r1 = new Scanner(System.in);
public void save(TradeWay t, Manage s) {
System.out.println("ATM,正在存钱,请输入存钱金额:");
int money = r1.nextInt();
if (money > 0) {
Card.money += money;
System.out.println("存钱成功,账户余额为:" + Card.money);
} else {
System.out.println("存钱失败,存钱金额错误");
System.out.println("账户余额为:" + Card.money);
}
}
Scanner r2 = new Scanner(System.in);
public void take(TradeWay t, Manage s) {
System.out.println("ATM,正在取钱,请输入取钱金额:");
int money2 = r2.nextInt();
if (Card.money >= money2) {
Card.money -= money2;
System.out.println("取钱成功,账户余额为:" + Card.money);
} else {
System.out.println("取钱失败,账户金额不足");
System.out.println("账户余额为:" + Card.money);
}
}
}
Worker类
package fourth_third;
import java.util.*;
public class Worker implements Manage {
Scanner r1 = new Scanner(System.in);
public void save(TradeWay t, Manage s) {
System.out.println("柜台人员,正在存钱,请输入存钱金额:");
int money = r1.nextInt();
if (money > 0) {
Card.money += money;
System.out.println("存钱成功,账户余额为:" + Card.money);
} else {
System.out.println("存钱失败,存钱金额错误");
System.out.println("账户余额为:" + Card.money);
}
}
Scanner r2 = new Scanner(System.in);
public void take(TradeWay t, Manage s) {
System.out.println("柜台人员,正在取钱,请输入取钱金额:");
int money = r2.nextInt();
if (Card.money >= money) {
Card.money -= money;
System.out.println("取钱成功,账户余额为:" + Card.money);
} else {
System.out.println("取钱失败,账户金额不足");
System.out.println("账户余额为:" + Card.money);
}
}
}
People类
package fourth_third;
public class People {
Card card;
BankBook bankbook;
String name;
People(){
}
People(String name){
this.name = name;
}
public boolean CreateCard() {
this.card = new Card(this, 100, "3171911118");
return true;
}
public boolean CreateBankBook() {
this.bankbook = new BankBook(this, 100, "3171911118");
return true;
}
public void save(TradeWay t, Manage s) {
s.save(t, s);
}
public void take(TradeWay t, Manage s) {
s.take(t, s);
}
}
Demo类(演示,模拟存钱过程)
package fourth_third;
public class Demo {
public static void main(String[] args) {
People people = new People("Tom");
System.out.println("账户的名字为:"+people.name);
people.CreateCard();
people.save(people.card, new ATM());
System.out.println();
people.take(people.card, new ATM());
System.out.println();
System.out.println();
people.save(people.card, new Worker());
System.out.println();
people.take(people.bankbook, new Worker());
People people2 = new People("Jack");
System.out.println("账户的名字为:"+people2.name);
people2.CreateCard();
people2.save(people2.card, new ATM());
System.out.println();
people2.take(people2.card, new ATM());
}
}