我正在学习Java,并且我已经完成了用Java编写邮资系统的任务。继承我的任务:
“实现方法public booelan updateTrackingHistory(String datetime,String location),它可以更新Parcel在从发件人传输到收件人时的跟踪历史记录,如下所示。 如果尚未为包裹设置书包尺寸,或者已经交付包裹,则该方法应立即返回false。 否则,该方法应构建一个新的跟踪条目,记录日期/时间和位置,之后新的跟踪条目应附加到新行的跟踪历史记录中。 跟踪历史记录更新后,该方法应返回true,表示跟踪更新已成功记录。“
这是我到目前为止编写的所有代码,但我有点困在构建新的跟踪条目而不确定如何继续这种方法。这段代码中的最后一个方法是我卡住了。
public class parcel {
//Instance Variables
//The info we need to keep track of
private String parcelNumber;
private String senderName;
private String returnAddress;
private String recipientName;
private String deliveryAddress;
private String contactNumber;
private String satchelSize;
private boolean deliveryComplete;
private String trackingHistory;
//Constructors
public parcel (String parcelNumber, String senderName, String returnAddress, String recipientName,
String deliveryAddress, String contactNumber, String trackingHistory) {
this.parcelNumber = parcelNumber;
this.senderName = senderName;
this.returnAddress = returnAddress;
this.recipientName = recipientName;
this.deliveryAddress = deliveryAddress;
this.contactNumber = contactNumber;
this.trackingHistory = "";
}
//Getters
public String getparcelNumber() {
return parcelNumber;
}
public String getrecipientName() {
return recipientName;
}
public String getdeliveryAddress() {
return deliveryAddress;
}
public String getcontactNumber() {
return contactNumber;
}
//Helper Method.
public int calculatePostageCost() {
int postCost = 0;
int trackingFee = 5;
if(satchelSize.equals("Small") || satchelSize.equals("small"))
postCost = trackingFee + 10;
else if(satchelSize.equals("Medium") || satchelSize.equals("medium"))
postCost = trackingFee + 13;
else if(satchelSize.equals("Large") || satchelSize.equals("large"))
postCost = trackingFee + 17;
return postCost;
}
//Method
public int selectSatchel(int parcelLength, int parcelWidth) {
if(satchelSize != null)
return -1;
if(parcelLength > 500 || parcelWidth > 400)
return 0;
else {
//code for finding out smallest parcel size it will fit in
return calculatePostageCost();
}
}
public boolean updateTrackingHistory(String datetime, String location ) {
if(satchelSize == null || satchelSize.isEmpty())
{
return false;
}
//Check if parcel has already been delivered
else if(trackingHistory != null) {
return false;
}
}
答案 0 :(得分:-2)
好,但我主要担心的是这个方法基于任务:
public boolean updateTrackingHistory(String datetime, String location ) {
if(satchelSize == null || satchelSize.isEmpty())
{
return false;
}
//Check if parcel has already been delivered
else if(trackingHistory != null) {
return false;
}
}