开发者

Scanner Input not Reading Method Calling as a Parameter

First of All here's the Codes:

Product Information:

public class ProductInformation
{
    public String code;
    public String itemName;
    public double price;
    //Constructor
    public ProductInformation()
    { itemName="-";
      code="-";
      price=0;}
    //Setter
    public void setItemName(String itemName)
    { this.itemName=itemName;}
    public void setCode(String code)
    { this.code=code;}
    public void setPrice(double price)
    { this.price=price;}
    //Getter
    public String getItemName()
    { return itemName;}
    public double getPrice()
    { return price;}
    public String getCode()
    { return code;}

    //Products
    public void getProduct(String code)
    {   if(code == "A001"){
            setCode("A001");
            setItemName("Mouse  ");
            setPrice(100);}
        else if(code == "A002"){
            setCode("A002");
            setItemName("Monitor    ");
            setPrice(2500);}
        else if(code == "A003"){
            setCode("A003");
            setItemName("Keyboard");
            setPrice(200);}
        else if(code == "A004"){
            setCode("A004");
            setItemName("Flash Disk");
            setPrice(300);}
        else if(code == "A005"){
            setCode("A005");
            setItemName("Hard Disk");
            setPrice(1500);}
            }
}

import java.util.*;

Product Display:

public class ProductDisplay
{   public ProductInformation product;
    public ProductDisplay()
    {
        System.out.println("\t\t\t\t  RG COMPUTER SHOP");
        System.out.println("\t\t\t\t    Makati City");
        System.out.println("\t\tP R O D U C T\tI N F O R M A T I O N");
        System.out.println("-------------------------------------------------------");
        System.out.println("\t\tCode\t\tDescription\t\tUnit Price");
        System.out.println("-------------------------------------------------------");
        product = new ProductInformation();
        product.getProduct("A001");
        display();
        product = new ProductInformation();
        product.getProduct("A002");
        display();
        product = new ProductInformation();
        product.getProduct("A003");
        display();
        product = new ProductInformation();
        product.getProduct("A004");
        display();
        product = new ProductInformation();
        product.getProduct("A005");
        display()开发者_高级运维;
    }
    // Display Method
    public void display()
    {  System.out.println("\t\t" + product.getCode()
                        + "\t\t" + product.getItemName()
                        + "\t\t" + product.getPrice()); }
}

My Problem is here in PointOfSale I tried to let the buyer decide by entering a Product Code after entering the getProduct I get is always "A005" Even if I enter "A001" or something else. I think the product.getProduct(code) is not working out. Can someone help me fix this problem so i can continue scripting this

import java.util.*;
public class PointOfSale extends ProductDisplay
{   
    public PointOfSale()
    {   System.out.print("\nPurchase Item(y/n)?");
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        if("y".equalsIgnoreCase(line)){
        OpenOrder();
            }
    }
    //=============================================
    public void OpenOrder() // New Order
    {   ArrayList<String> ProductList = new ArrayList<String>();
        ProductList.add("A001");    ProductList.add("A002");
        ProductList.add("A003");    ProductList.add("A004");
        ProductList.add("A005");
        System.out.print("Select Product Code:");
        Scanner sc = new Scanner(System.in);
        String code = sc.next();
        if(ProductList.contains(code))
        { product.getProduct(code);
          display(); EnterQuantity();       }
        else System.out.print("Product Code is Invalid\n"); System.exit(0);}
    //==============================================
    public void EnterQuantity()  //Entering Quantity
    {
        try{
            System.out.print("Enter Quantity:");
            Scanner sc = new Scanner(System.in);
            int quantity = sc.nextInt();
            double amount = quantity * product.getPrice();
            System.out.print("Amount: " + amount);}
        catch (InputMismatchException nfe)
            {System.out.print("\nInvalid Entry: Input must be a Number.\n"); System.exit(0);}
    }
    // Main Method
    public static void main(String[] args)
    {   new PointOfSale(); }
}


You are comparing strings for equality in your getProduct() method. Strings are objects, so you probably want to use the equals() method:

public void getProduct(String code)
{   if(code.equals("A001")){
        setCode("A001");
        setItemName("Mouse  ");
        setPrice(100);}
    else if(code.equals("A002")){
        setCode("A002");
        setItemName("Monitor    ");
        setPrice(2500);}
    else if(code.equals("A003")){
        setCode("A003");
        setItemName("Keyboard");
        setPrice(200);}
    else if(code.equals("A004")){
        setCode("A004");
        setItemName("Flash Disk");
        setPrice(300);}
    else if(code.equals("A005")){
        setCode("A005");
        setItemName("Hard Disk");
        setPrice(1500);}
        }


One major problem is that you're comparing Strings using == and you in general shouldn't do that as this compares if one object reference is the same as another and you don't care about this. Instead you care if the two String variables hold the same String representation, and for this use the equals() or equalsIgnoreCase() methods.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