mercredi 29 mars 2017

for some reason my if statement is not working

i made this program for internal assesment and i still have "write to text and read from text remaining", but here for some reason , my if statement: if(q < quantity[m]) in case 2 is not working and it always runs the other one, i seriously have no idea what is wrong

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package noorhamara;

import java.io.*;
public class Noorhamara2 {

    /**
     *
     * @param args
     * @throws IOException
     */
    @SuppressWarnings("empty-statement")
    public static void main(String[] args)throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter writer;
        writer = new PrintWriter("C:\\Users\\hares\\Desktop\\nicebro.txt");
        int ch,ch1,ch2,n,i,j,flag=-1,flag2=0,sum=0,k=0,q,i2,max=1,m;
        String name,p;
        String item[]=new String[500];
        int quantity[]=new int[500];
        int price[]=new int[500];
        //declaring variables
             while(flag!=1 && max<=5)
            {
                System.out.print("Enter Password:- ");
                p=br.readLine();
                if(p.compareTo("1234")==0)
                {
                    flag=1;
                    break;
                }
                else
                System.out.println("Wrong password! Try again?!");
                max++;
                if(max==5)
                {

                    System.out.println("\fTry after sometime....\nwait for 10 counts..");
                    for(j=1;j<=10;j++)
                    {
                        for(k=0;k<=899999999;k++);
                        System.out.print(j+"..");
                    }
                    max=0;

                    System.out.println("\f");
                }
        }
        System.out.print("\f");
        z:while(true)
            {
                System.out.println("1.Enter Stock Items\n2.make customer bill\n3.view items remaining\n4.Exit");
                System.out.print("Please enter your choice in no.:-");
                ch1=Integer.parseInt(br.readLine());
                switch(ch1)
                {
                    case 1:
                    System.out.println("\fhow many items do you wish to feed:");
                    n=Integer.parseInt(br.readLine());
                    for(i=k;i<k+n;i++)
                    {
                        System.out.print("Please enter name of item "+(i+1)+":-");
                        item[i]=br.readLine();
                        writer.write(item[i]);
                        System.out.print("Please enter price:-");
                        price[i]=Integer.parseInt(br.readLine());
                        writer.write(item[i]);
                        System.out.print("Please enter quantity available:-");
                        quantity[i]=Integer.parseInt(br.readLine());
                        writer.write(item[i]);
                        System.out.print("\f");
                    }
                    k=k+n;
                    break;
                    case 2:
                    System.out.print("\f");
                    System.out.println("How many items");
                    i2=Integer.parseInt(br.readLine());
                    for(i=0;i<i2;i++)
                    {
                        System.out.println("enter item name:");
                        name=br.readLine();
                        for(m=0;m<k;m++)
                            {  
                                if(name.equals(item[m]))
                                flag2=1;
                            }
                        if(flag2==1)
                           { System.out.println("enter quantity:");
                             q=Integer.parseInt(br.readLine());
                             if(q < quantity[m])
                             {
                               System.out.println("This finally works");
                                 sum+=(price[m]*q);
                               quantity[m]=quantity[m]-q;
                               if(quantity[m]<10)
                                 System.out.println("quantity of "+item[m]+" is very low");
                             }
                             else if (q > quantity[m])
                              System.out.println("quantity exeeded"); 
                             else
                                System.out.println("error");
                            }
                        else
                         System.out.println("item not available in shop");
                    }

                    System.out.println("Total value of the purchase : "+sum);
                    sum=0;
                    flag2=0;
                    break;
                    case 3:
                    System.out.println("\fitem\t\t\tprice\t\t\tavailibility");
                    for(i=0;i<k;i++)
                    {
                        System.out.println(item[i]+"\t\t\t"+price[i]+"\t\t\t"+quantity[i]);
                    }
                    System.out.println("\n1.continue\n2.exit");
                    ch2=Integer.parseInt(br.readLine());
                    if(ch2==2)
                    break z;
                    System.out.print("\f");
                    break;
                    case 4:
                    System.out.println("thank you!!");
                    break z;
                }
            }
         writer.close();
        }
    }

Aucun commentaire:

Enregistrer un commentaire