所以你们现在可能都厌倦了我的问题。但是我超级卡住了。我有一个日历类,一个日历测试器类和一个事件类。我在日历类中有一个HashMap,我从事件类中放入了它。问题是,只有事件类有其信息。如果我尝试从它存储的类或测试器类中访问/打印HashMap,我会得到一个空的HashMap。我似乎已经在事件类中找出了问题(标有令人讨厌的*' s)。但我不知道如何继续或为什么这会影响我的其他2个课程而不是这个课程。有没有人有任何想法?
MyCalendarTester.java
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;
import java.util.Map.Entry;
enum MONTHS
{
January, February, March, April, May, June, July, August, September, October, November, December;
}
enum DAYS
{
Su, Mo, Tu, We, Th, Fr, Sa;
}
public class MyCalendarTester {
static MONTHS[] arrayOfMonths = MONTHS.values();
static DAYS[] arrayOfDays = DAYS.values();
static MyCalendar myCal = new MyCalendar();
static Event myEvent = new Event(null, null, null);
public static void main(String[] args) {
// TODO Auto-generated method stub
GregorianCalendar cal = new GregorianCalendar(); // capture today
printCalendar(cal);
menuHandler();
System.out.println("\nI think we're done here!");
}
public static void printCalendar(Calendar cal){
GregorianCalendar dayCounter = new GregorianCalendar(); // capture today
dayCounter.set(Calendar.DAY_OF_MONTH, 1);
int formatCounter = 0;
System.out.println(" " + arrayOfMonths[cal.get(Calendar.MONTH) ] + " " + cal.get(Calendar.YEAR)); //prints the month and year
for(int i = 0; i < arrayOfDays.length; i++){
if(i == 0){
System.out.print(arrayOfDays[i]);
}
else{
System.out.print(" " + arrayOfDays[i]);
}
}//print days of week
System.out.println();
for(int i = 0; i < arrayOfDays.length; i++){
if(!arrayOfDays[i].equals(arrayOfDays[dayCounter.get(Calendar.DAY_OF_WEEK) - 1])){
System.out.print(" ");
formatCounter++;
}
else{
System.out.print(" " + cal.getActualMinimum(Calendar.DAY_OF_MONTH) + " ");
formatCounter++;
break;
}
}
for(int i = 1; i < cal.getMaximum(Calendar.DAY_OF_MONTH); i++){
if(formatCounter == 7){
System.out.println();
formatCounter = 0; //reset counter
}
dayCounter.roll(Calendar.DAY_OF_MONTH, true);
if(dayCounter.get(Calendar.DATE) == cal.get(Calendar.DATE)){
System.out.print("[" + dayCounter.get(Calendar.DATE) + "]");
formatCounter++;
}
else if(dayCounter.get(Calendar.DATE) <= 9){
System.out.print(" " + dayCounter.get(Calendar.DATE) + " ");
formatCounter++;
}
else{
System.out.print(dayCounter.get(Calendar.DATE) + " ");
formatCounter++;
}
}
}
public static void menuHandler(){
Scanner sc = new Scanner(System.in);
System.out.println("\nSelect one of the following options:");
System.out.println("[L]oad [V]iew by [C]reate [G]o to [E]vent list [D]elete [Q]uit");
char userChoice = sc.next().charAt(0);
switch(Character.toUpperCase(userChoice)){
case 'L':
case 'V':
myCal.viewCalendar();
menuHandler();
case 'C':
myEvent.createEvent();
System.out.println("Here is what is in the HashMap103:");
System.out.println("HashMap size = " + myCal.getMyCalHash().size());
for (Entry<GregorianCalendar, Event> entry : myCal.getMyCalHash().entrySet()) {
Event value = entry.getValue();
System.out.println("key, " + entry.getKey().get(Calendar.MONTH) + "/" + entry.getKey().get(Calendar.DATE) + "/" + entry.getKey().get(Calendar.YEAR) +
" value: " + value.eventName + " " + value.startTime.get(Calendar.HOUR_OF_DAY) + ":" +
value.startTime.get(Calendar.MINUTE) + " " + value.endTime.get(Calendar.HOUR_OF_DAY) + ":" + value.endTime.get(Calendar.MINUTE));
}
menuHandler();
case 'G':
myEvent.goTo();
menuHandler();
case 'E':
myEvent.eventList();
menuHandler();
case 'D':
myEvent.deleteEvent();
menuHandler();
case 'Q': return;
}
}
}
MyCalendar.java
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Map.Entry;
public class MyCalendar {
GregorianCalendar calendar = new GregorianCalendar();
private HashMap<GregorianCalendar, Event> myCalHash = new HashMap<GregorianCalendar, Event>();
GregorianCalendar dayCounter = new GregorianCalendar(); // capture today
Scanner sc = new Scanner(System.in);
static MONTHS[] arrayOfMonths = MONTHS.values();
static DAYS[] arrayOfDays = DAYS.values();
MyCalendar(){
System.out.println("myCalendar()!!");
}
public HashMap<GregorianCalendar, Event> getMyCalHash(){
System.out.println("myCalHash.size()23 = " + myCalHash.size());
return myCalHash;
}
public void setMyCalHash(HashMap<GregorianCalendar, Event> myCalHash) {
this.myCalHash = myCalHash;
}
public void viewCalendar() {
System.out.println("myCalHash.size() = " + myCalHash.size()); // 0
System.out.print("[D]ay view or [M]view? ");
char userChoice = sc.next().charAt(0);
if(Character.toUpperCase(userChoice) == 'D'){ dayView(); }
else if(Character.toUpperCase(userChoice) == 'M'){ monthView(); }
else{
System.out.println("Invalid choice.");
}
}
public void dayView(){
//print day calendar
//GregorianCalendar dayCounter = new GregorianCalendar(); // capture today
System.out.println("myCalHash.size() = " + myCalHash.size()); // 0
System.out.println(arrayOfDays[calendar.get(Calendar.DAY_OF_WEEK) - 1] + ", " + arrayOfMonths[calendar.get(Calendar.MONTH)]
+ " " + calendar.get(Calendar.DATE) + ", " + calendar.get(Calendar.YEAR));
System.out.print("[P]revious or [N]ext or [M]ain menu ? ");
char userChoice = sc.next().charAt(0);
if(Character.toUpperCase(userChoice) == 'P'){
calendar.add(Calendar.DAY_OF_MONTH, -1);
dayView();
}
else if(Character.toUpperCase(userChoice) == 'N'){
calendar.add(Calendar.DAY_OF_MONTH, 1);
dayView();
}
else if(Character.toUpperCase(userChoice) == 'M'){
return;
}
else{
System.out.println("Invalid choice.");
return;
}
}
public void monthView(){
//print month calendar
int formatCounter = 0;
System.out.println("myCalHash.size() = " + myCalHash.size()); // 0
dayCounter.set(Calendar.DAY_OF_MONTH, 1);
System.out.println(" " + arrayOfMonths[calendar.get(Calendar.MONTH) ] + " " + calendar.get(Calendar.YEAR)); //prints the month and year
for(int i = 0; i < arrayOfDays.length; i++){
if(i == 0){
System.out.print(arrayOfDays[i]);
}
else{
System.out.print(" " + arrayOfDays[i]);
}
}//print days of week
System.out.println();
for(int i = 0; i < arrayOfDays.length; i++){
if(!arrayOfDays[i].equals(arrayOfDays[dayCounter.get(Calendar.DAY_OF_WEEK) - 1])){
System.out.print(" ");
formatCounter++;
}
else{
System.out.print(" " + calendar.getActualMinimum(Calendar.DAY_OF_MONTH) + " ");
formatCounter++;
break;
}
}
for(int i = 1; i < dayCounter.getActualMaximum(Calendar.DAY_OF_MONTH); i++){
if(formatCounter == 7){
System.out.println();
formatCounter = 0; //reset counter
}
dayCounter.roll(Calendar.DAY_OF_MONTH, true);
if(dayCounter.get(Calendar.DATE) <= 9){
System.out.print(" " + dayCounter.get(Calendar.DATE) + " ");
formatCounter++;
}
else{
System.out.print(dayCounter.get(Calendar.DATE) + " ");
formatCounter++;
}
}
System.out.print("\n[P]revious or [N]ext or [M]ain menu ? ");
char userChoice = sc.next().charAt(0);
if(Character.toUpperCase(userChoice) == 'P'){
calendar.add(Calendar.MONTH, -1);
dayCounter.add(Calendar.MONTH, -1);
monthView();
}
else if(Character.toUpperCase(userChoice) == 'N'){
calendar.add(Calendar.MONTH, 1);
dayCounter.add(Calendar.MONTH, 1);
monthView();
}
else if(Character.toUpperCase(userChoice) == 'M'){
return;
}
else{
System.out.println("Invalid choice.");
return;
}
}
}
Event.java
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Scanner;
public class Event {
String eventName;
GregorianCalendar startTime;
GregorianCalendar endTime;
MyCalendar mCal = new MyCalendar();
Event(String eventName, GregorianCalendar startTime, GregorianCalendar endTime){
this.eventName = eventName;
this.startTime = startTime;
this.endTime = endTime;
} //end Event Constructor
public void createEvent(){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the name of the event: ");
String name = sc.nextLine();
System.out.print("Date of event: ");
String dateAsString = sc.nextLine();
String startTime = null;
String endTime = null;
GregorianCalendar calDate = new GregorianCalendar();
String[] dateArr = dateAsString.split("/");
calDate.set(GregorianCalendar.YEAR, Integer.parseInt(dateArr[2]));
calDate.set(GregorianCalendar.MONTH, Integer.parseInt(dateArr[0]));
calDate.set(GregorianCalendar.DATE, Integer.parseInt(dateArr[1]));
System.out.print("Enter starting time and ending time(24 hour format, comma separated):");
String times = sc.nextLine();
// checks if user entered both start and end time
if(times.contains(",")){
String[] time = times.split(",");
startTime = time[0];
endTime = time[1];
GregorianCalendar calSTime = new GregorianCalendar();
String[] sTime = startTime.split(":");
calSTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(sTime[0]));
calSTime.set(Calendar.MINUTE, Integer.parseInt(sTime[1]));
GregorianCalendar calETime = new GregorianCalendar();
String[] eTime = endTime.split(":");
calETime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(eTime[0]));
calETime.set(Calendar.MINUTE, Integer.parseInt(eTime[1]));
//*********************
Event myEvent = new Event(name, calSTime, calETime); // causing a new instance of myCalendar???
//*********************
mCal.getMyCalHash().put(calDate, myEvent); //adding date and event to HashMap
}else{ //if user did not specify an event end time
startTime = times;
endTime = null;
GregorianCalendar calSTime = new GregorianCalendar();
String[] sTime = startTime.split(":");
calSTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(sTime[0]));
calSTime.set(Calendar.MINUTE, Integer.parseInt(sTime[1]));
Event myEvent = new Event(name, calSTime, null);
mCal.getMyCalHash().put(calDate, myEvent);//adding date and event to HashMap
}
System.out.println("Here is what is in the HashMap73:");
System.out.println("HashMap size = " + mCal.getMyCalHash().size());
for (Entry<GregorianCalendar, Event> entry : mCal.getMyCalHash().entrySet()) {
Event value = entry.getValue();
System.out.println("key, " + entry.getKey().get(Calendar.MONTH) + "/" + entry.getKey().get(Calendar.DATE) + "/" + entry.getKey().get(Calendar.YEAR) +
" value: " + value.eventName + " " + value.startTime.get(Calendar.HOUR_OF_DAY) + ":" +
value.startTime.get(Calendar.MINUTE) + " " + value.endTime.get(Calendar.HOUR_OF_DAY) + ":" + value.endTime.get(Calendar.MINUTE));
}
}
public void deleteEvent(){
Scanner sc = new Scanner(System.in);
System.out.println("[S]elected or [A]ll ?");
char userChoice = sc.next().charAt(0);
if(Character.toUpperCase(userChoice) == 'S'){
//delete all events on the day
Scanner nsc = new Scanner(System.in);
System.out.println("Enter the date: ");
String deleteDate = nsc.nextLine();
String[] parsed = deleteDate.split("/");
System.out.println("Integer.parseInt(parsed[0] = " + Integer.parseInt(parsed[0]));
System.out.println("Integer.parseInt(parsed[1] = " + Integer.parseInt(parsed[1]));
System.out.println("Integer.parseInt(parsed[2] = " + Integer.parseInt(parsed[2]));
for (Entry<GregorianCalendar, Event> entry : mCal.getMyCalHash().entrySet()) {
System.out.println("entry.getKey().get(Calendar.MONTH) = " + entry.getKey().get(Calendar.MONTH));
System.out.println("entry.getKey().get(Calendar.DATE) = " + entry.getKey().get(Calendar.DATE));
System.out.println("entry.getKey().get(Calendar.YEAR) = " + entry.getKey().get(Calendar.YEAR));
if(entry.getKey().get(Calendar.MONTH) == Integer.parseInt(parsed[0])
&& entry.getKey().get(Calendar.DATE) == Integer.parseInt(parsed[1])
&& entry.getKey().get(Calendar.YEAR) == Integer.parseInt(parsed[2]) ){
mCal.getMyCalHash().remove(entry.getKey());
System.out.println("HashMap size = " + mCal.getMyCalHash().size());
}
}
}
else if(Character.toUpperCase(userChoice) == 'A'){
mCal.getMyCalHash().clear();
System.out.println("HashMap size = " + mCal.getMyCalHash().size());
}
else{
System.out.println("Invalid choice.");
}
}
public void eventList(){
if(mCal.getMyCalHash().equals(null)){
System.out.println("Your calendar is empty!");
}
else{
System.out.println("Here are your events: ");
for (Entry<GregorianCalendar, Event> entry : mCal.getMyCalHash().entrySet()) {
Event value = entry.getValue();
System.out.println(value.eventName + " " + value.startTime.get(Calendar.HOUR_OF_DAY) + ":" +
value.startTime.get(Calendar.MINUTE) + " " + value.endTime.get(Calendar.HOUR_OF_DAY)
+ ":" + value.endTime.get(Calendar.MINUTE));
}
}
}
public void goTo(){
System.out.print("Enter a date in MM/DD/YYYY format: ");
Scanner sc = new Scanner(System.in);
String userDate = sc.nextLine();
String[] dateArr = userDate.split("/");
// dayCounter.set(GregorianCalendar.YEAR, Integer.parseInt(dateArr[2]));
// dayCounter.set(GregorianCalendar.MONTH, Integer.parseInt(dateArr[0]) - 1);
// dayCounter.set(GregorianCalendar.DATE, Integer.parseInt(dateArr[1]));
// System.out.println(arrayOfDays[dayCounter.get(Calendar.DAY_OF_WEEK) - 1] + ", " +
// arrayOfMonths[dayCounter.get(Calendar.MONTH)] + " " + dayCounter.get(Calendar.DATE) + ", " + dayCounter.get(Calendar.YEAR));
for (Entry<GregorianCalendar, Event> entry : mCal.getMyCalHash().entrySet()) {
// System.out.println("entry.getKey().get(Calendar.MONTH) = " + entry.getKey().get(Calendar.MONTH));
// System.out.println("Integer.parseInt(dateArr[0]) = " + Integer.parseInt(dateArr[0]));
// System.out.println("entry.getKey().get(Calendar.DATE) = " + entry.getKey().get(Calendar.DATE));
// System.out.println("Integer.parseInt(dateArr[1]) = " + Integer.parseInt(dateArr[1]) );
if(entry.getKey().get(Calendar.MONTH) == Integer.parseInt(dateArr[0])
&& entry.getKey().get(Calendar.DATE) == Integer.parseInt(dateArr[1])
&& entry.getKey().get(Calendar.YEAR) == Integer.parseInt(dateArr[2]) ){
Event value = entry.getValue();
System.out.println(value.eventName + " " + value.startTime.get(Calendar.HOUR_OF_DAY) + ":" +
value.startTime.get(Calendar.MINUTE) + " " + value.endTime.get(Calendar.HOUR_OF_DAY)
+ ":" + value.endTime.get(Calendar.MINUTE));
}
}
}
}
我不确定我问这个问题的方式是否与本论坛规则一致,所以如果我做错了,请告诉我。但我无法想出一个更好的方法来清楚地说明我的所有代码。