2014-11-02 122 views
-1

好吧,我做了三個類。一個是測試人員,另外兩個是藍圖班。我似乎無法將藍圖類ScanShop中的私人雙重購物車變量稱爲ShoppingCart。我想過使用訪問器和getter,但是我現在混淆了自己,不知道自己出錯的地方。這裏是我的代碼:調用另一個類的變量JAVA

package exercise3; 
import java.util.Scanner; 
public class ScanShop 
{ 
private double cart =0; 

public double getcart() 
{ 
    return cart; 
} 
public void setcart(double cart) 
{ 
    this.cart =cart; 
} 
public void scan() 
{ 

    //the prices of items 
    double p1; 
    double p2; 
    double p3; 
    double total; 

    Scanner in = new Scanner(System.in); 

    System.out.println("what price is item one?"); 
    p1 = in.nextDouble(); 
    System.out.println("What is price of item two?"); 
    p2= in.nextDouble(); 
    System.out.println("And what is the price of the third item?"); 
    p3= in.nextDouble(); 

     total = p1 + p2 + p3; 


    System.out.printf("The total bill is %.2f\n\n", total); 


    setcart(total); 

    System.out.println("the cart is: " + getcart()); 

    in.close(); 




} 
} 

這裏是我的購物藍圖類:

package exercise3; 

import java.util.Scanner; 
import javax.swing.JOptionPane; 
public class ShoppingCart 
{ 
ScanShop amount = new ScanShop(); 





public void getbill() 
{ 

    JOptionPane.showMessageDialog(null,"your total is: " + amount.getcart()); 
} 

public void billCal() 
{ 
    String answer; 
    int number; 
    Scanner input = new Scanner(System.in); 

    /*System.out.println("please enter how much your bill is:..."); 
    //how much bill is: 
    cart = in.nextDouble(); 
    in.nextLine(); 
    System.out.printf("you have entered: %.2f", cart);*/ 

    System.out.println("Do you have a loyalty card? y or n"); 
    // asking do you have loyalty card 
    answer= input.next(); 

    if (answer.equalsIgnoreCase("y")) 
    { 

     amount.setcart(amount.getcart()*0.9); 

     //other vouchers to discount 
     System.out.println("thats great! do you have a voucher: " 
       + "\n(1) £5 " 
       + "\n(2) £10 " 
       + "\n (3) no vouchers"); 
     number= input.nextInt(); 
     switch(number) 
     { 
     case 1 : 
      amount.setcart(amount.getcart()-5); 
      getbill(); 
      break; 

     case 2 : 
      amount.setcart(amount.getcart()-10); 
      getbill(); 
      break; 

     default : 
      getbill(); 
      break; 
     } 
    } 

    else 
    { 
     getbill(); 
    } 

    input.close(); 
}//closing billCal 

} 

最後這裏是我的測試類:

package exercise3; 

public class ShoppingCart_Test { 
public static void main (String [] arg){ 
    ShoppingCart customerOne = new ShoppingCart(); 
    //c1 is customer one 
    ScanShop c1 = new ScanShop(); 


    c1.scan(); 
    customerOne.billCal(); 


} 
} 
+0

出現這個問題因爲它需要對OO編程進行介紹,所以是無關緊要的。堆棧溢出不是一本好書,教程或老師的替代品。 – 2014-11-02 21:47:15

+0

'in.close();'你知道你的程序因拋出了'NoSuchElementException'而崩潰嗎? – Radiodef 2014-11-02 22:22:14

回答

2

不能達到的private變量來自另一班的班級。 private字段只能從字段的所有者類訪問。

如果你想獲得私人領域的價值,你應該爲該領域創建公共的getter方法。

+0

所以我改變了ScanShop類:私人雙購物車公共雙推車,我仍然得到同樣的錯誤。公共獲得者和制定者是否與私人一樣? – Crazypigs 2014-11-03 20:04:57

2

如果你想使用ScanShop變量在我的購物billCal方法,你應該改變它取號(雙)和它的工作。如果您要在測試儀中創建它們兩個,請勿在ShoppingCart內部創建ScanShop的實例。就像你的代碼改成這樣:

public void getbill(double total) 
{ 
JOptionPane.showMessageDialog(null,"your total is: " + total); 
} 

,做你的billCal這樣的:

public void billCal(Double totalAmount) 
{ 
    (...) 
switch(number) 
    { 
    case 1 : 
     getbill(totalAmount-5); 
     break; 
    (...) 
} 

最後你的主要方法是:

public static void main (String [] arg){ 
ShoppingCart customerOne = new ShoppingCart(); 
ScanShop c1 = new ScanShop(); 


c1.scan(); 
customerOne.billCal(c1.getCart()); 

}

+0

謝謝你這樣做的方式,但我想知道你是否可以在同一臺測試儀上使用兩個類。 – Crazypigs 2014-11-03 20:02:44

相關問題