food.java 3.71 KB
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() > 3) {
			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;
		}
	}
}