+ 2
Create a menu need help
9 Antworten
+ 10
I think you've messed it up by creating extra classes which are not necessary to solve this problem. Menu / Submenu classes are making it complicated. If you're trying to show a menu with deposit / withdraw options, then you may follow these steps:
1. Create a class named Account (it will contain methods for deposit and withdraw)
2. In main method, ask user for choice. (say you can show your options like - deposit / withdraw / show current balance and so on.
You may use switch to call the appropriate method by creating object of Account.
Try it out and feel free to ask if you get stuck. I may respond tomorrow, it's good night for me now! 🌙 😊
+ 4
For starters, you're calling Account.getBalance(), but getBalance() isn't a static method. If you want to use it in this fashion make the method static otherwise you'll need to create an instance of the Account class. The same goes for the withdraw() method.
+ 2
Can you provide a better detailed description of what you want your code to do. As of right now balance starts at 0 and will only go negative, the menu doesn't describe what your choices are etc. How do you want it to look? Typically a descriptive menu is displayed and the user will give an input which will then go to another menu or perform some action and return to the main menu etc. If you can write out some pseudo code that may also help.
+ 2
I'm not really sure what you're asking. Are you wanting your menu to work or are you trying to figure out how to pass objects? You can pass an object just like any other variable. It can be passed as an argument in a method and/or returned from a method. Note that this just passes the value of the object at the time it is passed and makes a copy of it. See code below:
public class Obj {
public static void main(String[] args) {
A a = new A();
B b = new B();
b.inBsetC(a.inAgetC());
C c = b.inBgetC();
System.out.println(c.getName());
System.out.println(c.getAge());
}
}
class A {
private C c = new C("SoloLearn", 42);
public C inAgetC() {
return c;
}
}
class B {
private C c = null;
void inBsetC(C c) {
this.c = new C(c.getName(), c.getAge());
}
C inBgetC() {
if(c != null)
return c;
else
return new C("", 0);
}
}
class C {
private String name;
private int age;
C(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
FYI: pseudocode is generally a written description of what the code will do in a line by line set of statements using little to no actual written code and can often be used to write a program in any language. What you have is actual code.
I.E.
get user input for bank menu choice
if choice is A then do X
else if choice is B then do Y
...
etc
+ 2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String choice = "y";
while(choice.equals("y")) {
Menu.display();
Menu.decide(Integer.parseInt(sc.nextLine()));
System.out.print("Would you like to make another transaction (Y/N)? ");
choice = sc.nextLine().trim().toLowerCase();
}
System.out.print("Thank you. Have a nice day.");
}
}
class Menu {
static void display() {
System.out.println("Please enter your selection:");
System.out.println("1. Check Balance");
System.out.println("2. Deposit");
System.out.println("3. Withdraw");
System.out.print("Enter your choice (1/2/3): ");
}
static void decide(int choice) {
int amount;
switch (choice) {
case 1:
System.out.println("Your current balance is: " + Account.getBalance());
break;
case 2:
amount = DepositMenu.display();
Account.deposit(amount);
break;
case 3:
amount = WithdrawMenu.display();
Account.withdraw(amount);
break;
}
}
}
class DepositMenu {
static int display() {
System.out.print("How much would you like to deposit? ");
return new Scanner(System.in).nextInt();
}
}
class WithdrawMenu {
static int display() {
System.out.println("Your current balance is: " + Account.getBalance());
System.out.print("How much would you like to withdraw? ");
return new Scanner(System.in).nextInt();
}
}
class Account {
static private int balance ;
static void deposit(int i) {
balance += i;
}
static void withdraw(int i){
balance -= i;
}
static int getBalance(){
return balance ;
}
}
+ 1
can you rewrite my code please
+ 1
Hmm but I need this format for another programm. do you have a solution for the code like it is with the clases?
+ 1
that thing I wrote is the pseudocode
+ 1
I'm searching for: how can I get acces to an created object from one class a to an other class b. if the object is created in class a