0

Ok in this code the user has to type in their first and last name together otherwise it is invalid. I know I have to use some sort of java.lang.character but i do not know how to write it up. I know that it needs to be a part of the scientist class where the code checks to see if the code is empty. Here is the codes.

import java.text.NumberFormat;
import javax.swing.JOptionPane;
public class Scientist {
	private String username;
	private String password;
	private String applicantName;
	private String instituteName;
	private String departmentName;
	private int positionRank;
	private String researchTopic;
	private double pastFunding;
	private int numOfPaper;
	private final int scientistId;
	private static int numOfScientistsCreated;
	public static final double MAX_SCORE = 100;
	public static final double MAX_FUNDING = 500000;
	
	public Scientist(){
		applicantName = "";
		instituteName = "";
		departmentName = "";
		positionRank = 0;
		researchTopic = "";
		pastFunding = 0;
		numOfPaper = 0;
		numOfScientistsCreated++;
		scientistId = numOfScientistsCreated;	
	}
	
	public Scientist(String iUsername, String iPassword){
		this();
		username = iUsername;
		password = iPassword;
	}
	
	public boolean setUsername(String iUsername){
		if (iUsername == null || iUsername.equals(""))
			return false;
		else{
			username = iUsername;
			return true;
		}
	}
	
	public String getUsername(){
		return username;
	}
	
	public boolean setPassword(String iPassword){
		if (iPassword == null || iPassword.equals(""))
			return false;
		else{
			password = iPassword;
			return true;
		}
	}
	
	public String getPassword(){
		return password;
	}
	
	public boolean setApplicantName(String iName){
		if (iName == null || iName.equals("")){
			JOptionPane.showMessageDialog(null, "Sorry! " +	"No name is entered. Please enter it again.");
			return false;
		}
		
		applicantName = iName;
		
		return true;
	}
	
	public String getApplicantName(){
		return applicantName;
	}
	
	public boolean setInstituteName(String iInstituteName){
		if (iInstituteName == null || iInstituteName.equals(""))
			return false;
		else{
			instituteName = iInstituteName;
			return true;
		}
	}
	
	public String getInstituteName(){
		return instituteName;
	}
	
	public boolean setDepartmentName(String iDepartmentName){
		if (iDepartmentName ==  null || iDepartmentName.equals(""))
			return false;
		else{
			departmentName = iDepartmentName;
			return true;
		}
	}
	
	public String getDepartmentName(){
		return departmentName;
	}
	
	public boolean setResearchTopic(String iTopic){
		if (iTopic == null || iTopic.equals(""))
			return false;
		else{
			researchTopic = iTopic;
			return true;
		}
	}
	
	public String getResearchTopic(){
		return researchTopic;
	}
	
	public boolean setPositionRank(int iRank){
		if (iRank < 1 || iRank > 4)
			return false;
		else {
			positionRank = iRank;
			return true;			
		}
	}
	
	public int getPositionRank(){
		return positionRank;
	}
	
	public boolean setNumOfPaper(int iNumOfPaper){
		if (iNumOfPaper < 0)
			return false;
		else {
			numOfPaper = iNumOfPaper;
			return true;
		}
	}
	
	public int getNumOfPaper(){
		return numOfPaper;
	}
	
	public boolean setPastFunding(double iPastFunding){
		if (iPastFunding < 0)
			return false;
		else {
			pastFunding = iPastFunding;
			return true;
		}
	}
	
	public double getPastFunding(){
		return pastFunding;
	}
		
	public static int getNumOfScientistsCreated(){
		return numOfScientistsCreated;
	}
	
	public int getScientistId(){
		return scientistId;
	}
	
	public String toString(){
		String position = "", str = "";
		NumberFormat currency = NumberFormat.getCurrencyInstance();
		currency.setMinimumFractionDigits(2);
		currency.setMaximumFractionDigits(2);
		
		if (getPositionRank() == 1)
			position = "Full Professor";
		else if (getPositionRank() == 2)
			position = "Associate Professor";
		else if (getPositionRank() == 3)
			position = "Assistant Professor";
		else if (getPositionRank() == 4)
			position = "Others";
			
		str =	"Applicant Name: " + getApplicantName() + 
				"\nPersonal ID: " + getScientistId() +
				"\nInstitute Name: " + getInstituteName() + 
				"\nDepartment Name: " + getDepartmentName() +
				"\nPosition Title : " + position +
				"\nResearch Topic: " + getResearchTopic() +
				"\nNumber of Papers Published: " + getNumOfPaper() +
				"\nPast Funding Awarded: " + currency.format(getPastFunding());
		
		return str;
	}
		
