docs: explain registration variables and fix eligibility
This commit is contained in:
1 parent
abbe8afdc8
commit
82a831960c
1 file changed
+35
-14
@@ -3,39 +3,60 @@ package com.imwangzhuang.events.practice;
|
|||||||
public class RegistrationCalculator {
|
public class RegistrationCalculator {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
String eventName = "Java 入门分享会";
|
String eventName = "Java 入门分享会"; // 活动名称
|
||||||
int totalSeats = 30;
|
int totalSeats = 30; // 活动最多允许多少人报名
|
||||||
int registeredSeats = 18;
|
int registeredSeats = 18; // 已经有多少人报名
|
||||||
int requestedSeats = 13;
|
int requestedSeats = 13; // 这一次想报名多少人
|
||||||
int pricePerPerson = 50;
|
int pricePerPerson = 50; // 每人费用,单位:元
|
||||||
boolean registrationOpen = true;
|
boolean registrationOpen = true; // 报名是否开放:true 是开放,false 是关闭
|
||||||
|
|
||||||
|
// 剩余名额 = 总名额 - 已报名人数;30 - 18 = 12。
|
||||||
int remainingSeats = calculateRemainingSeats(totalSeats, registeredSeats);
|
int remainingSeats = calculateRemainingSeats(totalSeats, registeredSeats);
|
||||||
int totalPrice = calculateTotalPrice(requestedSeats, pricePerPerson);
|
// allowed 表示“这一次是否可以报名”,只有所有条件都满足才是 true。
|
||||||
boolean enoughSeats = requestedSeats <= remainingSeats;
|
boolean allowed = canRegister(totalSeats, registeredSeats, requestedSeats, registrationOpen);
|
||||||
|
|
||||||
System.out.println("活动:" + eventName);
|
System.out.println("活动:" + eventName);
|
||||||
System.out.println("剩余名额:" + remainingSeats);
|
System.out.println("剩余名额:" + remainingSeats);
|
||||||
|
System.out.println("能否报名:" + allowed);
|
||||||
|
|
||||||
System.out.println(canRegister(totalSeats, registeredSeats, requestedSeats, registrationOpen));
|
if (allowed) {
|
||||||
|
// 本次费用 = 本次报名人数 × 每人费用;只有可以报名时才计算。
|
||||||
|
int totalPrice = calculateTotalPrice(requestedSeats, pricePerPerson);
|
||||||
|
System.out.println("本次费用:" + totalPrice + " 元");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 根据活动总名额和已报名人数,计算还剩多少名额。
|
||||||
static int calculateRemainingSeats(int totalSeats, int registeredSeats) {
|
static int calculateRemainingSeats(int totalSeats, int registeredSeats) {
|
||||||
return totalSeats - registeredSeats;
|
return totalSeats - registeredSeats;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 根据本次报名人数和每人费用,计算本次总费用。
|
||||||
static int calculateTotalPrice(int requestedSeats, int pricePerPerson) {
|
static int calculateTotalPrice(int requestedSeats, int pricePerPerson) {
|
||||||
return requestedSeats * pricePerPerson;
|
return requestedSeats * pricePerPerson;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 参数依次是:总名额、已报名人数、本次报名人数、报名是否开放。
|
||||||
|
// 返回 true 表示可以报名,返回 false 表示不可以报名。
|
||||||
static boolean canRegister(int totalSeats, int registeredSeats,
|
static boolean canRegister(int totalSeats, int registeredSeats,
|
||||||
int requestedSeats, boolean registrationOpen) {
|
int requestedSeats, boolean registrationOpen) {
|
||||||
// 在这里写你的判断,最后返回 true 或 false
|
// ! 的意思是“不是”:如果报名没有开放,就不能报名。
|
||||||
if (!registrationOpen || requestedSeats <= 0 || requestedSeats <= calculateRemainingSeats(totalSeats, registeredSeats)) {
|
if (!registrationOpen) {
|
||||||
return false;
|
return false;
|
||||||
} else {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// || 的意思是“或者”:以下任意一种名额数据无效,就不能报名。
|
||||||
|
if (totalSeats < 0 || registeredSeats < 0 || registeredSeats > totalSeats) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 一次报名至少要有 1 人。
|
||||||
|
if (requestedSeats <= 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 本次报名人数不超过剩余名额,才可以报名。
|
||||||
|
int remainingSeats = calculateRemainingSeats(totalSeats, registeredSeats);
|
||||||
|
return requestedSeats <= remainingSeats;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user