I made a Mobile Store, with price in Indian rupees. Now I went for nested switch over functions, cause I wanted to try out something new- biggest mistake in my life.
Since 3 days i've been trying to perfect it, it just won't happen. I have to make sure the menu shows again if negative number or a character is entered. There is Try Catch for this, but it's bugging me.
Now my coding is huge, i'm not sure I can post the whole thing here
import java.io.*;
import java.util.Date;
class Mobile_Store_Project
{
public static void main () throws IOException
{
InputStreamReader isr=new InputStreamReader (System.in);
BufferedReader br=new BufferedReader (isr);
int ch=0,a,gt=0;
int pr[]=new int [10];
int qu[]=new int [10];
String x[]=new String[10];
for (a=0;a<100;a++)
{
try
{
System.out.println(" Welcome to the Mobile Store. We have mobiles of all major manufacturers. \n \n Menu: \n 1)Apple \n 2)Samsung \n 3)Nokia \n 4)HTC \n 5)Bill \n 6)Exit \n Enter the number of your choice.");
ch=Integer.parseInt(br.readLine());
switch (ch)
{
case 1: int g;
System.out.println("Apple makes the popular iPhones. We are currently stocking: \n 1)iPhone 5 \n 2)iPhone 4S \n 3)iPhone 4 \n 4) Back \nEnter the no of your choice.");
g=Integer.parseInt(br.readLine());
if (g<0)
{
System.out.println("Negative numbers not allowed. Lets try this again. Lets try this again.");
g=0;
continue;
}
switch (g)
{
case 1: int c1;
System.out.println("The iPhone 5 is the latest iPhone. Specifications: \n 1)4 inch retina display screen \n 2)OS: iOS 6 \n 3)8MP Camera with Panorama,HDR option. \n \n There are 6 options available.");
System.out.println ("1)16 GB White. Price Rs.45,504 \n 2)16 GB Black. Price Rs.45,504 \n 3)32 GB White.Price Rs.48,708 \n 4)32GB Black.Price Rs.48,708 \n 5)64 GB White. Price Rs.50,434 \n 6)64GB Black. Price Rs.50,434 \n Enter the number of your choice");
c1=Integer.parseInt(br.readLine());
if (c1<0)
{
System.out.println("Negative numbers not allowed. Lets try this again. Lets try this again.");
c1=0;
continue;
}
switch (c1)
{
case 1: x[a]="iPhone 5 16GB White ";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=45504;
a++;
break;
case 2: x[a]="iPhone 5 16GB Black ";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=45504;
break;
case 3: x[a]="iPhone 5 32GB White ";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=48708;
break;
case 4: x[a]="iPhone 5 32GB Black ";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=48708;
break;
case 5: x[a]=("iPhone 5 64GB White ");
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=50434;
break;
case 6: x[a]="iPhone 5 16GB White ";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=50434;
break;
default: System.out.println("Oops, lets try this again.");
}
break;
case 2: int c2;
System.out.println("The iPhone 4S is the next popular phone from Apple. The specs are quite similar to the iPhone 5. Specifications: \n 1)3.5 inch retina display screen \n 2)OS: iOS 5 (Upgradable to iOS 6) \n 3)8MP Camera with Panorama,HDR option. \n \n There are 6 options available.");
System.out.println ("1)16 GB White. Price Rs.30,504 \n 2)16 GB Black. Price Rs.30,504 \n 3)32 GB White.Price Rs.35,708 \n 4)32GB Black.Price Rs.35,708 \n 5)64 GB White. Price Rs.40,434 \n 6)64GB Black. Price Rs.40,434 \n Enter the number of your choice");
c2=Integer.parseInt (br.readLine());
if (c2<0)
{
System.out.println("Negative numbers not allowed. Lets try this again. Lets try this again.");
c2=0;
continue;
}
switch (c2)
{
case 1: x[a]="iPhone 4S 16GB White ";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=30504;
break;
case 2: x[a]="iPhone 4S 16GB Black ";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=30504;
break;
case 3: x[a]="iPhone 4S 32GB White ";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=35708;
break;
case 4: x[a]="iPhone 4S 32GB Black ";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=35708;
break;
case 5: x[a]="iPhone 4S 64GB White ";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=40434;
break;
case 6: x[a]="iPhone 4S 16GB White ";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=40434;
break;
default: System.out.println("Oops, lets try this again.");
}
break;
case 3: int ch3;
System.out.println("The iPhone 4 was the first iPhone to sport the glass and aluminium look. Specifications: \n 1)3.5 inch retina display screen \n 2)OS: iOS 4 (Upgradable to iOS 6) \n 3)5MP Camera with HDR option. \n \n There are 6 options available.");
System.out.println ("1)16 GB White. Price Rs.20,104 \n 2)16 GB Black. Price Rs.20,104 \n 3)32 GB White.Price Rs.23,658 \n 4)32GB Black.Price Rs.23,658 \n 5)64 GB White. Price Rs.27,123 \n 6)64GB Black. Price Rs.27,123 \n Enter the number of your choice");
ch3=Integer.parseInt(br.readLine());
if (ch3<0)
{
System.out.println("Negative numbers not allowed. Lets try this again. Lets try this again.");
ch3=0;
continue;
}
switch (ch3)
{
case 1: x[a]="iPhone 4 16GB White ";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=20104;
break;
case 2: x[a]="iPhone 4 16GB Black ";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=20104;
break;
case 3: x[a]="iPhone 4 32GB White ";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=23658;
break;
case 4: x[a]="iPhone 4 32GB Black ";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=23658;
break;
case 5: x[a]="iPhone 4 64GB White ";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=27123;
break;
case 6: x[a]="iPhone 4 16GB White ";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=27123;
break;
default: System.out.println("Oops, lets try this again.");
}
break;
case 4:
break;
default: System.out.println("Oops, lets try this again.");
}
break;
case 2: System.out.println("Samsung has revolutionized the smartphone market with it's amazing phones. We are currently stocking: \n 1)Galaxy S3 \n 2)Galaxy S Duos \n 3)Note 2 \n 4) Back \n Enter the number of you choice");
int ch1;
ch1=Integer.parseInt(br.readLine());
if (ch1<0)
{
System.out.println("Negative numbers not allowed. Lets try this again. Lets try this again.");
ch1=0;
continue;
}
switch (ch1)
{
case 1: System.out.println ("The Samsung Galaxy S3 is one of the most popular selling Android devices, selling 30 million+ of them. Specifications: \n 1)4.7 inch AMOLED Screem \n 2)8MP Camera \n 3)1.4 GhZ Quad Core Processor\n There is only one memory option available, 16 GB, but there are two colour options available: \n 1)Marble White. Price Rs.31,454 \n 2)Pebble Blue. Price Rs.31,454 \n Enter the number of your choice. ");
int cho1;
cho1=Integer.parseInt (br.readLine());
if (cho1<0)
{
System.out.println("Negative numbers not allowed. Lets try this again. Lets try this again.");
cho1=0;
continue;
}
else if (cho1==1)
{ x[a]="Samsung Galaxy S3 16 GB Marble White";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=31454;
}
else if (cho1==2)
{ x[a]="Samsung Galaxy S3 16 GB Pebble Blue";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=31454;
}
else
System.out.println("Oops. Lets try this again.");
break;
case 2: System.out.println ("The Samsung Galaxy S Duos is the look alike of S3, but with lower specs and dual sim option. Specifications: \n 1)1GhZ Dual Core processor \n 2)5 MP Camera \n 3)Android 4.0.4 Ice Cream Sandwich \n There is only one option available: 1)8GB White. Price Rs.14,999. \n Enter 1 to select.");
int ch2;
ch2=Integer.parseInt (br.readLine());
if (ch2<0)
{
System.out.println("Negative numbers not allowed. Lets try this again. Lets try this again.");
ch2=0;
continue;
}
else if (ch2==1)
{
x[a]="Samsung Galaxy S Duos 8GB White ";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=14999;
}
if (ch2>1)
{
System.out.println("Oops. Lets try this again.");
ch2=0;
continue;
}
break;
case 3: System.out.println ("The Samsung Galaxy Note 2 is the sucessor of the sucessful Galaxy Note. Specifications: \n 1)5.5 inch screen \n 2)Android 4.1 Jelly Bean \n 3)1.5 GhZ Quad Core Processor \n 4)S Pen for super productivity \n The phone is available for Rs.38,656 and only in white option. \n Enter 1 to select.");
int ch3;
ch3=Integer.parseInt(br.readLine());
if (ch3<0)
{
System.out.println("Negative numbers not allowed. Lets try this again. Lets try this again.");
ch3=0;
continue;
}
else if (ch3==1)
{
x[a]="Samsung Galaxy Note 2 16 GB White ";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");;
pr[a]=38656;
}
if (ch3>1)
{
System.out.println("Oops. Lets try this again.");
ch3=0;
continue;
}
break;
case 4:
break;
default: System.out.println("Oops, lets try this again.");
}
break;
case 3: System.out.println("Nokia is a household name. Everyone has owned atleast one Nokia phone in their life. We are currently stocking: \n 1)Nokia Lumia 920 \n 2)Nokia Lumia 820 \n 3) Nokia Lumia 510. \n 4) Back \n Enter the number of your choice");
int ch2;
ch2=Integer.parseInt (br.readLine());
if (ch2<0)
{
System.out.println("Negative numbers not allowed. Lets try this again. Lets try this again.");
ch2=0;
continue;
}
switch (ch2)
{
case 1: System.out.println ("The Nokia Lumia 920 is a beautiful looking device. Specifications: \n 1)4.5 inch PureMotion+ screen \n 2)Windows Phone 8 OS \n 3)8.7MP Pureview+ Camera with Optical Image Stabilization (OIS) \n There is only one memory option available, 32 GB. There are 3 color options available : \n 1)Blue. Price Rs.27,545 \n 2)Red. Price Rs.27,545 \n 3)Yellow. Price Rs.27,545 \n Select the number of your choice.");
int cho1;
cho1=Integer.parseInt (br.readLine ());
if (cho1<0)
{
System.out.println("Negative numbers not allowed. Lets try this again. Lets try this again.");
cho1=0;
continue;
}
switch (cho1)
{
case 1:x[a]="Nokia Lumia 920 Blue 32 GB ";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=27545;
break;
case 2:x[a]="Nokia Lumia 920 Red 32 GB ";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=27545;
break;
case 3:x[a]="Nokia Lumia 920 Yellow 32 GB ";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=27545;
break;
default: System.out.println("Oops, lets try this again.");
}
break;
case 2:System.out.println ("The Nokia Lumia 820 is another great phone from Nokia. Specifications: \n 1)4.5 inch PureMotion+ screen \n 2)Windows Phone 8 OS \n 3)8 MP Camera \n There is only 32 GB memory option available. There are 3 colour choice available: \n 1)Blue. Price Rs.22,545 \n 2)Red. Price Rs.22,545 \n 3)Yellow. Price Rs.22,545 \n Select the number of your choice.");
int cho2;
cho2=Integer.parseInt(br.readLine());
if (cho2<0)
{
System.out.println("Negative numbers not allowed. Lets try this again. Lets try this again.");
cho2=0;
continue;
}
switch (cho2)
{
case 1: x[a]="Nokia Lumia 820 32GB Blue ";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=22545;
break;
case 2: x[a]="Nokia Lumia 820 32GB Red ";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=22545;
break;
case 3: x[a]="Nokia Lumia 820 32GB Yellow ";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=22545;
break;
default: System.out.println("Oops, lets try this again.");
}
break;
case 3: System.out.println ("The Nokia Lumia 510 is a budget Windows Phone from Nokia. Specifications: \n 1)3.7 inch screen \n 2)5MP Camera \n 3)Windows Phone 7.5 (Upgradable to 7.8) \n There are 2 colour options available: \n 1)Black. Price Rs.9999 \n 2)White. Price Rs.9999 \n Enter the number of your choice.");
int cho3;
cho3=Integer.parseInt (br.readLine());
if (cho3<0)
{
System.out.println("Negative numbers not allowed. Lets try this again. Lets try this again.");
cho3=0;
continue;
}
else if (cho3==1)
{
x[a]="Nokia Lumia 510 4GB Black";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=9999;
}
else if (cho3==2)
{
x[a]="Nokia Lumia 510 4GB White";
System.out.println("How many do you want to buy?");
qu[a]=Integer.parseInt (br.readLine());
System.out.println(qu[a]+" "+x[a]+"Added to cart.");
pr[a]=9999;
}
else
{
System.out.println("Oops. Lets try this again.");
continue; }
break;
case 4:
break;
default: System.out.println("Oops, lets try this again.");
}
break;
case 5: //Printing bill
Date date = new Date();
System.out.println("\t\t\t\t\t\tMobile Store\n");
System.out.println("\t\t\t\t\t\t\t\t\tBill Date : "+date.toString()+"\n");
int i,j=1;
System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
System.out.println("Sr.No "+setSpace("ITEM",17)+" "+setSpace("PRICE",17)+" "+setSpace("QUANTITY",12)+" "+setSpace("TOTAL",25));
System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
for (a=0;a<qu[a];a++)
{
System.out.println(j+" "+setSpace(""+x[a],25)+" "+setSpace(""+pr[a],20)+" "+setSpace(""+qu[a],20)+" "+setSpace(""+pr[a]*qu[a],20));
j++;
gt+=pr[a]*qu[a];
}
System.out.println("\t\t\t\t\tGrand Total :- Rs"+(gt)+"/-\n\n\n");
System.out.println("\t\t\t\tThank you. Visit Again.");
System.exit(0);
break;
} // End of main switch
}
catch(NumberFormatException nfe)
{
System.out.println("Oops. Lets try this again.");
ch=0;
}
}
}
static String setSpace(String data,int size) // Aligning the bill
{
int len=data.length();
if(len<size)
{
for(int i=0;i<size-len;i++)
{
data+=" ";
}
}
else
{
data=data.substring(0,size-2);
data+="..";
}
return data;
}
}
PS: I cut out the 4th case in Switch (ch) to get some space.