以下是我的说明:
这个作业已经过期了,我的教授说他会评论一下,帮助我理解,但我想他太忙了& &;这让我的灵魂感到烦恼,我不能把它做好,所以....我来了……
ToDo列表
- 你们要交2节课。java(定义todoitem的数据类型)和MyList.java(允许用户输入数据、创建ToDoItems和管理ToDoItems的驱动程序)。 为ToDoItem设计一个类。ToDoItem跟踪你需要做的一件事(作为一个字符串),它的截止日期(嗯,做这件事的好方法是什么?),该项目的优先级(1是高的,2是中等的,3是低的),以及该项目是否完成。
- 勾勒出一个粗略的UML图,说明该类的外观以及该类需要的方法。你必须把这个写进作业里。您可以使用像ArgoUML这样的CASE工具,或者您可以简单地手绘图表并在作业中提交图片。
- 你必须提供一个构造函数来接受该类的参数来设置对象的默认状态。
- 编写一个重载方法,以便用户可以通过int(1,2,3)或String ("high", "medium", "low")设置优先级。 然后,编写一个简单的菜单驱动程序,允许用户创建待办事项,删除它们,查看它们全部未排序,并将它们标记为已完成。请参阅附件MyList.java的一个小框架,以帮助使您的菜单系统更有组织。
待办事项&提示
附带的文件MyList.java是一个简单的命令行驱动的ToDo列表的基本框架。将此作为ToDo分配的基础。这个启动文件中缺少很多内容,但它将帮助您更快地启动和运行。
import java.util.ArrayList;
import java.util.Scanner;
public class MyList {
public static ArrayList todoItems = new ArrayList();
public static void main(String[] args) {
while(true) {
printMenu();
processInput();
}
}
public static void printMenu() {
System.out.println("[a]dd an item");
System.out.println("[p]rint all");
System.out.println("[q]uit");
}
public static void processInput() {
Scanner s = new Scanner(System.in);
String input = s.next();
if(input.equals("a")) {
//addToDoItem();
}
else if(input.equals("p")) {
//printAll();
}
else if(input.equals("q")) {
System.exit(0);
}
}
// implement rest of processInput methods here
}
- 每个特性,如创建、查看、删除和标记读取,都应该定义为一个方法,这样你的代码可以很容易地阅读。你的ToDoItem类应该没有与用户界面相关的代码。将用户界面代码与数据分开(此建议基于称为模型-视图-控制器或MVC的模式)。这意味着你的ToDoItem类可能是非常基本的,你的"驱动程序"文件与你的主要方法是做大部分的工作。
- 使用Array或ArrayList来存储ToDoItems。根据数据结构中的索引引用单个todoitem(打印所有todoitem时打印每个todoitem的索引)。 一旦ToDoItem被创建,它就不能被编辑,除非被标记为已完成。如果用户输入了错误的日期或拼写错误的标题,则只能删除该条目,然后重新创建以进行修复。这是为了简化赋值。同样,将项目标记为完整/不完整是ToDoItem可以编辑的唯一方面。不能编辑的对象称为不可变的。
ToDo Program Thoughts
这是一些需要思考的事情,我们将在几周后的课程中讨论。我现在把这些项目列出来,这样你就可以开始考虑它们了。
如果我们想按到期日期或优先级或两者都排序,该怎么办?不要写这个,想想我们该怎么做。
是什么使得一个待办事项小于、等于或大于另一个待办事项?
我们正在编写大量代码来管理ToDoItems数组。无论如何,我们可以简化它吗?
我有一个程序演示的视频: https://youtu.be/9eWkn7uOLs0
这是我到目前为止写的代码。我被卡住了,在解析日期和打印出来时遇到了麻烦。
MyList.java
import java.util.Scanner;
import java.util.ArrayList;
import java.io.*;
import java.text.*;
public class MyList {
public static ArrayList<ToDoItem> toDoItems = new ArrayList<>();
public static Scanner k = new Scanner(System.in);
private static String description;
private static String dueDate;
public static void main(String[] args) throws ParseException {
while(true) {
printMenu();
processInput();
}
}
public static void printMenu() {
System.out.println("[a]dd an item");
System.out.println("[d]elete an item");
System.out.println("[t]oggle complete");
System.out.println("[p]rint all");
System.out.println("[q]uit");
}
public static void processInput() throws ParseException{
Scanner s = new Scanner(System.in);
String input = s.next();
if(input.equals("a")) {
addToDoItem();
}
else if(input.equals("d")) {
//deleteToDoItem();
}
else if(input.equals("t")) {
// toggleComplete();
}
else if(input.equals("p")) {
printAll();
}
else if(input.equals("q")) {
System.exit(0);
}
}
public static ToDoItem addToDoItem() throws ParseException {
ToDoItem newItem;
newItem = null;
System.out.print("Enter an item to add to list: ");
String desc = k.nextLine();
if(desc.trim().length()==0) return newItem;
System.out.print("Enter Date (MM/dd/YYYY): ");
String dueDate = k.nextLine();
System.out.print("Enter priority between 1 and 3 (3 being the highest): ");
String prior = k.nextLine();
int p = Integer.parseInt(prior);
if(dueDate.trim().length()==0){
newItem = new ToDoItem(desc);
}
else {
newItem = new ToDoItem(desc, dueDate);
}
newItem.setPriority(p);
return newItem;
//toDoItems.add(new ToDoItem(desc, p, dueDate));
}
public static void printAll() throws ParseException {
ToDoItem item = new ToDoItem();
System.out.println(item);
//ToDoItem task = newItem.get(i);
// ***************
// You should not need to create ToDoItems here
// This method should loop through your array and print out each item
// Since you have a toString() method you can print the objects by passing
// them into like so inside of a loop System.out.println( item.get(i) )
//for(int i = 0; i < newItem.size(); i++) {
// System.out.println(toDoItems.get(i));
// }
// for(ToDoItem myItem : toDoItems) {
//ToDoItem myItem = toDoItems.get(i);
//System.out.println(myItem);
// System.out.println(myItem.getDescription()+" -"+myItem.getPriority()+"- ("+myItem.getDueDate()+")");
}
}
//public static void deleteToDoItem() {
// **********
// You won't need a loop here, you can directly
// delete the item at the given index.
// Prompt for an int, read in the int, then call item.remove(i);
//System.out.print("Enter index of item to delete: ");
//int delete = k.nextInt();
//toDoItems.remove(i);
// }
// public static void toggleComplete() {
/////
// }
//}
ToDoItem.java
import java.text.*;
import java.util.Date;
//import java.lang.NullPointerException;
public class ToDoItem {
private String description;
private Date dueDate;
private int priority;
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
public ToDoItem() {
}
public ToDoItem(String desc) {
description = desc;
dueDate = null;
priority = 0;
}
public ToDoItem(String desccription, String dDate) throws ParseException {
this.description = description;
dueDate = df.parse(dDate.trim());
}
public String toString() {
if(dueDate != null) {
return( description + " -"+priority+"- " + "Date not Set");
}
else {
return( description + " -"+priority+"- " + df.format(dueDate));
}
}
public void setPriority( int prio) {
if(prio<0) this.priority = 0;
else if(prio > 3) this.priority = 3;
else this.priority = prio;
}
public int getPriority() {
return this.priority;
}
public void setDueDate(String date) throws ParseException {
Date d = df.parse(date.trim());
this.dueDate = d;
}
public String getDescription() {
return description;
}
public String getDueDate() {
if(dueDate == null) return "";
return df.format(dueDate);
}
}
如果您有解析和打印日期类型的问题,也许这可以帮助您:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String dateString = sdf.format(new Date());
Date date = null;
try {
date = sdf.parse("10/16/2015");
} catch (ParseException objcPException) {
// TODO Auto-generated catch block
objcPException.printStackTrace();
}
System.out.println(date);
System.out.println(dateString);
你必须改变这个方法:
public ToDoItem(String desccription, String dDate) throws ParseException {
this.description = description;
dueDate = df.parse(dDate.trim());
}到此:
public ToDoItem(String desccription, String dDate) throws ParseException {
this.description = description;
dueDate = sdf.parse(dDate);
}并将DateFormat切换为SimpleDateFormat,就像我的例子中一样。在解析之前,可能必须先验证输入的形式(MM/dd/yyyy)。
替换以下代码
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
下面有语句,try
SimpleDateFormat df = new SimpleDateFormat("MM/dd/YYYY");
因此,构造函数将使用这个日期格式来解析输入。
dueDate = df.parse(dDate.trim());
当用户以格式(MM/dd/YYYY)呈现日期并作为字符串读取时,最好的方法是使用dateformat对其进行解析。检查下面来自(stackoverflow)的类似示例。您可以运行它并检查。
String string = "January 2, 2010";
DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date);
希望能有所帮助。