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

    Default Optimize my Code please....

    Hey Guys! :p

    I written a program that works well, program is made for a Shop.

    There are 2 objects in my program, SalesMan, Costumer.

    Sales Man Class
    Java Code:
    package mainapp;
    
    /**
     *
     * @author abansari
     */
    import java.util.Scanner;
    public class SalesMan 
    {
        String sName;
        String sOrder;
        String cosName;
        Boolean isStock;
        int cosMoney;
        String[] List = {"Sofa","Chair","Watch"};
        int[] Price = {2000,500,250};
        int myPrice = 0;
        SalesMan(Costumer c, String sname)
        {
            sName = sname;
            sOrder = c.getOrder();
            cosName = c.getName();
            cosMoney = c.getMoney();
        }
    
        public void Check()
        {
            System.out.println("Processing....\n\n");
            for(int i = 0; i < List.length; i ++)
            {
                if(List[i].equals(sOrder))
                {
                    myPrice = Price[i];
                    isStock = true;
                }
            }
        }
    
        public void takeOrder()
        {          
            if(isStock)
            {
                String ans;
                Scanner input = new Scanner(System.in);
                System.out.println("Price of " + sOrder + " is " + myPrice);
                System.out.println("Are you want to Buy it? (y/n)");
                ans = input.next();
                if(ans.equalsIgnoreCase("y"))
                {
                if(myPrice > cosMoney)
                {
                    System.out.println("You Have not Enough Money to Purchased it.");
                }
            
            else
            {
                System.out.println("Order is Placed Sir!");
                System.out.println(cosName + " Purchased " + sOrder);
                System.out.println("Price is " + myPrice);
                System.out.println("Costumer gives " + cosMoney);
                System.out.println("Money Back " + (cosMoney - myPrice));
            }
            }
            }
            System.out.println("Thank you Sir!");
        }
    }
    Costumer Class
    Java Code:
    package mainapp;
    
    
    /**
     *
     * @author abansari
     */
    import java.util.Scanner;
    public class Costumer
    {
        String cName;
        String Order;
        int money;
        Costumer(int m)
        {
            money = m;
        }
    
        public void GiveOrder()
        {
            Scanner input = new Scanner(System.in);
            System.out.println("Your Order Sir:");
            Order = input.next();
            System.out.println("Your Good Name Sir:");
            cName = input.next();        
        }
        public String getOrder()
        {
            return Order;
        }
        public String getName()
        {
            return cName;
        }
        public int getMoney()
        {
            return money;
        }
    }

    Main Class


    Java Code:
    package mainapp;
    /**
     *
     * @author abansari
     */
    //import java.util.*;
    public class Main {
    
        public static void main(String[] args)
        {
            Costumer c = new Costumer(3000);
            c.GiveOrder();
            SalesMan s = new SalesMan(c, "Basit");
            s.Check();        
            s.takeOrder();
            
        }
    }

    this program works well, please give me some suggestion to improve this program

    I'm newbie in java i wanna learn new thing from experts of java,

    Thanks:cool:
    Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

  2. #2
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    The code looks ok to me, the best optimisation I'd say would be tracking the logical path of how the program should work and then try to replicate it in code. If you have done it, the you are good to go.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Costumer (Customer?) shouldn't have a Scanner object and shouldn't concern itself with user interaction as that should be elsewhere in the code. It should only concern itself with maintaining the state of Customer. In other words, I'd get the giveOrder method out of the Customer class.

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

    Default

    Quote Originally Posted by Fubarable View Post
    In other words, I'd get the giveOrder method out of the Customer class.
    but why?:confused:

    i get input in costumer class because eace when you create other costumer obj
    it get Input by that costumer.
    Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by mindblaster View Post
    but why?:confused:

    i get input in costumer class because eace when you create other costumer obj
    it get Input by that costumer.
    You should build your classes to be as flexible as possible, to be useful in as many situations as possible. Right now you're using your Customer class in a console environment but what if later you want to use it in a GUI? Your tying the Customer class to the Console prevents this from happening. Better is to create Customer constructors that take appropriate parameters. Then if you want to create Customer objects using Scanner, the class does your user interaction can get a Customer's information with a Scanner object and create a Customer object, and conversely, if you want to use Customer objects in a GUI, your user interface classes will change as will the program flow, but the Customer class can be used unchanged.

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

    Default

    Quote Originally Posted by Fubarable View Post
    You should build your classes to be as flexible as possible, to be useful in as many situations as possible. Right now you're using your Customer class in a console environment but what if later you want to use it in a GUI? Your tying the Customer class to the Console prevents this from happening. Better is to create Customer constructors that take appropriate parameters. Then if you want to create Customer objects using Scanner, the class does your user interaction can get a Customer's information with a Scanner object and create a Customer object, and conversely, if you want to use Customer objects in a GUI, your user interface classes will change as will the program flow, but the Customer class can be used unchanged.
    oh, great explained. i never think at this point to create this program.:rolleyes:

    thanks for teach me some tips. is their more any optimization in my whole program ??:confused:
    Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

Similar Threads

  1. Red5 0.7 code to 0.9 rc2 code
    By gerd33 in forum New To Java
    Replies: 4
    Last Post: 12-29-2009, 07:50 PM
  2. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  3. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM
  4. Why doesn't this code accept my code?
    By PeterFeng in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 01:39 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 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
  •