gpt4 book ai didi

Java:基本议程程序,人们可以在其中操纵他们的事件,并根据需要获得当前一周或一天的事件通知

转载 作者:行者123 更新时间:2023-12-02 12:49:39 25 4
gpt4 key购买 nike

我以为我可以制作这个简单的程序,而不需要寻求人类的帮助并浪费他们的时间,但我的“想法”在这里被绊倒了。到目前为止,谷歌一直在帮助我,但不幸的是,这是我所能得到的。因此,我们非常感谢任何帮助,在开始之前请注意,我刚刚开始接触 Java。

正如标题所说,这是一个类似于 Google Keep 的迷你议程程序,我们可以这么说,用户会看到一个基于控制台的菜单,如下所示:

  1. 注册个人 Activity
  2. 当天或本周的 Activity
  3. 删除 Activity
  4. 编辑 Activity
  5. 关闭议程

当他/她转到选项 2 时,程序应正确列出事件。目前,它仅显示所有内容。实际上,这是 main 中的代码:

public class Agenda
{
public static void main(String[] args) throws IOException
{
Person person = new Person();
Event event = new Event();
Scanner sc1 = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);

//System.out.println(date.toString());
//System.out.println(hourMinutesSeconds.toString());



boolean agenda = true;
while (agenda)
{
System.out.println("Menu");
System.out.println("====");
System.out.println("1. Register personal events");
System.out.println("2. Current week/day events");
System.out.println("3. Delete events");
System.out.println("4. Edit events");
System.out.println("5. Close agenda");
int choice = sc1.nextInt();
switch (choice)
{
case 1:
{
int exit = 1;
String save = "";
while (exit == 1)
{
System.out.println("Register personal events");
System.out.println("=========================");

System.out.println("Your name:");
person.setName(sc2.nextLine());

System.out.println("Person phone number:");
person.setPhoneNumber(sc2.nextLine());

System.out.println("Add details about someone's anniversary(optional: schedule a date in the format as follows (hour/minutes/seconds day/month/year))");
event.setBirthDay(sc2.nextLine());


System.out.println("Add details about the conference(optional: schedule a date in the format as follows (hour/minutes/seconds day/month/year))");
event.setMeeting(sc2.nextLine());

System.out.println("Add details about the debate(optional: schedule a date in the format as follows (hour/minutes/seconds day/month/year))");
event.setDebate(sc2.nextLine());


System.out.println("Save the event?yes\\no");
save = sc2.nextLine();
if (save.equals("yes"))
{
String fileName = person.toString().substring(6, person.toString().indexOf("\n")) + ".txt";
EventSaver.saveToFile(person.toString() + event.toString(), fileName);
}


System.out.println("Do you wish to add more events? 1-Yes, 0-No");
exit = sc1.nextInt();

}
break;
}

case 2:
{

String[] events = EventSaver.readEvents();
for (int i = 0; i < events.length; i++)
{

System.out.println(events[i]);

}
break;
}

case 3:
{
break;

}

case 4:
{
break;

}

case 5:
{
agenda = false;
sc1.close();
sc2.close();
System.out.println("Agenda closed");
break;
}
default:
{
throw new IllegalArgumentException("Invalid option...\ngoing back to main menu");
}

}

}
}
}

EventSaver 类:

public class EventSaver 
{
private static String FILENAME = "D:\\my_stuff\\some_dir\\some_dir\\Workspace\\EventAgenda\\src\\";

public static void saveToFile(String content, String fileName)
{
BufferedWriter bw = null;
FileWriter fw = null;
try
{
fw = new FileWriter(FILENAME + fileName);
bw = new BufferedWriter(fw);
bw.write(content);

System.out.println("Event saved");

}
catch (IOException e)
{
e.printStackTrace();

}
finally
{
try
{
if (bw != null)
{
bw.close();
}
if (fw != null)
{
fw.close();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}


public static String[] readEvents() throws IOException
{
File folder = new File("D:\\my_stuff\\some_dir\\some_dir\\Workspace\\EventAgenda\\src\\");
File[] listOfFiles = folder.listFiles();

String fileContents = "";
String[] content;
for (int i = 0; i < listOfFiles.length; i++)
{
File file = listOfFiles[i];
if (file.isFile() && file.getName().endsWith(".txt"))
{

fileContents += FileUtils.readFileToString(file, "UTF-8") + "\n";

}
}
content = fileContents.split("\n");
return content;

保存事件后,属于特定用户的 .txt 文件如下所示:

Name: Bob
Phone Number: 0741123458
Birthday: Elena, at LongDrinks(19:00:00 19/06/2017)
Conference: Current stage of the project SMFA(20:00:00 24/06/2017)
Debate: The meaning of this world(21:30:00 19/06/2017)

我尝试获取这些日期并以某种方式比较它们,但使用此代码却徒劳无功:

public class SetEvents {

if (event.toString().indexOf("(") != -1)
{
String[] eventTimeAndDate = (event.toString().substring(event.toString().indexOf("(") + 1, event.toString().indexOf(")"))).split(" ");
String[] eventTime = eventTimeAndDate[0].split(":");
String[] eventDate = eventTimeAndDate[1].split("/");

Time hourMinutesSeconds = new Time();
hourMinutesSeconds.setHour(Integer.parseInt(eventTime[0]));
hourMinutesSeconds.setMinute(Integer.parseInt(eventTime[1]));
hourMinutesSeconds.setSecond(Integer.parseInt(eventTime[2]));

MyDate date = new MyDate(Integer.parseInt(eventDate[2]), Integer.parseInt(eventDate[1]), Integer.parseInt(eventDate[0]));
}

但是有了这个,我只能从文件中提取第一个日期出现的情况,并且我需要获取所有出现的情况,使用其他类以某种方式比较它们,以查找当前周和天的事件,并在选择选项 2 时正确显示它们。我可以做吗?谢谢

最佳答案

在 Java 中还有第二个 indexOf 方法

indexOf(String str, int fromIndex)

Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

(参见:https://docs.oracle.com/javase/7/docs/api/java/lang/String.html)

所以你可以这样:

int startAt = 0;
String evt = event.toString();
while (evt.indexOf("(", startAt) != -1){

startAt = indexOf("(", startAt)+2;

String[] eventTimeAndDate = (event.toString().substring(event.toString().indexOf("(") + 1, event.toString().indexOf(")"))).split(" ");
String[] eventTime = eventTimeAndDate[0].split(":");
String[] eventDate = eventTimeAndDate[1].split("/");

Time hourMinutesSeconds = new Time();
hourMinutesSeconds.setHour(Integer.parseInt(eventTime[0]));
hourMinutesSeconds.setMinute(Integer.parseInt(eventTime[1]));
hourMinutesSeconds.setSecond(Integer.parseInt(eventTime[2]));

MyDate date = new MyDate(Integer.parseInt(eventDate[2]), Integer.parseInt(eventDate[1]), Integer.parseInt(eventDate[0]));
}

编辑:没有测试它,但在这个 while 循环中你应该能够提取你得到的所有日期

关于Java:基本议程程序,人们可以在其中操纵他们的事件,并根据需要获得当前一周或一天的事件通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44648650/

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