我正在制作公共交通卡。我希望你能帮助定义三种相关的方法:
isCheckedIn(long time) 返回它是否成功地在最后120分钟内签入,并且自上一次成功的checkIn之后还没有签出。
checkIn(int x, int y, long time) 哪一个
checkOut(int x,int y,长时间)
其中:
示例消息:
我到目前为止所做的事:
import java.time.Instant;
import java.time.temporal.ChronoUnit;
public class TravelCard {
//Card balance
private int balance;
// isCheckedIn
private boolean isCheckedIn;
private Instant instant;
// distant x,y
private int x;
private int y;
public TravelCard()
{
balance = 100;
isCheckedIn = false;
}
public void depositMoney(int amount) {
if (amount >= 0) {
balance += amount;
System.out.println(amount + " dollars deposited. New balance: " + balance + " dollars");
} else {
System.out.println("Error: Cannot deposit negative amount");
}
}
public boolean isCheckedIn(long time) {
Instant instant1 = Instant.now();
time = ChronoUnit.MINUTES.between(instant, instant1);
// time
if (isCheckedIn && time <= 120) return true;
else return false;
}
public void checkIn(int x, int y, long time) {
this.x = x;
this.y = y;
if (time == 0) {
System.out.println("Checked in");
} else if (time <= 120) {
System.out.println("Continued journey");
}
}
public void checkOut(int x, int y, long time) {
}
public static void main(String[] args) {
}
}我完全迷上了checkOut方法,除了打印消息的方法。有人能帮我解决这个问题吗?事先非常感谢!
发布于 2017-10-01 20:30:19
下面的代码只是为了向您展示一种如何存储x和y的最大值和最小值的方法,另一种方法是在数组中存储所有的x和y值。但是内存消耗会不必要地增加。
public TravelCard(){
x_min = 9999999;
y_min = 9999999;
x_max = 0;
y_max = 0;
balance = 100;
isCheckedIn = false;
}
public void checkIn(int x, int y, long time) {
//we save the new x and y, only if they are larger than previous ones
if (x >this.x_max){
this.x_max = x;
} else if(x < this.x_min){
this.x_min = x;
}
if (y >this.y_max){
this.y_max = y;
} else (y<y_min){
this.y_min = y;
}
if (ischeckedIn()) {
System.out.println("Continued Journey");
} else if {
System.out.println("CheckingIn Now");
this.instant = Instant.now()
isCheckedIn = true;
}
}
public boolean isCheckedIn(l) {
Instant instant1 = Instant.now();
if(this.isCheckedIn){
time = ChronoUnit.MINUTES.between(instant, instant1);
} else {
return false;
}
// time
if (isCheckedIn && time <= 120) return true;
else{
System.out.println("Your time is out! i.e Auto Checkout")
this.isCheckedIn = false;
return false;
}
public void checkOut(int x,int y,long time){
if(this.isCheckedIn){
//updating x and y at checkout time
if (x >this.x_max){
this.x_max = x;
} else if(x < this.x_min){
this.x_min = x;
}
if (y >this.y_max){
this.y_max = y;
} else (y<y_min){
this.y_min = y;
}
//Now we alredy have max and min for x and y
int expense = 12 +((this.x_max - this.x_min)+(this.y_max - this.y_min))*3;
this.balance = this.balance - expense;
System.out.println("Expense for trip: "+expense+", Available Balance:" +this.balance);
}else{
System.out.println("Error: Cannot check out; Not currently checked in");
}即使在编写了这段代码之后,仍然存在一个问题,如果person在120分钟后被自动签出,您将如何在120分钟内检索x和y值。在现实生活中,我们经常听到x和y值的变化,并不断更新。
发布于 2017-10-01 20:49:01
这听起来很像大学家庭作业的问题。我认为,了解问题的全部背景,而不仅仅是你对问题的解释,可能会更有帮助。例如,为什么isCheckedIn()函数要求您传递一个时间值?这是一个要求,还是仅仅是你对答案的解释。实际上,你似乎并没有在任何计算中使用这个变量,所以不管我把它放在0还是1000,结果都不会改变。我还建议在来这里之前请你的助教/老师帮忙,因为这是一个特殊的问题,你可能会得到更多的即时帮助。
但是,试着回答你的一些问题。
ischeckedIn(lomg time)我注意到的第一件事是,您使用即时变量而没有在构造函数中初始化它。看起来,在checkIn时间之前,该变量不应该有一个值,所以您应该对该逻辑进行空检查。第二件事是,由于您有一个值来查看票证是否已经签入,这应该是您的第一步,因此您不会重复工作:
public boolean isCheckedIn() {
if(!isCheckedIn) return false;
....
//some logic
}函数的第二件事是,由于120是签入时间的常量,所以应该将其移出函数,并将其存储为类的静态最后int。
我还建议,如果您的isCheckedIn()方法要返回false,那么应该将isCheckedIn值设置为false,直到它们再次签入为止。
checkIn(int x, int y, long time)这里有几点。因为您需要跟踪x和y的最小值和最大值,所以需要将签入坐标存储在某种数组中,或者需要为坐标设置3个变量。minX,maxX,currentX,y也一样。
在执行了存储坐标的逻辑之后,您应该检查此人是否已经签入。
if(isCheckedIn(time)) {
// your logic
}如果时间>120分钟,当前的方法将失败。如果时间超过120时,最好在这里使用一些方法来失败checkIn进程,其中的消息是,在再次签入之前,他们必须支付余额。如果您已签入,则应使用您的时间值来创建一个新的瞬间来替换旧的时间。我会考虑将您的返回值设置为字符串,这样您就可以让您的方法的使用者决定如何处理结果。
checkOut(int x, int y, long time)这个方法应该非常类似于checkIn方法。您的第一步应该是检查您是否实际签入。
if(!isCheckedIn(time)) {
// return error message
}如果您是签入的,那么您将执行为checkIn创建的存储坐标的逻辑。此时,您可以运行计算成本、减少余额和返回消息的公式。
https://stackoverflow.com/questions/46515742
复制相似问题