28. 16 Жишээ ЭхлээдГуанзны ажилчдад зориулсан классыг үүсгэе public abstract class CafeteriaWorker { protected String name; protected int startHour; protected int stopHour; protected CafeteriaWorker(String name, int startHour, int stopHour) { this.name = name; this.startHour = startHour; this.stopHour = stopHour; } public String getName() { return name; } public int getStartHour() { return startHour; } public int getStopHour() { return stopHour; } /** удамшсан класс бүр өөр өөрийнхөөр ажилдаг */ public abstract void work(); /** тухайн ажилтаны ажлын цаг мөн эсэхийг тогтоох */ protected boolean isWorkHour() { int currentHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); return currentHour >= startHour && currentHour < stopHour; } } “Гуанзны ажилтан” хийсвэр класс Гуанзны ажилтан бүр өөр өөрийн ажилтай тул “ажиллах” гэсэн функцыг хийсвэрээр зарласан байна
29. 17 Жишээ одооГуанзны ажилчдын зарим төрлийн албыг үүсгэе public class Cashier extends CafeteriaWorker { public Cashier(String name, int startHour, int stopHour) { super(name, startHour, stopHour); } public void work() { if (isWorkHour()) { System.out.println(getName() + " will now work at the cashier."); } else { System.err.println(getName() + " is not working at this time. " + "Cafeteria should be closed now."); } } } “Кассчин” бол “Гуанзны ажилтан” юм. public class Cleaner extends CafeteriaWorker { public Cleaner(String name, int startHour, int stopHour) { super(name, startHour, stopHour); } public void work() { if (isWorkHour()) { System.out.println(getName() + " will now collect the dishes from " + "the dinning area. When finished, " + getName() + " will clean the dishes."); } else { System.err.println(getName() + " is not working at this time."); } } } “Цэвэрлэгч” бол “Гуанзны ажилтан” юм.
30. 18 Жишээ Chefбол онцгой үүрэг гүйцэтгэх гуанзны ажилтан юм. Өмнө дурдсан ёсоор олон төрлийн тогооч (chef) байдаг . Ж нь: Baker – Талх, нарийн боов барьдаг тогооч HotMealCook – Нэг, хоёрдугаар хоол бэлтгэдэг тогооч SaladPrep – Зууш салат бэлтгэдэг тогооч MeatPrep –HotDog, CheeseBurger г.м бүтээгдэхүүн бэлтгэдэг тогооч PiroshkiPrep – Пирошки бэлтгэдэг тогооч public abstract class Chef extends CafeteriaWorker { protected Chef(String name, int startHour, int stopHour) { super(name, startHour, stopHour); } public void work() { if (isWorkHour()) { doWork(); } else { System.err.println(getName() + " is not working at this time."); } } protected abstract void doWork(); protected int getDayOfWeek() { return Calendar.getInstance().get(Calendar.DAY_OF_WEEK); } } “Тогооч” бол “Гуанзны ажилтан” юм.
31. 19 Жишээ Baker класс public class Baker extends Chef { public Baker(String name, int startHour, int stopHour) { super(name, startHour, stopHour); } protected void doWork() { switch (getDayOfWeek()) { case Calendar.MONDAY: case Calendar.TUESDAY: case Calendar.WEDNESDAY: System.out.println(getName() + " will bake 5 chocolate cakes today."); break; case Calendar.THURSDAY: case Calendar.FRIDAY: System.out.println(getName() + " will bake 3 loaves of bread and 2 " + "meat pies."); break; default: System.out.println(getName() + " will bake some surprises... :-)"); break; } } }
32. 20 Жишээ HotMealCook класс public class HotMealCook extends Chef { public HotMealCook(String name, int startHour, int stopHour) { super(name, startHour, stopHour); } protected void doWork() { switch (getDayOfWeek()) { case Calendar.MONDAY: case Calendar.WEDNESDAY: case Calendar.FRIDAY: System.out.println(getName() + " will cook fried rice, guliash " + "and noodles today."); break; case Calendar.TUESDAY: case Calendar.THURSDAY: System.out.println(getName() + " will cook vegetable soup, lamb " + "meat and curry today."); break; default: System.out.println(getName() + " will re-cook the leftovers " + "from this week... :-)"); break; } } }
33. 21 Жишээ SaladPrep класс public class SaladPrep extends Chef { public SaladPrep(String name, int startHour, int stopHour) { super(name, startHour, stopHour); } protected void doWork() { switch (getDayOfWeek()) { case Calendar.MONDAY: case Calendar.WEDNESDAY: case Calendar.TUESDAY: case Calendar.THURSDAY: System.out.println(getName() + " will make potato salad and " + "cabbage salad today."); break; case Calendar.FRIDAY: System.out.println(getName() + " will make potato salad, " + "cabbage salad and carrot salad today."); break; default: System.err.println(getName() + " will not work today. :-)"); break; } } }
34. 22 MeatPrep класс Жишээ public class MeatPrep extends Chef { public MeatPrep(String name, int startHour, int stopHour) { super(name, startHour, stopHour); } protected void doWork() { switch (getDayOfWeek()) { case Calendar.MONDAY: case Calendar.WEDNESDAY: case Calendar.FRIDAY: case Calendar.TUESDAY: case Calendar.THURSDAY: System.out.println(getName() + " prepare beef and lamb meat today."); break; default: System.err.println(getName() + " will not work today. :-)"); break; } } }
35. 23 PiroshkiPrep класс Мэдээж бид Гуанз классыг мөн тодорхойлох хэрэгтэй болно. Гуанз (Cafeteria) класс нь Гуанзны ажилчин (CafeteriaWorker) классыг өөртөө агуулна. “Гуанз бол хэд хэдэн төрлийн ажилчидтай”. Тэдгээр ажилчид нь өөр өөрийн үүрэгийг гүйцэтэснээр гуанзны үйл ажиллагаа хэвийн явагдана. Эндээс үзэхэд: Гуанз нь олон төрлийн ажилчидаас бүрдэнэ. Тэдгээр ажилтангууд нь Гуанз(cafeteria) классын бүрэлдэхүүн хэсэг болно. Жишээ public class PiroshkiPrep extends Chef { public PiroshkiPrep(String name, int startHour, int stopHour) { super(name, startHour, stopHour); } protected void doWork() { System.out.println(getName() + " will prepare pirshki now."); } }
36. 24 Cafeteria буюу Гуанз класс Жишээ public class Cafeteria { private String name; private int startHour; private int stopHour; private CafeteriaWorker[] workers; public Cafeteria(String name) { this.name = name; } public String getName() { return name; } public int getStartHour() { return startHour; } public void setStartHour(int startHour) { this.startHour = startHour; } public int getStopHour() { return stopHour; } public void setStopHour(int stopHour) { this.stopHour = stopHour; } public CafeteriaWorker[] getWorkers() { return workers; } public void setWorkers(CafeteriaWorker[] workers) { this.workers = workers; } public void callWorkersToWork() { for (int i = 0; i < workers.length; i++) { workers[i].work(); } } }
37. 25 Дээрх классуудыг тестлэх код бичье Жишээ public class TestClass { private static String DOS_CMD = "c:windowssystem32cmd.exe"; private static String TIME_CMD = DOS_CMD + " /k time "; public static void main(String[] args) throws Throwable { // гуанз объект үүсгэх Cafeteria guanz = new Cafeteria("Mongolian Univeristy Guanz"); guanz.setStartHour(8); guanz.setStopHour(16); // 10 ажилтан үүсгэх CafeteriaWorker[] workers = new CafeteriaWorker[11]; workers[0] = new Cashier("Narantuya", 8, 16); workers[1] = new Cleaner("Baatar", 10, 12); workers[2] = new Cleaner("Enkh-Munkh", 12, 16); workers[3] = new Cleaner("Erika", 10, 14); workers[4] = new Baker("Chuloo", 10, 15); workers[5] = new HotMealCook("Ganbat", 11, 15); workers[6] = new SaladPrep("Zaya", 8, 14); workers[7] = new SaladPrep("Sukhee", 8, 15); workers[8] = new MeatPrep("Zolbayar", 10, 15); workers[9] = new PiroshkiPrep("Bat-Erdene", 9, 14); workers[10] = new PiroshkiPrep("Erdene-Bat", 9, 14); guanz.setWorkers(workers); // Үргэлжлэл дараагийн хуудсанд
39. 27 Жишээ Гарах ҮР ДҮН: The time is now 08:00:00.... ===================================================== Narantuya is not working at this time. Cafeteria should be closed now. Baatar is not working at this time. Enkh-Munkh is not working at this time. Erika is not working at this time. Chuloo is not working at this time. Ganbat is not working at this time. Zaya is not working at this time. Sukhee is not working at this time. Zolbayar is not working at this time. Bat-Erdene is not working at this time. Erdene-Bat is not working at this time. The time is now 12:00:00.... ===================================================== Narantuya will now work at the cashier. Baatar will now collect the dishes from the dinning area. When finished, Baatar will clean the dishes. Enkh-Munkh is not working at this time. Erika will now collect the dishes from the dinning area. When finished, Erika will clean the dishes. Chuloo will bake 3 loaves of bread and 2 meat pies. Ganbat will cook vegitable soup, lamb meat and curry today. Zaya will make potato salad and cabbage salad today. Sukhee will make potato salad and cabbage salad today. Zolbayar prepare beef and lamb meat today. Bat-Erdene will prepare pirshki now. Erdene-Bat will prepare pirshki now. // Үргэлжлэл нь дараагийн хуудсанд
40. 28 Жишээ // Өмнөх хуудсын үргэлжлэл The time is now 15:00:00.... ===================================================== Narantuya will now work at the cashier. Baatar is not working at this time. Enkh-Munkh will now collect the dishes from the dinning area. When finished, Enkh-Munkh will clean the dishes. Erika is not working at this time. Chuloo will bake 3 loaves of bread and 2 meat pies. Ganbat will cook vegetable soup, lamb meat and curry today. Zaya is not working at this time. Sukhee will make potato salad and cabbage salad today. Zolbayar prepare beef and lamb meat today. Bat-Erdene is not working at this time. Erdene-Bat is not working at this time. Narantuya will now work at the cashier. Baatar is not working at this time. Enkh-Munkh will now collect the dishes from the dinning area. When finished, Enkh-Munkh will clean the dishes. Erika is not working at this time. Chuloo will bake 3 loaves of bread and 2 meat pies. Ganbat will cook vegitable soup, lamb meat and curry today. Zaya is not working at this time. Sukhee will make potato salad and cabbage salad today. Zolbayar prepare beef and lamb meat today. Bat-Erdene is not working at this time. Erdene-Bat is not working at this time. // Үргэлжлэл дараагийн хуудсанд...
41. 29 Жишээ // Үргэлжлэл нь өмнөх хуудсанд... The time is now 20:00:00.... ===================================================== Narantuya is not working at this time. Cafeteria should be closed now. Baatar is not working at this time. Enkh-Munkh is not working at this time. Erika is not working at this time. Chuloo is not working at this time. Ganbat is not working at this time. Zaya is not working at this time. Sukhee is not working at this time. Zolbayar is not working at this time. Bat-Erdene is not working at this time. Erdene-Bat is not working at this time. Don't forget to reset your clock! Энэ жишээнээс үзэхэд Гуанзны ажилтан классын объектууд үүсээгүй байхад гуанз ямар ч үйл ажиллагаа явагдахгүй болох нь харагдаж байна. Гуанз нь гуанзны ажилчдын ажлыг оноож өгдөг Гуанзны бүх ажилчид нь гуанзны үйл ажиллагааг явуулдаг- Бүрдмэл Тайлбар: Энэхүү жишээ нь java.util-ийн date/time-д зориулсан зарим багцыг ашигласан бөгөөд түүнд анхаарлаа хандуулахгүй байж болно