diff --git a/src/main/java/com/msb/visitor/Board.java b/src/main/java/com/msb/visitor/Board.java new file mode 100644 index 0000000..a3c2fe7 --- /dev/null +++ b/src/main/java/com/msb/visitor/Board.java @@ -0,0 +1,25 @@ +package com.msb.visitor;/** + * @Author bingor + * @Date 2022/10/17 19:42 + * @Description: com.msb.visitor + * @Version: 1.0 + */ + +/** + *@ClassName Board + *@Description TODO + *@Author bingor + *@Date 2022/10/17 19:42 + *@Version 3.0 + */ +public class Board extends ComputerPart { + @Override + public void accept(Visitor visitor) { + visitor.visitBoard(this); + } + + @Override + public double getPrice() { + return 800; + } +} diff --git a/src/main/java/com/msb/visitor/CompanyVisitor.java b/src/main/java/com/msb/visitor/CompanyVisitor.java new file mode 100644 index 0000000..9995f31 --- /dev/null +++ b/src/main/java/com/msb/visitor/CompanyVisitor.java @@ -0,0 +1,38 @@ +package com.msb.visitor;/** + * @Author bingor + * @Date 2022/10/17 19:44 + * @Description: com.msb.visitor + * @Version: 1.0 + */ + +/** + *@ClassName CompanyVisitor + *@Description TODO + *@Author bingor + *@Date 2022/10/17 19:44 + *@Version 3.0 + */ +public class CompanyVisitor implements Visitor { + + double totalPrice = 0.0; + + @Override + public void visitCpu(Cpu cpu) { + this.totalPrice += cpu.getPrice() * 0.7; + } + + @Override + public void visitMemory(Memory memory) { + this.totalPrice += memory.getPrice() * 0.8; + } + + @Override + public void visitBoard(Board board) { + this.totalPrice += board.getPrice() * 0.7; + } + + @Override + public double getTotalPrice() { + return totalPrice; + } +} diff --git a/src/main/java/com/msb/visitor/Computer.java b/src/main/java/com/msb/visitor/Computer.java new file mode 100644 index 0000000..4816b3b --- /dev/null +++ b/src/main/java/com/msb/visitor/Computer.java @@ -0,0 +1,46 @@ +package com.msb.visitor;/** + * @Author bingor + * @Date 2022/10/17 19:48 + * @Description: com.msb.visitor + * @Version: 1.0 + */ + +/** + *@ClassName Computer + *@Description TODO + *@Author bingor + *@Date 2022/10/17 19:48 + *@Version 3.0 + */ +public class Computer { + private Cpu cpu; + private Memory memory; + private Board board; + + public Computer(Cpu cpu, Memory memory, Board board) { + this.cpu = cpu; + this.memory = memory; + this.board = board; + } + + public void accept(Visitor visitor) { + this.cpu.accept(visitor); + this.board.accept(visitor); + this.memory.accept(visitor); + } + + public static void main(String[] args) { + + Computer computer = new Computer(new Cpu(), new Memory(), new Board()); + + Visitor personelVisitor = new PersonelVisitor(); + computer.accept(personelVisitor); + System.out.println(personelVisitor.getTotalPrice()); + + Visitor companyVisitor = new CompanyVisitor(); + computer.accept(companyVisitor); + System.out.println(companyVisitor.getTotalPrice()); + + } + +} diff --git a/src/main/java/com/msb/visitor/ComputerPart.java b/src/main/java/com/msb/visitor/ComputerPart.java new file mode 100644 index 0000000..d74db35 --- /dev/null +++ b/src/main/java/com/msb/visitor/ComputerPart.java @@ -0,0 +1,18 @@ +package com.msb.visitor;/** + * @Author bingor + * @Date 2022/10/17 19:38 + * @Description: com.msb.visitor + * @Version: 1.0 + */ + +/** + *@ClassName ComputerPart + *@Description TODO + *@Author bingor + *@Date 2022/10/17 19:38 + *@Version 3.0 + */ +public abstract class ComputerPart { + public abstract void accept(Visitor visitor); + public abstract double getPrice(); +} diff --git a/src/main/java/com/msb/visitor/Cpu.java b/src/main/java/com/msb/visitor/Cpu.java new file mode 100644 index 0000000..9f490cf --- /dev/null +++ b/src/main/java/com/msb/visitor/Cpu.java @@ -0,0 +1,26 @@ +package com.msb.visitor;/** + * @Author bingor + * @Date 2022/10/17 19:40 + * @Description: com.msb.visitor + * @Version: 1.0 + */ + +/** + *@ClassName Cpu + *@Description TODO + *@Author bingor + *@Date 2022/10/17 19:40 + *@Version 3.0 + */ +public class Cpu extends ComputerPart { + + @Override + public void accept(Visitor visitor) { + visitor.visitCpu(this); + } + + @Override + public double getPrice() { + return 1000; + } +} diff --git a/src/main/java/com/msb/visitor/Memory.java b/src/main/java/com/msb/visitor/Memory.java new file mode 100644 index 0000000..c6e1a41 --- /dev/null +++ b/src/main/java/com/msb/visitor/Memory.java @@ -0,0 +1,25 @@ +package com.msb.visitor;/** + * @Author bingor + * @Date 2022/10/17 19:42 + * @Description: com.msb.visitor + * @Version: 1.0 + */ + +/** + *@ClassName Memory + *@Description TODO + *@Author bingor + *@Date 2022/10/17 19:42 + *@Version 3.0 + */ +public class Memory extends ComputerPart { + @Override + public void accept(Visitor visitor) { + visitor.visitMemory(this); + } + + @Override + public double getPrice() { + return 900; + } +} diff --git a/src/main/java/com/msb/visitor/PersonelVisitor.java b/src/main/java/com/msb/visitor/PersonelVisitor.java new file mode 100644 index 0000000..fe50a5f --- /dev/null +++ b/src/main/java/com/msb/visitor/PersonelVisitor.java @@ -0,0 +1,40 @@ +package com.msb.visitor;/** + * @Author bingor + * @Date 2022/10/17 19:43 + * @Description: com.msb.visitor + * @Version: 1.0 + */ + +import org.omg.IOP.TAG_ORB_TYPE; + +/** + *@ClassName PersonelVisitor + *@Description TODO + *@Author bingor + *@Date 2022/10/17 19:43 + *@Version 3.0 + */ +public class PersonelVisitor implements Visitor { + + double totalPrice = 0.0; + + @Override + public void visitCpu(Cpu cpu) { + this.totalPrice += cpu.getPrice() * 0.8; + } + + @Override + public void visitMemory(Memory memory) { + this.totalPrice += memory.getPrice() * 0.9; + } + + @Override + public void visitBoard(Board board) { + this.totalPrice += board.getPrice() * 0.8; + } + + @Override + public double getTotalPrice() { + return totalPrice; + } +} diff --git a/src/main/java/com/msb/visitor/Visitor.java b/src/main/java/com/msb/visitor/Visitor.java new file mode 100644 index 0000000..52c4a2c --- /dev/null +++ b/src/main/java/com/msb/visitor/Visitor.java @@ -0,0 +1,14 @@ +package com.msb.visitor; + +/** + * @Author bingor + * @Date 2022/10/17 19:39 + * @Description: com.msb.visitor + * @Version: 1.0 + */ +public interface Visitor { + public void visitCpu(Cpu cpu); + public void visitMemory(Memory memory); + public void visitBoard(Board board); + public double getTotalPrice(); +}