Advertisement
Kristianqa

Assignment 2

Apr 4th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.23 KB | None | 0 0
  1. MAIN METODE::
  2.  
  3. package Assignments;
  4.  
  5. import java.util.Scanner;
  6.  
  7. import org.omg.Messaging.SyncScopeHelper;
  8.  
  9. public class WebShopping {
  10.  
  11. public static void main(String[] args) {
  12.  
  13. System.out.println("*********Welcome to the CBS webshop!*********");
  14. Scanner input = new Scanner(System.in);
  15. System.out.println("To create your profile, please fill out the"
  16. + " following personal information:");
  17.  
  18.  
  19. // First name
  20. System.out.println("First name: ");
  21. String firstname = input.nextLine();
  22. System.out.println(firstname + " is now registrated \n");
  23.  
  24. //Last name
  25. System.out.println("Last name: ");
  26. String lastname = input.nextLine();
  27. System.out.println(lastname + " is now registrated \n");
  28.  
  29.  
  30. //Address
  31. System.out.println("Address: ");
  32. String address = input.nextLine();
  33. System.out.println(address + " is now registrated \n");
  34.  
  35.  
  36. //Postalcode
  37. System.out.println("Postalcode - it needs to be within the CPH area (1000-2500): ");
  38. int Postalcode = input.nextInt();
  39. while (Postalcode < 1000 || Postalcode > 2500) {
  40. System.out.println("Your Postalcode is not within the CPH area - try again");
  41. Postalcode = input.nextInt();
  42. }
  43.  
  44.  
  45. //CPR combined with Date of birth
  46. System.out.println(Postalcode + " is now registrated \n");
  47. System.out.println("Please insert your CPR in the following format ddmmyy-xxxx: ");
  48. String CPR = input.next();
  49. while (CPR.length() != 11 || CPR.charAt(6) != '-') {
  50. System.out.println("You have not entered your CPR in the right format - try again");
  51. CPR = input.next();
  52. }
  53.  
  54.  
  55. //Telephone
  56. System.out.println(CPR + " is now registrated \n");
  57. System.out.println("Please insert your eight digit telephonenumber: ");
  58. String Telephone = input.next();
  59. while (Telephone.length() != 8) {
  60. System.out.println("You have not entered your telephone number in the right format - try again");
  61. Telephone = input.next();
  62.  
  63. }
  64.  
  65.  
  66. //Successful registration
  67. System.out.println(Telephone + " is now registrated \n" +
  68. "You have now succesfully registrated your personal informaton. You can find your log-in information beneath");
  69.  
  70.  
  71. //Username and password
  72.  
  73. //Username:
  74. String s1 = firstname.substring(0,1);
  75. String s2 = lastname.substring(0,3);
  76. String username = s1 + s2;
  77. System.out.println("\nYour username has now been created. It is: " + username);
  78.  
  79. //Password:
  80. String s3 = lastname.substring(0,3);
  81. String s4 = CPR.substring(7,CPR.length());
  82. String password = s3 + s4;
  83. System.out.println("Your password has now been created. It is: " + password);
  84.  
  85.  
  86. //Log-in or exit
  87.  
  88. System.out.println("\nTo exit the program, please press 1: ");
  89. System.out.println("To login, please press 2: ");
  90.  
  91. int exitorlogin = input.nextInt();
  92. while (exitorlogin > 3) {
  93. System.out.println("You haven't chosen one of above mentioned possibilities, try again");
  94. exitorlogin = input.nextInt(); }
  95.  
  96. switch (exitorlogin) {
  97. case 1 :
  98. System.out.println("You have now exited the program, goodbye");
  99. break;
  100. case 2 :
  101. System.out.println("You can now log in. Please follow the next step");
  102. break;
  103.  
  104.  
  105. }
  106.  
  107. //Type in username and password
  108.  
  109. //username, three attempts:
  110.  
  111. {
  112. System.out.println("\nEnter Username");
  113. String UsernameLogin = input.next();
  114. int count = 3;
  115. while (!UsernameLogin.equals(username)) {
  116. count--;
  117. System.out.println("Wrong Username, you have " + count + " attempts left");
  118. if (count==0) {
  119. System.out.print("Sorry you have exceeded the number of tries. Please try again after few hours");
  120. System.exit(0);
  121. }
  122. UsernameLogin = input.next();
  123.  
  124. }
  125. }
  126.  
  127. //password, three attempts:
  128.  
  129. System.out.println(username + " is accepted");
  130. System.out.println("\nEnter Password");
  131. String PasswordLogin = input.next();
  132. int count = 3;
  133. while (!PasswordLogin.equals(password)) {
  134. count--;
  135. System.out.println("Wrong Password, you have " + count + " attempts left");
  136. if (count==0) {
  137. System.out.print("Sorry you have exceeded the number of tries. Please try again after few hours");
  138. System.exit(0);
  139. }
  140. PasswordLogin = input.next();
  141.  
  142.  
  143. }
  144.  
  145. System.out.println("\nYou are now logged in!" );
  146.  
  147. ProductDatabase pd = new ProductDatabase ();
  148.  
  149.  
  150. String mainMenu = ("Press 1 to browse boooks +\n"
  151. + " Press 2 to browse Sweatshirts\n"
  152. + " Press 3 to browse Caps");
  153.  
  154. System.out.println(mainMenu);
  155.  
  156.  
  157. String chooseProducts = input.next();
  158.  
  159. switch (chooseProducts) {
  160. case "1": pd.browseBooks(); break;
  161. case "2": pd.browseSwearshirts(); break;
  162. case "3": pd.browseCaps(); break;
  163.  
  164.  
  165. }
  166. }
  167. }
  168.  
  169. ____________________________________________________________________________________________________________
  170.  
  171.  
  172. PRODUCT-DATABASE:
  173.  
  174. package Assignments;
  175.  
  176. import java.util.ArrayList;
  177.  
  178.  
  179. public class ProductDatabase {
  180.  
  181.  
  182. private ArrayList<Book> books = new ArrayList<Book>();
  183. private ArrayList<Cap> caps = new ArrayList<Cap>();
  184. private ArrayList<Swearshirt> sweatshirts = new ArrayList<Swearshirt>();
  185.  
  186. ProductDatabase () {
  187.  
  188. books.add(new Book(140, 1, "Lord of the rings", 11111111, "Jjr tolkien", 500));
  189. books.add(new Book(100, 2, "Harry Potter", 1234567, "Rowlings", 600));
  190.  
  191. System.out.println("/n");
  192.  
  193. sweatshirts.add(new Swearshirt(100, 1, "Black", "Medium"));
  194. sweatshirts.add(new Swearshirt(100, 2, "Yellow", "Medium"));
  195. sweatshirts.add(new Swearshirt(100, 3, "Red", "Small"));
  196. sweatshirts.add(new Swearshirt(100, 4,"Green", "Medium"));
  197.  
  198. System.out.println("\n Caps 1: ");
  199.  
  200. caps.add(new Cap(100, 1, "Medium fit", "Medium", "Black", true));
  201. caps.add(new Cap(50, 2, "Medium fit", "Medium", "Black", false));
  202. caps.add(new Cap(100, 3, "Medium fit", "Medium", "Black", true));
  203. caps.add(new Cap(75, 4, "Medium fit", "Medium", "Black", true));
  204. }
  205.  
  206. public void addBook() {
  207.  
  208.  
  209. }
  210.  
  211. public void addSweatshirt(Swearshirt sweatshirt) {
  212. sweatshirts.add(sweatshirt);
  213.  
  214.  
  215. }
  216.  
  217. public void addCap(Cap cap) {
  218. caps.add(cap);
  219. }
  220.  
  221. public void browseBooks() {
  222. if (!books.isEmpty())
  223. {
  224. for (Book b : books)
  225. {
  226. System.out.println("\n Available books: ");
  227. System.out.println("Title: " + b.getTitle());
  228. System.out.println("Price: " + b.getPrice());
  229. System.out.println("Id: " + b.getId());
  230. System.out.println("ISBN: " + b.getISBN());
  231. System.out.println("Author: " + b.getAuthor());
  232. System.out.println("Number of pages: " + b.getPage());
  233. }
  234.  
  235. }
  236.  
  237. else System.out.println("Sorry, no books available");
  238.  
  239. }
  240.  
  241. public void browseSwearshirts(){
  242. if (!sweatshirts.isEmpty())
  243. {
  244. for (Swearshirt s : sweatshirts)
  245. {
  246. System.out.println("\n");
  247. System.out.println("Price: " + s.getPrice());
  248. System.out.println("Id: " + s.getId());
  249. System.out.println("Colour: " + s.getcolour());
  250. System.out.println("Size: " + s.getsize());
  251. }
  252. }
  253.  
  254. else System.out.println("Sorry, no sweatshirts available");
  255.  
  256. }
  257.  
  258. public void browseCaps(){
  259. if (!caps.isEmpty())
  260. {
  261. for (Cap c : caps)
  262. {
  263. System.out.println("\n");
  264. System.out.println("Price: " + c.getPrice());
  265. System.out.println("Id: " + c.getId());
  266. System.out.println("Colour: " + c.getColour());
  267. System.out.println("Size: " + c.getSize());
  268. System.out.println("Fit: " + c.getFit());
  269. System.out.println("Embroidery: " + c.getEmbroidery());
  270. }
  271. }
  272. else System.out.println("Sorry, no caps available");
  273.  
  274. }
  275. }
  276.  
  277. ____________________________________________________________________________________________________________
  278.  
  279. CAP::
  280.  
  281. package Assignments;
  282.  
  283. public class Cap extends Product{
  284.  
  285. private String fit, size, colour;
  286. private boolean embroidery;
  287.  
  288.  
  289. public Cap (double price, int id, String fit, String size, String colour, boolean embroidery ) {
  290.  
  291. this.fit = fit;
  292. this.size = size;
  293. this.colour = colour;
  294. this.embroidery = embroidery;
  295.  
  296. }
  297.  
  298. public String getFit() {
  299. return fit;
  300. }
  301.  
  302. public void setFit(String fit) {
  303. this.fit = fit;
  304. }
  305.  
  306. public String getSize() {
  307. return size;
  308. }
  309.  
  310. public void setSize(String size) {
  311. this.size = size;
  312. }
  313.  
  314. public String getColour() {
  315. return colour;
  316. }
  317.  
  318. public void setColour(String colour) {
  319. this.colour = colour;
  320. }
  321.  
  322. public boolean getEmbroidery() {
  323. return embroidery;
  324. }
  325.  
  326. public void setEmbroidery(boolean embroidery) {
  327. this.embroidery = embroidery;
  328.  
  329.  
  330. }
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement