Using Java, How can I change the following code to be a GUI. import java.text.Nu
ID: 3833828 • Letter: U
Question
Using Java, How can I change the following code to be a GUI.
import java.text.NumberFormat; // Helps with formatting doubles as currency
import java.util.Scanner; // Will be used to get input from the user
public class SavingsAccount {
public static void main(String[] args) {
// Create and instantiate two Account objects
Account checking = new Account();
checking.setType("Checking");
checking.setBalance(4000.00);
checking.setRate(4000.00);
Account savings = new Account();
savings.setType("Savings");
savings.setBalance(4000.00);
savings.setRate(4000.00);
NumberFormat formatter = NumberFormat.getCurrencyInstance(); // Creates the formatter object for currency
Scanner sc = new Scanner(System.in); // Creates the sc object to read user input
boolean session = true; // This variable will break the (while) loop when false
while (session) {
// Present the user with a menu of 5 options
System.out.print("======================== "
+"ATM Menu: "
+ "1. Deposit Money "
+ "2. Withdraw Money "
+ "3. Transfer Funds "
+ "4. Check Account Balance "
+ "5. End Session "
+ "======================== "
+ " Enter selection: ");
int selection = sc.nextInt(); // assign the user's input to the selection variable
// This switch block will handle one of five selections and deal with them appropriately
switch (selection) {
// case 1 handles the depositing of money
case 1:
System.out.print("Enter (1) for Savings or (2) for Checking: ");
int depAccount = sc.nextInt(); // Keeps track of which account to deposit money to
if (depAccount == 1) {
System.out.println(" Your current Savings balance is: " + formatter.format(savings.getBalance()) + " ");
System.out.println("How much money would you like to deposit?");
double deposit = sc.nextDouble();
savings.deposit(deposit);
System.out.println(" Your Savings balance is now: " + formatter.format(savings.getBalance()) + " ");
}
else if (depAccount == 2) {
System.out.println(" Your current Checking balance is: " + formatter.format(checking.getBalance()) + " ");
System.out.println("How much money would you like to deposit?");
double deposit = sc.nextDouble();
checking.deposit(deposit);
System.out.println(" Checking balance is now: " + formatter.format(checking.getBalance()) + " ");
}
break;
// case 2 handles the withdrawal of money
case 2:
System.out.print(" Enter (1) for Savings or (2) for Checking: ");
int witAccount = sc.nextInt(); // Keeps track of which account to withdraw from
if (witAccount == 1) {
System.out.println(" Your current Savings balance is: " + formatter.format(savings.getBalance()) + " ");
System.out.println("How much money would you like to withdraw?");
double withdraw = sc.nextDouble();
savings.withdraw(withdraw);
System.out.println(" Your Savings balance is now: " + formatter.format(savings.getBalance()) + " ");
}
else if (witAccount == 2) {
System.out.println(" Your current Checking balance is: " + formatter.format(checking.getBalance()) + " ");
System.out.println("How much money would you like to withdraw?");
double withdraw = sc.nextDouble();
checking.withdraw(withdraw);
System.out.println(" Your Checking balance is now: " + formatter.format(checking.getBalance()) + " ");
}
break;
// case 3 handles the transfer of funds
case 3:
System.out.print(" From which account do you wish to transfer funds from?, (1) for Savings or (2) for Checking: ");
int tranAccount = sc.nextInt();
if (tranAccount == 1) {
System.out.println(" Your current Savings balance is: " + formatter.format(savings.getBalance()) + " ");
System.out.print("How much money do you wish to transfer from Savings to Checking?: ");
double tranAmount = sc.nextDouble();
savings.withdraw(tranAmount);
checking.deposit(tranAmount);
System.out.println(" You successfully transferred " + formatter.format(tranAmount) + " from Savings to Checking");
System.out.println(" Checking Balance: " + formatter.format(checking.getBalance()));
System.out.println("Savings Balance: " + formatter.format(savings.getBalance()) + " ");
}
else if (tranAccount == 2) {
System.out.println(" Your current Checking balance is: " + formatter.format(checking.getBalance()) + " ");
System.out.print("How much money do you wish to transfer from Checking to Saving?: ");
double tranAmount = sc.nextDouble();
checking.withdraw(tranAmount);
savings.deposit(tranAmount);
System.out.println(" You successfully transferred " + formatter.format(tranAmount) + " from Checking to Savings");
System.out.println(" Checking Balance: " + formatter.format(checking.getBalance()));
System.out.println("Savings Balance: " + formatter.format(savings.getBalance()) + " ");
}
break;
// case 4 simply outputs the balances of both Checking and Savings accounts
case 4:
System.out.println(" Checking Balance: " + formatter.format(checking.getBalance()));
System.out.println("Savings Balance: " + formatter.format(savings.getBalance()) + " ");
break;
// case 5 breaks out of the (while) loop when the user is finished using the ATM
case 5:
session = false;
break;
}
}
System.out.println(" Thank you for banking with us! ");
}
}
class Account {
// Here we declare some variables that a typical bank account will have
String type;
double balance;
double rate;
// The following methods are a combination of getter/setter methods as well
// as two special deposit() and withdraw() methods
void setType(String accType) {
type = accType;
}
void setBalance(double accBal) {
balance = accBal;
}
void setRate(double accRate) {
rate = accRate;
}
void deposit(double dep) {
balance += dep; // Take the Account object's balance and add to it the current deposit
}
void withdraw(double wit) {
balance -= wit; // Take the Account object's balance and subtract from it the current withdrawal
}
String getType() {
return type;
}
double getBalance() {
return balance;
}
double getRate() {
return rate;
}
}
Explanation / Answer
//Woring on it
//Updating soon
//It is time taking
import java.awt.Color;
import java.awt.Font;
import java.text.NumberFormat; // Helps with formatting doubles as currency
import java.util.Scanner; // Will be used to get input from the user
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class SavingAccount {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JLabel lable = new JLabel("Enter Your Choice");
JTextArea ta = new JTextArea();
JTextArea taResult = new JTextArea();
JTextField choice = new JTextField();
JButton btn = new JButton("Sumbit");
frame.setVisible(true);
frame.setSize(500, 520);
frame.setTitle("Account Project");
frame.add(panel);
frame.setLayout(null);
panel.setBounds(0, 0, 500, 520);
panel.add(ta);
panel.add(choice);
panel.add(taResult);
panel.add(lable);
panel.add(btn);
panel.setLayout(null);
ta.setBounds(100, 50, 300, 250);
ta.setBackground(Color.LIGHT_GRAY);
String menu = "======================== "
+"ATM Menu: "
+ "1. Deposit Money "
+ "2. Withdraw Money "
+ "3. Transfer Funds "
+ "4. Check Account Balance "
+ "5. End Session "
+ "======================== "
+ " Enter selection: ";
ta.setText(menu);
ta.setFont(new Font("",4,16));
taResult.setBounds(100, 400, 310, 300);
taResult.setBackground(Color.LIGHT_GRAY);
taResult.setFont(new Font("",4,16));
lable.setBounds(50, 300, 150, 30);
lable.setFont(new Font("",4,16));
choice.setBounds(180, 300, 150, 30);
choice.setFont(new Font("",4,16));
btn.setBounds(350, 300, 100, 30);
btn.setFont(new Font("",4,16));
// Create and instantiate two Account objects
Account checking = new Account();
checking.setType("Checking");
checking.setBalance(4000.00);
checking.setRate(4000.00);
Account savings = new Account();
savings.setType("Savings");
savings.setBalance(4000.00);
savings.setRate(4000.00);
NumberFormat formatter = NumberFormat.getCurrencyInstance(); // Creates the formatter object for currency
Scanner sc = new Scanner(System.in); // Creates the sc object to read user input
boolean session = true; // This variable will break the (while) loop when false
while (session) {
// Present the user with a menu of 5 options
System.out.print("======================== "
+"ATM Menu: "
+ "1. Deposit Money "
+ "2. Withdraw Money "
+ "3. Transfer Funds "
+ "4. Check Account Balance "
+ "5. End Session "
+ "======================== "
+ " Enter selection: ");
ta.setText(menu);
int selection = sc.nextInt(); // assign the user's input to the selection variable
// This switch block will handle one of five selections and deal with them appropriately
switch (selection) {
// case 1 handles the depositing of money
case 1:
System.out.print("Enter (1) for Savings or (2) for Checking: ");
int depAccount = sc.nextInt(); // Keeps track of which account to deposit money to
if (depAccount == 1) {
System.out.println(" Your current Savings balance is: " + formatter.format(savings.getBalance()) + " ");
System.out.println("How much money would you like to deposit?");
double deposit = sc.nextDouble();
savings.deposit(deposit);
System.out.println(" Your Savings balance is now: " + formatter.format(savings.getBalance()) + " ");
}
else if (depAccount == 2) {
System.out.println(" Your current Checking balance is: " + formatter.format(checking.getBalance()) + " ");
System.out.println("How much money would you like to deposit?");
double deposit = sc.nextDouble();
checking.deposit(deposit);
System.out.println(" Checking balance is now: " + formatter.format(checking.getBalance()) + " ");
}
break;
// case 2 handles the withdrawal of money
case 2:
System.out.print(" Enter (1) for Savings or (2) for Checking: ");
int witAccount = sc.nextInt(); // Keeps track of which account to withdraw from
if (witAccount == 1) {
System.out.println(" Your current Savings balance is: " + formatter.format(savings.getBalance()) + " ");
System.out.println("How much money would you like to withdraw?");
double withdraw = sc.nextDouble();
savings.withdraw(withdraw);
System.out.println(" Your Savings balance is now: " + formatter.format(savings.getBalance()) + " ");
}
else if (witAccount == 2) {
System.out.println(" Your current Checking balance is: " + formatter.format(checking.getBalance()) + " ");
System.out.println("How much money would you like to withdraw?");
double withdraw = sc.nextDouble();
checking.withdraw(withdraw);
System.out.println(" Your Checking balance is now: " + formatter.format(checking.getBalance()) + " ");
}
break;
// case 3 handles the transfer of funds
case 3:
System.out.print(" From which account do you wish to transfer funds from?, (1) for Savings or (2) for Checking: ");
int tranAccount = sc.nextInt();
if (tranAccount == 1) {
System.out.println(" Your current Savings balance is: " + formatter.format(savings.getBalance()) + " ");
System.out.print("How much money do you wish to transfer from Savings to Checking?: ");
double tranAmount = sc.nextDouble();
savings.withdraw(tranAmount);
checking.deposit(tranAmount);
System.out.println(" You successfully transferred " + formatter.format(tranAmount) + " from Savings to Checking");
System.out.println(" Checking Balance: " + formatter.format(checking.getBalance()));
System.out.println("Savings Balance: " + formatter.format(savings.getBalance()) + " ");
}
else if (tranAccount == 2) {
System.out.println(" Your current Checking balance is: " + formatter.format(checking.getBalance()) + " ");
System.out.print("How much money do you wish to transfer from Checking to Saving?: ");
double tranAmount = sc.nextDouble();
checking.withdraw(tranAmount);
savings.deposit(tranAmount);
System.out.println(" You successfully transferred " + formatter.format(tranAmount) + " from Checking to Savings");
System.out.println(" Checking Balance: " + formatter.format(checking.getBalance()));
System.out.println("Savings Balance: " + formatter.format(savings.getBalance()) + " ");
}
break;
// case 4 simply outputs the balances of both Checking and Savings accounts
case 4:
System.out.println(" Checking Balance: " + formatter.format(checking.getBalance()));
System.out.println("Savings Balance: " + formatter.format(savings.getBalance()) + " ");
break;
// case 5 breaks out of the (while) loop when the user is finished using the ATM
case 5:
session = false;
break;
}
}
System.out.println(" Thank you for banking with us! ");
}
}
class Account {
// Here we declare some variables that a typical bank account will have
String type;
double balance;
double rate;
// The following methods are a combination of getter/setter methods as well
// as two special deposit() and withdraw() methods
void setType(String accType) {
type = accType;
}
void setBalance(double accBal) {
balance = accBal;
}
void setRate(double accRate) {
rate = accRate;
}
void deposit(double dep) {
balance += dep; // Take the Account object's balance and add to it the current deposit
}
void withdraw(double wit) {
balance -= wit; // Take the Account object's balance and subtract from it the current withdrawal
}
String getType() {
return type;
}
double getBalance() {
return balance;
}
double getRate() {
return rate;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.