Question

FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

FOR JAVA:

Summary: Create a program that adds students to the class list (see below).

The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program.

-------------------------------------------------------------------------------------

List of student names and IDs for class (this will be your separate text file):

  • Jones, Jim,45
  • Hicks, Josh,56
  • Perry, Andy,82
  • Rush, Ashely,19
  • Salenga, Aaron,55
  • Matos, Pamela,22
  • Jeffries, Rick,87
  • Bush, Fred,40
  • Ashford, Kate,52
  • Naamah, Ester,68
  • Lachlan, Bae,35
  • Eachann, Steven,41
  • Ubon, Peter,16
  • Bacon, Gabe,59
  • Rockson, Kurt,77

----------------------------------------------------------------------------

Student.java

public class Student {
private String lastName, firstName;
private int ID;
  
public Student()
{
this.lastName = this.firstName = "";
this.ID = 0;
}

public Student(String lastName, String firstName, int ID) {
this.lastName = lastName;
this.firstName = firstName;
this.ID = ID;
}

public String getLastName() {
return lastName;
}

public String getFirstName() {
return firstName;
}

public int getID() {
return ID;
}
  
@Override
public String toString()
{
return(this.ID + " - " + this.firstName + " " + this.lastName);
}
}

Roster.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Scanner;

public class Roster {
private LinkedList<Student> students;
  
public Roster()
{
this.students = new LinkedList<>();
}
  
public void readStudents(String fileName)
{
Scanner fileReader;
try
{
fileReader = new Scanner(new File(fileName));
while(fileReader.hasNextLine())
{
String[] data = fileReader.nextLine().trim().split(",");
String lastName = data[0];
String firstName = data[1];
int ID = Integer.parseInt(data[2]);
  
this.students.add(new Student(lastName, firstName, ID));
}
fileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println("Couldn't find " + fileName);
System.exit(1);
}
}
  
public void sortByLastName()
{
Collections.sort(this.students, new CompareByLastName());
}
  
public void sortByID()
{
Collections.sort(this.students, new CompareByID());
}
  
public void displayList()
{
for(Student student : this.students)
{
System.out.println(student);
}
}
  
private class CompareByLastName implements Comparator<Student>
{
@Override
public int compare(Student o1, Student o2) {
if(o1.getLastName().compareTo(o2.getLastName()) < 0)
return -1;
else if(o1.getLastName().compareTo(o2.getLastName()) == 0)
return 0;
else
return 1;
}
}
  
class CompareByID implements Comparator<Student>
{
@Override
public int compare(Student o1, Student o2) {
if(o1.getID() < o2.getID())
return -1;
else if(o1.getID() == o2.getID())
return 0;
else
return 1;
}
}
}

RosterDemo.java (Main class)

import java.util.Scanner;

public class RosterDemo {
  
private static final String FILENAME = "students.txt";
  
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Roster roster = new Roster();
roster.readStudents(FILENAME);
  
System.out.println("ORIGINAL LIST OF ALL STUDENTS:\n------------------------------");
roster.displayList();
System.out.println();
  
int choice;
do
{
printMenu();
choice = Integer.parseInt(sc.nextLine().trim());
switch(choice)
{
case 1:
{
System.out.println("\nSorting students based on their last names:");
roster.sortByLastName();
roster.displayList();
System.out.println();
break;
}
  
case 2:
{
System.out.println("\nSorting students based on their IDs:");
roster.sortByID();
roster.displayList();
System.out.println();
break;
}
  
case 3:
{
System.out.println("\nThank you..Goodbye!\n");
System.exit(0);
}
  
default:
System.out.println("\nInvalid selection!\n");
}
}while(choice != 3);
}
  
