编写基于命令行形式的个人信息管理(PIMCmd)程序,要求如下:
This assignment involves the creation of simple Personal Information Management (个人信息管理 ) system that can deal with 4 kinds of items:
todo, items, notes, appointments and contacts.
Each of these kinds of items is described in more detail below. The assignment requires that you create a class for each item type, and that each class extends an abstract base class provided for you. In addition to creating the four classes, you need to create a manager class that supports some simple text-based commands for creating and managing items.
Each of your 4 item type classes will be derived from the following abstract class:
public abstract class PIMEntity {
String Priority; // every kind of item has a priority// default constructor sets priority to “normal”
PIMEntity() {
Priority = “normal”;
}
// priority can be established via this constructor.
PIMEntity(String priority) {
Priority = priority;
}
// accessor method for getting the priority string
public String getPriority() {
return Priority;
}
// method that changes the priority string
public void setPriority(String p) {
Priority = p;
}
// Each PIMEntity needs to be able to set all state information
// (fields) from a single text string.
abstract public void fromString(String s);
// This is actually already defined by the super class
// Object, but redefined here as abstract to make sure
// that derived classes actually implement it
abstract public String toString();
}
注释:PIM可以处理4种类别事项:待办事项,备忘,约会和联系人,PIMEntity是公共抽象父类,创建PIMManager进行测试,(有给定名称的要按给定*的名称)。
输入提示:
java.util.ScannerScanner in = new Scanner(System.in);
//创建Scanner对象String command = in.nextLine();//获取输入的一行字符串
PIMTodo:Todo items must be PIMEntites defined in a class named PIMTodo. Each todo item must have a priority (a string), a date and a string that contains the actual text of the todo item.
PIMNote: Note items must be PIMEntites defined in a class named PIMNote. Each note item must have a priority (a string), and a string that contains the actual text of the note.
PIMAppointment: Appointment items must be PIMEntites defined in a class named PIMAppointment. Each appointment must have a priority (a string), a date and a description (a string).
PIMContact: Contact items must be PIMEntites defined in a class named PIMContact. Each contact item must have a priority (a string), and strings for each of the following: first name, last name, email address.There is one additional requirement on the implementation of the 4 item classes listed above, the 2 classes that involve a date must share an interface that you define. You must formally create this interface and have both PIMAppointment and PIMTodo implement this interface.
PIMManager: You must also create a class named PIMManager that includes a main and provides some way of creating and managing items (from the terminal). You must support the following commands (functionality):
List: print a list of all PIM items
Create: add a new item
Save: save the entire list of items(本次不用做)
Load: read a list of items from a storage(本次不用做)
Welcome to PIM.
—Enter a command (suported commands are List Create Save Load Quit)— List There are 0 items.
—Enter a command (suported commands are List Create Save Load Quit)— Create Enter an item type ( todo, note, contact or
appointment ) todo Enter date for todo item: 04/26/2020 Enter todo
text: Submit java homework. Enter todo priority: urgent
—Enter a command (suported commands are List Create Save Load Quit)— List There are 1 items. Item 1: TODO urgent 04/26/2020 Submit
Java homework.
—Enter a command (suported commands are List Create Save Load Quit)— Save Items have been saved.
—Enter a command (suported commands are List Create Save Load Quit)— Quit
接口方案:PIMEntity
Reimpletement PIMEntity as interface, add change the rest of all code to use it.
设计UML图如下:
抽象父类题目已给出,只需完成测试类PIMManager、四个子类以及Date日期型接口即可。
下面展示一些 内联代码片
。
PIMTodo
import java.text.*;
public class PIMTodo extends PIMEntity implements MyDate{
public static String kind = "TODO";
public String Priority; // each TODO has a priority
public String content; // each TODO has a content
public java.util.Date deadline = null; // each TODO has a deadline
// default constructor sets priority to "normal"
PIMTodo() {
Priority = "normal";
}
// priority can be established via this constructor.
PIMTodo(String priority) {
Priority = priority;
}
@Override
public String getPriority() {
// TODO Auto-generated method stub
return Priority;
}
@Override
public void setPriority(String p) {
// TODO Auto-generated method stub
this.Priority = p;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public java.util.Date getDeadline() {
return deadline;
}
public void setDeadline(java.util.Date deadline) {
this.deadline = deadline;
}
@Override
public void fromString(String s) {
// TODO Auto-generated method stub
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
try {
this.deadline = formatter.parse(s);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "[" + kind +" " + Priority
+ " " + dateToStr(deadline) + " " + content + "]";
}
@Override
public String dateToStr(java.util.Date ddl) {
// TODO Auto-generated method stub
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
String str = format.format(ddl);
return str;
}
}
PIMNote
public class PIMNote extends PIMEntity {
public static String kind = "NOTE";
public String Priority; // each note has a priority
public String content; // each note has a content
PIMNote() {
Priority = "normal";
}
PIMNote(String priority){
this.Priority = priority;
}
@Override
public String getPriority() {
// TODO Auto-generated method stub
return Priority;
}
@Override
public void setPriority(String p) {
// TODO Auto-generated method stub
this.Priority = p;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public void fromString(String s) {
// TODO Auto-generated method stub
return ;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "[" + kind + " " + Priority
+ " " + content + "]";
}
}
PIMContact
public class PIMContact extends PIMEntity {
public static String kind = "CONTACT";
public String Priority; // each contact has a priority
public String firstName;
public String familyName;
public String emailAddress;
PIMContact() {
Priority = "normal";
}
public void set(String priority, String fir, String fam, String e){
this.Priority = priority;
this.firstName = fir;
this.familyName = fam;
this.emailAddress = e;
}
@Override
public String getPriority() {
// TODO Auto-generated method stub
return super.getPriority();
}
@Override
public void setPriority(String p) {
// TODO Auto-generated method stub
super.setPriority(p);
}
@Override
public void fromString(String s) {
// TODO Auto-generated method stub
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "[" + kind + " " + Priority
+ " " + firstName + " " + familyName + " " + emailAddress + "]";
}
}
PIMAppointment
import java.text.*;
import java.util.Date;
public class PIMAppointment extends PIMEntity implements MyDate{
public static String kind = "APPOINTMENT";
public String Priority; // each appointment has a priority
public String content; // each appointment has a content
public java.util.Date deadline = null; // each TODO has a deadline
PIMAppointment() {
Priority = "normal";
}
PIMAppointment(String priority){
this.Priority = priority;
}
@Override
public String getPriority() {
// TODO Auto-generated method stub
return super.getPriority();
}
@Override
public void setPriority(String p) {
// TODO Auto-generated method stub
this.Priority = p;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public java.util.Date getDeadline() {
return deadline;
}
public void setDeadline(java.util.Date deadline) {
this.deadline = deadline;
}
@Override
public void fromString(String s) {
// TODO Auto-generated method stub
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
// ParsePosition pos = new ParsePosition(0);
try {
this.deadline = formatter.parse(s);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "[" + kind + " " + Priority
+ " " + dateToStr(deadline) + " " + content + "]";
}
@Override
public String dateToStr(Date date) {
// TODO Auto-generated method stub
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
String str = format.format(date);
return str;
}
}
MyDate
public interface MyDate {
public String dateToStr(java.util.Date date);
}
PIMManager——测试类
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class PIMManager {
//save the 100 operation
public static Queue<String> operate = new LinkedList<String>();
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in); //创建Scanner对象
System.out.println("Welcome to PIM.");
String command = null;
while (!"Quit".equals(command)) {
int k = 1;
System.out.println("---Enter a command (suported commands are List Create Save Load Quit)---");
command = in.nextLine(); //获取输入的一行字符串
switch (command) {
case "List":
System.out.println("There are " + operate.size() + " items.");
if(operate.size() > 0)
for(String s:operate) {
System.out.println("Item "+ k + ": " + s);
k++;
}
break;
case "Create":
System.out.println("Enter an item type ( todo, note, contact or appointment )");
String subcmd = in.nextLine();
switch (subcmd) {
case "todo":
PIMTodo todo = new PIMTodo();
// input date(String format), then turn it into Date format
System.out.println("Enter date for todo item: ");
String dt = in.nextLine();
todo.fromString(dt);
// input specific content
System.out.println("Enter todo text:");
String ct = in.nextLine();
todo.setContent(ct);
// input priority
System.out.println("Enter todo priority:");
System.out.println("AVAILABLE: ex-urgent , urgent , normal, unrestricted");
String pr = in.nextLine();
todo.setPriority(pr);
String opt = todo.toString();
operate.add(opt);
break;
case "note":
PIMNote note = new PIMNote();
// input specific content
System.out.println("Enter note text:");
String ct1 = in.nextLine();
note.setContent(ct1);
// input priority
System.out.println("Enter note priority:");
System.out.println("AVAILABLE: ex-urgent , urgent , normal, unrestricted");
String pr1 = in.nextLine();
note.setPriority(pr1);
String opt1 = note.toString();
operate.add(opt1);
break;
case "appointment":
PIMAppointment appointment = new PIMAppointment();
// input date(String format), then turn it into Date format
System.out.println("Enter date for appointment item: ");
String dt1 = in.nextLine();
appointment.fromString(dt1);
// input specific content
System.out.println("Enter appointment text:");
String ct2 = in.nextLine();
appointment.setContent(ct2);
// input priority
System.out.println("Enter appointment priority:");
System.out.println("AVAILABLE: ex-urgent , urgent , normal, unrestricted");
String pr2 = in.nextLine();
appointment.setPriority(pr2);
String opt2 = appointment.toString();
operate.add(opt2);
break;
case "contact":
PIMContact contact = new PIMContact();
// input priority
System.out.println("Enter contact priority:");
System.out.println("AVAILABLE: ex-urgent , urgent , normal, unrestricted");
String pr3 = in.nextLine();
System.out.println("Enter contact firstname:");
String firn = in.nextLine();
System.out.println("Enter contact familyname:");
String famn = in.nextLine();
System.out.println("Enter contact email address:");
String ea = in.nextLine();
// initialize new PIMContact
contact.set(pr3, firn, famn, ea);
String opt3 = contact.toString();
operate.add(opt3);
break;
default:
System.out.println("Invalid Command! ");
break;
}
case "Save":
System.out.println("Items have been saved.");
break;
case "Load":
case "Quit":
break;
default:
System.out.println("Invalid Command! ");
break;
}
in.close();
}
}
-
如何解决在IDE环境下可以运行,而在命令行编译时出现错误: 找不到符号
改正方法: 退出到包外所在的目录。
编译: javac 包名/PIMManager.java
执行: 用 java 包名.PIMManager附: 其他解决办法:
https://blog.youkuaiyun.com/bcbobo21cn/article/details/45247869