MyListDriver.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import java.util.Scanner;
  2. public class MyListDriver {
  3. public static void main(String[] args) {
  4. Scanner scan = new Scanner(System.in);
  5. MyList intList = new MyList();
  6. System.out.println("Choose one of the following operations:");
  7. System.out.println("- add (enter the letter a)");
  8. System.out.println("- find (enter the letter f)");
  9. System.out.println("- print (enter the letter p)");
  10. System.out.println("- sum (enter the letter s)");
  11. System.out.println("- quit (enter the letter q)");
  12. String temp = scan.nextLine();
  13. while (temp.length() != 1) {
  14. System.out.println("Invalid choice");
  15. System.out.println("Do another operation? (refer to menu above)");
  16. temp = scan.nextLine();
  17. }
  18. char answer = temp.charAt(0);
  19. while (answer != 'q'){
  20. switch (answer){
  21. case 'a':
  22. System.out.println("What int do you want to add?");
  23. if(scan.hasNextInt()){
  24. int item = scan.nextInt();
  25. intList.add(item);
  26. System.out.println(item+" added");
  27. }
  28. else System.out.println("Invalid value");
  29. scan.nextLine();
  30. break;
  31. case 'f':
  32. System.out.println("What int do you want to search for?");
  33. if(scan.hasNextInt()){
  34. int item = scan.nextInt();
  35. if (intList.find(item)) System.out.println(item+" found");
  36. else System.out.println(item+" not found");
  37. }
  38. else System.out.println("Invalid value");
  39. scan.nextLine();
  40. break;
  41. case 'p':
  42. intList.print();
  43. System.out.println();
  44. break;
  45. case 's':
  46. System.out.println("Sum of all values is " + intList.sum());
  47. break;
  48. default:
  49. System.out.println("Invalid case");
  50. break;
  51. }
  52. System.out.println("Do another operation? (refer to menu above)");
  53. temp = scan.nextLine();
  54. while (temp.length() != 1) {
  55. System.out.println("Invalid choice");
  56. System.out.println("Do another operation? (refer to menu above)");
  57. temp = scan.nextLine();
  58. }
  59. answer = temp.charAt(0);
  60. }
  61. scan.close();
  62. System.out.println("quitting");
  63. System.out.println();
  64. }
  65. }