|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
python to java
i have been trying to convert it but i cannot can do it can anyone help me thanks
import javax.swing.JOptionPane; public class define{ private int CURRENT = 1; private int DEPOSIT = 2; private int POUND = £; // ==== GLOBAL VARIABLES ======== // [ ] means list has no (0) elements on it initially. static String [ ] customers; static Boolean [ ] hasCurrent; static Boolean [ ]hasDeposit; static int [ ]currentAccountBalances;static int [ ]depositAccountBalances; public static void main(String [] args) { choice = displayMenu(); while (choice != 0) { // NB choice == 0 is "quit" if (choice == 1) addCustomer( ); else if (choice == 2) viewBalance(); else if (choice == 3) withdrawCash(); else if (choice == 4) depositFunds(); }//while choice = displayMenu(); System.out.println ("Bye thanks for using the bank..."); }//main() public static void displayMenu() { ( ... ) valid = False; while (valid = true) { System.out.println ("\n\n******* MAIN MENU ****************\n"); System.out.println ("Please select one of the following options:"); System.out.println ("\t 1: create new account"); System.out.println ("\t 2: view a Customers Balance"); System.out.println ("\t 3: withdraw cash from a customers account"); System.out.println ("\t 4: deposit funds into a customers account"); System.out.println ("\t 0: quit...."); choice = input(" \t>>> " ); valid = check(choice); } return choice; }//display menu () public void addCustomer() { name = raw_input("what is the customers name? >>> "); System.out.println ("what kind of account do you want to create:"); System.out.println ("\t 1: CURRENT"); System.out.println ("\t 2: DEPOSIT"); System.out.println (" \t>>>"); type = input( ); while ((type != CURRENT) and (type != DEPOSIT)) { System.out.println (" Sorry, input not recognised. Plese select 1 or 2:"); System.out.println ("\t 1: current"); System.out.println ("\t 2: deposit"); type = input(" \t>>> " ); }//while updateCustomer(name, type); }//add customer() public void viewBalance() { //remember len(...) is a function which returns the length of a list // its Java equivalent is <arrayName>.length if (customers.length>0) //# ie we have customers { displayCustomers(); Boolean active = False; String account = ""; custId = getCustId(); name = customers[custId]; accountType = getAccount( ); if (accountType () == CURRENT) { balance = currentAccountBalances[custId]; account = "current"; active = hasCurrent[custId]; }//if else { balance = depositAccountBalances[custId]; account = "deposit"; active = hasDeposit[custId]; } if (active) { System.out.println("Balance on"+ name+ "'s"+ account +"account is:" +balance); } else System.out.println ("sorry that account has not been set up"); } else System.out.println ("No Customers available"); }//view balance() public static void withdrawCash() { if (customers.length> 0); { displayCustomers(); custId = getCustId(); name = customers[custId]; accountType = getAccount( ); if ((accountType == CURRENT)and(hasCurrent[custId])) withdraw(currentAccountBalances + custId + accountType); else if ((accountType == DEPOSIT) and (hasDeposit[custId])) withdraw(depositAccountBalances, custId, accountType); else if (System.out.println ("sorry! that account has not been set up")); else System.out.println ("No Customers available..."); } }//withdraw cash() public static void depositFunds() { { if (customers.length> 0) displayCustomers(); custId = getCustId(); name = customers[custId]; accountType = getAccount( ); if ((accountType) == (CURRENT) and (hasCurrent[custId])) deposit(currentAccountBalances, custId, accountType); else if ((accountType == DEPOSIT) and (hasDeposit[custId])) desposit(depositAccountBalances, custId, accountType); else System.out.println ("sorry! that account has not been set up"); else System.out.println ("No Customers available..."); } }//deposit funds() public static void check(){ // might not need this function in Java if showOptionDialog is used bValue = False; if (choice in range(0, 5)) bValue = True; return bValue; }//check() public static void updateCustomer(custName,accountType){ // This condition might take a bit of thought to deal with in Java. Hint: what does isMember( ) do?? : if (custName in customers) custId = customers.index(custName); currValue = hasCurrent[custId]; depValue = hasDeposit[custId]; create = True; if (currValue) { System.out.println custName, ("already has a current account."); }//currValue() if (accountType) == CURRENT System.out.println ("Sorry, you can't create another current account with us"); create = False; if (depValue) { System.out.println custName, ("already has a deposit account with us"); }//depvalue() if (accountType) == DEPOSIT System.out.println ("Sorry, you can't create another deposit account with us"); create = False; if (create) { createAnotherAccount(custId, accountType); }//create () else() createNewAccount(custName, accountType); }// update customer() public static void displayCustomers(){ length = len(customers); System.out.println ("CUST ID\tCUST NAME"); System.out.println ("=======\t========="); for count in range(0, length); System.out.println (" " + str(count) + "\t ", customers[count]); }//display customer() public static void getCustId(){ tempId = input("Please enter custId >>> "); length = len(customers); while (tempId < 0 or tempId > length) tempId = input(" invalid data: Please enter custId in range 0 to " + str(length)); return tempId }//get custID() public static void getAccount( ){ tempAcc = input("Please select \n1. CURRENT or \n2. DEPOSIT \n>>>"); while (tempAcc != CURRENT) and (tempAcc != DEPOSIT) tempAcc = input("Invalid selection. Please select \n1. CURRENT or \n2. DEPOSIT \n>>>"); return tempAcc; }// get account public static void deposit(accountBalanceArray, id, type){ deposit = input("How much money does the customer want to deposit (0 to quit)? >>> " + POUND); while (deposit < 0) System.out.println ("use the withdraw function to take money from an account..."); deposit = input("How much money does the customer want to deposit (0 to quit)? >>> "+ POUND); if (deposit >= 0) accountBalanceArray[id] += deposit; account = ""; name = customers[id]; if (type == CURRENT) account = "current"; else () account = "deposit"; System.out.println ("New balance on" name+"'s", account,"account is: "+ POUND + str(accountBalanceArray[id])); }//deposit public static void withdraw(accountBalanceArray, id, type){ withdrawal = input("How much money does the customer want to take out (0 to quit)? >>> "+ POUND); { while (accountBalanceArray[id] - withdrawal) < 0 or withdrawal < 0 if (withdrawal < 0) System.out.println ("use the deposit function to add money to an account..."); else System.out.println ("Sorry. That amount would put the customer into the red."); System.out.println ("The max amount the customer can withdraw is " + POUND + str(accountBalanceArray[id])); withdrawal = input("How much money do they want to take out (0 to quit)? >>> " + POUND ); if (withdrawal >= 0) accountBalanceArray[id] -= withdrawal; account = ""; name = customers[id]; if (type == CURRENT) account = "current"; else() account = "deposit"; System.out.println ("New balance on", name+"'s", account,"account is: " + POUND + str(accountBalanceArray[id])); } public static void createNewAccount(custName, accountType){ account = ""; // hmm.. append is a built in Python program. We'll have to use our own append function in Java: customers.append(custName) { if (accountType == CURRENT) hasCurrent.append(True); hasDeposit.append(False); account = "current"; } { else if (accountType == DEPOSIT) hasCurrent.append(False); hasDeposit.append(True); account = "deposit"; currentAccountBalances.append(0); depositAccountBalances.append(0); print custName + "'s", account, "account set up..."; } }//create new account() public static void createAnotherAccount(custId, accountType) { account = ""; { if (accountType == CURRENT) hasCurrent[custId] = True; account = "current"; } { else if (accountType == DEPOSIT) hasDeposit[custId] = True; account = "deposit"; System.out.println (customers[custId] + "'s", account, "account set up..."); } } } |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > python to java |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|