【发布时间】:2016-01-14 20:19:36
【问题描述】:
这是我的指示:
这个作业已经过期了,我的教授说他会对它发表评论以帮助我理解,但我认为他太忙了,而且我不能把它做对,这让我很恼火,所以......我在这里...任何帮助都会很棒。
待办事项列表
- 您将上两节课。 ToDoItem.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 类可能非常基本,并且您的“驱动程序”文件和您的 main 方法正在完成大部分工作。
- 使用 Array 或 ArrayList 来存储您的 ToDoItem。通过在该数据结构中的索引引用各个 ToDoItem(打印所有 ToDoItem 时打印每个项目的索引)。
- 一旦创建了 ToDoItem,就不能对其进行编辑,除非它被标记为已完成。如果用户输入错误的日期或拼写错误的标题,则只能删除该项目,然后重新创建以进行修复。这是为了简化分配。 同样,将项目标记为完整/不完整是 ToDoItem 唯一可以编辑的方面。无法编辑的对象称为不可变对象。
ToDo 计划思考
以下是一些需要考虑的问题,我们将在本课程的几周内解决这些问题。我现在将这些项目包括在内,因此您可以开始考虑它们。
如果我们想按到期日期或优先级或两者对项目进行排序怎么办? - - 不要写这个,想想我们应该怎么做。
是什么让一个 ToDo 项小于、等于或大于另一个?
我们正在编写大量代码来管理我们的 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);
}
}
【问题讨论】:
-
这个任务是给你的还是给我们的?请提供它所需的内容并准确说明您的问题?看看这个MCVE。
-
我提供了我所提供的一切......这是我从教授那里得到的所有信息。以下是我根据他提供的信息编写的代码。我已经说过我无法正确解析日期。估计我说得不够清楚。现在我把它加粗了,所以人们现在可以看到它。对不起。
标签: java class methods arraylist