I have seen this question asked a few times, but none that addressed my specific issues. I feel like I am actually pretty close to finishing this up. I will post the whole question and then my code below. The problem that I am having is when I select Sunday (which is the first day of the week) as my starting day it doesn't know how to get back to Saturday, which is the previous week. Also the adding of days doesn't seem to be working correctly. Looking for some hints on how to get this working. The code compiles and runs, it's just my code isn't doing what I want it to. the thing is, when I use the test option of my code everything seems to work correctly, I just can't get it to work with the user entered data.
the full question is: Design and implement the class Day that implements the day of the week in a program. The class Day should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of type Day:
A. Set the day.
B. Print the day.
C. Return the day.
D. Return the next day.
E. Return the previous day.
F. Calculate and return the day by adding certain days to the current day. For example, if the current day is Monday and we add four days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday.
G. Add the appropriate constructors.
H. Write the definitions of the methods to implement the operations for the class Day, as defined in A through G.
I. Write a program to test various operations on the class Day.
CODE:
import java.util.*;
public class Day
{
private static final int SUNDAY = 0;
private static final int MONDAY = 1;
private static final int TUESDAY = 2;
private static final int WEDNESDAY = 3;
private static final int THURSDAY = 4;
private static final int FRIDAY = 5;
private static final int SATURDAY = 6;
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
Day dy = new Day(Day.WEDNESDAY);
int choice;
do
{
System.out.println("Welcome, to my final project please select
an option.\n");
menu2();
choice = keyboard.nextInt();
System.out.println();
switch (choice)
{
case 1:
do
{
menu1();
choice = keyboard.nextInt();
System.out.println();
switch (choice)
{
case 1:
day = SUNDAY;
break;
case 2:
day = MONDAY;
break;
case 3:
day = TUESDAY;
break;
case 4:
day = WEDNESDAY;
break;
case 5:
day = THURSDAY;
break;
case 6:
day = FRIDAY;
break;
case 7:
day = SATURDAY;
break;
case 99:
System.exit(0);
break;
default:
System.out.println("Invalid Input");
}
System.out.print("The day you selected as your starting
day is: ");
dy.print();
System.out.println();
System.out.print("The next day to your selected day is: ");
dy.setDay(dy.getNextDay());
dy.print();
System.out.println();
System.out.print("The previous day to your selected
day is: ");
dy.setDay(dy.getPreviousDay());
dy.setDay(dy.getPreviousDay());
dy.print();
System.out.println();
System.out.print("How many days would you like to add? ");
int days = keyboard.nextInt();
dy.setDay(days);
System.out.print("\nAdding " + days + " day(s) makes your
new day: ");
dy.print();
System.out.println();
}
while (choice != 99);
break;
case 2:
System.out.println("Test Data For Day Class");
System.out.print("\nInitial day: ");
dy = new Day(Day.SUNDAY);
dy.print();
System.out.print("\nNext day: ");
dy.setDay(dy.getNextDay());
dy.print();
System.out.print("\nAdd 12 Days: ");
dy.setDay(dy.addDays(12));
dy.print();
System.out.print("\nPrevious day: ");
dy.setDay(dy.getPreviousDay());
dy.print();
System.out.print("\nAdd 3 days: ");
dy.setDay(dy.addDays(3));
dy.print();
System.out.println("\n\n");
break;
case 99:
System.exit(0);
break;
default:
System.out.println("Invalid Input");
}
}
while (choice != 99);
}
public static void menu1()
{
System.out.println("Please Select Your Initial Day (or 99 to quit):");
System.out.println(" 1: Sunday.");
System.out.println(" 2: Monday.");
System.out.println(" 3: Tuesday.");
System.out.println(" 4: Wednesday.");
System.out.println(" 5: Thursday.");
System.out.println(" 6: Friday.");
System.out.println(" 7: Saturday.");
System.out.println("99: To quit the program.");
}
public static void menu2()
{
System.out.println("Enter: ");
System.out.println("1: To Enter Data into the program.");
System.out.println("2: For Test Data.");
System.out.println("99: To quit the program.");
}
//Stores the day
public static int day;
//Set the day.
public void setDay(int day)
{
this.day = day;
}
//Print the day
public void print()
{
System.out.println(this.toString());
}
//Return the day.
public int getDay()
{
return day;
}
//Return the next day.
public int getNextDay() {
return (day + 1) % 7;
}
@Override
public String toString() {
switch (Day.day) {
case SUNDAY:
return "Sunday";
case MONDAY:
return "Monday";
case TUESDAY:
return "Tuesday";
case WEDNESDAY:
return "Wednesday";
case THURSDAY:
return "Thursday";
case FRIDAY:
return "Friday";
case SATURDAY:
return "Saturday";
}
return "";
}
//Return the previous day.
public int getPreviousDay()
{
return (day - 1) % 7;
}
//Calculate and return the day by adding days
public int addDays(int days)
{
return (day + days) % 7;
}
//Add the constructors.
public Day()
{
this.day = SUNDAY;
}
public Day(int day)
{
this.day = day;
}
}
Aucun commentaire:
Enregistrer un commentaire