2013-05-19 46 views
3

到目前爲止,我還創建了兩個類,方法,我會告訴下面不知道如何創建一個使用從我的其他類

我檢測了兩個班,他們都似乎工作的菜單。

我現在需要創建將使用我創建了兩個類,並允許用戶輸入的信息,並找到他們的帳戶信息,以下菜單:

  1. 添加客戶詳細介紹
  2. 進行存款的業務帳戶
  3. 錄製抄表業務帳戶
  4. 顯示業務帳戶
  5. 顯示器充分考慮d的當前餘額etails
  6. 更改折扣價值,爲企業帳戶
  7. 更改單位成本爲所有業務佔
  8. 如何使用菜單系統

雖然我已經盡了最大努力找出如何在線使用資源,我現在丟失了。到目前爲止,每次嘗試創建菜單系統時,我都無法使其工作。如果有人能夠幫助我瞭解如何去做,我會非常感激。

謝謝:)


public class GasAccount 
{ 
    private int intAccRefNo; 
    private String strName; 
    private String strAddress; 
    public double dblBalance; 
    private double dblUnits; 
    public static double dblUnitsCosts = 0.02; 

    public GasAccount (int intNewAccRefNo , String strNewName , String strNewAddress) 
    { 
    } 

    public GasAccount (int intNewAccRefNo , String strNewName , String strNewAddress , double dblNewUnits) 
    { 
     intAccRefNo = intNewAccRefNo; 
     strName = strNewName; 
     strAddress = strNewAddress; 
     dblUnits = dblNewUnits; 
     dblBalance = dblUnits * dblUnitsCosts; 
    } 

    public int getAccRefNo() 
    { 
     return intAccRefNo; 
    } 

    public String getName() 
    { 
     return strName; 
    } 

    public String getAddress() 
    { 
     return strAddress; 
    } 

    public void deposit(double dblDepositAmount) 
    { 
     dblBalance = dblBalance - dblDepositAmount; 
    } 

    public double getBalance() 
    { 
     return dblBalance; 
    } 

    public double getUnitCost() 
    { 
     return dblUnitsCosts; 
    } 

    public void recordUnits (double dblUnitsUsed) 
    { 
     dblBalance = dblBalance + dblUnitsUsed * dblUnitsCosts; 
     dblUnits = dblUnitsUsed + dblUnits; 
    } 

    public double getUnits() 
    { 
     return dblUnits; 
    } 

    public void updateUnitsCosts(double dblNewUnitsCosts) 
    { 
     this.dblUnitsCosts = dblNewUnitsCosts; 
    } 
} 

而另一延伸它 -

public class BusinessAccount extends GasAccount 
{ 

    private double dblDiscount; 

    public BusinessAccount (int intNewAccRefNo, String strNewName, String strNewAddress, double dblNewUnits, double dblNewDiscount) 
    { 
     super (intNewAccRefNo , strNewName , strNewAddress, dblNewUnits); 
     dblDiscount = dblNewDiscount; 
    } 

    public void setNewDiscount(double dblNewDiscount) 
    { 
     dblDiscount = dblNewDiscount; 
    } 

    public double getDiscount() 
    { 
     return dblDiscount; 
    } 

    @Override 
    public void recordUnits (double dblUnitsUsed) 
    { 
     double dblNewBalance; 
     dblBalance = dblBalance + dblUnitsUsed * dblUnitsCosts; 
     dblNewBalance = dblUnitsUsed * dblUnitsCosts * dblDiscount/100; 
     dblBalance = dblBalance - dblNewBalance; 
    } 

這裏是我的嘗試菜單看起來像到第五個選項。由於BuisnessAccount.getMethod始終顯示爲錯誤,因此我正在調用其他類的方法時發生了可怕的錯誤。我也很確定再次聲明變量是完全錯誤的,因爲他們沒有鏈接到我的其他類。

如果有人可以幫我解決這一點,將不勝感激

import java.util.Scanner; 

public class Menu 
{ 
    public static void main(String [] args) 
    { 

     Scanner input = new Scanner(System.in); 

     int Choice; 

     { 
      System.out.println("------------------------------"); 
      System.out.println ("1. Add the customers details") ; 
      System.out.println ("2. Make a deposit to the business account"); 
      System.out.println ("3. Record a meter reading to the business account") ; 
      System.out.println ("4. Display current balance of the business account") ; 
      System.out.println ("5. Display full account details") ; 
      System.out.println ("6. Change the discount value for the business account") ; 
      System.out.println ("7. Change the cost per unit for all business accounts "); 
      System.out.println ("8. How to use the menu system "); 
      System.out.println ("Any other number will exit the program"); 
      System.out.println("------------------------------"); 
      System.out.println ("\n\nEnter a number from 1 to 8"); 
      Choice = input.nextInt(); 

      switch (Choice) 
      { 
      case 1 : 
       int intNewAccRefNo; 
       String strNewName; 
       String strNewAddress; 
       Double dblNewUnits; 
       Double dblNewDiscount; 

       System.out.println("Please enter the account number?"); 
       intNewAccRefNo = input.nextInt(); 

       System.out.println("Please enter the account name?"); 
       input.nextLine(); 
       strNewName = input.nextLine(); 

       System.out.println("Please enter the account address?"); 
       strNewAddress = input.nextLine(); 

       System.out.println("Please enter the number of initial number of units used?"); 
       dblNewUnits = input.nextDouble(); 

       System.out.println("Please enter the discount?"); 
       dblNewDiscount = input.nextDouble(); 

      case 2: 

       double dblDeposit; 

       System.out.println("Please enter the amount you want to deposit?"); 
       dblDeposit = input.nextDouble(); 

       System.out.println ("The current balance: " + BusinessAccount.getBalance()) ; 

      case 3: 

       double dblUnits; 

       System.out.println("Enter the number of Units Used"); 
       dblUnits = input.nextDouble(); 
       BusinessAccount.recordUnits(dblUnits); 

      case 4: 

       System.out.println("\n Current Balance: £"+ BusinessAccount.getBalance()); 

      case 5: 

       System.out.println("Account Reference Number: " + BusinessAccount.getAccRefNo()); 
       System.out.println("Address: " + BusinessAccount.getAddress()); 
       System.out.println("Name: " + BusinessAccount.getName()); 
       System.out.println("Balance: " + BusinessAccount.getBalance()); 
       System.out.println("Discount: " + BusinessAccount.getDiscount()); 
       System.out.println("Units: " + BusinessAccount.getUnits()); 

case 1 : 

String strAddress; 

System.out.println("Please enter the account address?"); 
strAddress = input.nextLine(); 
System.out.println("Address:" + firstAccount.getAddress()); 

有什麼用戶輸入和我打電話的方法之間沒有聯繫,不知道如何固定。

+0

的[不知道如何創建一個菜單可能重複它使用我的其他類](http://stackoverflow.com/questions/16631050/not-sure-how-to-create-a-menu-which-uses-my-other-class) – camickr

回答

3

通過使用BusinessAccount.someMethod(),您試圖在靜態上下文中調用該方法,但您的方法不是靜態的。要在你的方法調用,您可能需要讓他們的靜態或需要創建一個對象,然後可以給他們打電話,即:

BusinessAccount ba= new BusinessAccount (4, "name", "address", 3.4, 43.4); 
ba.someMethodFromClass(); 

在你的情況,你不希望他們是靜態的。詳細瞭解靜態方法/變量(見下文)。

有些文檔可能會有所幫助:
http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
http://docs.oracle.com/javase/7/docs/api/javax/swing/JMenu.html

要回答你的第二個問題,你需要有一個方法(在你的對象的類,而不是主要的),它可以賦值的變量。即:

public void setAddress (String address) 
{ 
    strAddress=address; 
} 

然後,你會將你從main讀入的地址傳遞給你的函數。通過這樣做,你初始化/從用戶指定的值存儲在你的類中的值,即:

System.out.println("Please enter the account address?"); 
String temp = input.nextLine(); 
ba.setAddress(temp); 
System.out.println("Address:" + ba.getAddress()); 

或更好,但

System.out.println("Please enter the account address?"); 
ba.setAddress(input.nextLine()); 
System.out.println("Address:" + ba.getAddress()); 
+1

另外,你的構造函數/類有不同的名字...業務vs商業 –

+0

好吧,我已經做了對象,現在可以調用我的其他類的方法。但是,我現在有一個不同的問題,我已經添加在我的帖子的底部。 – Mark

+0

我不太確定你的意思,「然後,你會將你從main讀入的地址傳遞給你的函數。」你能給我一個例子嗎? – Mark

相關問題