Assignment brief:
A local toy store wishes you to create an application for parents to enter the details of their children’s Santa list. The Santa list is a database that contains 5 toys, each with a name and a price of format “TOYNAME_PRICE”.
Download full brief here


Download source code here

 //Start of code---------------------
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

/* Usage notes:
 * Redistribution, alteration and use of this code with or without
 * modification, are permitted provided that the following conditions are met:
 * In no event shall the copyright owner be liable for any direct, indirect,
 * incidental, special, exemplary, or consequential damages arising in any
 * way out of the use of this software, even if advised of the possibility
 * of such damage.
 */

public class SantaWishList {

  
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        String sname, child1 = null, child2 = null, child3 = null, child4 = null, child5 = null, emailAddress;

        int childTotal, giftReceipt, uniqueToyId = 0;

        String toyDetails = null;

        // • Buy 2 toys, get 10% off the total price
        // • Buy 3 toys or more, get the cheapest toy free

        // Code below is for the surname
        do {
            System.out.print("\nPlease enter your surname to continue\nMinimum 3 charaters (A - Z only): ");

            sname = input.next();
            // sname.toUpperCase().charAt(0);
        } while (sname.length() <= 2 || !sname.matches("[a-zA-Z]+"));

        System.out.println(sname);

        // Code below is for the email address
        System.out.print("\nPlease enter your email address to continue: ");
        emailAddress = input.next();
        while (!emailAddress.matches("[a-zA-Z]*[0-9]*@[a-zA-Z]*.*[a-zA-Z]*")) {

            System.out.print("\nInvalid address!!\nPlease enter a valid email address to continue: ");

            emailAddress = input.next();
        }
        System.out.println(emailAddress);

        // Code below is for amount of children
        System.out.print("\nPlease enter amount of children\nMaximum of 5 per transaction: ");
        childTotal = input.nextInt();

        while (childTotal < 1 || childTotal > 5 ){
            System.out.print("\ndue to the recession, only one toy per child\n");
            System.out.print("\nPlease enter amount of children\nMaximum of 5 per transaction: ");
            childTotal = input.nextInt();
        }