private static void printMenu()
{
System.out.print("1. Sort by last name\n"
+ "2. Sort by ID\n"
+ "3. Exit\n"
+ "Your selection >> ");
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In this program i have added one more option in the menu as Add Student

which will ask you to enter first name,last name and id. After that it will update the student.txt file.

Then it will ask you whether you want to see new roster or not according to your response it will show.

class CompareByID implements Comparator<Student> {

@Override
public int compare(Student o1, Student o2) {
if (o1.getID() < o2.getID()) {
return -1;
} else if (o1.getID() == o2.getID()) {
return 0;
} else {
return 1;
}
}
}

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Scanner;

public class Roster {

private LinkedList<Student> students;

public Roster() {
this.students = new LinkedList<>();
}

public void readStudents(String fileName) {
Scanner fileReader;
try {
fileReader = new Scanner(new File("C:\\Users\\i334075\\Documents\\NetBeansProjects\\JavaApplication1\\src\\javaapplication1\\students.txt"));
while (fileReader.hasNextLine()) {
String[] data = fileReader.nextLine().trim().split(",");
String lastName = data[0];
String firstName = data[1];
int ID = Integer.parseInt(data[2]);

this.students.add(new Student(lastName, firstName, ID));
}
fileReader.close();
} catch (FileNotFoundException fnfe) {
System.out.println("Couldn't find " + fileName);
System.exit(1);
}
}
// This function write the detail at the end of the file.
public void writeToStudents(String entry) throws IOException {
String fileName = "students.txt";
try {   
  
PrintWriter writer = new PrintWriter (new FileWriter(fileName, true));
writer.append(' ');
writer.append(entry+"\n");
writer.close();
} catch (FileNotFoundException fnfe) {
System.out.println("Couldn't find " + fileName);
System.exit(1);
}
}

public void sortByLastName() {
Collections.sort(this.students, new CompareByLastName());
}

public void sortByID() {
Collections.sort(this.students, new CompareByID());
}

public void displayList() {
for (Student student : this.students) {
System.out.println(student);
}
}

private class CompareByLastName implements Comparator<Student> {

@Override
public int compare(Student o1, Student o2) {
if (o1.getLastName().compareTo(o2.getLastName()) < 0) {
return -1;
} else if (o1.getLastName().compareTo(o2.getLastName()) == 0) {
return 0;
} else {
return 1;
}
}
}
}

import java.io.IOException;
import java.util.Scanner;

public class RosterDemo {

private static final String FILENAME = "students.txt";

public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
Roster roster = new Roster();
roster.readStudents(FILENAME);

System.out.println("ORIGINAL LIST OF ALL STUDENTS:\n------------------------------");
roster.displayList();
System.out.println();

int choice;
String firstName;
String lastName;
int userInput;
int id;
do {
printMenu();
choice = Integer.parseInt(sc.nextLine().trim());
switch (choice) {
case 1: {
System.out.println("\nSorting students based on their last names:");
roster.sortByLastName();
roster.displayList();
System.out.println();
break;
}

case 2: {
System.out.println("\nSorting students based on their IDs:");
roster.sortByID();
roster.displayList();
System.out.println();
break;
}
case 3: {
System.out.println("\nAdd student:");
System.out.println("\nEnter first name:");
firstName = sc.nextLine().trim();
System.out.println("\nEnter last name:");
lastName = sc.nextLine().trim();
System.out.println("\nEnter ID:");
id = Integer.parseInt(sc.nextLine().trim());
roster.writeToStudents(firstName + "," + lastName + "," + id);
System.out.println("\nDo you want to see your new roster ?:");
System.out.println("\n1. YES");
System.out.println("\n2. NO");
userInput = Integer.parseInt(sc.nextLine().trim());
if (userInput == 1) {
roster.readStudents(FILENAME);
roster.displayList();
}else{
System.exit(0);
}
break;
}

case 4: {
System.out.println("\nThank you..Goodbye!\n");
System.exit(0);
}

default:
System.out.println("\nInvalid selection!\n");
}
} while (choice != 3);
}

private static void printMenu() {
System.out.print("1. Sort by last name\n"
+ "2. Sort by ID\n"
+ "3. Add Student\n"
+ "4. Exit\n"
+ "Your selection >> ");
}
}

public class Student {

private String lastName, firstName;
private int ID;

public Student() {
this.lastName = this.firstName = "";
this.ID = 0;
}

public Student(String lastName, String firstName, int ID) {
this.lastName = lastName;
this.firstName = firstName;
this.ID = ID;
}

public String getLastName() {
return lastName;
}

public String getFirstName() {
return firstName;
}

public int getID() {
return ID;
}

@Override
public String toString() {
return (this.ID + " - " + this.firstName + " " + this.lastName);
}
}

