cs506 Assignment 1 download file
CS506 Assignment 1 2024
Alternate Link to Download Java
JAVA CODE
import java.util.ArrayList;
import java.util.Comparator;
import javax.swing.*;
public class InventroExpo {
public static void main(String[] args) {
InventroExpo expo = new InventroExpo();
expo.init();
}
private void init() {
int numberOfInventors = getNumberOfInventors();
ArrayList<Inventor> inventors = new ArrayList<>();
for (int i = 0; i < numberOfInventors; i++) {
String name = getInventorName(i + 1);
int score = getInventorScore(name);
inventors.add(new Inventor(name, score));
}
Inventor topInventor = findTopInventor(inventors);
JOptionPane.showMessageDialog(null, "The top inventor is " + topInventor.getName() +
" with an invention score of " + topInventor.getScore() + " out of 100.");
JOptionPane.showMessageDialog(null, "Thanks for participating in the Invention Expo Competition\nDeveloped by [Your Student ID]");
}
private int getNumberOfInventors() {
while (true) {
try {
String input = JOptionPane.showInputDialog("Enter the number of inventors:");
int numberOfInventors = Integer.parseInt(input);
if (numberOfInventors > 0) {
return numberOfInventors;
} else {
JOptionPane.showMessageDialog(null, "Please enter a number greater than zero.");
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Invalid input. Please enter a valid integer.");
}
}
}
private String getInventorName(int inventorNumber) {
while (true) {
String name = JOptionPane.showInputDialog("Please enter the name of inventor " + inventorNumber + ":");
if (name != null && !name.trim().isEmpty()) {
return name;
}
JOptionPane.showMessageDialog(null, "Name cannot be empty.");
}
}
private int getInventorScore(String name) {
while (true) {
try {
String scoreInput = JOptionPane.showInputDialog("Please enter the score for the invention of " + name + " (out of 100):");
int score = Integer.parseInt(scoreInput);
if (score >= 1 && score <= 100) {
return score;
} else {
JOptionPane.showMessageDialog(null, "Please enter a score between 1 and 100.");
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Invalid input. Please enter a valid integer between 1 and 100.");
}
}
}
public Inventor findTopInventor(ArrayList<Inventor> inventors) {
return inventors.stream().max(Comparator.comparingInt(Inventor::getScore)).orElseThrow();
}
private static class Inventor {
private String name;
private int score;
public Inventor(String name, int score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
}
}
Comments
Post a Comment