        if (childTotal == 1) {
            System.out.println("\n\tPlease enter childs name");
            child1 = input.next();
            System.out.print("\nPlease select a toy for *" + child1    + "* from the list below\n");
            toyList();
            uniqueToyId = input.nextInt();
            toyDataBase(uniqueToyId);
        } else if (childTotal == 2) {
            System.out.println("\n\tPlease enter the first childs name");
            System.out.print("\nPlease select a toy for " + child1    + " from the list below\n");
            child1 = input.next();
            toyList();
            uniqueToyId = input.nextInt();
            toyDataBase(uniqueToyId);
            System.out.println("\n\tPlease enter the second childs name");
            child2 = input.next();
            System.out.print("\nPlease select a toy for " + child2    + " from the list below\n");
            toyList();
            uniqueToyId = input.nextInt();
            toyDataBase(uniqueToyId);
        } else if (childTotal == 3) {
            System.out.println("\n\tPlease enter the first childs name");
            child1 = input.next();
            System.out.print("\nPlease select a toy for " + child1    + " from the list below\n");
            toyList();
            uniqueToyId = input.nextInt();
            toyDataBase(uniqueToyId);
            System.out.println("\n\tPlease enter the second childs name");
            child2 = input.next();
            System.out.print("\nPlease select a toy for " + child2    + " from the list below\n");
            toyList();
            uniqueToyId = input.nextInt();
            toyDataBase(uniqueToyId);
            System.out.println("\n\tPlease enter the third childs name");
            child3 = input.next();
            System.out.print("\nPlease select a toy for *" + child3    + "* from the list below\n");
            toyList();
            uniqueToyId = input.nextInt();
            toyDataBase(uniqueToyId);
        } else if (childTotal == 4) {
            System.out.println("\n\tPlease enter the first childs name");
            child1 = input.next();
            System.out.print("\nPlease select a toy for *" + child1    + "* from the list below\n");
            toyList();
            uniqueToyId = input.nextInt();
            toyDataBase(uniqueToyId);
            System.out.println("\n\tPlease enter the second childs name");
            child2 = input.next();
            System.out.print("\nPlease select a toy for *" + child2    + "* from the list below\n");
            toyList();
            uniqueToyId = input.nextInt();
            toyDataBase(uniqueToyId);
            System.out.println("\n\tPlease enter the third childs name");
            child3 = input.next();
            System.out.print("\nPlease select a toy for *" + child3    + "* from the list below\n");
            toyList();
            uniqueToyId = input.nextInt();
            toyDataBase(uniqueToyId);
            System.out.println("\n\tPlease enter the fourth childs name");
            child4 = input.next();
            System.out.print("\nPlease select a toy for *" + child4    + "* from the list below\n");
            toyList();
            uniqueToyId = input.nextInt();
            toyDataBase(uniqueToyId);
        } else if (childTotal == 5) {
            System.out.println("\n\tPlease enter the first childs name");
            child1 = input.next();
            System.out.print("\nPlease select a toy for *" + child1    + "* from the list below\n");
            toyList();
            uniqueToyId = input.nextInt();
            toyDataBase(uniqueToyId);
            System.out.println("\n\tPlease enter the second childs name");
            child2 = input.next();
            System.out.print("\nPlease select a toy for *" + child2    + "* from the list below\n");
            toyList();
            uniqueToyId = input.nextInt();
            toyDataBase(uniqueToyId);
            System.out.println("\n\tPlease enter the third childs name");
            child3 = input.next();
            System.out.print("\nPlease select a toy for " + child3 + " from the list below\n");
            toyList();
            uniqueToyId = input.nextInt();
            toyDataBase(uniqueToyId);
            System.out.println("\n\tPlease enter the fourth childs name");
            child4 = input.next();
            System.out.print("\nPlease select a toy for " + child4 + " from the list below\n");
            toyList();
            uniqueToyId = input.nextInt();
            toyDataBase(uniqueToyId);
            System.out.println("\n\tPlease enter the fifth childs name");
            child5 = input.next();
            System.out.print("\nPlease select a toy for " + child5 + " from the list below\n");
            toyList();
            uniqueToyId = input.nextInt();
            toyDataBase(uniqueToyId);
        }

        // Code below is for receipt for one child
        String toy = toyDataBase(uniqueToyId);

        // toy substring
        String type = (toy);
        String toyType;
        toyType = type.substring(0, type.indexOf("_"));
        // System.out.print(type.substring(5,type.indexOf("_")));

        // price substring
        String price = (toy);
        // Double doubleprice = Double.parseDouble(price);
        String price1 = null;
        // Double doubleprice1 = Double.parseDouble(price1);
        price1 = price
                .substring(price.indexOf("_") + 1, price.lastIndexOf(" "));
        // System.out.print(type.substring(5,type.indexOf("_")));

        // String total = (price1 + price1);

