Click here to Skip to main content
15,911,139 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
package assingment3;

import java.text.NumberFormat;
import java.util.Scanner;



public class Assingment3 {

  static  NumberFormat fmt1 = NumberFormat.getCurrencyInstance();
    public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
     final double basic = 0.025;
     final double premium = 3.5;
     final double platinum = 4.5;
      System.out.println("Welcome to CSE110 Bank:");
      System.out.println("What is your name?:");
      String name = in.next();
      System.out.println("Initial deposit into savings:");
      double savings=in.nextDouble();
      System.out.println("Initial deposit into checkings:");      
      double checking=in.nextDouble();
      double total = savings+checking;
      if (total>1000)
      {
      System.out.println(name+"'s "+"basic account balance: "+fmt1.format(total)+"(S:"+fmt1.format(savings)+"C:"+fmt1.format(checking)+")" );
      }
      else if(total>=5000)
      {
       System.out.println(name+"'s "+"premium account balance: "+fmt1.format(total)+"(S:"+fmt1.format(savings)+"C:"+fmt1.format(checking)+")" );   
      }
      else if(total<5000)
              {
              System.out.println(name+"'s "+" account balance: "+fmt1.format(total)+"(S:"+fmt1.format(savings)+"C:"+fmt1.format(checking)+")" );    
              }
      System.out.println("Bank options:");
      System.out.println("1.Deposit Money"+"\n2.Withdraw Money"+"\n3.Transfer Money"+"\n4.Quit");
      int choice1 = in.nextInt();
      System.out.println("your choice:  "+choice1);
    
      switch(choice1)
              {
          case 1:
              System.out.println("deposit money options:"+"\n1.Deposit to checking"+"\n2. deposit to savings"+"\n3.cancel");
              int deposit = in.nextInt();
              System.out.println("Your choice"+deposit);
              if (deposit==1)
              {
                 
                  System.out.println("Enter amount to deposit:");
                  double deposit2 = in.nextInt();
                  double total1=total+deposit2;
                  System.out.println("You depositited "+fmt1.format(deposit2)+" to checking");
                 System.out.println("after this transaction the balance is "+(fmt1.format(total1))+"(S"+savings+"C:"+fmt1.format(checking)+")");
                 if(total1>1000)
                 {
                 double interest=total1*.025;
                     System.out.println("We have added interest to your account!"+interest+"(S"+savings+"C:"+checking);
                  break;
                 }
              }
              else if (deposit==2)
              {
                System.out.println("Enter amount to deposit"); 
                int deposit3 = in.nextInt();
                System.out.println("You deposited  "+fmt1.format(deposit3)+" to savings");
                break;
              }
              else if(deposit==3)
              {
                  System.out.println("canceling");
              }
              
              break;
          case 2:
              System.out.println("Withdraw Money options:");
              System.out.println("1.withdraw from checking"+"\n2.withdraw from savings"+""+"\n3.cancel");
              int withdraw = in.nextInt();
              if (withdraw==1)
              {
                  System.out.println("enter amount to withdraw from checking:");
                  int withdraw1= in.nextInt();
                  System.out.println("you withdraw "+fmt1.format(withdraw1)+" from checking");
                  break;
              }
              else if(withdraw==1)
              {
                  System.out.println("enter amount to withdraw from savings:");
                  int withdraw2= in.nextInt();
                  System.out.println("you withdraw "+fmt1.format(withdraw2)+" from savings");
                  break;
              }
              else if(withdraw==3)
              {
                   System.out.println("canceling");
              }
              break;
          case 3:
              System.out.println("Transfer money options");
              System.out.println("1.transfer from savings"+"\n2.transfer from checking"+"\n3.cancel");
              int transfer = in.nextInt();
              System.out.println("Your choice"+transfer);
              if (transfer ==1)
              {
                  System.out.println("enter amount to transfer from savings:");
                  int transfer1= in.nextInt();
                  System.out.println("you transfer "+fmt1.format(transfer1)+" from savings");
                  break;
                  
              }
              else if(transfer==2)
              {
                 System.out.println("enter amount to transfer from checking:");
                  int transfer2= in.nextInt();
                  System.out.println("you transfer "+fmt1.format(transfer2)+" from checking");
                  break;
              }
              else if(transfer==3)
              {
                  System.out.println("cancelling");
              }
              break;
              
                  
              }                            
      }    
    }
Posted
Updated 28-Sep-12 20:24pm
v2

As long as the logics of each input differ, you have to use multiple options.

You can reduce the code if the final usage of the input is same. You can generalize the implementation. Get your input and just based on the condition, set the necessary values and then move on to common code to use the set value.
 
Share this answer
 
One thing that would improve this quite dramatically would be to separate out each activity into a self-contained method. You could then get a clearer picture of any common code which could be split out further into helper methods. Something like:
Java
public static void withdrawal(int account type, int transaction, int amount) {
    // process withdrawal transactions
}

public static void deposit(int account type, int transaction, int amount) {
    // process deposit transactions
}

public static void main(String[] args) {
  // get transaction type
  System.out.println("1.Deposit Money"+"\n2.Withdraw Money"+"\n3.Transfer Money"+"\n4.Quit");
  int choice1 = in.nextInt();

  // get account
  System.out.println("1.Checking"+"\n2.Savings");
  int choice2 = in.nextInt();

  // get amount of money
  System.out.println("Enter amount");
  int amount = in.nextInt();

  // other inputs ...

  // call the method that deals with the transaction type
  switch(choice1)
      {
      case 1:
          deposit(choice2, choice3, amount);
          break;
      ...
      }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900