alexandra84

customman

Nov 2nd, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.23 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class test {
  5.  
  6. public static void main(String[] args) {
  7.  
  8. customerManagement();
  9.  
  10. }
  11.  
  12. private static void customerManagement() {
  13. Scanner input = new Scanner(System.in);
  14. String customerName;
  15.  
  16.  
  17. String customerNewName="";
  18. String customerOldName;
  19. String oldAddress;
  20. String newAddress;
  21. String oldMoveInDate;
  22. String newMoveInDate;
  23. String oldSegment;
  24. String newSegment;
  25. int oldNoOfWaterMeters;
  26. int newNoOfWaterMeters;
  27. int oldZipcode;
  28. int newZipcode;
  29. String moveInDate;
  30. String zipcode;
  31. String address;
  32. String segment;
  33. String waterMeter;
  34. String moveOutDate;
  35. String customerID;
  36. boolean quit = false;
  37.  
  38.  
  39. do {
  40.  
  41. int menuItem; //This will save your menu choice
  42.  
  43.  
  44. System.out.println("/////////////////////////////////////////");
  45. System.out.println("Press '1' for: Adding new customer"); //Change these depending on the number of choices
  46. System.out.println("Press '2' for: Deleting customer"); //Change these depending on the number of choices
  47. System.out.println("Press '3' for: Update customer");
  48. System.out.println("Press '4' for: Returning to the main menu"); //
  49. System.out.print("Enter your choice here: ");
  50. menuItem = validateIntInput(); //Keep this to validate menu
  51. System.out.println("/////////////////////////////////////////");
  52. switch (menuItem) {
  53. case 1: //
  54.  
  55.  
  56. System.out.println("Enter the customer name:");
  57. customerName = input.next();
  58.  
  59. System.out.println("Enter the address:");
  60. input.nextLine();
  61. address = input.nextLine();
  62.  
  63.  
  64. System.out.println("Enter the zipcode:");
  65. zipcode = input.next();
  66.  
  67. System.out.println("Enter the move in date:");
  68. moveInDate = input.next();
  69.  
  70. System.out.println("Enter the move out date");
  71. moveOutDate = input.next();
  72.  
  73. System.out.println("Enter the segment:");
  74. segment = input.next();
  75.  
  76. System.out.println("Enter the number of water meters:");
  77. waterMeter = input.next();
  78.  
  79.  
  80. //Inserting new data into the customer table
  81.  
  82. DB.insertSQL("INSERT INTO tblCustomer (fldCustomerName, fldAddress, fldZipcode, fldMoveInDate, fldSegment, fldNumberOfWaterMeters, fldMoveOutDate) VALUES('"+customerName+"', '"+address+"', '"+zipcode+"', '"+moveInDate+"', '"+segment+"', '"+waterMeter+"', '"+moveOutDate+"')");
  83. System.out.println(customerName +" " + address +" " + zipcode+ " "+ moveInDate + " " + segment + " " + waterMeter + " " + moveOutDate);
  84. break;
  85.  
  86.  
  87. case 2:
  88.  
  89. DB.selectSQL("SELECT fldCustomerName FROM tblCustomer");
  90. do {
  91. String data = DB.getDisplayData();
  92. if (data.equals(DB.NOMOREDATA)) {
  93. break;
  94. } else
  95. System.out.print(data);
  96. }
  97. while (true);
  98.  
  99. System.out.println("You can enter the customer name here you want to delete from the system:");
  100. customerName = input.next();
  101.  
  102. //Delete data
  103. DB.deleteSQL("DELETE from tblCustomer where fldCustomerName='"+customerName+"';" );
  104. System.out.println(customerName + " is removed from the list");
  105.  
  106. break;
  107.  
  108. case 3:
  109.  
  110. System.out.println("You can update information about customers here:");
  111.  
  112. do {
  113.  
  114. System.out.println("/////////////////////////////////////////");
  115. System.out.print("Please choose which information would you like to update:");
  116. System.out.println();
  117. System.out.println("Press '1' for: Customer Name");
  118. System.out.println("Press '2' for: Address");
  119. System.out.println("Press '3' for: Zipcode");
  120. System.out.println("Press '4' for: Move in date");
  121. System.out.println("Press '5' for: Segment");
  122. System.out.println("Press '6' for: Number of Water meters");
  123. System.out.println("Press '7' for: Go back to main menu");
  124.  
  125.  
  126. menuItem = validateIntInput(); //Keep this to validate menu
  127. System.out.println("/////////////////////////////////////////");
  128.  
  129. switch (menuItem) {
  130. case 1:
  131. 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.
  132. //input = input.reset();
  133. input.nextLine();
  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://
  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