Results 1 to 7 of 7
  1. #1
    mindblaster's Avatar
    mindblaster is offline Member
    Join Date
    Dec 2009
    Location
    Pakistan
    Posts
    18
    Rep Power
    0

    Default Computer Shop - New Version

    Hey this is the New Version of my program. i use Interface and Inheritance in this program, hope you like this:D

    I use Three classes Costumer, Salesman, Maneger and one interface Stock.

    Maneger is superclass of Salesman.

    Main Class
    Java Code:
    package mainapp;
    
    /**
     *
     * @author abansari
     */
    import java.util.Scanner;
    public class Main
    {
        public static void main(String[] args)
        {
            String cOrder;
            String cName;
            int Qty;
            Scanner input = new Scanner(System.in);
            System.out.println("Your Good Name Sir:");
            cName = input.next();
            System.out.println("What do you want to Purchased Sir:");
            cOrder = input.next();
            System.out.println("How Many (Qty):");
            Qty = input.nextInt();
            
            Costumer c = new Costumer(cName,cOrder,5000,Qty);
    
            SalesMan s = new SalesMan(c,"Basit" );
            s.CheckInStock();
            s.takeOrder(); 
         }
    }
    Costumer Class
    Java Code:
    package mainapp;
    
    /**
     *
     * @author abansari
     */
    
    public class Costumer
    {
        String cName;
        String cOrder;
        int cMoney;
        int cQty;
    
        public Costumer(String n, String o, int m, int q)
        {
            cQty = q;
            cMoney = m;
            cName = n;
            cOrder = o;
        }
        
        public String getOrder()
        {
            return cOrder;
        }
        public String getName()
        {
            return cName;
        }
        public int getMoney()
        {
            return cMoney;
        }
        public void setMoney(int m)
        {
            cMoney = m;
        }
    }
    SalesMan Class
    Java Code:
    package mainapp;
    
    /**
     *
     * @author abansari
     */
    import java.util.Scanner;
    public class SalesMan extends Maneger implements Stock
    {
        String sName;
        String cOrder;
        String cName;
        int cMoney;
        Boolean isStock;
        int myPrice;
        int cQty;
        
            
        public SalesMan(Costumer c, String s)
        {
            cOrder = c.cOrder;
            cName = c.cName;
            cMoney = c.cMoney;
            sName = s;
            cQty = c.cQty;
        }
    
        public void CheckInStock()
        {
            System.out.println("Checking....\n");
            for(int i = 0; i<List.length; i++)
            {
                if(List[i].equalsIgnoreCase(cOrder))
                {
                    myPrice = Price[i];
                    isStock = true;
                }
            }                
        }
    
        public void takeOrder()
        {
            try{
                if(isStock)
                {
                    String ans;
                    Scanner input = new Scanner(System.in);
                    System.out.println("Price of " + cOrder + " is " + myPrice);
                    System.out.println("Are you want to Buy it? (y/n)");
                    ans = input.next();
                    if(ans.equalsIgnoreCase("y"))
                    {
                        if(myPrice > cMoney)
                        {
                            System.out.println("You Have not Enough Money to Purchased it.");
                        }
                    else
                        {
                            System.out.println("************************");
                            System.out.println("Name: " + cName);
                            System.out.println("Item\tPrice");
                            System.out.println("________________________");
                            System.out.println(cOrder + "("+ cQty +")\t" + myPrice);
                            
                            System.out.println("Money\t" + cMoney);
                            System.out.println("Return\t" + (cMoney - myPrice));
                             System.out.println("________________________");
                            System.out.println("Bill\t" + cQty*myPrice);
                            setSales();
                            //System.out.println(getSales());
                            System.out.println("************************");
                        }
                    }
            }
            }catch(NullPointerException e)
            {
                System.out.println("Invalid Order");
            }
    
            System.out.println("Thank you Sir!");
        }    
    }
    Stock Interface
    Java Code:
    package mainapp;
    
    /**
     *
     * @author abansari
     */
    
    public interface Stock
    {
        public static String[] List = {"CPU","HardDisk","CDRom"};
        public static int[] Price = {2000,500,250};
    }
    Maneger Class
    Java Code:
    package mainapp;
    
    /**
     *
     * @author abansari
     */
    
    public class Maneger
    {
        int totalSales;
    
        public Maneger()
        {totalSales = 0;}
    
    
    
        public void setSales()
        {
            totalSales++;
        }
        public int getSales()
        {
            return totalSales;
        }
    }
    Please Optimize my code (JAVA EXPERTS), i will very thank full to you guys.:p
    Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

  2. #2
    mindblaster's Avatar
    mindblaster is offline Member
    Join Date
    Dec 2009
    Location
    Pakistan
    Posts
    18
    Rep Power
    0

    Default

    Please reply guyz....
    Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

  3. #3
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    11

    Default

    hey
    am kinda confused what do you mean by optimize?
    is the program running slow?

  4. #4
    mindblaster's Avatar
    mindblaster is offline Member
    Join Date
    Dec 2009
    Location
    Pakistan
    Posts
    18
    Rep Power
    0

    Default

    no, i mean the Is there any improvement of my logic to improve this program, i am very very newbie in java, so i wanna learn the new techniques by Java Experts. ;)
    Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

  5. #5
    thomasdevos's Avatar
    thomasdevos is offline Member
    Join Date
    Feb 2010
    Posts
    19
    Rep Power
    0

    Default

    hi mindblaster
    if you are really a java newbie, you DO a great job. i can feel you get very good talent to become one java guru.
    good luck.

    below is my try on your code:
    Java Code:
    Your Good Name Sir:
    thomas
    What do you want to Purchased Sir:
    CPU
    How Many (Qty):
    1
    Checking....
    
    Price of CPU is 2000
    Are you want to Buy it? (y/n)
    y
    ************************
    Name: thomas
    Item	Price
    ________________________
    CPU(1)	2000
    Money	5000
    Return	3000
    ________________________
    Bill	2000
    ************************
    Thank you Sir!
    i hold 7 years develop exp. now i start a thread to share my knowlege about a j2ee project. welcome to participate.Study Java Through Real Java Project

  6. #6
    mindblaster's Avatar
    mindblaster is offline Member
    Join Date
    Dec 2009
    Location
    Pakistan
    Posts
    18
    Rep Power
    0

    Default

    Thanks Mr.thomasdevos , I am really the newbie in java and thanks for appreciate. :)
    Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

  7. #7
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    11

    Default

    Hey

    the code looks fine enough from my perspective.

    If you want a peek at java efficiency books..
    Java efficiency by joshua bloch is a pretty good book.( it needs a bit advanced knowledge in some of its topics)
    Effective Java Second Edition

Similar Threads

  1. Help needed with 'shop' program
    By jkhamler in forum New To Java
    Replies: 1
    Last Post: 12-06-2009, 05:35 PM
  2. I am new, can anyone help me in Computer Vision?
    By howardL in forum Introductions
    Replies: 0
    Last Post: 04-23-2009, 09:37 AM
  3. E-shop with error
    By snowbirdie in forum Advanced Java
    Replies: 1
    Last Post: 05-04-2008, 05:35 AM
  4. How to scan your computer for ports in use
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 08:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •