food.java
3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package food;
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Array;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.Scanner;
public class food {
static ArrayList<String> food = new ArrayList<String>();
static String[] forday = new String[10];
static String[] fordaycheck = new String[10];
static String[] foreat = new String[10];
static DateFormat TypeDate = new SimpleDateFormat("EEEE ·Õè dd");
public static void main(String[] args) throws FileNotFoundException {
int today = 100, yesterday = 99, twodayago = 98,threedayago=97;
File file = new File("food.txt");
Scanner scname = new Scanner(file);
Scanner sc = new Scanner(System.in);
String Inputday = sc.nextLine();
Random random = new Random();
String name;
// get food name
while (scname.hasNext()) {
name = scname.nextLine();
food.add(name);
}
boolean[] checkfood = new boolean[food.size()+1];
Calendar c = Calendar.getInstance();
// Set the calendar to monday of the current week
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
// Print dates of the current week starting on Monday to Friday
DateFormat CheckDate = new SimpleDateFormat("dd/M/yyyy");
for (int i = 0; i < 10; i++) {
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
forday[i] = TypeDate.format(c.getTime());
fordaycheck[i] = CheckDate.format(c.getTime());
if (dayOfWeek == Calendar.FRIDAY) { // If it's Friday so skip to
// Monday
c.add(Calendar.DATE, 3);
} else if (dayOfWeek == Calendar.SATURDAY) { // If it's Saturday
// skip to Monday
c.add(Calendar.DATE, 2);
} else {
c.add(Calendar.DATE, 1);
}
}
if (food.size() > 4) {
for (int j = 0; j < foreat.length; j++) {
today = random.nextInt(food.size());
checkfood[checkfood.length-1]=false;
checkfood = checkdupi(today,checkfood);
if(checkfood[checkfood.length-1]){
if (today == yesterday || today == twodayago||today == threedayago) {
j = j - 1;
checkfood[today]=false;
continue;
} else {
foreat[j] = forday[j] + " " + food.get(today);
}
threedayago = twodayago;
twodayago = yesterday;
yesterday = today;
}
else{
j = j - 1;
continue;
}
}
}
else {
System.out.println("cannot create list food [Need more food list] ");
System.exit(0);
}
selectdayforprint(Inputday);
}
//for print
public static void selectdayforprint(String Inputday){
if ("".equals(Inputday)) {
Date t = new Date();
int j = 0;
String x = TypeDate.format(t.getTime());
for (int i = 0; i < forday.length; i++) {
if (x.equals(forday[i])) {
j = i;
break;
}
}
for (int k = j; k < 10; k++) {
System.out.println(foreat[k]);
}
} else {
int j = -1;
for (int i = 0; i < fordaycheck.length; i++) {
if (Inputday.equals(fordaycheck[i])) {
j = i;
break;
}
}
if (j == -1) {
System.out.println("No Have Day");
} else {
for (int k = j; k < 10; k++)
System.out.println(foreat[k]);
}
}
}
//check dupicate food
public static boolean[] checkdupi(int food,boolean[] checkfood){
boolean checkfull = true;
if(checkfood[food] == false){
checkfood[food]=true;
checkfood[checkfood.length-1]=true;
checkfull=false;
}
else{
for(int i =0;i<checkfood.length-1;i++){
if(checkfood[i]==false){
checkfull=false;
break;
}
}
}
if(checkfull){
clearboolean(checkfood);
}
return checkfood;
}
public static void clearboolean (boolean[] checkfood){
for(int i=0;i<checkfood.length;i++){
checkfood[i]=false;
}
}
}