forked from ZCW-Summer25/JavaFundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopic.jsh
More file actions
582 lines (471 loc) · 16.8 KB
/
topic.jsh
File metadata and controls
582 lines (471 loc) · 16.8 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
// Nested Objects Practice - Objects containing other objects
import java.util.ArrayList;
import java.util.HashMap;
// Exercise 1: Basic Object Composition - Address and Person
class Address {
String street;
String city;
String state;
String zipCode;
public Address(String street, String city, String state, String zipCode) {
// TODO: Initialize all fields
}
public String getFullAddress() {
// TODO: Return formatted address: "123 Main St, Springfield, IL 62701"
return "";
}
public boolean isInState(String state) {
// TODO: Check if address is in given state (case insensitive)
return false;
}
public String toString() {
// TODO: Return readable address format
return "";
}
}
class Person {
String name;
int age;
Address homeAddress;
Address workAddress; // Can be null
public Person(String name, int age, Address homeAddress) {
// TODO: Initialize name, age, homeAddress
}
public void setWorkAddress(Address workAddress) {
// TODO: Set work address
}
public double getDistanceToWork() {
// TODO: Return 0.0 if same city, 10.0 if different city, 50.0 if different state
// Return 0.0 if no work address
return 0.0;
}
public void relocate(Address newAddress) {
// TODO: Change home address
}
public String getAddressSummary() {
// TODO: Return string with both addresses
// Format: "Home: [address], Work: [address]" or "Home: [address], Work: None"
return "";
}
public boolean livesAndWorksInSameState() {
// TODO: Return true if both addresses in same state
// Return false if no work address
return false;
}
}
// Exercise 2: Collection of Objects - Course, Student, University
class Course {
String courseCode;
String courseName;
int credits;
ArrayList<Student> enrolledStudents;
public Course(String courseCode, String courseName, int credits) {
// TODO: Initialize fields, create empty student list
}
public boolean enrollStudent(Student student) {
// TODO: Add student if not already enrolled and under 30 students
// Return true if successful
return false;
}
public boolean dropStudent(Student student) {
// TODO: Remove student from course
// Return true if student was enrolled
return false;
}
public int getEnrollmentCount() {
// TODO: Return number of enrolled students
return 0;
}
public ArrayList<String> getStudentNames() {
// TODO: Return list of student names
return new ArrayList<>();
}
public boolean hasStudent(String studentName) {
// TODO: Check if student is enrolled by name
return false;
}
public double getAverageGPA() {
// TODO: Return average GPA of enrolled students
// Return 0.0 if no students
return 0.0;
}
}
class Student {
String name;
String studentId;
double gpa;
ArrayList<Course> enrolledCourses;
public Student(String name, String studentId, double gpa) {
// TODO: Initialize fields, create empty course list
}
public boolean enrollInCourse(Course course) {
// TODO: Add course if not already enrolled and under 6 courses
// Also enroll this student in the course
// Return true if successful
return false;
}
public boolean dropCourse(Course course) {
// TODO: Remove course and drop student from course
// Return true if student was enrolled in course
return false;
}
public int getTotalCredits() {
// TODO: Sum credits from all enrolled courses
return 0;
}
public ArrayList<String> getCourseList() {
// TODO: Return list of course names
return new ArrayList<>();
}
public boolean isFullTimeStudent() {
// TODO: Return true if >= 12 credits
return false;
}
public String toString() {
// TODO: Return student info with courses
return "";
}
}
class University {
String name;
ArrayList<Student> students;
ArrayList<Course> courses;
public University(String name) {
// TODO: Initialize name and empty lists
}
public void addStudent(Student student) {
// TODO: Add student to university
}
public void addCourse(Course course) {
// TODO: Add course to university
}
public Student findStudent(String studentId) {
// TODO: Return student with matching ID or null
return null;
}
public Course findCourse(String courseCode) {
// TODO: Return course with matching code or null
return null;
}
public String getUniversityStats() {
// TODO: Return string with total students, courses, and total enrollments
return "";
}
public ArrayList<Student> getStudentsInCourse(String courseCode) {
// TODO: Return list of students enrolled in specific course
return new ArrayList<>();
}
}
// Exercise 3: Hierarchical Object Structure - Room, Floor, Building
class Room {
String roomNumber;
String roomType;
int capacity;
boolean isOccupied;
public Room(String roomNumber, String roomType, int capacity) {
// TODO: Initialize fields, set isOccupied to false
}
public boolean occupyRoom() {
// TODO: Set as occupied if not already occupied
// Return true if successful
return false;
}
public boolean vacateRoom() {
// TODO: Set as vacant if currently occupied
// Return true if successful
return false;
}
public boolean canAccommodate(int people) {
// TODO: Check if room can fit people and is not occupied
return false;
}
public String getRoomInfo() {
// TODO: Return formatted room information with all details
return "";
}
public String toString() {
// TODO: Return "Room [number]: [type] (capacity: [capacity])"
return "";
}
}
class Floor {
int floorNumber;
ArrayList<Room> rooms;
String floorType;
public Floor(int floorNumber, String floorType) {
// TODO: Initialize fields, create empty room list
}
public void addRoom(Room room) {
// TODO: Add room to floor
}
public boolean removeRoom(String roomNumber) {
// TODO: Remove room by number, return true if found and removed
return false;
}
public Room findRoom(String roomNumber) {
// TODO: Return room with matching number or null
return null;
}
public ArrayList<Room> getAvailableRooms() {
// TODO: Return list of unoccupied rooms
return new ArrayList<>();
}
public int getTotalCapacity() {
// TODO: Sum capacity of all rooms on floor
return 0;
}
public double getOccupancyRate() {
// TODO: Return percentage of occupied rooms (0.0 to 1.0)
return 0.0;
}
public String getFloorSummary() {
// TODO: Return string with floor stats
return "";
}
}
class Building {
String buildingName;
String address;
ArrayList<Floor> floors;
public Building(String buildingName, String address) {
// TODO: Initialize fields, create empty floor list
}
public void addFloor(Floor floor) {
// TODO: Add floor to building
}
public boolean removeFloor(int floorNumber) {
// TODO: Remove floor by number, return true if found and removed
return false;
}
public Floor findFloor(int floorNumber) {
// TODO: Return floor with matching number or null
return null;
}
public Room findRoom(String roomNumber) {
// TODO: Search all floors for room with matching number
return null;
}
public String getBuildingStats() {
// TODO: Return comprehensive building statistics
return "";
}
public ArrayList<Room> getAllAvailableRooms() {
// TODO: Return all unoccupied rooms in entire building
return new ArrayList<>();
}
public boolean reserveRoom(String roomNumber) {
// TODO: Find and occupy specific room
// Return true if room was found and successfully occupied
return false;
}
}
// Exercise 4: Financial System - Transaction, Account, Customer, Bank
class Transaction {
String transactionId;
String type; // "DEBIT" or "CREDIT"
double amount;
String date;
String description;
public Transaction(String transactionId, String type, double amount, String date, String description) {
// TODO: Initialize all fields
}
public boolean isDebit() {
// TODO: Return true if type is "DEBIT"
return false;
}
public boolean isCredit() {
// TODO: Return true if type is "CREDIT"
return false;
}
public String getTransactionSummary() {
// TODO: Return formatted transaction string
return "";
}
public String toString() {
// TODO: Return transaction details
return "";
}
}
class Account {
String accountNumber;
String accountType;
double balance;
ArrayList<Transaction> transactions;
public Account(String accountNumber, String accountType) {
// TODO: Initialize fields, balance starts at 0, create empty transaction list
}
public void deposit(double amount, String description) {
// TODO: Add money, create CREDIT transaction
// Transaction ID can be simple: "TXN" + transaction count
}
public boolean withdraw(double amount, String description) {
// TODO: Remove money if sufficient funds, create DEBIT transaction
// Return true if successful
return false;
}
public double getBalance() {
// TODO: Return current balance
return 0.0;
}
public ArrayList<Transaction> getTransactionHistory() {
// TODO: Return copy of transactions list
return new ArrayList<>();
}
public int getTransactionCount() {
// TODO: Return number of transactions
return 0;
}
public String getMonthlyStatement() {
// TODO: Return string summary of recent transactions
return "";
}
public double calculateInterest(double rate) {
// TODO: Return interest amount for current balance
return 0.0;
}
}
class Customer {
String customerId;
String name;
String email;
ArrayList<Account> accounts;
public Customer(String customerId, String name, String email) {
// TODO: Initialize fields, create empty account list
}
public void addAccount(Account account) {
// TODO: Add account to customer
}
public boolean removeAccount(String accountNumber) {
// TODO: Remove account by number, return true if found and removed
return false;
}
public Account findAccount(String accountNumber) {
// TODO: Return account with matching number or null
return null;
}
public double getTotalBalance() {
// TODO: Sum balances of all accounts
return 0.0;
}
public String getAccountSummary() {
// TODO: Return string with all account info
return "";
}
public boolean hasAccount(String accountNumber) {
// TODO: Check if customer has account with given number
return false;
}
public boolean transferBetweenAccounts(String fromAccount, String toAccount, double amount) {
// TODO: Transfer within customer's accounts
// Return true if successful
return false;
}
}
class Bank {
String bankName;
ArrayList<Customer> customers;
HashMap<String, Account> allAccounts;
public Bank(String bankName) {
// TODO: Initialize fields
}
public void addCustomer(Customer customer) {
// TODO: Add customer to bank and add all customer accounts to allAccounts map
}
public Customer findCustomer(String customerId) {
// TODO: Return customer with matching ID or null
return null;
}
public Account findAccount(String accountNumber) {
// TODO: Return account from allAccounts map
return null;
}
public double getTotalDeposits() {
// TODO: Sum all account balances
return 0.0;
}
public int getCustomerCount() {
// TODO: Return number of customers
return 0;
}
public int getAccountCount() {
// TODO: Return total number of accounts
return 0;
}
public String getBankReport() {
// TODO: Return comprehensive bank statistics
return "";
}
public boolean processInterPayment(String fromAccount, String toAccount, double amount) {
// TODO: Transfer between any accounts in the bank
// Return true if successful
return false;
}
}
// Test your nested object implementations:
// Test Exercise 1: Address and Person
System.out.println("=== Testing Address and Person ===");
Address homeAddr = new Address("123 Main St", "Springfield", "IL", "62701");
Address workAddr = new Address("456 Oak Ave", "Chicago", "IL", "60601");
Person person = new Person("Alice Johnson", 30, homeAddr);
person.setWorkAddress(workAddr);
System.out.println("Full home address: " + homeAddr.getFullAddress());
System.out.println("Is home in IL: " + homeAddr.isInState("IL"));
System.out.println("Distance to work: " + person.getDistanceToWork());
System.out.println("Address summary: " + person.getAddressSummary());
System.out.println("Same state: " + person.livesAndWorksInSameState());
// Test Exercise 2: Course, Student, University
System.out.println("\n=== Testing Course, Student, University ===");
University university = new University("State University");
Course javaCourse = new Course("CS101", "Introduction to Java", 3);
Course mathCourse = new Course("MATH201", "Calculus I", 4);
Student student1 = new Student("Bob Smith", "S001", 3.5);
Student student2 = new Student("Carol Brown", "S002", 3.8);
university.addCourse(javaCourse);
university.addCourse(mathCourse);
university.addStudent(student1);
university.addStudent(student2);
student1.enrollInCourse(javaCourse);
student1.enrollInCourse(mathCourse);
student2.enrollInCourse(javaCourse);
System.out.println("Java course enrollment: " + javaCourse.getEnrollmentCount());
System.out.println("Student1 total credits: " + student1.getTotalCredits());
System.out.println("Student1 is full-time: " + student1.isFullTimeStudent());
System.out.println("Java course average GPA: " + javaCourse.getAverageGPA());
System.out.println("University stats: " + university.getUniversityStats());
// Test Exercise 3: Room, Floor, Building
System.out.println("\n=== Testing Room, Floor, Building ===");
Building building = new Building("Office Tower", "789 Business Ave");
Floor floor1 = new Floor(1, "Office");
Floor floor2 = new Floor(2, "Conference");
Room room101 = new Room("101", "Office", 4);
Room room102 = new Room("102", "Office", 2);
Room room201 = new Room("201", "Conference", 20);
floor1.addRoom(room101);
floor1.addRoom(room102);
floor2.addRoom(room201);
building.addFloor(floor1);
building.addFloor(floor2);
room101.occupyRoom();
System.out.println("Room 101 info: " + room101.getRoomInfo());
System.out.println("Floor 1 occupancy rate: " + floor1.getOccupancyRate());
System.out.println("Building stats: " + building.getBuildingStats());
System.out.println("Available rooms count: " + building.getAllAvailableRooms().size());
System.out.println("Reserve room 102: " + building.reserveRoom("102"));
// Test Exercise 4: Financial System
System.out.println("\n=== Testing Financial System ===");
Bank bank = new Bank("First National Bank");
Customer customer1 = new Customer("C001", "David Wilson", "david@email.com");
Account checking = new Account("ACC001", "Checking");
Account savings = new Account("ACC002", "Savings");
customer1.addAccount(checking);
customer1.addAccount(savings);
bank.addCustomer(customer1);
checking.deposit(1000.0, "Initial deposit");
checking.withdraw(200.0, "ATM withdrawal");
savings.deposit(500.0, "Transfer from checking");
System.out.println("Checking balance: $" + checking.getBalance());
System.out.println("Customer total balance: $" + customer1.getTotalBalance());
System.out.println("Transaction count: " + checking.getTransactionCount());
System.out.println("Bank total deposits: $" + bank.getTotalDeposits());
System.out.println("Transfer between accounts: " + customer1.transferBetweenAccounts("ACC001", "ACC002", 100.0));
System.out.println("\n=== All Nested Object Tests Complete! ===");