/home/sjenks/NetBeansProjects/CS1/src/arrayplay/NumberList.java |
1
2
3
4 package arrayplay;
5
6 import java.io.File;
7 import java.io.FileNotFoundException;
8 import java.io.PrintWriter;
9 import java.util.Scanner;
10 import java.util.logging.Level;
11 import java.util.logging.Logger;
12
13
14
15 @author
16
17 public class NumberList {
18
19 private static final int LIMIT = 1000;
20 private static int[] numbers = new int[LIMIT];
21 private static int numberOfNumbers = 0;
22 private static Scanner commandReader = new Scanner(System.in);
23
24
25 @param args
26
27
28 public static void main(String[] args) {
29 try{
30
31 readNumbers();
32
33 System.out.println("/nThe original list of numbers...");
34 displayNumbers();
35
36 interpreter();
37 } catch (FileNotFoundException ex){
38 System.out.println ("The file was not found. Please think again.");
39 System.exit(-1);
40 }
41 }
42
43
44 private static Scanner establishScanner (String fn) throws FileNotFoundException{
45 String separator = System.getProperty("file.separator");
46 String homeDirectory = System.getProperty("user.home");
47 String path = homeDirectory + separator + "CS1Files" + separator + "data" + separator;
48 String fullFileName = path + fn;
49 return new Scanner (new File (fullFileName));
50 }
51
52 private static void readNumbers() throws FileNotFoundException {
53 Scanner scanner = establishScanner ("NumberSet.text");
54 while ( scanner.hasNext()){
55 numbers [numberOfNumbers] = scanner.nextInt();
56 numberOfNumbers = numberOfNumbers +1;
57
58 }
59
60 }
61
62 private static void displayNumbers() {
63 for ( int x = 0; x< numberOfNumbers; x= x+1){
64 System.out.println (numbers[x]);
65 }
66
67 }
68
69 private static void interpreter() {
70 System.out.print(">>>");
71 String command = commandReader.next();
72 if ( command.equalsIgnoreCase("DISPLAY")){
73 interpreterDisplayCmmand();
74 }else if ( command.equalsIgnoreCase("PRINT")){
75 interpreterPrintCommand();
76 }else if ( command.equalsIgnoreCase("SWAP")){
77 interpretSwapCommand();
78 }else if ( command.equalsIgnoreCase("ADD")){
79 interpretAddCommand();
80 }else if ( command.equalsIgnoreCase("HELP")){
81 interpretHelpCommand();
82 }else if ( command.equalsIgnoreCase("SWAP")){
83 interpretSwapCommand();
84 }else if ( command.equalsIgnoreCase("EXIT")){
85 System.exit(0);
86 }else{
87 System.out.println("### Unrecognizable command: " + command);
88 }
89 interpreter();
90 }
91
92 private static void interpreterDisplayCmmand() {
93 displayNumbers();
94 }
95
96 private static void interpreterPrintCommand() {
97 String operand = commandReader.next();
98 if ( operand.equalsIgnoreCase("FIRST")){
99 System.out.println(numbers[0]);
100 }else if (operand.equalsIgnoreCase("LAST")){
101 System.out.println(numbers[numberOfNumbers - 1]);
102 }else {
103 int index = Integer.valueOf(operand);
104 System.out.println(numbers[index -1]);
105 }
106 }
107
108 private static void interpretSwapCommand() {
109 int position1 = commandReader.nextInt();
110 int position2 = commandReader.nextInt();
111 int temp = numbers[position1 - 1];
112 numbers[position1-1] = numbers[position2-1];
113 numbers[position2-1] = temp;
114 }
115
116 private static void interpretAddCommand() {
117 String position = commandReader.next();
118 if (position.equalsIgnoreCase("LAST")) {
119 addLast();
120 } else if (position.equalsIgnoreCase("FIRST")) {
121 addFirst();
122 } else {
123 System.out.println("### invalid operand for add command");
124 }
125 numberOfNumbers = numberOfNumbers + 1;
126 }
127
128 private static void interpretHelpCommand() {
129 System.out.println("HELP - display a menu of commands");
130 System.out.println("DISPLAY - display the list of numbers");
131 System.out.println("PRINT - display a number(FIRST; LAST; nth)");
132 System.out.println("SWAP - exchange two elements (nth; mth)");
133 System.out.println("ADD - ad a number to the list (FIRST;LAST)");
134 System.out.println("EXIT - terminate execution of the program");
135 }
136
137 private static void addLast() {
138 numbers [numberOfNumbers] = commandReader.nextInt();
139
140 }
141
142 private static void addFirst() {
143 for ( int x = numberOfNumbers; x>0; x= x-1){
144 numbers[x] = numbers[x-1];
145 }
146 numbers[0] = commandReader.nextInt();
147 }
148
149 }
150