-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudent.java
More file actions
150 lines (145 loc) · 4.14 KB
/
Copy pathStudent.java
File metadata and controls
150 lines (145 loc) · 4.14 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
import java.io.IOException;
import java.util.Scanner;
public class Student {
private int id;
private String name;
private String address;
private int age;
private double GPA;
final Scanner sc = new Scanner(System.in);
public Student(){
}
public Student(int id, String name, String address, double GPA, int age) {
this.id = id;
this.name = name;
this.address = address;
this.GPA = GPA;
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public double getGPA() {
return GPA;
}
public void setGPA(double GPA) {
this.GPA = GPA;
}
public void addStudent(){
System.out.println("------Enter student about information:\n------- ");
setId(inputId());
setName(inputName());
setAge(inputAge());
setAddress(inputAddress());
setGPA(inputGPA());
}
public void updateStudent(){
System.out.println("------Update student information:\n------- ");
System.out.println("------Original student information:\n------- ");
this.outStudent();
System.out.println("------New student information (Keep id only):\n------- ");
setName(inputName());
setAge(inputAge());
setAddress(inputAddress());
setGPA(inputGPA());
}
public int inputId() {
System.out.print("Input student id: ");
while (true) {
try {
int id = Integer.parseInt((sc.nextLine()));
return id;
} catch (NumberFormatException ex) {
System.out.print("invalid! Input student id again: ");
}
}
}
// input name - return
public String inputName() {
System.out.print("Input student name: ");
while (true) {
try {
String name = sc.nextLine();
if(name.equals("") || name == null || name.matches(".*\\d.*")){
throw new Throwable("Invalid name");
}
return name;
} catch (Throwable e) {
System.out.println("Invalid! Input student name again:");
}
}
}
// input age - return
public int inputAge() {
System.out.print("Input student age: ");
while (true) {
try {
byte age = Byte.parseByte((sc.nextLine()));
if (age <0 && age > 100) {
throw new NumberFormatException();
}
return age;
} catch (NumberFormatException ex) {
System.out.print("invalid! Input student age again: ");
}
}
}
// input address - return
public String inputAddress() {
System.out.print("Input student address: ");
return sc.nextLine();
}
// input GPD - return
private double inputGPA() {
System.out.print("Input student gpa: ");
while (true) {
try {
float gpa = Float.parseFloat((sc.nextLine()));
if (gpa < 0.0 || gpa > 10.0) {
throw new Throwable("Not in valid range");
}
return gpa;
} catch (Throwable ex) {
System.out.print("invalid! Input student age again: ");
}
}
}
public void outStudent(){
System.out.println("Id: "+ getId());
System.out.println("Name: "+ getName());
System.out.println("Address: "+ getAddress());
System.out.println("GPA: "+ getGPA());
System.out.println("Age: "+ getAge());
}
}
/*
* 2
* Nhap ma sinh vien can chinh sua
* (tim sinh vien)
* -> (obj) student
* -> ten sinh vien:
* -> ma sinh vien:
*
*
* */