Advertisement
jmccasusi

display

Apr 23rd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 23.40 KB | None | 0 0
  1. package view;
  2.  
  3. import java.text.DecimalFormat;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import exception.InvalidItemException;
  8.  
  9. import java.io.IOException;
  10.  
  11. import utility.CodeTranslator;
  12. import utility.ElixirValues;
  13. import utility.PotionValues;
  14. import utility.ShopHelper;
  15. import utility.TrinketValues;
  16. import model.Inventory;
  17.  
  18. public class Display {
  19.    
  20.     static String previousInput = "";
  21.     static boolean shopOpen = true;
  22.     static int shopState = 0;
  23.     final static int numPotionTypes = 3;
  24.     final static int numElixirTypes = 3;
  25.     final static int numTrinketTypes = 3;
  26.     final static int numAllTypes = numPotionTypes + numElixirTypes + numTrinketTypes;
  27.    
  28.     //constant variables for general
  29.     final static int TOTAL_SCREEN_WIDTH = 100;
  30.     static List<ArrayList<String>> itemCart = ShopHelper.getCart().getItems();
  31.    
  32.     //constant variables for shopInfoScreen
  33.     final static String shopName = "Wiz Magic Item Shop";
  34.     final static String shopAddress = "Axel, Belzerg";
  35.     final static String shopContact = "+8190-1790-1357";
  36.    
  37.    
  38.     //constant variables for itemListScreen
  39.     final static int CODE_NAME_SPACING = 3;
  40.     final static int NAME_COUNT_SPACING = 3;
  41.     final static int COUNT_PRICE_SPACING = 4;
  42.    
  43.     final static int CODE_CHAR_SLOT = 5;
  44.     final static int NAME_CHAR_SLOT = 18;
  45.     final static int COUNT_CHAR_SLOT = 5;
  46.     final static int PRICE_CHAR_SLOT = 6;
  47.    
  48.     final static int SHOP_ITEM_LIST_COLUMN_FIELD = 50;
  49.     final static int CART_ITEM_LIST_COLUMN_FIELD = 50;
  50.    
  51.     //sample values for messageScreen
  52.     static String longMessage = "Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden flows by their place and supplies it with the necessary regelialia. It is a paradisematic country, in which roasted parts of sentences fly into your mouth. Even the all-powerful Pointing has no control about the blind texts it is an almost unorthographic life One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far World of Grammar. The Big Oxmox advised her not to do so, because there were thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind Text didn�ft listen. She packed her seven versalia, put her initial into the belt and made herself on the way. When she reached the first hills of the Italic Mountains, she had a last view back on the skyline of her hometown Bookmarksgrove, the headline of Alphabet Village and the subline of her own road, the Line Lane. Pityful a rethoric question ran over her cheek, then";
  53.    
  54.     //constant variables for inputScreen
  55.     final static String addItemString = "A - ADD ITEM";
  56.     final static String removeItemString = "R - REMOVE ITEM";
  57.     final static String describeItemString = "D - DESCRIBE ITEM";
  58.     final static String checkoutCartString = "C - CHECKOUT";
  59.    
  60.        
  61.     //display-screen loader
  62.     public static void loadScreen(int screenCode) throws IOException {
  63.        
  64.         bufferScreen();
  65.        
  66.         switch(screenCode) {
  67.         case 1:
  68.             screenBuilder();
  69.             break;
  70.         case 2:
  71.             itemMenuScreen();
  72.             break;
  73.         default:
  74.             break;
  75.         }
  76.        
  77.     }
  78.    
  79.     //pushes up the previous screens / imitating a clearScreen function
  80.     private static void bufferScreen() {
  81.         for(int i=0; i < 70 ; i++)
  82.             System.out.println();
  83.     }
  84.  
  85.     //compiles all panel into one whole screen
  86.     private static void screenBuilder() throws IOException {
  87.        
  88.         bufferScreen();
  89.         shopInfoScreen();
  90.         itemListScreen();
  91.         messageScreen(longMessage);
  92.         inputScreen();
  93.     }
  94.    
  95.     //panel displaying shop info
  96.     private static void shopInfoScreen()
  97.     {
  98.         final int SHOP_INFO_ROW_COUNT = 6;
  99.        
  100.         for(int i=0; i < SHOP_INFO_ROW_COUNT ; i++)
  101.         {
  102.             if(i==0)
  103.             {
  104.                 for(int x = 0; x < TOTAL_SCREEN_WIDTH; x++)
  105.                 {
  106.                     System.out.print("=");
  107.                 }
  108.                 System.out.println();
  109.             }
  110.             else if(i>1 && i<=4)
  111.             {
  112.                 switch(i)
  113.                 {
  114.                 case 2:
  115.                     System.out.format("%100s", CenterAlignLeftTextInField(shopName,TOTAL_SCREEN_WIDTH));
  116.                     System.out.println();
  117.                     break;
  118.                 case 3:
  119.                     System.out.format("%100s", CenterAlignLeftTextInField(shopAddress,TOTAL_SCREEN_WIDTH));
  120.                     System.out.println();
  121.                     break;
  122.                 default:
  123.                     System.out.format("%100s", CenterAlignLeftTextInField(shopContact,TOTAL_SCREEN_WIDTH));
  124.                     System.out.println();
  125.                     break;
  126.                 }
  127.             }
  128.             else
  129.             {
  130.                 System.out.println();
  131.             }
  132.         }
  133.        
  134.     }
  135.    
  136.     //panel displaying the item lists (shop items and items in cart)
  137.     private static void itemListScreen()
  138.     {
  139.         itemCart = ShopHelper.getCart().getItems();
  140.        
  141.         final int ITEM_LIST_ROW_COUNT = 20;
  142.         final int ITEM_LIST_HEADER = 2;
  143.         final int ITEM_LIST_COLUMN_TITLE = 4;
  144.         final int ITEM_LIST_BEGIN = 6;
  145.        
  146.         String word1 = "SHOP ITEM LIST";
  147.         String word2 = "ITEMS IN CART";
  148.        
  149.         for(int i=0; i < ITEM_LIST_ROW_COUNT ; i++)
  150.         {
  151.             if(i==0)
  152.             {
  153.                 for(int x = 0; x < TOTAL_SCREEN_WIDTH; x++)
  154.                 {
  155.                     System.out.print("=");
  156.                 }
  157.                 System.out.println();
  158.             }
  159.             // START DISPLAYING HEADER
  160.             else if(i==ITEM_LIST_HEADER)
  161.             {
  162.                 System.out.format("%50s%1s", CenterAlignLeftTextInField(word1,SHOP_ITEM_LIST_COLUMN_FIELD),"|");
  163.                 System.out.format("%50s", CenterAlignLeftTextInField(word2,CART_ITEM_LIST_COLUMN_FIELD));
  164.                 System.out.println();
  165.             }
  166.             // START DISPLAYING COLUMN TITLE
  167.             else if(i==ITEM_LIST_COLUMN_TITLE)
  168.             {
  169.                 String stringBuild01 = stringLineBuilder(1,SHOP_ITEM_LIST_COLUMN_FIELD,"CODE","ITEM NAME","STOCK", "PRICE");
  170.                 String stringBuild02 = stringLineBuilder(2,CART_ITEM_LIST_COLUMN_FIELD,"CODE","ITEM NAME","COUNT", "PRICE");
  171.                
  172.                 System.out.format("%50s%1s", LeftAlignLeftTextInField(stringBuild01,SHOP_ITEM_LIST_COLUMN_FIELD),"|");
  173.                 System.out.format("%50s", LeftAlignLeftTextInField(stringBuild02,CART_ITEM_LIST_COLUMN_FIELD));
  174.                 System.out.println();
  175.             }
  176.             // START DISPLAYING THE ITEM LIST
  177.             else if(i>=ITEM_LIST_BEGIN)
  178.             {
  179.                 String type = "";
  180.                 if(i>=ITEM_LIST_BEGIN && (i<(numAllTypes+ITEM_LIST_BEGIN)))
  181.                 {
  182.                     ////////////////////////////// FOR SHOP ITEM LIST /////////////
  183.                     int k = 1;
  184.                     String[] print = PotionValues.toString(type);
  185.                     Inventory inv = new Inventory();
  186.                    
  187.                     for(int j = 1; j <= numAllTypes ; j++)
  188.                     {
  189.                         int stockCount = 0;
  190.                         if(j<=numPotionTypes)
  191.                         {
  192.                             type = "P";
  193.                             type += k;
  194.                             stockCount = inv.checkStock(type);
  195.                             k++;
  196.                             print = PotionValues.toString(type);
  197.                         }
  198.                         else if(j <= numPotionTypes+numElixirTypes)
  199.                         {
  200.                             if(k > numPotionTypes)
  201.                             {
  202.                                 k = 1;
  203.                             }
  204.                             type = "E";
  205.                             type += k;
  206.                             stockCount = inv.checkStock(type);
  207.                             k++;
  208.                             print = ElixirValues.toString(type);
  209.                         }
  210.                         else if(j <= numPotionTypes+numElixirTypes+numTrinketTypes)
  211.                         {
  212.                             if(k > numElixirTypes)
  213.                             {
  214.                                 k =1;
  215.                             }
  216.                             type = "T";
  217.                             type += k;
  218.                             stockCount = inv.checkStock(type);
  219.                             k++;
  220.                             print = TrinketValues.toString(type);
  221.                         }
  222.                        
  223.                         String stringBuild1 = stringLineBuilder(1,CART_ITEM_LIST_COLUMN_FIELD,Integer.parseInt(print[3]),print[0],stockCount,Double.parseDouble(print[2]));
  224.                        
  225.                         ////////////////////////////////////////////////////////////////
  226.                        
  227.                         ////////////////////////////// FOR CART LIST //////////////////////////
  228.                    
  229.                         String stringBuild2 = "";
  230.                         if(j <= itemCart.size())
  231.                         {
  232.                             stringBuild2 = stringLineBuilder(2,CART_ITEM_LIST_COLUMN_FIELD,Integer.parseInt(itemCart.get(j-1).get(3)),itemCart.get(j-1).get(0),Integer.parseInt(itemCart.get(j-1).get(1)),Double.parseDouble(itemCart.get(j-1).get(2)));
  233.                         }
  234.                        
  235.                         ////////////////////////////////////////////////////////////////////////
  236.                        
  237.                         System.out.format("%50s%1s", LeftAlignLeftTextInField(stringBuild1,SHOP_ITEM_LIST_COLUMN_FIELD),"|");
  238.                         System.out.format("%50s", LeftAlignLeftTextInField(stringBuild2,CART_ITEM_LIST_COLUMN_FIELD));
  239.                        
  240.                        
  241.                        
  242.                         System.out.println();
  243.                        
  244.                         type = type.substring(0, type.length()-1);
  245.                         i++;
  246.                     }
  247.                 }
  248.                 else
  249.                 {
  250.                     // if you want to see number of rows
  251.                     //System.out.print(i);
  252.                    
  253.                     /*
  254.                     if(i>=10)
  255.                     {
  256.                         System.out.format("%49s", "|");
  257.                     }
  258.                     else
  259.                     {
  260.                         System.out.format("%50s", "|");
  261.                     }
  262.                     */
  263.                    
  264.                     System.out.format("%51s", "|");
  265.                     System.out.println();
  266.                 }
  267.             }
  268.             else
  269.             {
  270.                 // if you want to see number of rows
  271.                 // System.out.print(i);
  272.                
  273.                 /*
  274.                 if(i>=10)
  275.                 {
  276.                     System.out.format("%49s", "|");
  277.                 }
  278.                 else
  279.                 {
  280.                     System.out.format("%50s", "|");
  281.                 }
  282.                 */
  283.                
  284.                 System.out.format("%51s", "|");
  285.                 System.out.println();
  286.             }
  287.            
  288.         }
  289.     }
  290.    
  291.     private static void messageScreen(String message)
  292.     {
  293.         final int MESSAGE_SCREEN_ROW_COUNT = 7;
  294.         final int MESSAGE_SLOT = TOTAL_SCREEN_WIDTH - 14;
  295.        
  296.         char[] messageArray = message.toCharArray();  
  297.         String[] words = new String[70];
  298.        
  299.         int wordCount = 0;
  300.        
  301.         for(int f = 0; f < messageArray.length-1 ; f++)
  302.         {
  303.             if(messageArray[f]==' ')
  304.             {
  305.                 wordCount++;
  306.             }
  307.         }
  308.        
  309.         int x = 0;
  310.         int m = 0;
  311.         int j = 0;
  312.        
  313.         for(int y = 0; y < words.length-1; y++)
  314.         {
  315.             String word = "";
  316.            
  317.             if(y < wordCount)
  318.             {
  319.                 for(; messageArray[x]!=' '; x++)
  320.                 {
  321.                     word += messageArray[x];
  322.                 }
  323.             }
  324.             else
  325.             {
  326.                 word =" ";
  327.             }
  328.             x++;
  329.            
  330.             words[y]=word;
  331.         }
  332.        
  333.         for(int i=0; i < MESSAGE_SCREEN_ROW_COUNT ; i++)
  334.             {
  335.                 if(i==0)
  336.                 {
  337.                     for(int k = 0; k < TOTAL_SCREEN_WIDTH; k++)
  338.                     {
  339.                         System.out.print("=");
  340.                     }
  341.                     System.out.println();
  342.                 }
  343.                 else if(i>1 && i<=5)
  344.                 {
  345.                     String displayMessage = "";
  346.                    
  347.                     switch(i)
  348.                     {
  349.                     case 2:
  350.                         for(; j < MESSAGE_SLOT; j++)
  351.                         {
  352.                             for(; displayMessage.length()<=MESSAGE_SLOT ; m++)
  353.                             {
  354.                                     displayMessage += (words[m] + " ");
  355.                             }
  356.                         }
  357.                         j++;
  358.                         break;
  359.                     case 3:
  360.                         for(; (j <= MESSAGE_SLOT*2)&&(j > MESSAGE_SLOT); j++)
  361.                         {
  362.                             for(; (displayMessage.length()<=MESSAGE_SLOT)&&(m<words.length-1) ; m++)
  363.                             {
  364.                                     displayMessage += (words[m] + " ");
  365.                             }
  366.                         }
  367.                         j++;
  368.                         break;
  369.                     case 4:
  370.                         for(; (j <= MESSAGE_SLOT*3)&&(j > MESSAGE_SLOT*2); j++)
  371.                         {
  372.                             for(; (displayMessage.length()<=MESSAGE_SLOT)&&(m<words.length-1) ; m++)
  373.                             {
  374.                                     displayMessage += (words[m] + " ");
  375.                             }
  376.                         }
  377.                         break;
  378.                     default:
  379.                         for(; (j <= MESSAGE_SLOT*4)&&(j > MESSAGE_SLOT*3); j++)
  380.                         {
  381.                             for(; (displayMessage.length()<=MESSAGE_SLOT)&&(m<words.length-1) ; m++)
  382.                             {
  383.                                     displayMessage += (words[m] + " ");
  384.                             }
  385.                         }
  386.                         break;
  387.                     }
  388.                    
  389.                     System.out.format("%100s", LeftAlignLeftTextInField(displayMessage,TOTAL_SCREEN_WIDTH));
  390.                     System.out.println();
  391.                 }
  392.                 else
  393.                 {
  394.                     System.out.println();
  395.                 }
  396.             }
  397.     }
  398.    
  399.     private static void inputScreen() throws IOException
  400.     {
  401.         final int INPUT_SCREEN_ROW_COUNT = 6;
  402.        
  403.         String shopStateString = Integer.toString(shopState);
  404.         char[] shopStateArray =  shopStateString.toCharArray();
  405.        
  406.         String inputPrompt = "";
  407.        
  408.         if(shopStateArray[0] == '4')
  409.         {
  410.             inputPrompt = "PLEASE ENTER CARD NUMBER";
  411.         }
  412.         else
  413.         {
  414.             if(shopState < 10 && shopState !=0)
  415.             {
  416.                 inputPrompt = " PLEASE ENTER ITEM CODE";
  417.             }
  418.             else if(shopState > 10 && shopState < 100)
  419.             {
  420.                 inputPrompt = " PLEASE ENTER AN AMOUNT";
  421.             }
  422.             else
  423.             {
  424.                 inputPrompt = "PLEASE SELECT AN OPTION";
  425.             }
  426.         }
  427.        
  428.        
  429.         for(int i=0; i < INPUT_SCREEN_ROW_COUNT ; i++)
  430.         {
  431.             if(i==0)
  432.             {
  433.                 for(int k = 0; k < TOTAL_SCREEN_WIDTH; k++)
  434.                 {
  435.                     System.out.print("=");
  436.                 }
  437.                 System.out.println();
  438.             }
  439.             else if(i==2)
  440.             {
  441.                 System.out.format("%25s%25s%25s%25s", CenterAlignLeftTextInField(addItemString,25),CenterAlignLeftTextInField(removeItemString,25),CenterAlignLeftTextInField(describeItemString,25),CenterAlignLeftTextInField(checkoutCartString,25));
  442.             }
  443.             else if(i==INPUT_SCREEN_ROW_COUNT-1)
  444.             {
  445.                 System.out.format("%85s", "===================================================     " + inputPrompt + ":     ");
  446.             }
  447.             else
  448.             {
  449.                 System.out.println();
  450.             }
  451.         }          
  452.     }
  453.    
  454.     private static void itemMenuScreen() {
  455.        
  456.     }
  457.    
  458.     //used by itemListScreen to build a formatted string per line
  459.     private static String stringLineBuilder(int column, int field, int itemCode, String itemName, int itemCount, double itemPrice)
  460.     {
  461.         String shopStateString = Integer.toString(shopState);
  462.         char[] shopStateArray =  shopStateString.toCharArray();
  463.        
  464.         if(shopStateArray[0]=='0')
  465.         {
  466.             itemCode = -1;
  467.         }
  468.         else if(shopStateArray[0]=='2')
  469.         {
  470.             if(column==1)
  471.             {
  472.                 itemCode = -1;
  473.             }
  474.         }
  475.         else if(shopStateArray[0]=='1')
  476.         {
  477.             if(column==2)
  478.             {
  479.                 itemCode = -1;
  480.             }
  481.         }
  482.        
  483.         String value = "";
  484.         DecimalFormat two = new DecimalFormat("0.00");
  485.        
  486.         String itemCodeString = Integer.toString(itemCode);
  487.         String itemCountString = Integer.toString(itemCount);
  488.         String itemPriceString = two.format(itemPrice);
  489.        
  490.         char[] itemNameArray =  itemName.toCharArray();
  491.         char[] itemCodeArray = itemCodeString.toCharArray();
  492.         char[] itemCountArray = itemCountString.toCharArray();
  493.         char[] itemPriceArray = itemPriceString.toCharArray();
  494.    
  495.         for(int i = 0; i < field; i++)
  496.         {
  497.             if(i==0)
  498.             {
  499.                 for(int j = 0; j < CODE_CHAR_SLOT; j++)
  500.                 {
  501.                     if(itemCodeString.length()>j)
  502.                     {
  503.                         if(itemCode<0)
  504.                         {
  505.                             value+= "  ";
  506.                             i++;
  507.                         }
  508.                         else if(itemCode>0 && itemCode<10)
  509.                         {
  510.                             value+= "0";
  511.                             i++;
  512.                             value+=itemCodeArray[j];
  513.                         }
  514.                         else
  515.                         {
  516.                             value+=itemCodeArray[j];
  517.                         }
  518.                         i++;
  519.                     }
  520.                     else
  521.                     {
  522.                         value+=" ";
  523.                         i++;
  524.                     }
  525.                 }
  526.             }
  527.             else if (i==(CODE_NAME_SPACING+CODE_CHAR_SLOT))
  528.             {
  529.                 for(int k = 0; k < NAME_CHAR_SLOT; k++)
  530.                 {
  531.                     if(itemName.length()>k)
  532.                     {
  533.                         value+=itemNameArray[k];
  534.                         i++;
  535.                     }
  536.                     else
  537.                     {
  538.                         value+=" ";
  539.                         i++;
  540.                     }
  541.                 }
  542.             }
  543.             else if (i==(CODE_NAME_SPACING+CODE_CHAR_SLOT+NAME_COUNT_SPACING+NAME_CHAR_SLOT))
  544.             {
  545.                 value+="x ";
  546.                 i+=2;
  547.                
  548.                 for(int m = 0; m < COUNT_CHAR_SLOT; m++)
  549.                 {
  550.                     if(itemCountString.length()>m)
  551.                     {
  552.                         value+=itemCountArray[m];
  553.                         i++;
  554.                     }
  555.                     else
  556.                     {
  557.                         value+=" ";
  558.                         i++;
  559.                     }
  560.                 }
  561.             }
  562.             else if (i==(CODE_NAME_SPACING+CODE_CHAR_SLOT+NAME_COUNT_SPACING+NAME_CHAR_SLOT+COUNT_PRICE_SPACING+PRICE_CHAR_SLOT))
  563.             {
  564.                 value+="P";
  565.                 i++;
  566.                
  567.                 for(int m = 0; m < PRICE_CHAR_SLOT; m++)
  568.                 {
  569.                     if(itemPriceString.length()>m)
  570.                     {
  571.                         value+=itemPriceArray[m];
  572.                         i++;
  573.                     }
  574.                     else
  575.                     {
  576.                         value+=" ";
  577.                         i++;
  578.                     }
  579.                 }
  580.             }
  581.             else
  582.             {
  583.                 value+=" ";
  584.             }
  585.            
  586.         }
  587.        
  588.         return value;
  589.     }
  590.    
  591.     //used by itemListScreen to build a formatted string per line (this one is overloaded)
  592.     private static String stringLineBuilder(int column, int field, String string1, String string2, String string3, String string4)
  593.     {
  594.        
  595.         String shopStateString = Integer.toString(shopState);
  596.         char[] shopStateArray =  shopStateString.toCharArray();
  597.        
  598.         if(shopStateArray[0]=='0')
  599.         {
  600.             string1 = "    ";
  601.         }
  602.         else if(shopStateArray[0]=='2')
  603.         {
  604.             if(column==1)
  605.             {
  606.                 string1 = "    ";
  607.             }
  608.         }
  609.         else if(shopStateArray[0]=='1')
  610.         {
  611.             if(column==2)
  612.             {
  613.                 string1 = "    ";
  614.             }
  615.         }
  616.        
  617.         String value = "";
  618.  
  619.         char[] string1Array =  string1.toCharArray();
  620.         char[] string2Array = string2.toCharArray();
  621.         char[] string3Array = string3.toCharArray();
  622.         char[] string4Array = string4.toCharArray();
  623.        
  624.         final int ONE_TWO_SPACING = CODE_NAME_SPACING;
  625.         final int TWO_THREE_SPACING = NAME_COUNT_SPACING;
  626.         final int THREE_FOUR_SPACING = COUNT_PRICE_SPACING;
  627.        
  628.         final int STRING1_SLOT = CODE_CHAR_SLOT;
  629.         final int STRING2_SLOT = NAME_CHAR_SLOT;
  630.         final int STRING3_SLOT = COUNT_CHAR_SLOT;
  631.         final int STRING4_SLOT = PRICE_CHAR_SLOT;
  632.    
  633.         for(int i = 0; i < field; i++)
  634.         {
  635.             if(i==0)
  636.             {
  637.                 for(int j = 0; j < STRING1_SLOT; j++)
  638.                 {
  639.                     if(string1.length()>j)
  640.                     {
  641.                         value+=string1Array[j];
  642.                         i++;
  643.                     }
  644.                     else
  645.                     {
  646.                         value+=" ";
  647.                         i++;
  648.                     }
  649.                 }
  650.             }
  651.             else if (i==(ONE_TWO_SPACING+STRING1_SLOT))
  652.             {
  653.                 for(int k = 0; k < STRING2_SLOT; k++)
  654.                 {
  655.                     if(string2.length()>k)
  656.                     {
  657.                         value+=string2Array[k];
  658.                         i++;
  659.                     }
  660.                     else
  661.                     {
  662.                         value+=" ";
  663.                         i++;
  664.                     }
  665.                 }
  666.             }
  667.             else if (i==(ONE_TWO_SPACING+STRING1_SLOT+TWO_THREE_SPACING+STRING2_SLOT))
  668.             {
  669.                 for(int m = 0; m < STRING3_SLOT; m++)
  670.                 {
  671.                     if(string3.length()>m)
  672.                     {
  673.                         value+=string3Array[m];
  674.                         i++;
  675.                     }
  676.                     else
  677.                     {
  678.                         value+=" ";
  679.                         i++;
  680.                     }
  681.                 }
  682.             }
  683.             else if (i==(ONE_TWO_SPACING+STRING1_SLOT+TWO_THREE_SPACING+STRING2_SLOT+THREE_FOUR_SPACING+STRING4_SLOT))
  684.             {
  685.                 for(int m = 0; m < STRING4_SLOT; m++)
  686.                 {
  687.                     if(string4.length()>m)
  688.                     {
  689.                         value+=string4Array[m];
  690.                         i++;
  691.                     }
  692.                     else
  693.                     {
  694.                         value+=" ";
  695.                         i++;
  696.                     }
  697.                 }
  698.             }
  699.             else
  700.             {
  701.                 value+=" ";
  702.             }
  703.            
  704.         }
  705.        
  706.         return value;
  707.     }
  708.    
  709.     public static String CenterAlignLeftTextInField(String text, int field)
  710.     {
  711.         int space = (field-text.length())/2;
  712.         String alignedText = text;
  713.         for (int i=0; i<space; i++)
  714.             alignedText+=" ";
  715.         return alignedText;
  716.     }
  717.    
  718.     public static String LeftAlignLeftTextInField(String text, int field)
  719.     {
  720.         final int TAB_SPACING = 7;
  721.        
  722.         int space = (field-TAB_SPACING-text.length());
  723.         String alignedText = text;
  724.         for (int i=0; i<space; i++)
  725.             alignedText+=" ";
  726.         return alignedText;
  727.     }
  728.    
  729.     //former sampleOutput
  730.     public static void mainOperation(String input)
  731.     {
  732.         switch(input)
  733.         {
  734.             case "A":
  735.                 longMessage = "[ADD ITEM] selected. Please enter the code of the item you would like. |";
  736.                 break;
  737.             case "R":
  738.                 longMessage = "[REMOVE ITEM] selected. Please enter the code of the item you would like. |";
  739.                 break;
  740.             case "D":
  741.                 longMessage = "[DESCRIBE ITEM] selected. Please enter the code of the item you would like. |";
  742.                 break;
  743.             case "C":
  744.                 longMessage = "[CHECKOUT] selected. Please enter your credit card number. |";
  745.                 break;
  746.             default:
  747.                 longMessage = "Sorry. Wrong input. |";
  748.                 break;
  749.         }
  750.        
  751.         try
  752.         {screenBuilder();}
  753.         catch (IOException e)
  754.         {longMessage = e.getMessage();}
  755.     }
  756.    
  757.     public static void subOperation(String itemInput)
  758.     {
  759.         switch(Display.getState())
  760.         {
  761.             case 04:
  762.                 longMessage = "Sorry, we can't do that. Please refer to the commands below: |";
  763.                 Display.setState(0);
  764.                 break;
  765.             case 12:
  766.                 longMessage = "[" + itemInput + "] " + printItem(itemInput, 0) + " selected. How many to add? |";
  767.                 previousInput = printItem(itemInput, 0);
  768.                 break;
  769.             case 13:
  770.                 longMessage = "Added [" + itemInput + "] pcs. of " + previousInput +  " to the cart. Would there be anything else? |";
  771.                 setState(0);
  772.                 break;
  773.             case 14:
  774.                 longMessage = "Sorry, we don't have that much. |";
  775.                 Display.setState(1);
  776.                 break;
  777.             case 22:
  778.                 longMessage = "[" + itemInput + "] "+ printItem(itemInput, 0) + " selected. How many to remove? |";
  779.                 break;
  780.             case 23:
  781.                 longMessage = "Removed [" + itemInput + "] pcs. of [" + previousInput +  "] from the cart. Would there be anything else? |";
  782.                 setState(0);
  783.                 break;
  784.             case 241:
  785.                 longMessage = "Cannot remove " + itemInput + " of the selected item from the cart. |";
  786.                  setState(0);
  787.                  break;
  788.             case 33:
  789.                 longMessage = "[" + printItem(itemInput, 0) + "] " + printItem(itemInput, 1) + " |";
  790.                 setState(0);
  791.                 break;
  792.             case 341:      
  793.                 longMessage = "Empty cart. No items can be removed. |";    
  794.                 setState(0);       
  795.                 break;
  796.             case 42:
  797.                 longMessage = "Checking out. Please enter card number |";
  798.                 setState(0);
  799.                 break;
  800.             case 43:
  801.                 longMessage = "Your card number is " + itemInput + " |";
  802.                 setState(0);
  803.                 break;
  804.             case 441:
  805.                 longMessage = "Cart is empty. Please add items before checking out. |";
  806.                 setState(0);
  807.                 break;
  808.             case 442:
  809.                 longMessage = "Invalid card number. |";
  810.                 setState(0);
  811.                 break;
  812.             case 45:
  813.                 longMessage = "Checking out. Please enter card number |";
  814.                 setState(0);
  815.                 break;
  816.             default:
  817.                 longMessage = "Sorry. Wrong input. |";
  818.                 break;
  819.         }
  820.        
  821.         try
  822.         {screenBuilder();}
  823.         catch (IOException e)
  824.         {longMessage = e.getMessage();}
  825.     }
  826.    
  827.     private static String printItem(String productCode, int itemPart)
  828.     {
  829.         String itemCode = CodeTranslator.getProductmap().get(productCode);
  830.         String type = itemCode.substring(0, 1); //expected P, E or T
  831.         switch(type)
  832.         {
  833.             case "P":
  834.                 return PotionValues.toString(itemCode)[itemPart];
  835.             case "E":
  836.                 return ElixirValues.toString(itemCode)[itemPart];
  837.             case "T":
  838.                 return TrinketValues.toString(itemCode)[itemPart];
  839.             default:
  840.                 throw new InvalidItemException();
  841.         }
  842.     }
  843.    
  844.     public static void displayItem(String itemInput)
  845.     {
  846.         //logic for displaying an item's description :D
  847.     }
  848.    
  849.     //////////////////////////////////////////////////////////////////////
  850.    
  851.     public static void printCatalog()
  852.     {//shoplist print be like:
  853.         int code=1;
  854.         int itype=1; //i<subtypes of each
  855.         String type="";
  856.        
  857.         if (itype==1)
  858.         {
  859.             type = "P";
  860.             for(int i=1; i<numPotionTypes+1; i++) //i<subtypes of potion
  861.             {
  862.                 type += i;
  863.                 String[] print = PotionValues.toString(type);
  864.                 System.out.print("Item Entry [" + code + "]");
  865.                 for(int j=0; j<3; j++) //j<itemAttributes, name,desc.price,etc.
  866.                 {
  867.                     System.out.println(print[j]);
  868.                 }
  869.                 type = type.substring(0, type.length()-1); //remove subtype
  870.                 code++; //iterate item code
  871.             }
  872.             itype++; //move to next type: elixirs
  873.         }
  874.        
  875.         if (itype==2)
  876.         {
  877.             type = "E";
  878.             for(int i=1; i<numElixirTypes+1; i++)
  879.             {
  880.                 type += i;
  881.                 String[] print = ElixirValues.toString(type);
  882.                 System.out.print("Item Entry [" + code + "]");
  883.                 for(int j=0; j<3; j++)
  884.                 {
  885.                     System.out.println(print[j]);
  886.                 }
  887.                 type = type.substring(0, type.length()-1);
  888.                 code++;
  889.             }
  890.             itype++; //move to next type: trinket
  891.         }
  892.         if (itype==3)
  893.         {
  894.             type = "T";
  895.             for(int i=1; i<numTrinketTypes+1; i++)
  896.             {
  897.                 type += i;
  898.                 String[] print = TrinketValues.toString(type);
  899.                 System.out.print("Item Entry [" + code + "]");
  900.                 for(int j=0; j<3; j++)
  901.                 {
  902.                     System.out.println(print[j]);
  903.                 }
  904.                 type = type.substring(0, type.length()-1); //remove subtype
  905.                 code++;
  906.             }
  907.             itype++;
  908.         }
  909.     }
  910.    
  911.     public static boolean isOpen() { return shopOpen; }
  912.    
  913.     public static void setIsOpen(boolean openShop) { shopOpen = openShop; }
  914.  
  915.     public static int getState() { return shopState; }
  916.    
  917.     public static void setState(int stateId) { shopState = stateId; }
  918.  
  919.  
  920. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement