Advertisement
cynthiarez

CHAR / SWITCH (FIX AT HOME)

Sep 15th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. // PROBLEM: Somewhere after the last input. It does not go through the switch statements to output.
  2.  
  3. package Java;
  4. import java.io.FileReader;
  5. import java.io.PrintWriter;
  6. import java.io.FileNotFoundException;
  7. import java.util.Scanner;
  8.  
  9. import javax.swing.*;
  10.  
  11. public class lab3 {
  12.  
  13. public static void main(String[] args) throws FileNotFoundException {
  14.  
  15. PrintWriter balance = new PrintWriter ("cust.txt");
  16.  
  17. String name;
  18. char code;
  19. double oldbal;
  20. int amount;
  21.  
  22.  
  23. name = JOptionPane.showInputDialog("Please enter your name");
  24. amount = Integer.parseInt(JOptionPane.showInputDialog("Enter the amount"));
  25. oldbal = Double.parseDouble(JOptionPane.showInputDialog("Enter your old balance"));
  26. code = JOptionPane.showInputDialog("Please enter your transaction code").charAt(0);
  27.  
  28. balance.println(name + " " + code + " " + amount);
  29. balance.close();
  30.  
  31.  
  32. Scanner infile = new Scanner (new FileReader("cust.txt"));
  33. String fname;
  34. char fcode;
  35. int famount;
  36.  
  37. fname = infile.next();
  38. fcode = infile.next().charAt(0);
  39. famount = infile.nextInt();
  40.  
  41. infile.close();
  42.  
  43. switch (fcode) {
  44.  
  45. case 'D':
  46. double add = (famount+oldbal);
  47. JOptionPane.showMessageDialog(null, "full name: " + name + "\n Trx code: " + code + "\n Old balance: " + oldbal + "\n New Balance: " + add);
  48. break;
  49.  
  50. case 'W':
  51. double deduct = (oldbal-famount);
  52. JOptionPane.showMessageDialog(null, "full name: " + name + "\n Trx code: " + code + "\n Old balance: " + oldbal + "\n New Balance: " + deduct);
  53. break;
  54.  
  55. case 'C':
  56. double deposit = (famount+oldbal);
  57. JOptionPane.showMessageDialog(null, "full name: " + name + "\n Trx code: " + code + "\n Old balance: " + oldbal + "\n New Balance: " + deposit);
  58. break;
  59.  
  60. default:
  61. break;
  62. }
  63.  
  64. }
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement