Step 1:-
Step 2:- Create a constructor and Run.
Step 4:- Enroll in courses:
// Enroll in courses
public void enroll() {
// Get inside a loop, user hits 0
do {
System.out.print("Enter course to enroll (Q to quit): ");
Scanner in = new Scanner(System.in);
String course = in.nextLine();
if(!course.equals("Q")) {
course = course + "\n" + course;
tuitionBalance = tuitionBalance + costOfCourse;
}else {
System.out.println("BREAK!");
break;
}
} while(1 != 0);
System.out.println("ENROLLED IN: " + course);
System.out.println("TUITION BALANCE: " + tuitionBalance);
}
StudentDatabaseApp.java
public class StudentDatabaseApp {
public static void main(String[] args) {
Student stu1 = new Student();
stu1.enroll();
}
}
Step 5:- Paying tuition fees:
Code:-
// View balance
public void viewBalance() {
System.out.println("Your balance is: $" + tuitionBalance);
}
// Pay Tuition
public void payTuition(int payment) {
viewBalance();
tuitionBalance = tuitionBalance - payment;
System.out.println("Thank you for your payment of $" + payment);
viewBalance();
}
StudentDatabaseApp.java
public class StudentDatabaseApp {
public static void main(String[] args) {
Student stu1 = new Student();
stu1.enroll(); stu1.payTuition(500);
}
}
Step 5:- Show information and comment on the unwanted SYS:
output:
Code:-
// Show Status
public String toString() {
return "Name: " + firstName + " " + lastName +
"\nGrade Level: " + gradeYear +
"\nStudent ID: " + studentID +
"\nCourses Enrolled:" + courses +
"\nBalance: $" + tuitionBalance;
}
StudentDatabaseApp.java
public class StudentDatabaseApp {
public static void main(String[] args) {
Student stu1 = new Student();
stu1.enroll(); stu1.payTuition(500); System.out.println(stu1.toString());
}
}
Step 6:- Completed code to show the three student's "3" information:
Code: Create Array
Student.java
import java.util.Scanner;
public class Student {
private String firstName;
private String lastName;
private int gradeYear;
private String studentID;
private String courses = "";
private int tuitionBalance;
private static int costOfCourse = 600;
private static int id = 1001;
// Constructor : prompt user to enter student's name and year
public Student() {
Scanner in = new Scanner(System.in);
System.out.print("Enter student first name: ");
this.firstName = in.nextLine();
System.out.print("Enter student last name: ");
this.lastName = in.nextLine();
System.out.print("1 - Freshmen\n2 - Sophmore\n3 - Junior\n4 - Senior\nEnter student class level: ");
this.gradeYear = in.nextInt();
setStudentID();
//System.out.println(firstName + " " + lastName + " " + gradeYear + " " + studentID);
}
// Generate an ID
private void setStudentID() {
// Grade level + ID
id++;
this.studentID = gradeYear + "" + id;
}
// Enroll in courses
public void enroll() {
// Get inside a loop, user hits 0
do {
System.out.print("Enter course to enroll (Q to quit): ");
Scanner in = new Scanner(System.in);
String course = in.nextLine();
if(!course.equals("Q")) {
courses = courses + "\n " + course;
tuitionBalance = tuitionBalance + costOfCourse;
}else {
//System.out.println("BREAK!");
break;
}
} while(1 != 0);
//System.out.println("ENROLLED IN: " + course);
}
// View balance
public void viewBalance() {
System.out.println("Your balance is: $" + tuitionBalance);
}
// Pay Tuition
public void payTuition(int payment) {
viewBalance();
tuitionBalance = tuitionBalance - payment;
System.out.println("Thank you for your payment of $" + payment);
viewBalance();
}
// Show Status
public String toString() {
return "Name: " + firstName + " " + lastName +
"\nGrade Level: " + gradeYear +
"\nStudent ID: " + studentID +
"\nCourses Enrolled:" + courses +
"\nBalance: $" + tuitionBalance;
}
}
StudentDatabaseApp.java
(Three student information print)
public class StudentDatabaseApp {
public static void main(String[] args) {
Student stu1 = new Student();
stu1.enroll();
stu1.payTuition(500);
System.out.println(stu1.toString());
Student stu2 = new Student();
stu2.enroll();
stu2.payTuition(500);
System.out.println(stu2.toString());
Student stu3 = new Student();
stu3.enroll();
stu3.payTuition(500);
System.out.println(stu3.toString());
// Ask how many new students we want to add
// Create n numbers of students
}
}
Step 7:- Now we can dynamically update the student information:
Code:-
import java.util.Scanner;
public class StudentDatabaseApp {
public static void main(String[] args) {
// Ask how many new student we want to add
System.out.print("Enter number of new student to enroll: ");
Scanner in = new Scanner(System.in);
int numOfStudents = in.nextInt();
Student[] student = new Student[numOfStudents];
// Create n numbers of student
for (int n = 0; n < numOfStudents; n++) {
student[n] = new Student();
student[n].enroll();
student[n].payTuition(500);
System.out.println(student[n].toString());
}
}
}
output:-
Full Code:-
Student.java
package studentdatabaseapp;
import java.util.Scanner;
public class Student {
private String firstName;
private String lastName;
private int gradeYear;
private String studentID;
private String courses = "";
private int tuitionBalance;
private static int costOfCourse = 600;
private static int id = 1001;
// Constructor : prompt user to enter student's name and year
public Student() {
Scanner in = new Scanner(System.in);
System.out.print("Enter student first name: ");
this.firstName = in.nextLine();
System.out.print("Enter student last name: ");
this.lastName = in.nextLine();
System.out.print("1 - Freshmen\n2 - Sophmore\n3 - Junior\n4 - Senior\nEnter student class level: ");
this.gradeYear = in.nextInt();
setStudentID();
//System.out.println(firstName + " " + lastName + " " + gradeYear + " " + studentID);
}
// Generate an ID
private void setStudentID() {
// Grade level + ID
id++;
this.studentID = gradeYear + "" + id;
}
// Enroll in courses
public void enroll() {
// Get inside a loop, user hits 0
do {
System.out.print("Enter course to enroll (Q to quit): ");
Scanner in = new Scanner(System.in);
String course = in.nextLine();
if(!course.equals("Q")) {
courses = courses + "\n " + course;
tuitionBalance = tuitionBalance + costOfCourse;
}else {
//System.out.println("BREAK!");
break;
}
} while(1 != 0);
//System.out.println("ENROLLED IN: " + course);
}
// View balance
public void viewBalance() {
System.out.println("Your balance is: $" + tuitionBalance);
}
// Pay Tuition
public void payTuition(int payment) {
viewBalance();
tuitionBalance = tuitionBalance - payment;
System.out.println("Thank you for your payment of $" + payment);
viewBalance();
}
// Show Status
public String toString() {
return "Name: " + firstName + " " + lastName +
"\nGrade Level: " + gradeYear +
"\nStudent ID: " + studentID +
"\nCourses Enrolled:" + courses +
"\nBalance: $" + tuitionBalance;
}
}
StudentDatabaseApp.java
package studentdatabaseapp;
import java.util.Scanner;
public class StudentDatabaseApp {
public static void main(String[] args) {
// Ask how many new student we want to add
System.out.print("Enter number of new student to enroll: ");
Scanner in = new Scanner(System.in);
int numOfStudents = in.nextInt();
Student[] student = new Student[numOfStudents];
// Create n numbers of student
for (int n = 0; n < numOfStudents; n++) {
student[n] = new Student();
student[n].enroll();
student[n].payTuition(500);
System.out.println(student[n].toString());
}
}
}







No comments:
Post a Comment
If you have any problem please let me know.