gpt4 book ai didi

java - 将字符串转换为日期并在 ArrayList Java 中进行比较

转载 作者:行者123 更新时间:2023-11-30 08:02:10 28 4
gpt4 key购买 nike

我对编程和 Java 完全陌生,所以请原谅任何愚蠢的错误和非常糟糕的代码(我不知道如何排序/格式化)。我的任务是对视频进行盘点,它具有三个功能:列出、出租和检查。我有一个 ArrayList,其中包含当前可用视频的库存。在检查函数下,我希望能够将 returndate 字符串转换为日期,然后将其与今天的日期进行比较。如果返回日期和今天的日期相等,我想返回一条消息说:“视频今天到期”,如果返回日期已经过去(早于今天的日期),我想返回一条消息说“视频已过期”。

我一直在研究如何将字符串转换为日期,反之亦然,并且一整天都在尝试使用这个以及更多的方法来尝试使其工作,但我似乎无法让它工作。我知道有很多类似的问题已经被提出和回答,但我已经尝试遵循它们,但它不起作用。任何帮助将非常感激。正如我之前所说,我是一个完全的新手,所以请原谅任何愚蠢的行为。

整个程序代码:

import java.text.DateFormat;
import java.text.ParseException;
import java.time.LocalDate;
import java.util.*;

class InventoryRow {
private String name;
private String type;
private Character availability;
private String returndate;

public InventoryRow(String name, String type, Character availability, String returndate) {
this.name = name;
this.type = type;
this.availability = availability;
this.returndate = returndate;
}

public String getReturndate() {
return returndate;
}

public void setReturndate(String returndate) {
this.returndate = returndate;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public Character getAvailability() {
return availability;
}

public void setAvailability(Character availability) {
this.availability = availability;
}

public String toString() {
return name + " " + type + " " + availability + " " + returndate;
}
}

public class InventorySort {

public static void main(String[] args) throws ParseException {
List<InventoryRow> videos = new ArrayList<InventoryRow>();

videos.add(new InventoryRow("Casablanca", "Old", 'Y', null));
videos.add(new InventoryRow("Jurassic Park", "Regular", 'N', "2015-07-30"));
videos.add(new InventoryRow("2012", "Regular", 'Y', null));
videos.add(new InventoryRow("Ant-Man", "New", 'Y', null));

LocalDate dateReturn = LocalDate.now().plusDays(3);

LocalDate dateToday = LocalDate.now();

Scanner input = new Scanner(System.in);

// Output the prompt
System.out.println("Do you want to list (l), rent (r) or check (c)?");

// Wait for the user to enter a line of text
String line = input.nextLine();

// List, rent and check functions
// List function
if (line.equals("l")) {
// //////////////////////////// Sort function
Collections.sort(videos, new Comparator<InventoryRow>() {
public int compare(InventoryRow o1, InventoryRow o2) {
return o1.getName().compareTo(o2.getName());
}
});
for (InventoryRow inventory : videos) {
System.out.println(inventory);
}
// /////////////////////////// Rent function
} else if (line.equals("r")) {
System.out.println("Which video would you like to rent?");
String line2 = input.nextLine();
// /////////////////////////// Search through ArrayList
boolean foundIt = false;
for (InventoryRow ir : videos) {
if (line2.equals(ir.getName()) && ir.getAvailability() == 'Y') {
foundIt = true;
break;
}
}
if (foundIt) {
System.out.println("Video available to rent! Would you like to rent this video?");
String line3 = input.nextLine();
if (line3.equals("Yes")) {
System.out.println("You have rented this video until " + dateReturn + ".");
for (InventoryRow ir : videos) {
if (ir != null && line2.equals(ir.getName())) {
ir.setAvailability('N');
ir.setReturndate(dateReturn.toString());
// //////////////// Just to test if this works
for (InventoryRow inventory : videos) {
System.out.println(inventory);
}
}
}
} else {
System.out.println("You have not rented this video.");
}

} else {
System.out.println("Video unavailable to rent.");
}
// /////////////////////////// Check function
} else if (line.equals("c")) {
System.out.println("Which video would you like to check is in the inventory?");
String line4 = input.nextLine();
// /////////////////////////// Search through ArrayList
boolean foundIt = false;
for (InventoryRow ir : videos) {
if (line4.equals(ir.getName())) {
foundIt = true;
break;
}
}
if (foundIt) {
System.out.println("Video found!");
for (InventoryRow ir : videos) {
LocalDate returnDate = LocalDate.parse(ir.getReturndate());
if (line4.equals(ir.getName()) && ir.getAvailability() == 'N' && returnDate.isEqual(dateToday)) {
System.out.println("Video due for return today.");
} else if (line4.equals(ir.getName()) && ir.getAvailability() == 'N'
&& returnDate.isBefore(dateToday)) {
System.out.println("Video is overdue!");
} else if (line4.equals(ir.getName()) && ir.getAvailability() == 'N') {
System.out.println("Video is due for return on: " + ir.getReturndate());

}
}
} else {
System.out.println("Video not found. Please see the inventory below.");
Collections.sort(videos, new Comparator<InventoryRow>() {
public int compare(InventoryRow o1, InventoryRow o2) {
return o1.getName().compareTo(o2.getName());
}
});
for (InventoryRow inventory : videos) {
System.out.println(inventory);
}
}
// /////////////////////////// If anything else is entered
} else {
System.out.println("The only options are to list (l), rent (r) or check (c).");
}

}
}

这是不起作用的部分,我不知道为什么:

if (foundIt) {
System.out.println("Video found!");
for (InventoryRow ir : videos) {
LocalDate returnDate = LocalDate.parse(ir.getReturndate());
if (line4.equals(ir.getName()) && ir.getAvailability() == 'N' && returnDate.isEqual(dateToday)) {
System.out.println("Video due for return today.");
} else if (line4.equals(ir.getName()) && ir.getAvailability() == 'N'
&& returnDate.isBefore(dateToday)) {
System.out.println("Video is overdue!");
} else if (line4.equals(ir.getName()) && ir.getAvailability() == 'N') {
System.out.println("Video is due for return on: " + ir.getReturndate());

}
}

这是我收到的错误消息:

Exception in thread "main" java.lang.NullPointerException: text
at java.util.Objects.requireNonNull(Objects.java:228)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1846)
at java.time.LocalDate.parse(LocalDate.java:400)
at java.time.LocalDate.parse(LocalDate.java:385)
at InventorySort.main(InventorySort.java:141)

最佳答案

根据您的示例:

// Gets today's date

    Date todayDate = Calendar.getInstance().getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.UK);

您的返回日期采用字符串格式,如下所示:

videos.add(new InventoryRow("Jurassic Park", "Regular", 'N', "30/07/2015"));

现在当天和返回日期之间的差异:

        Date todayDate = Calendar.getInstance().getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.UK);
String currDateStr = dateFormat.format(todayDate);
String returnDateStr = "01/08/2015";
Date returnDate = null;
try {
returnDate = dateFormat.parse(returnDateStr);
todayDate = dateFormat.parse(currDateStr);
}
catch (ParseException e) {
e.printStackTrace();
}
long diffInDays = (todayDate.getTime() - returnDate.getTime()) / (24 * 60 * 60 * 1000);
if (diffInDays == 0) {
System.out.println("Video is due today!");
}
else if (diffInDays > 0) {
System.out.println("Video is overdue :(");
}
else {
System.out.println("Video returned in advance :) ");
}

关于java - 将字符串转换为日期并在 ArrayList Java 中进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31731790/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com