Guest User

Untitled

a guest
Dec 4th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package custompcrusphase4;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import javax.swing.JOptionPane;
  6.  
  7. /**
  8.  * @author Aaron Hendrickx
  9.  */
  10. public class CustomPCrUsPhase4 {
  11.     public static void main(String[] args) {
  12.         // Show the welcome message.
  13.         String welcomeMessage = new StringBuilder()
  14.                 .append("*** Welcome to the Custom PC-R-Us Online Ordering System ***\n\n")
  15.                 .append("You are now able to order your PC through online custom pc design system\n\n")
  16.                 .append("The PC options you can choose from are: Screen Size, CPU, RAM, HDD, and GPU")
  17.                 .toString();
  18.         JOptionPane.showMessageDialog(null, welcomeMessage);
  19.  
  20.         // The current username and password.
  21.         String username = null;
  22.         String password = null;
  23.  
  24.         // Is this a returning user?
  25.         int returning = JOptionPane.showConfirmDialog(null, "Are you a returning customer?");
  26.         switch (returning) {
  27.             case JOptionPane.YES_OPTION:
  28.                 username = requestString("Enter your username:");
  29.                 password = requestString("Enter your password:");
  30.                 break;
  31.  
  32.             case JOptionPane.NO_OPTION:
  33.                 username = requestString("Choose a username for your new account:");
  34.                 password = requestString("Choose a password for your new account:");
  35.                 break;
  36.  
  37.             case JOptionPane.CANCEL_OPTION:
  38.                 JOptionPane.showMessageDialog(null, "Your order has been cancelled.");
  39.                 return;
  40.         }
  41.  
  42.         // Request the customer's contact information:
  43.         String name          = requestString("Please enter your full name:");
  44.         String phoneNumber   = requestString("Please enter your phone number:");
  45.         String streetAddress = requestString("Please enter your street address:");
  46.         String city          = requestString("Please enter your city:");
  47.         String state         = requestString("Please enter your state:");
  48.         String zip           = requestString("Please enter your zip code:");
  49.  
  50.         // Request Custom PC specifications:
  51.         int screenSize = requestInt(1, 2, new StringBuilder()
  52.                 .append("Please select a screen size:\n\n")
  53.                 .append("Valid options are:\n")
  54.                 .append("1 - 15\"\n")
  55.                 .append("2 - 17\"\n")
  56.                 .toString());
  57.         int cpuType = requestInt(1, 2, new StringBuilder()
  58.                 .append("Please select a CPU type:\n\n")
  59.                 .append("Valid options are:\n")
  60.                 .append("1 - AMD\n")
  61.                 .append("2 - Intel\n")
  62.                 .toString());
  63.         int ramSize = requestInt(1, 4, new StringBuilder()
  64.                 .append("Please select the total amount of RAM:\n\n")
  65.                 .append("Valid options are:\n")
  66.                 .append("1 - 4 GB\n")
  67.                 .append("2 - 8 GB\n")
  68.                 .append("3 - 16 GB\n")
  69.                 .append("4 - 32 GB\n")
  70.                 .toString());
  71.         int hddSize = requestInt(1, 2, new StringBuilder()
  72.                 .append("Please select a HDD size:\n\n")
  73.                 .append("Valid options are:\n")
  74.                 .append("1 - 500GB HDD\n")
  75.                 .append("2 - 500GB HDD w/ 100GB SSD\n")
  76.                 .toString());
  77.         int gpuSize = requestInt(1, 2, new StringBuilder()
  78.                 .append("Please select GPU size:\n\n")
  79.                 .append("1 - 512 MB\n")
  80.                 .append("2 - 1 GB\n")
  81.                 .toString());
  82.         int quantity = requestInt(1, 99, "Please enter the desired quantity (1 - 99):");
  83.        
  84.         try {
  85.             // Open the output file.
  86.             PrintWriter writer = new PrintWriter("Order.txt");
  87.  
  88.             // Write the custom information:
  89.             writer.println(new StringBuilder()
  90.                     .append(name).append(", ")
  91.                     .append(phoneNumber).append(", ")
  92.                     .append(streetAddress).append(", ")
  93.                     .append(city).append(", ")
  94.                     .append(state).append(", ")
  95.                     .append(zip).append(", ")
  96.                     .append(quantity).append(", ")
  97.                     .append(screenSize).append(", ")
  98.                     .append(cpuType).append(", ")
  99.                     .append(ramSize).append(", ")
  100.                     .append(hddSize).append(",")
  101.                     .append (gpuSize)
  102.                     .toString());
  103.  
  104.             // Flush and close the writer.
  105.             writer.flush();
  106.             writer.close();
  107.         } catch (IOException e) {
  108.             JOptionPane.showMessageDialog(null, "An internal error occurred, please try again later.");
  109.             System.exit(-1);
  110.         }
  111.  
  112.         // Show the order complete message:
  113.         String completeMessage = new StringBuilder()
  114.                 .append("*** Thank you for ordering from Custom PC-R-Us ***\n\n")
  115.                 .append("Your order has been received and will be processed and shipped to:\n")
  116.                 .append("Name: ").append(name).append("\n")
  117.                 .append("Phone Number: ").append(phoneNumber).append("\n")
  118.                 .append("Street Address: ").append(streetAddress).append("\n")
  119.                 .append("City: ").append(city).append("\n")
  120.                 .append("State: ").append(state).append("\n")
  121.                 .append("Zip: ").append(zip).append("\n")
  122.                 .toString();
  123.         JOptionPane.showMessageDialog(null, completeMessage);
  124.     }
  125.  
  126.     private static int requestInt(int min, int max, String message) {
  127.         for (int i = 0; i < 3; i++) {
  128.             try {
  129.                 int result = Integer.parseInt(JOptionPane.showInputDialog(message));
  130.  
  131.                 // Accept the value if the value is within the specified minimum and maximum range.
  132.                 if (result >= min && result <= max) {
  133.                     return result;
  134.                 }
  135.             } catch (NullPointerException e1) {
  136.                 // The order has been cancelled.
  137.                 JOptionPane.showMessageDialog(null, "Your order has been cancelled.");
  138.                 System.exit(-1);
  139.             } catch (NumberFormatException e2) {
  140.                 // Ignore this exception.
  141.             }
  142.         }
  143.  
  144.         // The customer failed to enter the appropriate data if this point has been reached.
  145.         JOptionPane.showMessageDialog(null, "Maximum tries exceeded, please try again later.");
  146.         System.exit(-1);
  147.  
  148.         // This point should never be reached.
  149.         return -1;
  150.     }
  151.  
  152.     private static String requestString(String message) {
  153.         for (int i = 0; i < 3; i++) {
  154.             // Show the input dialog.
  155.             String result = JOptionPane.showInputDialog(message);
  156.  
  157.             // Allow the customer to cancel the order at any stage.
  158.             if (result == null) {
  159.                 JOptionPane.showMessageDialog(null, "Your order has been cancelled.");
  160.                 System.exit(-1);
  161.             }
  162.  
  163.             // Accept the user's input if the result is not empty.
  164.             if (!result.isEmpty()) {
  165.                 return result;
  166.             }
  167.         }
  168.  
  169.         // The customer failed to enter the appropriate data if this point has been reached.
  170.         JOptionPane.showMessageDialog(null, "Maximum tries exceeded, please try again later.");
  171.         System.exit(-1);
  172.  
  173.         // This point should never be reached.
  174.         return null;
  175.     }
  176. }
Add Comment
Please, Sign In to add comment