So I'm still fairly new to Java and it's also been a while since I've programmed in Java. What I want know is how to assign an integer, like a student number, to an element in an array list. I've got three elements in my arraylist, and each element has to have a 7 digit student number with the 7th digit as the next number in sequence (ex. el 1 with student number 1234567, el 2 with 1234568, el 3 with 1234569). I'm not sure if I'm on the right track or not. This code is a work in progress and there are many things I still have to add or fix. My arraylist is in the next code block titled StudentApp.
public class Student {
//fields
private String firstName;
private String lastName;
private int sNumber;
private String major;
private double gpa;
private static int count() {
int count = 1234567;
for (int i = 1; i <= count; i++) {
System.out.print(count);
}
return count;
}
//constructors
public Student() {
firstName = null;
lastName = null;
sNumber = 1234567;
major = null;
gpa = 0.0;
}
public Student(String fName, String lName, int sNumber, String maj, double gpa) {
firstName = fName;
lastName = lName;
sNumber = sNumber;
major = maj;
gpa = gpa;
}
//methods
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public int getsNumber() {
for(int sNumber = 0; sNumber >= 7; sNumber++) {
int sNumber = 1234567;
sNumber++
}
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public double getGpa() {
return gpa;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
@Override
public String toString() {
return "S" + sNumber + " " + firstName + " "+ lastName + " " + "(" + major +")" + " " + "gpa:" + gpa;
}
}
import java.util.Scanner;
import java.util.ArrayList;
public class StudentApp {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<Student> names = new ArrayList<Student> ();
Student student1 = new Student();
student1.setFirstName("Brian");
student1.setLastName("Scholl");
student1.setMajor("CS");
student1.setGpa(3.5);
Student student2 = new Student();
student2.setFirstName("Emily");
student2.setLastName("Davies");
student2.setMajor("ME");
student2.setGpa(3.7);
Student student3 = new Student();
student3.setFirstName("Sarah");
student3.setLastName("Dixon");
student3.setMajor("EE");
student3.setGpa(3.8);
names.add(student1);
names.add(student2);
names.add(student3);
int choice;
do{
displayMenu();
choice = input.nextInt();
switch(choice) {
case 1:
Student student4 = new Student();
System.out.print("First name: ");
String fName1 = input.nextLine();
System.out.println();
System.out.print("Last name: ");
String lName1 = input.nextLine();
System.out.println();
System.out.print("Major: ");
String major1 = input.nextLine();
System.out.println();
System.out.print("GPA: ");
double gpa1 = input.nextDouble();
student4.setFirstName(fName1);
student4.setLastName(lName1);
student4.setMajor(major1);
student4.setGpa(gpa1);
names.add(student4);
break;
case 2:
System.out.print("Find student with sNumber S");
input.nextInt();
student1.getsNumber();
//statement on finding a student
break;
case 3:
//statement on deleting a student
break;
case 4:
System.out.println(student1);
System.out.println(student2);
System.out.println(student3);
break;
case 5:
names.size();
break;
case 6:
System.out.println("Good bye");
break;
default:
System.out.println("Invalid Selection");
System.out.println();
break;
}
System.out.println();
}while(choice != 0 );
}
public static void displayMenu() {
System.out.println("1. Add a student");
System.out.println("2. Find a student");
System.out.println("3. Delete a student");
System.out.println("4. Display all students");
System.out.println("5. Display the total number of students");
System.out.println("6. Exit");
}
}
names.add(new Student(...));