Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Can someone help me convert the following java code which is in a GUI format into a text based interface?

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can someone help me convert the following java code which is in a GUI format into a text based interface?

    Can someone help me convert the following java code which is in a GUI format into a text based interface?

    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.io.IOException;
    import java.util.Scanner;

    public class BankAccount {
    private double balance;
    public BankAccount(){
    double initialBalance = 0;
    }
    public BankAccount (double initialBalance){
    balance = initialBalance;
    }
    public void deposit(double amount){
    balance = balance + amount;
    }
    public void withdraw(double amount){
    balance = balance - amount;
    }
    public double getBalance(){
    return balance;
    }


    public class Customer {
    private int customerNumber;
    private int pin;
    private BankAccount checkingAccount;
    private BankAccount savingsAccount;
    public Customer(int aNumber, int aPin)
    {
    customerNumber = aNumber;
    pin = aPin;
    checkingAccount = new BankAccount();
    savingsAccount = new BankAccount();
    }
    public boolean match(int aNumber, int aPin)
    {
    return customerNumber == aNumber && pin == aPin;
    }
    public BankAccount getCheckingAccount()
    {
    return checkingAccount;
    }
    public BankAccount getSavingsAccount()
    {
    return savingsAccount; } }



    public class Bank {
    private ArrayList<Customer> customers;
    public Bank()
    {
    customers = new ArrayList<Customer>();
    }
    public void readCustomers(String filename)
    throws IOException
    {
    Scanner in = new Scanner(new File(filename));
    while (in.hasNext())
    {
    int number = in.nextInt();
    int pin = in.nextInt();
    Customer h = new Customer(number, pin);
    addCustomer(h);
    }
    in.close();
    }
    public void addCustomer(Customer h)
    {
    customers.add(h);
    }
    public Customer findCustomer(int aNumber, int aPin)
    {
    for (Customer h : customers)
    { if (h.match(aNumber, aPin))
    return h;
    }
    return null; } }


    public class ATM {
    public static final int CHECKING_ACCOUNT = 1;
    public static final int SAVINGS_ACCOUNT = 2;
    private int state;
    private int customerNumber;
    private Customer currentCustomer;
    private BankAccount currentAccount;
    private Bank theBank;
    public static final int START = 1;
    public static final int PIN = 2;
    public static final int ACCOUNT = 3;
    public static final int TRANSACTION = 4;
    public ATM(Bank aBank){
    theBank = aBank;
    reset();
    }
    public void reset(){
    customerNumber = 1;
    currentAccount = null;
    state = START;
    } public void setCustomerNumber(int number){
    assert state == START;
    customerNumber = number;
    state = PIN;
    }
    public void selectCustomer(int pin){
    assert state == PIN;
    currentCustomer = theBank.findCustomer(customerNumber, pin);
    if(currentCustomer == null){
    state = START;
    }else{
    state = ACCOUNT;
    } }
    public void selectAnAccount(int account){
    assert state == ACCOUNT || state == TRANSACTION;
    if(account == CHECKING_ACCOUNT){
    currentAccount = currentCustomer.getCheckingAccount();
    }else{
    currentAccount = currentCustomer.getSavingsAccount();
    }
    state = TRANSACTION;
    }
    public void withdraw(double amount){
    assert state == TRANSACTION;
    currentAccount.withdraw(amount);
    }
    public void deposit(double amount){
    assert state == TRANSACTION;
    currentAccount.deposit(amount);
    }
    public void back(){
    if(state == TRANSACTION){
    state = ACCOUNT;
    }else if(state == ACCOUNT){
    state = PIN;
    }else if(state == PIN){
    state = START;
    } }
    public int getState(){
    return state;
    }
    public double getBalance(){
    assert state == TRANSACTION;
    return currentAccount.getBalance();
    } }


    //public static class ATMTester {
    public void main(String[] args) {
    ATM myATM;
    try {
    Bank xBank = new Bank();
    xBank.readCustomers("The_X_Bank.txt");
    myATM = new ATM(xBank);
    } catch (IOException e) {
    System.out.println("error in customer's file");
    return;
    }
    Scanner input = new Scanner(System.in);
    while (true) {
    int state = myATM.getState();
    if (state == ATM.START) {
    System.out.print("Enter customer number: ");
    int number = input.nextInt();
    myATM.setCustomerNumber(number);
    } else if (state == ATM.PIN) {
    int pin = input.nextInt();
    myATM.selectCustomer(pin);
    } else if (state == ATM.ACCOUNT) {
    System.out.print("A=Checking, B=Savings, C=Quit: ");
    String command = input.next();
    if (command.equalsIgnoreCase("A")) {
    myATM.selectAnAccount(ATM.CHECKING_ACCOUNT);
    } else if (command.equalsIgnoreCase("B")) {
    myATM.selectAnAccount(ATM.SAVINGS_ACCOUNT);
    } else if (command.equalsIgnoreCase("C")) {
    myATM.reset();
    } else {
    System.out.println("Illegal input!");
    } } else if (state == ATM.TRANSACTION) {

    System.out.println("Balance=" + myATM.getBalance());
    System.out.print("A=Deposit, B=Withdrawal, C=Cancel: ");
    String command = input.next();
    if (command.equalsIgnoreCase("A")) {
    System.out.print("Amount: ");
    double amount = input.nextDouble();
    myATM.deposit(amount);
    myATM.back();
    } else if (command.equalsIgnoreCase("B")) {
    System.out.print("Amount: ");
    double amount = input.nextDouble();
    myATM.withdraw(amount);
    myATM.back();
    } else if (command.equalsIgnoreCase("C")) {
    myATM.back();
    } else {
    System.out.println("illegal input");
    }
    }
    }
    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,447
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: Can someone help me convert the following java code which is in a GUI format into a text based interface?

    First of all, when posting code, please use the highlight tags to preserve formatting.

    Secondly, what part of this is giving you trouble? What have you tried? Where are you stuck?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. retrieving hashmap data in table format based key values
    By swapnareddy in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: April 16th, 2013, 08:48 AM
  2. Help with an object based level format
    By Gravity Games in forum Java Theory & Questions
    Replies: 41
    Last Post: August 3rd, 2012, 12:18 PM
  3. How Convert a Byte array to a image format
    By perlWhite in forum Algorithms & Recursion
    Replies: 7
    Last Post: February 19th, 2011, 02:16 PM
  4. Format some text with Java
    By vampire in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 18th, 2010, 10:30 AM
  5. Convert C/C++ code to Java
    By cristianll in forum What's Wrong With My Code?
    Replies: 13
    Last Post: November 14th, 2009, 01:43 AM