	private double calculateAwardFunding(){
		double score = 0;
		
		if (getPositionRank() == 1)
			score = 30;
		else if (getPositionRank() == 2)
			score = 20;
		else if (getPositionRank() == 3)
			score = 10;
		else if(getPositionRank() == 4)
			score = 5;
		
		if (getNumOfPaper() >= 30)
			score += 30;
		else if (getNumOfPaper() >= 20)
			score += 20;
		else if (getNumOfPaper() >= 10)
			score += 10;
		else if (getNumOfPaper() >= 1)
			score += 5;
		
		if (getPastFunding() >= 400000)
			score += 40;
		else if (getPastFunding() >= 300000)
			score += 30;
		else if (getPastFunding() >= 200000)
			score += 20;
		else if (getPastFunding() >= 1)
			score += 10;
		
		return (score / MAX_SCORE) * MAX_FUNDING;
	}
	
	public double getNewAwardFunding(){
		return calculateAwardFunding();
	}
}
import javax.swing.JOptionPane;
import java.text.NumberFormat;
public class NSFFunding {
	public static void main(String[] args) {
		Scientist[] scientist = new Scientist[10];
		createExistingUserData(scientist);
		int choice = 0, newAccount = 0;
		int login = loginPage(scientist);
		do{
			if (!(login == -1)){
				do {
					do {
						choice = Integer.parseInt(JOptionPane.showInputDialog(null, showMenu(), 
								"Take-Home Assignment 3", JOptionPane.QUESTION_MESSAGE));
						if (choice < 1 || choice > 4)
							JOptionPane.showMessageDialog(null, "Sorry! There is no such option " + 
						choice + ". Please enter the option again.", "Take-Home Assignment 3", 
						JOptionPane.ERROR_MESSAGE);
					}while (choice < 1 || choice > 4);
	
					if (choice == 1)
						modifyProfile(scientist, login);
					else if (choice == 2)
						displayScientistProfile(scientist, login);
					else if (choice == 3)
						displayApplicationResult(scientist, login);
					else {
						JOptionPane.showMessageDialog(null, "Thanks for your application!", "Take-Home Assignment 3", 
								JOptionPane.INFORMATION_MESSAGE);
						login = -1;
					}
				}while(choice != 4);
			}
			else{
				newAccount = JOptionPane.showConfirmDialog(null, "Sorry! We don't have your account. " +
						"Do you want to create a new account?", "Take-Home Assignment 3", JOptionPane.YES_NO_OPTION);
				if (newAccount != JOptionPane.YES_OPTION)
					JOptionPane.showMessageDialog(null, "Goodbye!", "Take-Home Assignment 3", 
							JOptionPane.INFORMATION_MESSAGE);
				else{
					login = createAccount(scientist);
					JOptionPane.showMessageDialog(null, "Please create your own profile now.", 
							"Take-Home Assignment 3", JOptionPane.INFORMATION_MESSAGE);
					createProfile(scientist, login);
				}
			}
		}while(!(login == -1));
	}
	
	private static void createExistingUserData(Scientist[] scientist){
		scientist[0] = new Scientist("user1", "password1");
		scientist[0].setApplicantName("Peter Lee");
		scientist[0].setInstituteName("George Washington University");
		scientist[0].setDepartmentName("Information System Department");
		scientist[0].setPositionRank(1);
		scientist[0].setResearchTopic("Software Engineering");
		scientist[0].setPastFunding(250000);
		scientist[0].setNumOfPaper(30);
	}
	
