Repository navigation
Expand file tree
/
Copy pathPhoneTest.java
More file actions
150 lines (119 loc) · 3.96 KB
/
Copy pathPhoneTest.java
File metadata and controls
150 lines (119 loc) · 3.96 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 test;
import java.sql.SQLException;
import database.bean.Admin;
import database.bean.student.Phone;
import database.managers.ConnectionManager;
import database.managers.DatabaseManager;
import database.managers.PhoneManager;
import exception.InvalidAdminException;
import exception.InvalidBeanException;
import utils.BeanType;
public class PhoneTest
{
public static void main(String[] args)
{
// First and very important step is to specify the Admin that is
// making the change as follows
Admin currentAdmin = new Admin("Chidiebere", "Fred" );
DatabaseManager.setCurrentAdmin(currentAdmin);
// Rest of the code
try
{
TestUtils.displayBean(BeanType.PHONE , 0 );
System.out.println("---------------INSERTING A NEW PHONE TEST--------------");
Phone newPhone = getPhone("new");
try
{
if( PhoneManager.insert(newPhone)) {
System.out.println("Successfully inserted a new phone number to "
+ "the specified student.");
TestUtils.displayBean(BeanType.PHONE , 0 );
}
else
{
System.out.println("Was Unsuccessful Maybe the studentId does not exist!!!");
}
}
catch (InvalidBeanException e)
{
e.printStackTrace();
System.err.println( "The format of the Phone object was invalid" );
}
System.out.println("---------------REMOVING AN EXISTING PHONE TEST--------------");
/*This should be used when we want to remove an existing Phone*/
Phone existingPhone = getPhone("existing");
try
{
if( PhoneManager.removePhone(existingPhone)){
System.out.println( "Phone was removed succcessfullly!!!");
TestUtils.displayBean(BeanType.PHONE , 0 );
}
else
{
System.out.println("Nothing was removed! "
+ "Maybe the phone object you inputed is not in the database");
}
}
catch (InvalidBeanException e)
{
System.err.println( "The format of the Phone object was invalid" );
}
System.out.println("-------------UPDATING AN EXISTING PHONE TEST--------------");
existingPhone = getPhone("existing" );
String newNumber = TestUtils.getStringInput("Enter new phone number: ");
newPhone = new Phone( existingPhone.getStudentId(), newNumber );
try
{
if( PhoneManager.update(existingPhone, newPhone) ){
System.out.println( "The Phone was updated succcessfullly!!!");
TestUtils.displayBean(BeanType.PHONE, 0 );
}
else
{
System.out.println("Nothing was updated! "
+ "Maybe the Phone pbject you inputed is not in the database");
}
}
catch (InvalidBeanException e)
{
System.err.println( "The format one of the Phone "
+ "object(s) is invalid" );
}
System.out.println("-------------RETRIEVING STUDENT PHONE TEST--------------");
String studentId = TestUtils.getStringInput( "Enter Student id card number" );
Phone[] numbers = PhoneManager.getPhoneNumber(studentId, 0);
if(numbers != null ){
System.out.println( "The Student's phone numbers are: ");
for( int i = 0 ; i< numbers.length ; i++ ){
System.out.println(numbers[i].getNumber());
}
}
else
System.out.println("No numbers was found for the specified student");
}
catch ( InvalidAdminException e)
{
System.err.println( e );
System.err.println("The Admin was invalid");
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
// This is also very important. Close the ConnectionManager when you are done
//
ConnectionManager.close();
}
}
/**
* @return
*/
public static Phone getPhone( String midString)
{
String studId = TestUtils.getStringInput("Enter " + midString+ " studentID: ");
String number = TestUtils.getStringInput("Enter " + midString + " phone number: ");
return new Phone(studId, number);
}
}