Created
January 29, 2023 12:48
-
-
Save pjastr/014713313176a63728dc59ef49377069 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Map; | |
import java.util.Scanner; | |
import java.util.TreeMap; | |
public class Main { | |
public static void main(String[] args) { | |
StudentGrades app = new StudentGrades(); | |
while (true) { | |
System.out.println("1. Add student"); | |
System.out.println("2. Remove student"); | |
System.out.println("3. Change grade"); | |
System.out.println("4. Print grades"); | |
System.out.println("5. Exit"); | |
System.out.print("Enter your choice: "); | |
int choice = app.getInput().nextInt(); | |
app.getInput().nextLine(); | |
if (choice == 1) { | |
app.addStudent(); | |
} else if (choice == 2) { | |
app.removeStudent(); | |
} else if (choice == 3) { | |
app.changeGrade(); | |
} else if (choice == 4) { | |
app.printGrades(); | |
} else { | |
break; | |
} | |
} | |
} | |
} | |
class StudentGrades { | |
private Map<String, String> students = new TreeMap<>(); | |
private Scanner input = new Scanner(System.in); | |
public Scanner getInput() { | |
return input; | |
} | |
public void addStudent() { | |
System.out.print("Enter student's name: "); | |
String name = input.nextLine(); | |
System.out.print("Enter student's grade: "); | |
String grade = input.nextLine(); | |
students.put(name, grade); | |
} | |
public void removeStudent() { | |
System.out.print("Enter student's name: "); | |
String name = input.nextLine(); | |
students.remove(name); | |
} | |
public void changeGrade() { | |
System.out.print("Enter student's name: "); | |
String name = input.nextLine(); | |
System.out.print("Enter new grade: "); | |
String grade = input.nextLine(); | |
students.put(name, grade); | |
} | |
public void printGrades() { | |
System.out.println("Grades:"); | |
for (Map.Entry<String, String> entry : students.entrySet()) { | |
System.out.println(entry.getKey() + ": " + entry.getValue()); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment