Link to home
Start Free TrialLog in
Avatar of chongkeyface
chongkeyface

asked on

one simple bug

there is one simple bug, after selecting the more items 'y'(sales menu), it doesnt aks for the product code anymore.


class bakemenu{
public static void main (String args[])
throws java.io.IOException {

int precheese = 2;
int soldcheese = 0;
double sellcheese = 36.00;
int prechoc = 4;
int soldchoc = 0;
double sellchoc = 30.00;
int prestraw = 2;
int soldstraw = 0;
double sellstraw = 28.00;

int preegg = 20;
int soldegg = 0;
double sellegg = 1.20;
int prepine = 20;
int soldpine = 0;
double sellpine = 1.20;

int prenuts = 100;
int soldnuts = 0;
double sellnuts = 0.80;
int prechips = 100;
int soldchips = 0;
double sellchips = 0.70;

int precheese2 = 2;
int prechoc2 = 4;
int prestraw2 = 2;
int preegg2 = 20;
int prepine2 = 20;
int prenuts2 = 100;
int prechips2 = 100;
String code = new String();
int quantity = 0;
int numberOfBytesToRead = 10;
double total = 0.00;
double totalsum = 0.00;


 System.out.println ("\n");
 System.out.println ("-------------------------------------------------------------------------------");
 System.out.println ("                         Famouze Bakery, Madamme Memziers                      ");
 System.out.println ("\n");
 System.out.println ("Category         Favorite          Qty Prepared   Qty Sold  Selling Price($)); ");
 System.out.println ("-------------------------------------------------------------------------------");
 System.out.print   ("Cake             [c1]Cheese" + "              " + precheese + "            " +soldcheese + "           " +sellcheese);
 System.out.print   ("                           [c2]Chocolate" + "           " + prechoc   + "            " +soldchoc   + "           " +sellchoc);
 System.out.println ("                           [c3]Strawberry" + "          " + prestraw   + "            " +soldstraw   + "           " +sellstraw);

 System.out.println ("\n");
 System.out.print   ("Pastry           [p1]Egg" + "                 " + preegg + "           " +soldegg + "            " +sellegg);
 System.out.print   ("                           [p2]Pineapple" + "           " + prepine   + "           " +soldpine   + "            " +sellpine);

 System.out.println ("\n");
 System.out.print   ("Cookie           [k1]Pecan and Nuts" + "     " + prenuts + "           " +soldnuts + "            " +sellnuts);
 System.out.println   ("                           [k2]Chocolate chips" + "    " + prechips   + "           " +soldchips   + "            " +sellchips);
 
 System.out.println ("\n");
 System.out.println ("-------------------------------------------------------------------------------");
 System.out.println ("[S]ales             [D]aily Order             [C]ollection              E[X]it");
 System.out.println ("\n");
 //char moreitems = 'y' ;
 //char option = 'y' ;
 char option;

   System.out.print ("Option:" );
   option = (char)System.in.read();
   System.in.skip(2);
   //System.out.println("\n");

   switch(option){
   case 's':{totalsum = 0.00;
             char cont = 'y';
   do {
             System.out.println("Items sold:-");

             
                   System.out.println("enter product code:");
                   byte[] buffer = new byte[numberOfBytesToRead];
                   int bytesRead = System.in.read(buffer);
                   byte[] bufferRead = new byte[bytesRead - 2];
                   System.arraycopy(buffer, 0, bufferRead, 0, bytesRead - 2);

                   code = new String(bufferRead);

                   System.out.print("Enter quantity:");
                   bytesRead = System.in.read(buffer);
                   bufferRead = new byte[bytesRead - 2];
                   System.arraycopy(buffer, 0, bufferRead, 0, bytesRead - 2);

                   quantity = new Integer(new String(bufferRead)).intValue();

                 if ("c1".equals(code))
                   { if (quantity > precheese2)
                       { System.out.println("not enough stock");}
                   else{
                        precheese2 = precheese2 - quantity;
                        soldcheese = soldcheese + quantity;
                        total = sellcheese * quantity;
                        totalsum = totalsum + total;
                       }
                    }

             if ("c2".equals(code))
                   { if (quantity > prechoc2)
                       { System.out.println("not enough stock");}
                   else{
                        prechoc2 = prechoc2 - quantity;
                        soldchoc = soldchoc + quantity;
                        total = sellchoc * quantity;
                        totalsum = totalsum + total;
                       }
                     }
               if ("c3".equals(code))
                   { if (quantity > prestraw2)
                       { System.out.println("not enough stock");}
                   else{
                        prestraw2 = prestraw2 - quantity;
                        soldstraw = soldstraw + quantity;
                        total = sellstraw * quantity;
                        totalsum = totalsum + total;
                       }
                    }  
               
                  if ("p1".equals(code))
                   { if (quantity > preegg2)
                       { System.out.println("not enough stock");}
                   else{
                        preegg2 = preegg2 - quantity;
                        soldegg = soldegg + quantity;
                        total = sellegg * quantity;
                        totalsum = totalsum + total;
                       }
                    }

                      if ("p2".equals(code))
                   { if (quantity > prepine2)
                       { System.out.println("not enough stock");}
                   else{
                        prepine2 = prepine2 - quantity;
                        soldpine = soldpine + quantity;
                        total = sellpine * quantity;
                        totalsum = totalsum + total;
                       }
                    }

               if ("k1".equals(code))
                   { if (quantity > prenuts2)
                       { System.out.println("not enough stock");}
                   else{
                        prenuts2 = prenuts2 - quantity;
                        soldnuts = soldnuts + quantity;
                        total = sellnuts * quantity;
                        totalsum = totalsum + total;
                       }
                    }

                   if ("k2".equals(code))
                   { if (quantity > prechips2)
                       { System.out.println("not enough stock");}
                   else{
                        prechips2 = prechips2 - quantity;
                        soldchips = soldchips + quantity;
                        total = sellchips * quantity;
                        totalsum = totalsum + total;
                       }
                    }
        System.out.println("More Items(y/n) ?");
      cont = (char) System.in.read();
       }while (cont != 'n');
        System.out.println("Total amount due =" +totalsum);
   }
   }
   }
   }
 
Avatar of Venci75
Venci75

use this:
....
while (System.in.available() > 0) System.in.read();
System.out.println("enter product code:");
....

and
while (System.in.available() > 0) System.in.read();
System.out.println("Enter quantity:");


to clear the buffer before reading with
System.in.read(buffer);
I'm sending you the whole code. I've made the necessary modifications to make it run. Just chk it over and tell me if ur satisfied. i changed the name to trial..pl do change to 1 which 'll suit u.

import java.io.*;
import java.io.InputStream;
import java.lang.*;
import java.util.*;

class trial
{
        public static void main (String args[]) throws java.io.IOException
        {
                int precheese = 2;
                int soldcheese = 0;
                double sellcheese = 36.00;
                int prechoc = 4;
                int soldchoc = 0;
                double sellchoc = 30.00;
                int prestraw = 2;
                int soldstraw = 0;
                double sellstraw = 28.00;
                int preegg = 20;
                int soldegg = 0;
                double sellegg = 1.20;
                int prepine = 20;
                int soldpine = 0;
                double sellpine = 1.20;
                int prenuts = 100;
                int soldnuts = 0;
                double sellnuts = 0.80;
                int prechips = 100;
                int soldchips = 0;
                double sellchips = 0.70;
                int precheese2 = 2;
                int prechoc2 = 4;
                int prestraw2 = 2;
                int preegg2 = 20;
                int prepine2 = 20;
                int prenuts2 = 100;
                int prechips2 = 100;
                String code = new String();
                int quantity = 0;
                int numberOfBytesToRead = 10;
                double total = 0.00;
                double totalsum = 0.00;
                System.out.println ("\n");
                /*System.out.println("-------------------------------------------------------------------------------");

                System.out.println (" Famouze Bakery, Madamme Memziers ");
                System.out.println ("\n");
                System.out.println ("Category         Favorite         Qty Prepared   Qty Sold  Selling Price($) ");
                System.out.println("-------------------------------------------------------------------------------");
                System.out.print   ("[c1]Cheese "+ "         " + precheese + "            " +soldcheese + "           " +sellcheese);

                System.out.print   ("[c2]Chocolate" + "           " + prechoc   + "      " +soldchoc   + "           " +sellchoc);

                System.out.println (" [c3]Strawberry" + "          " + prestraw   + "         " +soldstraw   + "           " +sellstraw);

                System.out.println ("\n");
                System.out.print   ("Pastry           [p1]Egg" + "       " + preegg + "           " +soldegg + "            " +sellegg);

                System.out.print   (" [p2]Pineapple" + "           " + prepine   + "     " +soldpine   + "            " +sellpine);

                System.out.println ("\n");
                System.out.print   ("Cookie           [k1]Pecan and Nuts" + "     " + prenuts + "           " +soldnuts + "            " +sellnuts);

                System.out.println   (" [k2]Chocolate chips" + "    " + prechips   + "      " +soldchips   + "            " +sellchips);

                System.out.println ("\n");
                System.out.println("-------------------------------------------------------------------------------");
                System.out.println ("[S]ales             [D]aily Order            [C]ollection              E[X]it");
                System.out.println ("\n");
                //char moreitems = 'y' ;
                //char option = 'y' ;*/
                char option;
                System.out.print ("Option:" );
                option = (char)System.in.read();
                System.in.skip(2);
                //System.out.println("\n");
                switch(option){
                case 's':
                {
                        totalsum = 0.00;
            String cont = "y";
            do
                        {
                System.out.println("Items sold:-");
                System.out.println("enter product code:");
                byte[] buffer = new byte[numberOfBytesToRead];
                int bytesRead = System.in.read(buffer);
                byte[] bufferRead = new byte[bytesRead - 2];
                System.arraycopy(buffer, 0, bufferRead, 0, bytesRead - 2);
                code = new String(bufferRead);
                //System.out.println("code="+code);
                System.out.print("Enter quantity:");
                bytesRead = System.in.read(buffer);
                bufferRead = new byte[bytesRead - 2];
                System.arraycopy(buffer, 0, bufferRead, 0, bytesRead - 2);
                quantity = new Integer(new String(bufferRead)).intValue();
                System.out.println("quantity="+quantity);
                if ("c1".equals(code))
                {
                        System.out.println("c1.equals(code)");
                        if (quantity > precheese2)
                    {
                            System.out.println("not enough stock");
                        }
                        else
                        {
                       precheese2 = precheese2 - quantity;
                       soldcheese = soldcheese + quantity;
                       total = sellcheese * quantity;
                       totalsum = totalsum + total;
                       System.out.println("precheese2="+precheese2);
                       System.out.println("soldcheese="+soldcheese);
                       System.out.println("total="+total);
                       System.out.println("totalsum="+totalsum);
                    }
               }
                   if ("c2".equals(code))
               {
                        System.out.println("c2.equals(code)");
                        if (quantity > prechoc2)
                    {
                        System.out.println("not enough stock");
                    }
                        else
                        {
                       prechoc2 = prechoc2 - quantity;
                       soldchoc = soldchoc + quantity;
                       total = sellchoc * quantity;
                       totalsum = totalsum + total;
                       
                       System.out.println("prechoc2="+prechoc2);
                       System.out.println("soldchoc="+soldchoc);
                       System.out.println("total="+total);
                       System.out.println("totalsum="+totalsum);
                    }
               }
               if ("c3".equals(code))
               {
                        System.out.println("c3.equals(code)");
                        if (quantity > prestraw2)
                    {
                        System.out.println("not enough stock");
                    }
                        else
                        {
                       prestraw2 = prestraw2 - quantity;
                       soldstraw = soldstraw + quantity;
                       total = sellstraw * quantity;
                       totalsum = totalsum + total;
                       System.out.println("prestraw2="+prestraw2);
                       System.out.println("soldstraw="+soldstraw);
                       System.out.println("total="+total);
                       System.out.println("totalsum="+totalsum);
                    }
               }  
               if ("p1".equals(code))
               {
                        System.out.println("p1.equals(code)");
                        if (quantity > preegg2)
                    {
                        System.out.println("not enough stock");
                    }
                        else
                        {
                       preegg2 = preegg2 - quantity;
                       soldegg = soldegg + quantity;
                       total = sellegg * quantity;
                       totalsum = totalsum + total;
                       System.out.println("preegg2="+preegg2);
                       System.out.println("soldegg="+soldegg);
                       System.out.println("total="+total);
                       System.out.println("totalsum="+totalsum);
                        }
               }
               if ("p2".equals(code))
               {
                        System.out.println("p2.equals(code)");
                        if (quantity > prepine2)
                    {
                         System.out.println("not enough stock");
                    }
                    else
                    {
                       prepine2 = prepine2 - quantity;
                       soldpine = soldpine + quantity;
                       total = sellpine * quantity;
                       totalsum = totalsum + total;
                       System.out.println("prepine2="+prepine2);
                       System.out.println("soldpine="+soldpine);
                       System.out.println("total="+total);
                       System.out.println("totalsum="+totalsum);
                    }
               }
               if ("k1".equals(code))
               {
                         System.out.println("k1.equals(code)");
                         if (quantity > prenuts2)
                     {
                        System.out.println("not enough stock");
                     }
                         else
                         {
                       prenuts2 = prenuts2 - quantity;
                       soldnuts = soldnuts + quantity;
                       total = sellnuts * quantity;
                       totalsum = totalsum + total;
                       System.out.println("prenuts2="+prenuts2);
                       System.out.println("soldnuts="+soldnuts);
                       System.out.println("total="+total);
                       System.out.println("totalsum="+totalsum);
                     }
                }
                if ("k2".equals(code))
                {
                        System.out.println("k2.equals(code)");
                        if (quantity > prechips2)
                    {
                        System.out.println("not enough stock");
                    }
                    else
                    {
                       prechips2 = prechips2 - quantity;
                       soldchips = soldchips + quantity;
                       total = sellchips * quantity;
                       totalsum = totalsum + total;
                       System.out.println("prechips2="+prechips2);
                       System.out.println("soldchips="+soldchips);
                       System.out.println("total="+total);
                       System.out.println("totalsum="+totalsum);
                    }
                }
                        System.out.println("More Items(y/n) ?");
                        DataInputStream user_input = new DataInputStream(System.in);
                        cont =  user_input.readLine();
                        }
                        while (cont.equals("y")||cont.equals("y"));
                        System.out.println("Total amount due =" +totalsum);
                        }
                }
        }
  }

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
PAQ'd and pts refunded
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venci75
EE Cleanup Volunteer
ASKER CERTIFIED SOLUTION
Avatar of Netminder
Netminder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial