mirror of
https://github.com/Dichgrem/Java.git
synced 2026-02-05 01:31:57 -05:00
update:demo13
This commit is contained in:
57
com/study/demo13/AttendanceService.java
Normal file
57
com/study/demo13/AttendanceService.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package com.study.demo13;
|
||||
|
||||
abstract class Employee {
|
||||
private String name;
|
||||
protected int workDays;
|
||||
|
||||
public Employee(String name, int workDays) {
|
||||
this.name = name;
|
||||
this.workDays = workDays;
|
||||
}
|
||||
|
||||
abstract int calculateAttendance();
|
||||
|
||||
boolean isExcellent() {
|
||||
return calculateAttendance() >= 40;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
class FullTimeEmployee extends Employee {
|
||||
public FullTimeEmployee(String name, int workDays) {
|
||||
super(name, workDays);
|
||||
}
|
||||
|
||||
@Override
|
||||
int calculateAttendance() {
|
||||
return workDays * 2;
|
||||
}
|
||||
}
|
||||
|
||||
class InternEmployee extends Employee {
|
||||
public InternEmployee(String name, int workDays) {
|
||||
super(name, workDays);
|
||||
}
|
||||
|
||||
@Override
|
||||
int calculateAttendance() {
|
||||
return workDays + 5;
|
||||
}
|
||||
}
|
||||
|
||||
public class AttendanceService {
|
||||
public static void main(String[] args) {
|
||||
Employee fullTime = new FullTimeEmployee("张三", 22);
|
||||
Employee intern = new InternEmployee("李四", 20);
|
||||
Employee fullTimeLow = new FullTimeEmployee("王五", 18);
|
||||
Employee internExcellent = new InternEmployee("赵六", 30);
|
||||
|
||||
System.out.println(fullTime.getName() + " - 出勤分: " + fullTime.calculateAttendance() + " - 是否优秀: " + fullTime.isExcellent());
|
||||
System.out.println(intern.getName() + " - 出勤分: " + intern.calculateAttendance() + " - 是否优秀: " + intern.isExcellent());
|
||||
System.out.println(fullTimeLow.getName() + " - 出勤分: " + fullTimeLow.calculateAttendance() + " - 是否优秀: " + fullTimeLow.isExcellent());
|
||||
System.out.println(internExcellent.getName() + " - 出勤分: " + internExcellent.calculateAttendance() + " - 是否优秀: " + internExcellent.isExcellent());
|
||||
}
|
||||
}
|
||||
72
com/study/demo13/GradeService.java
Normal file
72
com/study/demo13/GradeService.java
Normal file
@@ -0,0 +1,72 @@
|
||||
package com.study.demo13;
|
||||
|
||||
interface ScoreCalculator {
|
||||
double calculate();
|
||||
String getLevel();
|
||||
}
|
||||
|
||||
abstract class AbstractCourse implements ScoreCalculator {
|
||||
protected double score;
|
||||
|
||||
public AbstractCourse(double score) {
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
protected String levelByScore(double s) {
|
||||
if (s >= 75) {
|
||||
return "A";
|
||||
} else if (s >= 60) {
|
||||
return "B";
|
||||
} else if (s >= 60) {
|
||||
return "C";
|
||||
} else {
|
||||
return "D";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class TheoryCourse extends AbstractCourse {
|
||||
public TheoryCourse(double score) {
|
||||
super(score);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double calculate() {
|
||||
return score;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLevel() {
|
||||
return levelByScore(score);
|
||||
}
|
||||
}
|
||||
|
||||
class LabCourse extends AbstractCourse {
|
||||
public LabCourse(double score) {
|
||||
super(score);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double calculate() {
|
||||
return score * 0.9;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLevel() {
|
||||
return levelByScore(calculate());
|
||||
}
|
||||
}
|
||||
|
||||
public class GradeService {
|
||||
public static void main(String[] args) {
|
||||
ScoreCalculator theory = new TheoryCourse(85);
|
||||
ScoreCalculator lab = new LabCourse(85);
|
||||
ScoreCalculator theoryLow = new TheoryCourse(55);
|
||||
ScoreCalculator labMid = new LabCourse(70);
|
||||
|
||||
System.out.println("TheoryCourse: score = " + theory.calculate() + ", level = " + theory.getLevel());
|
||||
System.out.println("LabCourse: score = " + lab.calculate() + ", level = " + lab.getLevel());
|
||||
System.out.println("TheoryCourse(low): score = " + theoryLow.calculate() + ", level = " + theoryLow.getLevel());
|
||||
System.out.println("LabCourse(mid): score = " + labMid.calculate() + ", level = " + labMid.getLevel());
|
||||
}
|
||||
}
|
||||
62
com/study/demo13/MemberService.java
Normal file
62
com/study/demo13/MemberService.java
Normal file
@@ -0,0 +1,62 @@
|
||||
package com.study.demo13;
|
||||
|
||||
abstract class Member {
|
||||
protected int points;
|
||||
|
||||
public Member(int points) {
|
||||
this.points = points;
|
||||
}
|
||||
|
||||
abstract int calculateFinalPoints();
|
||||
|
||||
String getLevel() {
|
||||
int finalPoints = calculateFinalPoints();
|
||||
if (finalPoints >= 1000) {
|
||||
return "VIP";
|
||||
} else if (finalPoints >= 500) {
|
||||
return "GOLD";
|
||||
} else {
|
||||
return "NORMAL";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class NormalMember extends Member {
|
||||
public NormalMember(int points) {
|
||||
super(points);
|
||||
}
|
||||
|
||||
@Override
|
||||
int calculateFinalPoints() {
|
||||
return points;
|
||||
}
|
||||
}
|
||||
|
||||
class VIPMember extends Member {
|
||||
public VIPMember(int points) {
|
||||
super(points);
|
||||
}
|
||||
|
||||
@Override
|
||||
int calculateFinalPoints() {
|
||||
return points * 2;
|
||||
}
|
||||
}
|
||||
|
||||
public class MemberService {
|
||||
public static void main(String[] args) {
|
||||
Member normal1 = new NormalMember(300);
|
||||
Member normal2 = new NormalMember(600);
|
||||
Member normal3 = new NormalMember(1200);
|
||||
Member vip1 = new VIPMember(300);
|
||||
Member vip2 = new VIPMember(400);
|
||||
Member vip3 = new VIPMember(600);
|
||||
|
||||
System.out.println("NormalMember - 最终积分: " + normal1.calculateFinalPoints() + ", 等级: " + normal1.getLevel());
|
||||
System.out.println("NormalMember - 最终积分: " + normal2.calculateFinalPoints() + ", 等级: " + normal2.getLevel());
|
||||
System.out.println("NormalMember - 最终积分: " + normal3.calculateFinalPoints() + ", 等级: " + normal3.getLevel());
|
||||
System.out.println("VIPMember - 最终积分: " + vip1.calculateFinalPoints() + ", 等级: " + vip1.getLevel());
|
||||
System.out.println("VIPMember - 最终积分: " + vip2.calculateFinalPoints() + ", 等级: " + vip2.getLevel());
|
||||
System.out.println("VIPMember - 最终积分: " + vip3.calculateFinalPoints() + ", 等级: " + vip3.getLevel());
|
||||
}
|
||||
}
|
||||
66
com/study/demo13/NotificationService.java
Normal file
66
com/study/demo13/NotificationService.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package com.study.demo13;
|
||||
|
||||
interface Notifier {
|
||||
void send(String message, int level);
|
||||
String getChannel();
|
||||
}
|
||||
|
||||
class EmailNotifier implements Notifier {
|
||||
private String channel = "EMAIL";
|
||||
|
||||
@Override
|
||||
public void send(String message, int level) {
|
||||
System.out.println("[EMAIL] " + (level >= 3 ? "[紧急] " : "") + message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getChannel() {
|
||||
return channel;
|
||||
}
|
||||
}
|
||||
|
||||
class SMSNotifier implements Notifier {
|
||||
private String channel = "SMS";
|
||||
|
||||
@Override
|
||||
public void send(String message, int level) {
|
||||
System.out.println("[SMS] " + (level >= 3 ? "[紧急] " : "") + message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getChannel() {
|
||||
return channel;
|
||||
}
|
||||
}
|
||||
|
||||
class PushNotifier implements Notifier {
|
||||
private String channel = "PUSH";
|
||||
|
||||
@Override
|
||||
public void send(String message, int level) {
|
||||
System.out.println("[PUSH] " + (level >= 3 ? "[紧急] " : "") + message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getChannel() {
|
||||
return channel;
|
||||
}
|
||||
}
|
||||
|
||||
public class NotificationService {
|
||||
public static void main(String[] args) {
|
||||
Notifier email = new EmailNotifier();
|
||||
Notifier sms = new SMSNotifier();
|
||||
Notifier push = new PushNotifier();
|
||||
|
||||
email.send("您的订单已发货", 1);
|
||||
sms.send("验证码: 123456", 2);
|
||||
push.send("账户安全警告", 3);
|
||||
email.send("重要通知:系统维护", 4);
|
||||
|
||||
System.out.println("\n通知渠道类型:");
|
||||
System.out.println("Email渠道: " + email.getChannel());
|
||||
System.out.println("SMS渠道: " + sms.getChannel());
|
||||
System.out.println("Push渠道: " + push.getChannel());
|
||||
}
|
||||
}
|
||||
58
com/study/demo13/OrderService.java
Normal file
58
com/study/demo13/OrderService.java
Normal file
@@ -0,0 +1,58 @@
|
||||
package com.study.demo13;
|
||||
|
||||
abstract class Order {
|
||||
protected double amount;
|
||||
|
||||
public Order(double amount) {
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
abstract double calculatePrice();
|
||||
abstract String getOrderType();
|
||||
}
|
||||
|
||||
class NormalOrder extends Order {
|
||||
public NormalOrder(double amount) {
|
||||
super(amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
double calculatePrice() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
@Override
|
||||
String getOrderType() {
|
||||
return "NORMAL";
|
||||
}
|
||||
}
|
||||
|
||||
class DiscountOrder extends Order {
|
||||
public DiscountOrder(double amount) {
|
||||
super(amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
double calculatePrice() {
|
||||
return amount * 0.8;
|
||||
}
|
||||
|
||||
@Override
|
||||
String getOrderType() {
|
||||
return "DISCOUNT";
|
||||
}
|
||||
}
|
||||
|
||||
public class OrderService {
|
||||
public static void main(String[] args) {
|
||||
Order normal = new NormalOrder(100.0);
|
||||
Order discount = new DiscountOrder(100.0);
|
||||
Order normal2 = new NormalOrder(250.5);
|
||||
Order discount2 = new DiscountOrder(300.0);
|
||||
|
||||
System.out.println("Order Type: " + normal.getOrderType() + ", Final Price: " + normal.calculatePrice());
|
||||
System.out.println("Order Type: " + discount.getOrderType() + ", Final Price: " + discount.calculatePrice());
|
||||
System.out.println("Order Type: " + normal2.getOrderType() + ", Final Price: " + normal2.calculatePrice());
|
||||
System.out.println("Order Type: " + discount2.getOrderType() + ", Final Price: " + discount2.calculatePrice());
|
||||
}
|
||||
}
|
||||
77
com/study/demo13/PaymentService.java
Normal file
77
com/study/demo13/PaymentService.java
Normal file
@@ -0,0 +1,77 @@
|
||||
package com.study.demo13;
|
||||
|
||||
interface Payable {
|
||||
boolean pay(double amount);
|
||||
String getPayType();
|
||||
}
|
||||
|
||||
abstract class Account implements Payable {
|
||||
protected double balance;
|
||||
|
||||
public Account(double balance) {
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
protected boolean canPay(double total) {
|
||||
return balance >= total;
|
||||
}
|
||||
|
||||
public double getBalance() {
|
||||
return balance;
|
||||
}
|
||||
}
|
||||
|
||||
class BankAccount extends Account {
|
||||
public BankAccount(double balance) {
|
||||
super(balance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean pay(double amount) {
|
||||
if (canPay(amount)) {
|
||||
balance -= amount;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPayType() {
|
||||
return "BANK";
|
||||
}
|
||||
}
|
||||
|
||||
class WalletAccount extends Account {
|
||||
public WalletAccount(double balance) {
|
||||
super(balance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean pay(double amount) {
|
||||
double total = amount * 1.05;
|
||||
if (canPay(total)) {
|
||||
balance -= total;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPayType() {
|
||||
return "WALLET";
|
||||
}
|
||||
}
|
||||
|
||||
public class PaymentService {
|
||||
public static void main(String[] args) {
|
||||
Payable bank = new BankAccount(1000.0);
|
||||
Payable wallet = new WalletAccount(500.0);
|
||||
Payable bank2 = new BankAccount(100.0);
|
||||
Payable wallet2 = new WalletAccount(50.0);
|
||||
|
||||
System.out.println("支付方式: " + bank.getPayType() + ", 支付 200: " + (bank.pay(200) ? "成功" : "失败") + ", 余额: " + ((BankAccount) bank).getBalance());
|
||||
System.out.println("支付方式: " + wallet.getPayType() + ", 支付 100: " + (wallet.pay(100) ? "成功" : "失败") + ", 余额: " + String.format("%.2f", ((WalletAccount) wallet).getBalance()));
|
||||
System.out.println("支付方式: " + bank.getPayType() + ", 支付 200: " + (bank2.pay(200) ? "成功" : "失败") + ", 余额: " + ((BankAccount) bank2).getBalance());
|
||||
System.out.println("支付方式: " + wallet.getPayType() + ", 支付 100: " + (wallet2.pay(100) ? "成功" : "失败") + ", 余额: " + String.format("%.2f", ((WalletAccount) wallet2).getBalance()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user