	private static int loginPage(Scientist[] scientist){
		String username = "", password = "";
		do{
			username = JOptionPane.showInputDialog(null, "Please enter the username:", 
					"Take-Home Assignment 3", JOptionPane.QUESTION_MESSAGE);
			if (username == null || username.equals(""))
				JOptionPane.showMessageDialog(null, "Sorry! The username is empty. Please enter it again.", 
						"Take-Home Assignment 3", JOptionPane.ERROR_MESSAGE);
		}while(username == null || username.equals(""));
		
		do{
			password = JOptionPane.showInputDialog(null, "Please enter the password:", 
					"Take-Home Assignment 3", JOptionPane.QUESTION_MESSAGE);
			if (password == null || password.equals(""))
				JOptionPane.showMessageDialog(null, "Sorry! The password is empty. Please enter it again.", 
						"Take-Home Assignment 3", JOptionPane.ERROR_MESSAGE);
		}while(password == null ||password.equals(""));
		
		return validateAccount(scientist, username, password);
	}
	
	private static String showMenu(){
		String menu = "Scientists' NSF Funding Application System";
		menu += "\n1. Update your profile";
		menu += "\n2. Display your profile";
		menu += "\n3. Submit and Application Result";
		menu += "\n4. Exit the system";
		return menu;
	}
	
	private static int createAccount(Scientist[] scientist){
		String username = "", password = "";
		int login = 0;
		do{
			username = JOptionPane.showInputDialog(null, "Please enter the username:", 
					"Take-Home Assignment 3", JOptionPane.QUESTION_MESSAGE);
			password = JOptionPane.showInputDialog(null, "Please enter the password:", 
					"Take-Home Assignment 3", JOptionPane.QUESTION_MESSAGE);
			if (!(validateAccount(scientist, username, password) == -1))
				JOptionPane.showMessageDialog(null, "Sorry! The account has been chosen. Please choose another one.", 
						"Take-Home Assignment 3", JOptionPane.ERROR_MESSAGE);
		}while(!(validateAccount(scientist, username, password) == -1));
		
		if (Scientist.getNumOfScientistsCreated() < scientist.length){
			scientist[Scientist.getNumOfScientistsCreated()] = new Scientist(username, password);
			login = Scientist.getNumOfScientistsCreated() - 1;
		}
		
		return login;
	}
	
	private static int validateAccount(Scientist[] scientist, String username, String password){
		boolean same = false;
		int i = 0, index = 0;
		
		while(!same && i < Scientist.getNumOfScientistsCreated()){
			if (scientist[i].getUsername().equals(username) && scientist[i].getPassword().equals(password))
				same = true;
			else
				i++;
		}
		
		if (same)
			index = i;
		else
			index = -1;
		
		return index;
	}
	
