-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobotFactoryManagementSystem.java
More file actions
173 lines (133 loc) · 4.72 KB
/
Copy pathRobotFactoryManagementSystem.java
File metadata and controls
173 lines (133 loc) · 4.72 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Robot{
public String id;
public Robot(String id){
this.id = id;
}
public void Performtask(){
System.out.println("-----------");
}
@Override
public String toString() {
return "Robot ID: " + id;
}
}
class PaintingRobot extends Robot{
public PaintingRobot(String id){
super(id);
}
@Override
public void Performtask(){
super.Performtask();
System.out.println("PaintingRobot - ID: " + id + " is Painting components ");
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
class WieldingRobot extends Robot{
public WieldingRobot(String id){
super(id);
}
@Override
public void Performtask(){
super.Performtask();
System.out.println("WeldingRobot - ID: " + id + " is Welding metal parts");
}
}
class Worker extends Thread{
public String name;
public String robottype;
public int count=0;
public boolean running = true;
public Worker(String name,String robottype){
this.name = name;
this.robottype = robottype;
}
public void run(){
for(int i = 1 ; i <= 4 ; i++){
try{
Thread.sleep(100);
System.out.println(name + " is building " + robottype + "[robots made: "+ count +"]");
count++;
}
catch(InterruptedException e){
System.out.println(e);
}
}
System.out.println(name + " has completed his tasks [Robots made:" + count + " Robot type: "+ robottype +"]");
}
}
class RobotFactory {
private String name;
private List<Robot> robots;
public RobotFactory(String name) {
this.name = name;
this.robots = new ArrayList<>();
}
public void addRobot(Robot robot) {
robots.add(robot);
}
public List<Robot> getRobots() {
return robots;
}
}
public class RobotFactoryManagementSystem {
public static void main(String[] args) {
RobotFactory factory = new RobotFactory("Advanced Robotics Inc.");
// Create and equip robots
Robot robot1 = new PaintingRobot("R2-D2");
Robot robot2 = new WieldingRobot("WALL-E");
Robot robot3 = new PaintingRobot("C-3PO");
factory.addRobot(robot1);
factory.addRobot(robot2);
factory.addRobot(robot3);
Worker w1 = new Worker("bob","paintingrobot");
Worker w2 = new Worker("mick","wieldingrobot");
Scanner src = new Scanner(System.in);
while(true){
System.out.println("\n=== Robot Control Panel ===");
System.out.println("1. List all robots");
System.out.println("2. Assign task to robot");
System.out.println("3. Assign task to Workers");
System.out.println("4. Exit");
System.out.print("Choose option: ");
int choice = src.nextInt();
switch(choice){
case 1:
System.out.println("\nAvailable Robots:");
for (Robot robot : factory.getRobots()) {
System.out.println(robot);
}
break;
case 2:
System.out.println("\nAssigning tasks:");
robot1.Performtask();
robot2.Performtask();
robot3.Performtask();
break;
case 3:
System.out.println("\nAssigning tasks:");
w1.start();
w2.start();
try {
w1.join(); // Wait for w1 to finish
w2.join(); // Wait for w2 to finish
} catch (InterruptedException e) {
System.out.println("Main thread interrupted while waiting for workers.");
}
break;
case 4:
System.out.println("\n Exiting.....");
src.close();
return;
default:
System.out.println("Invalid option!");
}
}
}
}