alexandra84

CustomerManagement

Nov 1st, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.28 KB | None | 0 0
  1. import com.sun.org.apache.bcel.internal.generic.Select;
  2.  
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class test {
  7.  
  8. public static void main(String[] args) {
  9.  
  10. customerManagement();
  11.  
  12. }
  13.  
  14. private static void customerManagement() {
  15. Scanner input = new Scanner(System.in);
  16. String customerName;
  17.  
  18.  
  19. String customerNewName="";
  20. String customerOldName;
  21. String oldAddress;
  22. String newAddress;
  23. String oldMoveInDate;
  24. String newMoveInDate;
  25. String oldSegment;
  26. String newSegment;
  27. int oldNoOfWaterMeters;
  28. int newNoOfWaterMeters;
  29. int oldZipcode;
  30. int newZipcode;
  31. String moveInDate;
  32. String zipcode;
  33. String address;
  34. String segment;
  35. String waterMeter;
  36. String moveOutDate;
  37. String customerID;
  38. boolean quit = false;
  39.  
  40.  
  41. do {
  42.  
  43. int menuItem; //This will save your menu choice
  44.  
  45.  
  46. System.out.println("/////////////////////////////////////////");
  47. System.out.println("Press '1' for: Adding new customer"); //Change these depending on the number of choices
  48. System.out.println("Press '2' for: Deleting customer"); //Change these depending on the number of choices
  49. System.out.println("Press '3' for: Update customer");
  50. System.out.println("Press '4' for: Returning to the main menu"); //TODO is not returning (alse case:4)
  51. System.out.print("Enter your choice here: ");
  52. menuItem = validateIntInput(); //Keep this to validate menu
  53. System.out.println("/////////////////////////////////////////");
  54. switch (menuItem) {
  55. case 1: //TODO testing sql + changing data type
  56.  
  57.  
  58. System.out.println("Enter the customer name:");
  59. customerName = input.next();
  60.  
  61. System.out.println("Enter the address:");
  62. input.nextLine();
  63. address = input.nextLine();
  64.  
  65.  
  66. System.out.println("Enter the zipcode:");
  67. zipcode = input.next();
  68.  
  69. System.out.println("Enter the move in date:");
  70. moveInDate = input.next();
  71.  
  72. System.out.println("Enter the move out date");
  73. moveOutDate = input.next();
  74.  
  75. System.out.println("Enter the segment:");
  76. segment = input.next();
  77.  
  78. System.out.println("Enter the number of water meters:");
  79. waterMeter = input.next();
  80.  
  81.  
  82. //Inserting new data into the customer table
  83.  
  84. DB.insertSQL("INSERT INTO tblCustomer (fldCustomerName, fldAddress, fldZipcode, fldMoveInDate, fldSegment, fldNumberOfWaterMeters, fldMoveOutDate) VALUES('"+customerName+"', '"+address+"', '"+zipcode+"', '"+moveInDate+"', '"+segment+"', '"+waterMeter+"', '"+moveOutDate+"')");
  85. System.out.println(customerName +" " + address +" " + zipcode+ " "+ moveInDate + " " + segment + " " + waterMeter + " " + moveOutDate);
  86. break;
  87.  
  88.  
  89. case 2:
  90.  
  91. DB.selectSQL("SELECT fldCustomerName FROM tblCustomer");
  92. do {
  93. String data = DB.getDisplayData();
  94. if (data.equals(DB.NOMOREDATA)) {
  95. break;
  96. } else
  97. System.out.print(data);
  98. }
  99. while (true);
  100.  
  101. System.out.println("You can enter the customer name here you want to delete from the system:");
  102. customerName = input.next();
  103.  
  104. //Delete data
  105. DB.deleteSQL("DELETE from tblCustomer where fldCustomerName='"+customerName+"';" );
  106. System.out.println(customerName + " is removed from the list");
  107.  
  108. break;
  109.  
  110. case 3:
  111.  
  112. System.out.println("You can update information about customers here:");
  113.  
  114. do {
  115.  
  116. System.out.println("/////////////////////////////////////////");
  117. System.out.print("Please choose which information would you like to update:");
  118. System.out.println();
  119. System.out.println("Press '1' for: Customer Name");
  120. System.out.println("Press '2' for: Address");
  121. System.out.println("Press '3' for: Zipcode");
  122. System.out.println("Press '4' for: Move in date");
  123. System.out.println("Press '5' for: Segment");
  124. System.out.println("Press '6' for: Number of Water meters");
  125. System.out.println("Press '7' for: Go back to main menu");
  126.  
  127.  
  128. menuItem = validateIntInput(); //Keep this to validate menu
  129. System.out.println("/////////////////////////////////////////");
  130.  
  131. switch (menuItem) {
  132. case 1:
  133. System.out.println("Please input the customer name what you want to change:");//Enter the code that needs to run if 1 was the option choosed.
  134. customerOldName = input.nextLine();
  135. System.out.println(customerOldName);
  136. DB.selectSQL("SELECT fldCustomerName, fldCustomerID FROM tblCustomer WHERE fldCustomerName='"+customerOldName+"';");//
  137. do {
  138. String data = DB.getDisplayData();
  139. if (data.equals(DB.NOMOREDATA)) {
  140. break;
  141. } else
  142. System.out.print(data);
  143. }
  144. while (true);
  145. System.out.println("Insert Customer ID");
  146. customerID = input.next();
  147. System.out.println("Insert New Name");
  148. input.nextLine();
  149. customerNewName = input.nextLine();
  150.  
  151. //Update data in Customer Name field
  152. if (customerNewName.length()>0) {
  153. DB.updateSQL("UPDATE tblCustomer SET fldCustomerName='" + customerNewName + "' WHERE fldCustomerID='" + customerID + "'");
  154. System.out.println("Customer number " + customerID + " has his name changed to " + customerNewName);
  155. }
  156. break;
  157.  
  158. case 2:
  159. System.out.println("Please input the old address which you want to change:");
  160. oldAddress = input.nextLine();
  161.  
  162. System.out.println(oldAddress);
  163. DB.selectSQL("SELECT fldAddress, fldCustomerID FROM tblCustomer WHERE fldAddress='"+oldAddress +"';");//
  164. do {
  165. String data = DB.getDisplayData();
  166. if (data.equals(DB.NOMOREDATA)) {
  167. break;
  168. } else
  169. System.out.print(data);
  170. }
  171. while (true);
  172.  
  173. System.out.println("Insert Customer ID");
  174. customerID = input.next();
  175.  
  176. System.out.println("Please input the new address:");
  177. input.nextLine();
  178. newAddress = input.nextLine();
  179.  
  180. //Update data in Address field
  181. DB.updateSQL("UPDATE tblCustomer SET fldAddress='" + newAddress + "' WHERE fldCustomerID='" + customerID + "'");
  182. System.out.println("Customer number " + customerID + " has his name changed to " + newAddress);
  183. break;
  184.  
  185. case 3://TODO does it make sense to update it?
  186. System.out.println("Please input the old zipcode which you want to change:");
  187. oldZipcode = input.nextInt();
  188.  
  189. System.out.println(oldZipcode);
  190. DB.selectSQL("SELECT fldZipcode, fldCustomerID FROM tblCustomer WHERE fldZipcode='"+oldZipcode +"';");//
  191. do {
  192. String data = DB.getDisplayData();
  193. if (data.equals(DB.NOMOREDATA)) {
  194. break;
  195. } else
  196. System.out.print(data);
  197. }
  198. while (true);
  199.  
  200. System.out.println("Insert Customer ID");
  201. customerID = input.next();
  202.  
  203. System.out.println("Please input the new zipcode:");
  204. input.nextLine();
  205. newZipcode = input.nextInt();
  206.  
  207. //Update data in Address field
  208. DB.updateSQL("UPDATE tblCustomer SET fldZipcode='" + newZipcode + "' WHERE fldCustomerID='" + customerID + "'");
  209. System.out.println("Customer number " + customerID + " has his name changed to " + newZipcode);
  210. break;
  211.  
  212.  
  213. case 4:
  214. System.out.println("Please input the old move in date which you want to change:");
  215. oldMoveInDate = input.next();
  216.  
  217. System.out.println(oldMoveInDate);
  218. DB.selectSQL("SELECT fldMoveInDate, fldCustomerID FROM tblCustomer WHERE fldMoveInDate='"+oldMoveInDate +"';");//
  219. do {
  220. String data = DB.getDisplayData();
  221. if (data.equals(DB.NOMOREDATA)) {
  222. break;
  223. } else
  224. System.out.print(data);
  225. }
  226. while (true);
  227.  
  228. System.out.println("Insert Customer ID");
  229. customerID = input.next();
  230.  
  231. System.out.println("Please input the new date of move in:");
  232. input.nextLine();
  233. newMoveInDate = input.next();
  234.  
  235. //Update data in Address field
  236. DB.updateSQL("UPDATE tblCustomer SET fldMoveInDate='" + newMoveInDate + "' WHERE fldCustomerID='" + customerID + "'");
  237. System.out.println("Customer number " + customerID + " has his name changed to " + newMoveInDate);
  238. System.out.println();
  239. break;
  240.  
  241.  
  242. case 5:
  243. System.out.println("Please input the old segment type which you want to change:");
  244. oldSegment = input.next();
  245.  
  246. System.out.println(oldSegment);
  247. DB.selectSQL("SELECT fldSegment, fldCustomerID FROM tblCustomer WHERE fldSegment='"+oldSegment +"';");//
  248. do {
  249. String data = DB.getDisplayData();
  250. if (data.equals(DB.NOMOREDATA)) {
  251. break;
  252. } else
  253. System.out.print(data);
  254. }
  255. while (true);
  256.  
  257. System.out.println("Insert Customer ID");
  258. customerID = input.next();
  259.  
  260. System.out.println("Please input the new segment type:");
  261. input.nextLine();
  262. newSegment = input.next();
  263.  
  264. //Update data in Address field
  265. DB.updateSQL("UPDATE tblCustomer SET fldSegment='" + newSegment + "' WHERE fldCustomerID='" + customerID + "'");
  266. System.out.println("Customer number " + customerID + " has his name changed to " + newSegment);
  267. System.out.println();
  268. break;
  269.  
  270. case 6:
  271. System.out.println("Please input the number of water meters which you want to change:");
  272. oldNoOfWaterMeters = input.nextInt();
  273.  
  274. System.out.println(oldNoOfWaterMeters);
  275. DB.selectSQL("SELECT fldNumberOfWaterMeters, fldCustomerID FROM tblCustomer WHERE fldNumberOfWaterMeters='"+oldNoOfWaterMeters +"';");//
  276. do {
  277. String data = DB.getDisplayData();
  278. if (data.equals(DB.NOMOREDATA)) {
  279. break;
  280. } else
  281. System.out.print(data);
  282. }
  283. while (true);
  284.  
  285. System.out.println("Insert Customer ID");
  286. customerID = input.next();
  287.  
  288. System.out.println("Please input the new number of water meters:");
  289. input.nextLine();
  290. newNoOfWaterMeters = input.nextInt();
  291.  
  292. //Update data in Address field
  293. DB.updateSQL("UPDATE tblCustomer SET fldNoOfWaterMeters='" + newNoOfWaterMeters + "' WHERE fldCustomerID='" + customerID + "'");
  294. System.out.println("Customer number " + customerID + " has his name changed to " + newNoOfWaterMeters);
  295. System.out.println();
  296. break;
  297.  
  298. case 7:
  299. quit = true; //This will make the loop stop after running the program - Used when user want to go back
  300. break;
  301. default:
  302. System.out.println("Invalid choice. Try again: ");
  303. }
  304.  
  305. } while (!quit);
  306. break;
  307.  
  308. case 4:
  309. quit = true; //This will make the loop stop after running the program - Used when user want to go back
  310. break;
  311. default:
  312. System.out.println("Invalid choice. Try again: ");
  313. }
  314. } while (!quit);
  315.  
  316.  
  317. }
  318.  
  319. private static int validateIntInput() {
  320. // VALIDATES IF THE INPUT IS AN INT
  321. Scanner input = new Scanner(System.in);
  322. while (true) {
  323. //System.out.println("1: Billing");
  324. //System.out.println("2: Economy:");
  325. //System.out.println("3: Statistics");
  326. //System.out.println("0. Quit");
  327. try {
  328. int a = input.nextInt();
  329. if (a == 0) {
  330. return 0;
  331. } else {
  332. return a;
  333. }
  334. } catch (java.util.InputMismatchException e) {
  335. System.out.println("You didn't enter a number - Try again: ");
  336. input.nextLine();
  337. }
  338. }
  339. }
  340. }
Advertisement
Add Comment
Please, Sign In to add comment