	private static void createProfile(Scientist[] scientist, int index){
		int positionRank = 0, numOfPaper = 0;
		String applicantName = "", instituteName = "", departmentName = "", researchTopic = "";
		double pastFunding = 0;
		
		do{
			applicantName = JOptionPane.showInputDialog(null, "Please enter your name:", "Take-Home Assignment 3", 
					JOptionPane.QUESTION_MESSAGE);
		}while(!scientist[index].setApplicantName(applicantName));
				
		do{
			instituteName = JOptionPane.showInputDialog(null, "Please enter the institue name:", 
					"Take-Home Assignment 3", JOptionPane.QUESTION_MESSAGE);
			if (!scientist[index].setInstituteName(instituteName))
				JOptionPane.showMessageDialog(null, "Sorry! The name is empty. Please enter it again.", 
						"Take-Home Assignment 3", JOptionPane.ERROR_MESSAGE);
		}while(!scientist[index].setInstituteName(instituteName));
		
		do{
			departmentName = JOptionPane.showInputDialog(null, "Please enter your department name:", 
					"Take-Home Assignment 3", JOptionPane.QUESTION_MESSAGE);
			if (!scientist[index].setDepartmentName(departmentName))
				JOptionPane.showMessageDialog(null, "Sorry! The name is empty. Please enter it again.", 
						"Take-Home Assignment 3", JOptionPane.ERROR_MESSAGE);
		}while(!scientist[index].setDepartmentName(departmentName));
		
		do{
			positionRank = Integer.parseInt(JOptionPane.showInputDialog(null, "Please choose your position:" +
					"\n1) Full Professor\n2) Associate Professor\n3) Assistant Professor\n4) Others", 
					"Take-Home Assignment 3", JOptionPane.QUESTION_MESSAGE));
			if (!scientist[index].setPositionRank(positionRank))
				JOptionPane.showMessageDialog(null, "Sorry! The position choice is not one of the options. " +
						"Please enter it again.", "Take-Home Assignment 3", JOptionPane.ERROR_MESSAGE);
		}while(!scientist[index].setPositionRank(positionRank));
		
		do{
			researchTopic = JOptionPane.showInputDialog(null, "Please enter the research topic:", 
					"Take-Home Assignment 3", JOptionPane.QUESTION_MESSAGE);
			if (!scientist[index].setResearchTopic(researchTopic))
				JOptionPane.showMessageDialog(null, "Sorry! The topic is empty. Please enter it again.", 
						"Take-Home Assignment 3", JOptionPane.ERROR_MESSAGE);
		}while(!scientist[index].setResearchTopic(researchTopic));
		
		do{
			pastFunding = Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter the past funding:", 
					"Take-Home Assignment 3", JOptionPane.QUESTION_MESSAGE));
			if (!scientist[index].setPastFunding(pastFunding))
				JOptionPane.showMessageDialog(null, "Sorry! The funding is negative. Please enter it again.", 
						"Take-Home Assignment 3", JOptionPane.ERROR_MESSAGE);
		}while(!scientist[index].setPastFunding(pastFunding));
		
		do{
			numOfPaper = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the number of paper published:", 
					"Take-Home Assignment 3", JOptionPane.QUESTION_MESSAGE));
			if (!scientist[index].setNumOfPaper(numOfPaper))
				JOptionPane.showMessageDialog(null, "Sorry! The number is negative. Please enter it again.", 
						"Take-Home Assignment 3", JOptionPane.ERROR_MESSAGE);
		}while(!scientist[index].setNumOfPaper(numOfPaper));
	}
	
	private static void modifyProfile(Scientist[] scientist, int index){
		int positionRank = 0, numOfPaper = 0, choice = 0;
		String applicantName = "", instituteName = "", departmentName = "", researchTopic = "";
		double pastFunding = 0;
		
		choice = Integer.parseInt(JOptionPane.showInputDialog(null, "Please choose an iterm that you want to " +
				"update:\n1) Applicant Name\n2) Institute Name\n3) Department Name\n4) Position\n5) Research " +
				"Topic\n6) Past Funding\n7) Num of Papers Published", "Take-Home Assignment 3", 
				JOptionPane.QUESTION_MESSAGE));
		switch(choice){
			case 1:
				do{
					applicantName = JOptionPane.showInputDialog(null, "Please enter your new name:", 
							"Take-Home Assignment 3", JOptionPane.QUESTION_MESSAGE);
					if (!scientist[index].setApplicantName(applicantName))
						JOptionPane.showMessageDialog(null, "Sorry! The name is empty. Please enter it again.", 
								"Take-Home Assignment 3", JOptionPane.ERROR_MESSAGE);
				}while(!scientist[index].setApplicantName(applicantName));
				JOptionPane.showMessageDialog(null, "Your name has been updated.", "Take-Home Assignment 3", 
						JOptionPane.INFORMATION_MESSAGE);
			break;
			
			case 2:
				do{
					instituteName = JOptionPane.showInputDialog(null, "Please enter your new institute:", 
							"Take-Home Assignment 3", JOptionPane.QUESTION_MESSAGE);
					if (!scientist[index].setInstituteName(instituteName))
						JOptionPane.showMessageDialog(null, "Sorry! The name is empty. Please enter it again.", 
								"Take-Home Assignment 3", JOptionPane.ERROR_MESSAGE);
				}while(!scientist[index].setInstituteName(instituteName));
				JOptionPane.showMessageDialog(null, "Your institute has been updated.", "Take-Home Assignment 3", 
						JOptionPane.INFORMATION_MESSAGE);
			break;
			
			case 3:
				do{
					departmentName = JOptionPane.showInputDialog(null, "Please enter your new department name:", 
							"Take-Home Assignment 3", JOptionPane.QUESTION_MESSAGE);
					if (!scientist[index].setDepartmentName(departmentName))
						JOptionPane.showMessageDialog(null, "Sorry! The name is empty. Please enter it again.", 
								"Take-Home Assignment 3", JOptionPane.ERROR_MESSAGE);
				}while(!scientist[index].setDepartmentName(departmentName));
				JOptionPane.showMessageDialog(null, "Your department has been updated.", "Take-Home Assignment 3", 
						JOptionPane.INFORMATION_MESSAGE);
			break;
			
			case 4:
				do{
					positionRank = Integer.parseInt(JOptionPane.showInputDialog(null, "Please choose your position:" +
							"\n1) Full Professor\n2) Associate Professor\n3) Assistant Professor\n4) Others", 
							"Take-Home Assignment 3", JOptionPane.QUESTION_MESSAGE));
					if (!scientist[index].setPositionRank(positionRank))
						JOptionPane.showMessageDialog(null, "Sorry! The position choice is not one of the options. " +
								"Please enter it again.", "Take-Home Assignment 3", JOptionPane.ERROR_MESSAGE);
				}while(!scientist[index].setPositionRank(positionRank));
				JOptionPane.showMessageDialog(null, "Your position has been updated.", "Take-Home Assignment 3", 
						JOptionPane.INFORMATION_MESSAGE);
			break;
			
			case 5:
				do{
					researchTopic = JOptionPane.showInputDialog(null, "Please enter your new research topic:", 
							"Take-Home Assignment 3", JOptionPane.QUESTION_MESSAGE);
					if (!scientist[index].setResearchTopic(researchTopic))
						JOptionPane.showMessageDialog(null, "Sorry! The topic is empty. Please enter it again.", 
								"Take-Home Assignment 3", JOptionPane.ERROR_MESSAGE);
				}while(!scientist[index].setResearchTopic(researchTopic));
				JOptionPane.showMessageDialog(null, "Your research topic has been updated.", "Take-Home Assignment 3", 
						JOptionPane.INFORMATION_MESSAGE);
			break;
			
			case 6:
				do{
					pastFunding = Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter your funding " +
							"awarded:", "Take-Home Assignment 3", JOptionPane.QUESTION_MESSAGE));
					if (!scientist[index].setPastFunding(pastFunding))
						JOptionPane.showMessageDialog(null, "Sorry! The funding is negative. Please enter it again.", 
								"Take-Home Assignment 3", JOptionPane.ERROR_MESSAGE);
				}while(!scientist[index].setPastFunding(pastFunding));
				JOptionPane.showMessageDialog(null, "Your funding has been updated.", "Take-Home Assignment 3", 
						JOptionPane.INFORMATION_MESSAGE);
			break;
			
			case 7:
				do{
					numOfPaper = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter your number of paper " +
							"published:", "Take-Home Assignment 3", JOptionPane.QUESTION_MESSAGE));
					if (!scientist[index].setNumOfPaper(numOfPaper))
						JOptionPane.showMessageDialog(null, "Sorry! The number is negative. Please enter it again.", 
								"Take-Home Assignment 3", JOptionPane.ERROR_MESSAGE);
				}while(!scientist[index].setNumOfPaper(numOfPaper));				
				JOptionPane.showMessageDialog(null, "The number of papers published has been updated.", "Take-Home " +
						"Assignment 3", JOptionPane.INFORMATION_MESSAGE);
			break;
		}	
	}	
		
	private static void displayScientistProfile(Scientist[] scientist, int index){
		JOptionPane.showMessageDialog(null, scientist[index].toString(), "Take-Home Assignment 3", 
				JOptionPane.INFORMATION_MESSAGE);
	}
	
	private static void displayApplicationResult(Scientist[] scientist, int index){
		NumberFormat currency = NumberFormat.getCurrencyInstance();
		currency.setMinimumFractionDigits(2);
		currency.setMaximumFractionDigits(2);
		if (scientist[index].getNewAwardFunding() == 0)
			JOptionPane.showMessageDialog(null, "Sorry! You have not had any new funding.", "Take-Home Assignment 3", 
					JOptionPane.INFORMATION_MESSAGE);
		else
			JOptionPane.showMessageDialog(null, "Congratulation! You received a new funding, " + 
		currency.format(scientist[index].getNewAwardFunding()) + ".", "Take-Home Assignment 3", 
		JOptionPane.INFORMATION_MESSAGE);
	}
}
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by StephNicolaou
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.