java - Not sure how to create a menu which uses methods from my other classes -
so far have created 2 classes, show below
i have tested both classes , both seem work.
i need create menu use 2 classes have created , allow users enter information , find information on account following:
- add customers details
- make deposit business account
- record meter reading business account
- display current balance of business account
- display full account details
- change discount value business account
- change cost per unit business accounts
- how use menu system
while have tried best find out how using resources online lost. point every time have tried create menu system not work. appreciate if me basics on how go doing it.
thanks :)
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; } }
and extends -
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; }
here attempted menu looks fifth option. doing horribly wrong calling in methods other classes buisnessaccount.getmethod shows error. pretty sure declaring variables again wrong have no link other classes.
if me solve appreciated
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 customers details" ) ; system.out.println ( "2. make deposit business account" ); system.out.println ( "3. record meter reading business account" ) ; system.out.println ( "4. display current balance of business account" ) ; system.out.println ( "5. display full account details" ) ; system.out.println ( "6. change discount value business account" ) ; system.out.println ( "7. change cost per unit business accounts "); system.out.println ( "8. how use menu system "); system.out.println ( "any other number exit program"); system.out.println("------------------------------"); system.out.println ( "\n\nenter number 1 8" ); choice = input.nextint(); switch (choice) { case 1 : int intnewaccrefno; string strnewname; string strnewaddress; double dblnewunits; double dblnewdiscount; system.out.println("please enter account number?"); intnewaccrefno = input.nextint(); system.out.println("please enter account name?"); input.nextline(); strnewname = input.nextline(); system.out.println("please enter account address?"); strnewaddress = input.nextline(); system.out.println("please enter number of initial number of units used?"); dblnewunits = input.nextdouble(); system.out.println("please enter discount?"); dblnewdiscount = input.nextdouble(); case 2: double dbldeposit; system.out.println("please enter amount want deposit?"); dbldeposit = input.nextdouble(); system.out.println ( "the current balance: " + businessaccount.getbalance() ) ; case 3: double dblunits; system.out.println("enter 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 account address?"); straddress = input.nextline(); system.out.println("address:" + firstaccount.getaddress());
there no connection between user inputs , method calling, not sure how fix.
by using businessaccount.somemethod()
, you're attempting call said method in static context, methods not static. call upon methods, either need make them static or need create object can call upon them, ie:
businessaccount ba= new businessaccount (4, "name", "address", 3.4, 43.4); ba.somemethodfromclass();
in case, not want them static. read more on static methods/variables (see below).
some documentation may helpful:
http://docs.oracle.com/javase/tutorial/java/javaoo/classvars.html
http://docs.oracle.com/javase/7/docs/api/javax/swing/jmenu.html
to answer second question, need have method (in object's class, not main) can assign values variables. ie:
public void setaddress (string address) { straddress=address; }
then, you'd pass address read in main function. doing that, you're initializing/assigning value user value stored in class, ie:
system.out.println("please enter account address?"); string temp = input.nextline(); ba.setaddress(temp); system.out.println("address:" + ba.getaddress());
or better yet,
system.out.println("please enter account address?"); ba.setaddress(input.nextline()); system.out.println("address:" + ba.getaddress());
Comments
Post a Comment