        System.out.print("\n\n\t\t   Toy Kingdom\n\t\t  3 Joe Street,\n\t\t   Cork City.\n\n\t");
        date();
        // Receipt printout
        System.out.print("\n\nSANTAS LIST FOR THE *" + sname + "* FAMILY - " + emailAddress + "\n");
        System.out.print("\nCHILD                TOY            PRICE\n");
        if (childTotal == 1) {
        System.out.println(child1+"      " + "      " + type.substring(0, type.indexOf("_"))
                +"     "+ price.substring(price.indexOf("_") + 1,price.lastIndexOf(" ")));}
        else if (childTotal == 2) {
            System.out.println(child1+"      " + "      " + type.substring(0, type.indexOf("_"))
                    +"     "+ price.substring(price.indexOf("_") + 1,price.lastIndexOf(" ")));
            System.out.println(child2+"      " + "      " + type.substring(0, type.indexOf("_"))
                +"     "+ price.substring(price.indexOf("_") + 1,price.lastIndexOf(" ")));}
        else if (childTotal == 3) {
            System.out.println(child1+"      " + "      " + type.substring(0, type.indexOf("_"))
                    +"     "+ price.substring(price.indexOf("_") + 1,price.lastIndexOf(" ")));
            System.out.println(child2+"      " + "      " + type.substring(0, type.indexOf("_"))
                +"     "+ price.substring(price.indexOf("_") + 1,price.lastIndexOf(" ")));
        System.out.println(child3+"      " + "      " + type.substring(0, type.indexOf("_"))
                +"     "+ price.substring(price.indexOf("_") + 1,price.lastIndexOf(" ")));}
        else if (childTotal == 4) {
            System.out.println(child1+"      " + "      " + type.substring(0, type.indexOf("_"))
                    +"     "+ price.substring(price.indexOf("_") + 1,price.lastIndexOf(" ")));
            System.out.println(child2+"      " + "      " + type.substring(0, type.indexOf("_"))
                +"     "+ price.substring(price.indexOf("_") + 1,price.lastIndexOf(" ")));
        System.out.println(child3+"      " + "      " + type.substring(0, type.indexOf("_"))
                +"     "+ price.substring(price.indexOf("_") + 1,price.lastIndexOf(" ")));
        System.out.println(child4+"      " + "      " + type.substring(0, type.indexOf("_"))
                +"     "+ price.substring(price.indexOf("_") + 1,price.lastIndexOf(" ")));}
        else if (childTotal == 5) {
            System.out.println(child1+"      " + "      " + type.substring(0, type.indexOf("_"))
                    +"     "+ price.substring(price.indexOf("_") + 1,price.lastIndexOf(" ")));
            System.out.println(child2+"      " + "      " + type.substring(0, type.indexOf("_"))
                +"     "+ price.substring(price.indexOf("_") + 1,price.lastIndexOf(" ")));
        System.out.println(child3+"      " + "      " + type.substring(0, type.indexOf("_"))
                +"     "+ price.substring(price.indexOf("_") + 1,price.lastIndexOf(" ")));
        System.out.println(child4+"      " + "      " + type.substring(0, type.indexOf("_"))
                +"     "+ price.substring(price.indexOf("_") + 1,price.lastIndexOf(" ")));
        System.out.println(child5+"      " + "      " + type.substring(0, type.indexOf("_"))
                +"     "+ price.substring(price.indexOf("_") + 1,price.lastIndexOf(" ")));}
      
System.out.print("\n\nWould you like a Gift receipt?\nPlease input 1 for Yes or input 2 for No");
      
        giftReceipt = input.nextInt();
      
        while (giftReceipt < 1 || giftReceipt > 2 ){
            System.out.print("\nInvalid input!");
            System.out.print("\nIf you would you like a Gift receipt?\n\nPlease input 1 for Yes or input 2 for No\n\n");
            giftReceipt = input.nextInt();
        }

