Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package custompcrusphase4;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.swing.JOptionPane;
- /**
- * @author Aaron Hendrickx
- */
- public class CustomPCrUsPhase4 {
- public static void main(String[] args) {
- // Show the welcome message.
- String welcomeMessage = new StringBuilder()
- .append("*** Welcome to the Custom PC-R-Us Online Ordering System ***\n\n")
- .append("You are now able to order your PC through online custom pc design system\n\n")
- .append("The PC options you can choose from are: Screen Size, CPU, RAM, HDD, and GPU")
- .toString();
- JOptionPane.showMessageDialog(null, welcomeMessage);
- // The current username and password.
- String username = null;
- String password = null;
- // Is this a returning user?
- int returning = JOptionPane.showConfirmDialog(null, "Are you a returning customer?");
- switch (returning) {
- case JOptionPane.YES_OPTION:
- username = requestString("Enter your username:");
- password = requestString("Enter your password:");
- break;
- case JOptionPane.NO_OPTION:
- username = requestString("Choose a username for your new account:");
- password = requestString("Choose a password for your new account:");
- break;
- case JOptionPane.CANCEL_OPTION:
- JOptionPane.showMessageDialog(null, "Your order has been cancelled.");
- return;
- }
- // Request the customer's contact information:
- String name = requestString("Please enter your full name:");
- String phoneNumber = requestString("Please enter your phone number:");
- String streetAddress = requestString("Please enter your street address:");
- String city = requestString("Please enter your city:");
- String state = requestString("Please enter your state:");
- String zip = requestString("Please enter your zip code:");
- // Request Custom PC specifications:
- int screenSize = requestInt(1, 2, new StringBuilder()
- .append("Please select a screen size:\n\n")
- .append("Valid options are:\n")
- .append("1 - 15\"\n")
- .append("2 - 17\"\n")
- .toString());
- int cpuType = requestInt(1, 2, new StringBuilder()
- .append("Please select a CPU type:\n\n")
- .append("Valid options are:\n")
- .append("1 - AMD\n")
- .append("2 - Intel\n")
- .toString());
- int ramSize = requestInt(1, 4, new StringBuilder()
- .append("Please select the total amount of RAM:\n\n")
- .append("Valid options are:\n")
- .append("1 - 4 GB\n")
- .append("2 - 8 GB\n")
- .append("3 - 16 GB\n")
- .append("4 - 32 GB\n")
- .toString());
- int hddSize = requestInt(1, 2, new StringBuilder()
- .append("Please select a HDD size:\n\n")
- .append("Valid options are:\n")
- .append("1 - 500GB HDD\n")
- .append("2 - 500GB HDD w/ 100GB SSD\n")
- .toString());
- int gpuSize = requestInt(1, 2, new StringBuilder()
- .append("Please select GPU size:\n\n")
- .append("1 - 512 MB\n")
- .append("2 - 1 GB\n")
- .toString());
- int quantity = requestInt(1, 99, "Please enter the desired quantity (1 - 99):");
- try {
- // Open the output file.
- PrintWriter writer = new PrintWriter("Order.txt");
- // Write the custom information:
- writer.println(new StringBuilder()
- .append(name).append(", ")
- .append(phoneNumber).append(", ")
- .append(streetAddress).append(", ")
- .append(city).append(", ")
- .append(state).append(", ")
- .append(zip).append(", ")
- .append(quantity).append(", ")
- .append(screenSize).append(", ")
- .append(cpuType).append(", ")
- .append(ramSize).append(", ")
- .append(hddSize).append(",")
- .append (gpuSize)
- .toString());
- // Flush and close the writer.
- writer.flush();
- writer.close();
- } catch (IOException e) {
- JOptionPane.showMessageDialog(null, "An internal error occurred, please try again later.");
- System.exit(-1);
- }
- // Show the order complete message:
- String completeMessage = new StringBuilder()
- .append("*** Thank you for ordering from Custom PC-R-Us ***\n\n")
- .append("Your order has been received and will be processed and shipped to:\n")
- .append("Name: ").append(name).append("\n")
- .append("Phone Number: ").append(phoneNumber).append("\n")
- .append("Street Address: ").append(streetAddress).append("\n")
- .append("City: ").append(city).append("\n")
- .append("State: ").append(state).append("\n")
- .append("Zip: ").append(zip).append("\n")
- .toString();
- JOptionPane.showMessageDialog(null, completeMessage);
- }
- private static int requestInt(int min, int max, String message) {
- for (int i = 0; i < 3; i++) {
- try {
- int result = Integer.parseInt(JOptionPane.showInputDialog(message));
- // Accept the value if the value is within the specified minimum and maximum range.
- if (result >= min && result <= max) {
- return result;
- }
- } catch (NullPointerException e1) {
- // The order has been cancelled.
- JOptionPane.showMessageDialog(null, "Your order has been cancelled.");
- System.exit(-1);
- } catch (NumberFormatException e2) {
- // Ignore this exception.
- }
- }
- // The customer failed to enter the appropriate data if this point has been reached.
- JOptionPane.showMessageDialog(null, "Maximum tries exceeded, please try again later.");
- System.exit(-1);
- // This point should never be reached.
- return -1;
- }
- private static String requestString(String message) {
- for (int i = 0; i < 3; i++) {
- // Show the input dialog.
- String result = JOptionPane.showInputDialog(message);
- // Allow the customer to cancel the order at any stage.
- if (result == null) {
- JOptionPane.showMessageDialog(null, "Your order has been cancelled.");
- System.exit(-1);
- }
- // Accept the user's input if the result is not empty.
- if (!result.isEmpty()) {
- return result;
- }
- }
- // The customer failed to enter the appropriate data if this point has been reached.
- JOptionPane.showMessageDialog(null, "Maximum tries exceeded, please try again later.");
- System.exit(-1);
- // This point should never be reached.
- return null;
- }
- }
Add Comment
Please, Sign In to add comment