All pastes #948579 Raw Edit

Someone

public text v1 · immutable
·published 2008-03-19 07:47 UTC
rendered paste body
import javax.swing.*;

public class Program6Student {

	public static final String YOUR_NAME = "Roshan Patel";

	/** It makes sure a non-negative value was inserted */
	
	public static int getNonNegativeIntOrQuit(String inputString) {
		int inputtedInteger = 0;
		String input = "";
	   int count = 1;
		do {
			if(count > 1) {
					JOptionPane.showMessageDialog(null, "Your input cannot be " +
						"negative.", "Negative Input Error", JOptionPane.ERROR_MESSAGE);
					input = JOptionPane.showInputDialog(null, inputString,
					 	 "Input", JOptionPane.INFORMATION_MESSAGE);	

			}
			else {
					input = JOptionPane.showInputDialog(null, inputString,
					"Input", JOptionPane.INFORMATION_MESSAGE);
					count++;
			}
			getStringOrQuit(input);
			inputtedInteger = Integer.parseInt(input);
		} while (inputtedInteger < 0);

		  return inputtedInteger;
	}
	
	/** Checks to see if cancel was hit. If it was not, returns string.
	 * If it was, a message is posted to the user saying cancel was hit.
	 */
	 
	public static String getStringOrQuit(String inputString) {
		if (inputString == null) {

			JOptionPane.showMessageDialog(null,
					"Cancel was selected. Program will exit",
					"Program 6 (Roshan Patel)", JOptionPane.WARNING_MESSAGE);

			System.exit(0);
		}
		return inputString;
	}
	
	/** Fills in the array with the entries the user entered. */
	
	public static void fillArray(String[] databaseOfSoftware) {
		for (int i = 0; i < databaseOfSoftware.length; i++) {
			databaseOfSoftware[i] = getStringOrQuit(JOptionPane
					.showInputDialog(
							null,
							"Enter the software title and cost(eg \"Windows Vista, $135.00\")",
							"Input", JOptionPane.QUESTION_MESSAGE));
		}
	}
	
	/** Use bubble sort to move the invalid values to the back. */
	
	public static void sortArrayByValidInvalid(String[] databaseOfSoftware) {
		for (int i = 1; i < databaseOfSoftware.length; i++) {
			for (int j = 0; j < databaseOfSoftware.length - i; j++) {
				if (!(databaseOfSoftware[j].contains("$") && databaseOfSoftware[j]
						.contains(","))) {
					String tempInt = databaseOfSoftware[j + 1];
					databaseOfSoftware[j + 1] = databaseOfSoftware[j];
					databaseOfSoftware[j] = tempInt;
				}
			}
		}
	}
	
	/** Makes all the invalid entries null. */
	
	public static int removeInvalidItems(String[] databaseOfSoftware) {
		int lastIndex;
		for (int i = 0; i < databaseOfSoftware.length; i++) {
			if (!(databaseOfSoftware[i].contains("$") && databaseOfSoftware[i]
					.contains(","))) {
				lastIndex = i - 1;
				for (int k = i; k < databaseOfSoftware.length; k++) {
					databaseOfSoftware[k] = null;
				}
				return lastIndex;
			}

		}
		return databaseOfSoftware.length - 1;
	}
	
	/** Sorts the array in alphabetical order. */
	
	public static void sortArrayByName(String[] databaseOfSoftware,
			int lastIndex) {
			
		/* Note it is not lastIndex - 1 because
		   the final index is also one that we
		   would like to include. */
			
		for (int i = 0; i < lastIndex; i++) { 
														  
			for (int j = 0; j < lastIndex - i; j++) {
				String title = databaseOfSoftware[j].substring(0,
						databaseOfSoftware[j].indexOf(","));
				if ((title.compareToIgnoreCase(databaseOfSoftware[j + 1]
						.substring(0, databaseOfSoftware[j + 1].indexOf(",")))) < 0) {
					String tempInt = databaseOfSoftware[j];
					databaseOfSoftware[j] = databaseOfSoftware[j + 1];
					databaseOfSoftware[j + 1] = tempInt;
				}
			}
		}
	}
	
	/** Displays the valid entries to the user. */
	
	public static void displayArray(String[] databaseOfSoftware, int lastIndex) {
		String output = "";
		for (int i = lastIndex; i >= 0; i--) {
			output += databaseOfSoftware[i] + "\n";
		}
		JOptionPane.showMessageDialog(null, output, "Program 6 (Roshan Patel)",
				JOptionPane.INFORMATION_MESSAGE);
	}
	
	/** Adds up the total to all the software. */
	
	public static double sumTotal(String[] databaseOfSoftware, int lastIndex) {
		double sum = 0;
		for (int i = lastIndex; i >= 0; i--) {
			
			sum += Double.parseDouble(databaseOfSoftware[i].substring(
					databaseOfSoftware[i].indexOf("$") + 1, 
					databaseOfSoftware[i].length()));
		}
		return sum;
	}

	/** Searches the database for the software title. */
	
	public static int searchArray(String[] databaseOfSoftware, String title,
			int lastIndex) {
		String searchString = JOptionPane.showInputDialog(null, title, "Roshan Patel (CIS 120)",
				JOptionPane.QUESTION_MESSAGE);
		searchString = getStringOrQuit(searchString);
		for (int i = lastIndex; i >= 0; i--) {
			if (searchString.equalsIgnoreCase(databaseOfSoftware[i].substring(
					0, databaseOfSoftware[i].indexOf(","))) == true) {
				return i;
			}
		}
		return -1;
	}

	// You should have no reason to alter the main method below, except perhaps
	// to comment out portions of the code as you implement one method at a
	// time.
	
	public static void main(String[] args) {

		int size;
		int lastInUse;
		int response;

		size = getNonNegativeIntOrQuit("How many software titles would you like to"
				+ " enter into\n the price list system? (must be non-negative)");

		if (size == 0) {
			JOptionPane.showMessageDialog(null,
					"You entered 0. The program will exit.", "Program 6 ("
							+ YOUR_NAME + ")", JOptionPane.WARNING_MESSAGE);
			System.exit(0);
		}

		lastInUse = size - 1;

		String[] itemsArray = new String[size];

		fillArray(itemsArray);

		sortArrayByValidInvalid(itemsArray);

		lastInUse = removeInvalidItems(itemsArray);

		sortArrayByName(itemsArray, lastInUse);

		displayArray(itemsArray, lastInUse);

		JOptionPane.showMessageDialog(null, "The total cost is $"
				+ String.format("%.2f", sumTotal(itemsArray, lastInUse)));

		do {
			int index = searchArray(itemsArray,
					getStringOrQuit("Enter the name of "
							+ "an item you would like to search for"),
					lastInUse);

			if (index != -1) {
				JOptionPane.showMessageDialog(null, "Item listing found:\n"
						+ itemsArray[index], "Program 6 (" + YOUR_NAME + ")",
						JOptionPane.PLAIN_MESSAGE);
			} else {
				JOptionPane.showMessageDialog(null, "Item listing not found.",
						"Program 6 (" + YOUR_NAME + ")",
						JOptionPane.WARNING_MESSAGE);
			}
			response = JOptionPane.showConfirmDialog(null,
					"Would you like to perform another search?", "Program 6 ("
							+ YOUR_NAME + ")", JOptionPane.YES_NO_OPTION);

		} while (response == JOptionPane.YES_OPTION);

		System.exit(0);

	} // main

} // Program 6