        if (giftReceipt == 1) {
            System.out.print("\n\n\t    Toy Kingdom\n\t  3 Joe Street,\n\t  Cork City.\n\t");
        System.out.println("\t\n\t **GIFT RECEIPT**\n");  
        if (childTotal == 1) {
        System.out.println("\t|  "+type.substring(0, type.indexOf("_"))+"  |");}
        else if (childTotal == 2) {
            System.out.println("\t|  "+type.substring(0, type.indexOf("_"))+"  |");
        System.out.println("\t|  "+type.substring(0, type.indexOf("_"))+"  |");}
        else if (childTotal == 3) {
            System.out.println("\t|  "+type.substring(0, type.indexOf("_"))+"  |");
        System.out.println("\t|  "+type.substring(0, type.indexOf("_"))+"  |");
        System.out.println("\t|  "+type.substring(0, type.indexOf("_"))+"  |");}
        else if (childTotal == 4) {
            System.out.println("\t|  "+type.substring(0, type.indexOf("_"))+"  |");
        System.out.println("\t|  "+type.substring(0, type.indexOf("_"))+"  |");
        System.out.println("\t|  "+type.substring(0, type.indexOf("_"))+"  |");
        System.out.println("\t|  "+type.substring(0, type.indexOf("_"))+"  |");}
        else if (childTotal == 5) {
            System.out.println("\t|  "+type.substring(0, type.indexOf("_"))+"  |");
        System.out.println("\t|  "+type.substring(0, type.indexOf("_"))+"  |");
        System.out.println("\t|  "+type.substring(0, type.indexOf("_"))+"  |");
        System.out.println("\t|  "+type.substring(0, type.indexOf("_"))+"  |");
        System.out.println("\t|  "+type.substring(0, type.indexOf("_"))+"  |");}
        System.out.println("\n   Thank you for shopping at Toy Kingdom\n       ");
        date();}
        else if (giftReceipt == 2) {
        System.out.println("\nYou have chosen not to receive a gift receipt");
        System.out.println("\nThank you for shopping at Toy Kingdom");}
    }

    // System.out.print("     "+total);

    static String toyDataBase(int uniqueToyId) // Code below is for toy details
    {
        Scanner input = new Scanner(System.in);

        String toyDetails = " ";

        if (uniqueToyId == 1) {
            toyDetails = "Lego Star Wars_39.50 ";
        } else if (uniqueToyId == 2) {
            toyDetails = "3D Eiffel Tower jigsaw_24.99 ";
        } else if (uniqueToyId == 3) {
            toyDetails = "Monopoly Irish Edition_19.50 ";
        } else if (uniqueToyId == 4) {
            toyDetails = "The Irish speaking teddy_17.99 ";
        } else if (uniqueToyId == 5) {
            toyDetails = "Toy Story Laptop_34.95 ";
        }

        return toyDetails;
    }

    public static void toyList() // Code below is for toy list
    {

        System.out.print("\n\t\t Toy List\n");
        System.out.print("______________________________________________\n");
        System.out.print("\n  1.Lego Star Wars\t\t\u20AC39.50");
        System.out.print("\n  2.3D Eiffel Tower jigsaw\t\u20AC24.99");
        System.out.print("\n  3.Monopoly Irish Edition\t\u20AC19.50");
        System.out.print("\n  4.The Irish speaking teddy\t\u20AC17.99");
        System.out.print("\n  5.Toy Story Laptop\t\t\u20AC34.95");
        System.out.print("\n______________________________________________\n");
    }
  
    public static void date()
    {
    DecimalFormat fmt = new DecimalFormat("00");

           Calendar now = Calendar.getInstance();

           int hour = now.get(Calendar.HOUR_OF_DAY);

           int minute = now.get(Calendar.MINUTE);

           int month = now.get(Calendar.MONTH)+ 1;

           int day = now.get(Calendar.DAY_OF_MONTH);

           int year = now.get(Calendar.YEAR);
                    
           System.out.print("       "+hour+":" + minute );
             
           System.out.print(" " + day + " ");
         
           switch (month)

           {
               case 1:
                   System.out.print("Jan");
                   break;

               case 2:
                   System.out.print("Feb");
                   break;

               case 3:
                   System.out.print("Mar");
                   break;

               case 4:
                   System.out.print("Apr");
                   break;

               case 5:
                   System.out.print("May");
                   break;

               case 6:
                   System.out.print("June");
                   break;

               case 7:
                   System.out.print("July");
                   break;

               case 8:
                   System.out.print("Aug");
                   break;

               case 9:
                   System.out.print("Sept");
                   break;

               case 10:
                   System.out.print("Oct");
                   break;

               case 11:
                   System.out.print("Nov");
                   break;

               case 12:
                   System.out.print("Dec");
          }
           System.out.print(" "+year);
}
}//End of code---------------------

0 comments:

Post a Comment

Please feel free to leave comments or ask questions related to the tutorials.