Add a comment
Know the answer?
Add Answer to:
FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have a little problem about my code. when i'm working on "toString method" in "Course"...

    I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student {    private String firstName;    private String lastName;    private String id;    private boolean tuitionPaid;       public Student(String firstName, String lastName, String id, boolean tuitionPaid)    {        this.firstName=firstName;        this.lastName=lastName;        this.id=id;        this.tuitionPaid=tuitionPaid;    }   ...

  • NETBEANS JAVA BANK PROGRAM (TAKE SCREENSHOTS FROM NETBEANS INCLUDING OUTPUT PLEASE) Display the accounts for the...

    NETBEANS JAVA BANK PROGRAM (TAKE SCREENSHOTS FROM NETBEANS INCLUDING OUTPUT PLEASE) Display the accounts for the current displayed customer PLEASEEEEEEEEEE package bankexample; import java.util.UUID; public class Customer { private UUID id; private String email; private String password; private String firstName; private String lastName;    public Customer(String firstName, String lastName, String email, String password) { this.id = UUID.randomUUID(); this.firstName = firstName; this.lastName = lastName; this.email = email; this.password = password; } public String getFirstName() { return firstName; } public void setFirstName(String...

  • Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person...

    Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person and Student represent individuals with a first and last name as String class variables, and Student has an additional int class variable that represents a ID number. The Roster class represents a group of people that are objects of either the Person or Student class. It contains an ArrayList. The Person class has been completed for you with class variables, a constructor, and a...

  • 8.26 Encapsulate the Name class. Modify the existing code shown below to make its fields private,...

    8.26 Encapsulate the Name class. Modify the existing code shown below to make its fields private, and add appropriate accessor methods to the class named getFirstName, getMiddleInitial, and getLastName. code: class Name { private String firstName; private String middleNames; private String lastName;    //Constructor method public Name(String firstName, String middleNames, String lastName) { this.firstName = firstName; this.middleNames = middleNames; this.lastName = lastName;    } //Accessor for firstName public String getFirstName() { return firstName; } //Accessor for middleNames public String getMiddlesNames()...

  • Public class Person t publie alass EmployeeRecord ( private String firstName private String last ...

    public class Person t publie alass EmployeeRecord ( private String firstName private String last Nanei private Person employee private int employeeID publie EnmployeeRecord (Person e, int ID) publie Person (String EName, String 1Name) thia.employee e employeeID ID setName (EName, 1Name) : publie void setName (String Name, String 1Name) publie void setInfo (Person e, int ID) this.firstName- fName this.lastName 1Name this.employee e employeeID ID: publio String getFiritName) return firstName public Person getEmployee) t return employeei public String getLastName public int getIDO...

  • Hello, How can I make the program print out "Invalid data!!" and nothing else if the...

    Hello, How can I make the program print out "Invalid data!!" and nothing else if the file has a grade that is not an A, B, C, D, E, or F or a percentage below zero? I need this done by tomorrow if possible please. The program should sort a file containing data about students like this for five columns: one for last name, one for first name, one for student ID, one for student grade percentage, one for student...

  • In Java: Executable Class create an array of Employee objects. You can copy the array you...

    In Java: Executable Class create an array of Employee objects. You can copy the array you made for Chapter 20. create an ArrayList of Employee objects from that array. use an enhanced for loop to print all employees as shown in the sample output. create a TreeMap that uses Strings for keys and Employees as values. this TreeMap should map Employee ID numbers to their associated Employees. process the ArrayList to add elements to this map. print all employees in...

  • I have currently a functional Java progam with a gui. Its a simple table of contacts...

    I have currently a functional Java progam with a gui. Its a simple table of contacts with 3 buttons: add, remove, and edit. Right now the buttons are in the program but they do not work yet. I need the buttons to actually be able to add, remove, or edit things on the table. Thanks so much. Here is the working code so far: //PersonTableModel.java import java.util.List; import javax.swing.table.AbstractTableModel; public class PersonTableModel extends AbstractTableModel {     private static final int...

  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  • Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters...

    Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters and getter methods for the new variable and modify the toString method. The objectstudent program is in this weeks module, you can give it a default value of 19090. class Student { private int id; private String name; public Student() { id = 8; name = "John"; } public int getid() { return id; } public String getname() { return name; } public void...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT