3

到目前为止,我已经创建了两个类,我将在下面展示

我已经测试了这两个类,它们似乎都有效。

我现在需要创建一个菜单,该菜单将使用我创建的两个类,并允许用户输入信息并在其帐户中查找以下信息:

  1. 添加客户详细信息
  2. 存款到企业账户
  3. 将抄表记录到企业帐户
  4. 显示企业账户的当前余额
  5. 显示完整的帐户详细信息
  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());

用户输入的内容与我正在调用的方法之间没有联系,不知道如何解决。

4

2 回答 2

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

要回答您的第二个问题,您需要有一个可以为变量赋值的方法(在您的对象的类中,而不是主类中)。IE:

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());
于 2013-05-19T05:28:37.700 回答
0

根据 SteveP 的说法,将其他类的实例作为实例变量存储在 Menu 类中是个好主意。

此外,如果您使用的是 GUI 界面,这个 JMenu 类 API 可能会派上用场: http ://docs.oracle.com/javase/7/docs/api/javax/swing/JMenu.html

还要寻找 JMenuBar、JMenuItem

于 2013-05-19T05:48:33.590 回答