mirror of
https://github.com/Dichgrem/Java.git
synced 2025-12-16 12:41:58 -05:00
update:demo4
This commit is contained in:
94
.gitignore
vendored
94
.gitignore
vendored
@@ -1 +1,95 @@
|
||||
### Compiled class files
|
||||
*.class
|
||||
|
||||
### Log files
|
||||
*.log
|
||||
|
||||
### BlueJ files
|
||||
*.ctxt
|
||||
|
||||
### Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
### Package Files
|
||||
*.jar
|
||||
*.war
|
||||
*.nar
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
### VM crash logs
|
||||
hs_err_pid*
|
||||
replay_pid*
|
||||
|
||||
# ========================
|
||||
# Maven
|
||||
# ========================
|
||||
target/
|
||||
pom.xml.tag
|
||||
pom.xml.releaseBackup
|
||||
pom.xml.versionsBackup
|
||||
pom.xml.next
|
||||
release.properties
|
||||
dependency-reduced-pom.xml
|
||||
buildNumber.properties
|
||||
.mvn/timing.properties
|
||||
.mvn/wrapper/maven-wrapper.jar
|
||||
|
||||
# ========================
|
||||
# Gradle
|
||||
# ========================
|
||||
.gradle/
|
||||
build/
|
||||
!gradle-wrapper.jar
|
||||
!*/src/main/**/build/
|
||||
!*/src/test/**/build/
|
||||
|
||||
### Avoid ignoring Gradle wrapper
|
||||
gradle-app.setting
|
||||
|
||||
# ========================
|
||||
# IntelliJ IDEA
|
||||
# ========================
|
||||
.idea/
|
||||
*.iml
|
||||
*.ipr
|
||||
*.iws
|
||||
out/
|
||||
|
||||
# ========================
|
||||
# Eclipse
|
||||
# ========================
|
||||
.project
|
||||
.classpath
|
||||
.cproject
|
||||
.settings/
|
||||
bin/
|
||||
tmp/
|
||||
|
||||
# ========================
|
||||
# NetBeans
|
||||
# ========================
|
||||
nbproject/private/
|
||||
nbbuild/
|
||||
dist/
|
||||
nbdist/
|
||||
.nb-gradle/
|
||||
|
||||
# ========================
|
||||
# VS Code
|
||||
# ========================
|
||||
.vscode/
|
||||
|
||||
# ========================
|
||||
# OS files
|
||||
# ========================
|
||||
# macOS
|
||||
.DS_Store
|
||||
|
||||
# Windows
|
||||
Thumbs.db
|
||||
|
||||
#local
|
||||
com/study/test/
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
9
com/study/demo4/AlipayPayment.java
Normal file
9
com/study/demo4/AlipayPayment.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package com.study.demo4;
|
||||
|
||||
class AlipayPayment implements PaymentProcessor {
|
||||
@Override
|
||||
public boolean pay(double amount) {
|
||||
System.out.println("支付宝支付: " + amount);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
11
com/study/demo4/BikeDeliveryCalculator.java
Normal file
11
com/study/demo4/BikeDeliveryCalculator.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package com.study.demo4;
|
||||
|
||||
class BikeDeliveryCalculator implements DeliveryFeeCalculator {
|
||||
@Override
|
||||
public double calculateFee(double distance) {
|
||||
if (distance <= 5) {
|
||||
return 5.0; // 起步价
|
||||
}
|
||||
return 5.0 + (distance - 5) * 1.5;
|
||||
}
|
||||
}
|
||||
12
com/study/demo4/Cardeliverycalculator.java
Normal file
12
com/study/demo4/Cardeliverycalculator.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package com.study.demo4;
|
||||
|
||||
class CarDeliveryCalculator implements DeliveryFeeCalculator {
|
||||
@Override
|
||||
public double calculateFee(double distance) {
|
||||
if (distance <= 5) {
|
||||
return 10.0; // 起步价
|
||||
}
|
||||
return 10.0 + (distance - 5) * 2.0;
|
||||
}
|
||||
}
|
||||
|
||||
9
com/study/demo4/CreditCardPayment.java
Normal file
9
com/study/demo4/CreditCardPayment.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package com.study.demo4;
|
||||
|
||||
class CreditCardPayment implements PaymentProcessor {
|
||||
@Override
|
||||
public boolean pay(double amount) {
|
||||
System.out.println("银行卡支付: " + amount);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
5
com/study/demo4/DeliveryFeeCalculator.java
Normal file
5
com/study/demo4/DeliveryFeeCalculator.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package com.study.demo4;
|
||||
|
||||
interface DeliveryFeeCalculator {
|
||||
double calculateFee(double distance);
|
||||
}
|
||||
26
com/study/demo4/HomeAppliance.java
Normal file
26
com/study/demo4/HomeAppliance.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package com.study.demo4;
|
||||
|
||||
abstract class HomeAppliance implements SmartDevice {
|
||||
protected String name;
|
||||
protected boolean isOn;
|
||||
|
||||
public HomeAppliance(String name) {
|
||||
this.name = name;
|
||||
this.isOn = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void turnOn() {
|
||||
isOn = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void turnOff() {
|
||||
isOn = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getStatus() {
|
||||
return name + " is " + (isOn ? "ON" : "OFF");
|
||||
}
|
||||
}
|
||||
21
com/study/demo4/HomeController.java
Normal file
21
com/study/demo4/HomeController.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package com.study.demo4;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class HomeController {
|
||||
private List<SmartDevice> devices = new ArrayList<>();
|
||||
|
||||
public void addDevice(SmartDevice device) {
|
||||
devices.add(device);
|
||||
}
|
||||
|
||||
public String getDeviceStatus(String name) {
|
||||
for (SmartDevice device : devices) {
|
||||
if (device.getStatus().contains(name)) {
|
||||
return device.getStatus();
|
||||
}
|
||||
}
|
||||
return "Device not found";
|
||||
}
|
||||
}
|
||||
16
com/study/demo4/PaymentFactory.java
Normal file
16
com/study/demo4/PaymentFactory.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package com.study.demo4;
|
||||
|
||||
class PaymentFactory {
|
||||
public static PaymentProcessor getProcessor(String type) {
|
||||
switch (type.toLowerCase()) {
|
||||
case "wechat":
|
||||
return new WechatPayment();
|
||||
case "alipay":
|
||||
return new AlipayPayment();
|
||||
case "card":
|
||||
return new CreditCardPayment();
|
||||
default:
|
||||
throw new IllegalArgumentException("不支持的支付方式");
|
||||
}
|
||||
}
|
||||
}
|
||||
6
com/study/demo4/PaymentProcessor.java
Normal file
6
com/study/demo4/PaymentProcessor.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package com.study.demo4;
|
||||
|
||||
interface PaymentProcessor {
|
||||
boolean pay(double amount);
|
||||
}
|
||||
|
||||
7
com/study/demo4/SmartAirConditioner.java
Normal file
7
com/study/demo4/SmartAirConditioner.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package com.study.demo4;
|
||||
|
||||
class SmartAirConditioner extends HomeAppliance {
|
||||
public SmartAirConditioner(String name) {
|
||||
super(name);
|
||||
}
|
||||
}
|
||||
7
com/study/demo4/SmartDevice.java
Normal file
7
com/study/demo4/SmartDevice.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package com.study.demo4;
|
||||
|
||||
interface SmartDevice {
|
||||
void turnOn();
|
||||
void turnOff();
|
||||
String getStatus();
|
||||
}
|
||||
7
com/study/demo4/SmartLight.java
Normal file
7
com/study/demo4/SmartLight.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package com.study.demo4;
|
||||
|
||||
class SmartLight extends HomeAppliance {
|
||||
public SmartLight(String name) {
|
||||
super(name);
|
||||
}
|
||||
}
|
||||
7
com/study/demo4/SmartMasher.java
Normal file
7
com/study/demo4/SmartMasher.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package com.study.demo4;
|
||||
|
||||
class SmartMasher extends HomeAppliance {
|
||||
public SmartMasher(String name) {
|
||||
super(name);
|
||||
}
|
||||
}
|
||||
31
com/study/demo4/TestControl.java
Normal file
31
com/study/demo4/TestControl.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package com.study.demo4;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TestControl {
|
||||
public static void main(String[] args) {
|
||||
HomeController controller = new HomeController();
|
||||
|
||||
// 创建设备
|
||||
SmartLight light = new SmartLight("Living Room Light");
|
||||
SmartAirConditioner ac = new SmartAirConditioner("Bedroom AC");
|
||||
SmartMasher masher = new SmartMasher("Kitchen Masher");
|
||||
|
||||
// 添加到控制器
|
||||
controller.addDevice(light);
|
||||
controller.addDevice(ac);
|
||||
controller.addDevice(masher);
|
||||
|
||||
// 测试设备状态
|
||||
light.turnOff();
|
||||
ac.turnOff();
|
||||
masher.turnOn();
|
||||
|
||||
// 查询状态
|
||||
System.out.println(controller.getDeviceStatus("Living Room Light"));
|
||||
System.out.println(controller.getDeviceStatus("Bedroom AC"));
|
||||
System.out.println(controller.getDeviceStatus("Kitchen Masher"));
|
||||
System.out.println(controller.getDeviceStatus("Non-existent Device"));
|
||||
}
|
||||
}
|
||||
8
com/study/demo4/TestPay.java
Normal file
8
com/study/demo4/TestPay.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package com.study.demo4;
|
||||
|
||||
public class TestPay {
|
||||
public static void main(String[] args) {
|
||||
PaymentProcessor processor = PaymentFactory.getProcessor("alipay");
|
||||
processor.pay(666.0);
|
||||
}
|
||||
}
|
||||
22
com/study/demo4/TestTransport.java
Normal file
22
com/study/demo4/TestTransport.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package com.study.demo4;
|
||||
|
||||
public class TestTransport {
|
||||
public static void main(String[] args) {
|
||||
// 测试自行车配送
|
||||
DeliveryFeeCalculator bike = new BikeDeliveryCalculator();
|
||||
System.out.println("自行车配送 3km: ¥" + bike.calculateFee(3));
|
||||
System.out.println("自行车配送 8km: ¥" + bike.calculateFee(8));
|
||||
|
||||
// 测试汽车配送
|
||||
DeliveryFeeCalculator car = new CarDeliveryCalculator();
|
||||
System.out.println("汽车配送 3km: ¥" + car.calculateFee(3));
|
||||
System.out.println("汽车配送 8km: ¥" + car.calculateFee(8));
|
||||
|
||||
// 对比不同距离的费用
|
||||
System.out.println("\n费用对比:");
|
||||
for (int km = 1; km <= 10; km++) {
|
||||
System.out.printf("%dkm: 自行车¥%.1f, 汽车¥%.1f%n",
|
||||
km, bike.calculateFee(km), car.calculateFee(km));
|
||||
}
|
||||
}
|
||||
}
|
||||
9
com/study/demo4/WechatPayment.java
Normal file
9
com/study/demo4/WechatPayment.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package com.study.demo4;
|
||||
|
||||
class WechatPayment implements PaymentProcessor {
|
||||
@Override
|
||||
public boolean pay(double amount) {
|
||||
System.out.println("微信支付: " + amount);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user