Advertisement
Crenox

Joes Used Cars Java Program

Jan 29th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.39 KB | None | 0 0
  1. // Sammy Samkough
  2. // Joes Used Cars
  3. // Spec: Joe is interested in maintaining information for each of his cars in his fleet. The information he wants are:
  4. // Make, model, mileage, year, cylinders, price, and manual/automatic.
  5.  
  6. public class Car implements Comparable
  7. {
  8. private String make, model;
  9. private int mileage, year, cylinders, price;
  10. private boolean isManual; // this is for the transmission
  11.  
  12. public Car()
  13. {
  14. make = ""; // 1
  15. model = ""; // 2
  16. mileage = 0; // 3
  17. year = 0; // 4
  18. cylinders = 0; // 5
  19. price = 0; // 6
  20. isManual = false; // 7
  21. }
  22.  
  23. public Car(String ma, String mo, int mi, int y, int c, int p, boolean im)
  24. {
  25. make = ma;
  26. model = mo;
  27. mileage = mi;
  28. year = y;
  29. cylinders = c;
  30. price = p;
  31. isManual = im;
  32. }
  33.  
  34. // gets and sets
  35. /*-----------------------------------------------------------------------------------------------------------------------*/
  36. /*
  37. public Car getCar()
  38. {
  39.  
  40. }
  41. */
  42.  
  43. public void setCar(String ma, String mo, int mi, int y, int c, int p, boolean im)
  44. {
  45. make = ma;
  46. model = mo;
  47. mileage = mi;
  48. year = y;
  49. cylinders = c;
  50. price = p;
  51. isManual = im;
  52. }
  53.  
  54. public String getMake()
  55. {
  56. return make;
  57. }
  58.  
  59. public void setMake(String s)
  60. {
  61. make = s;
  62. }
  63.  
  64. public String getModel()
  65. {
  66. return model;
  67. }
  68.  
  69. public void setModel(String s)
  70. {
  71. model = s;
  72. }
  73.  
  74. public int getMileage()
  75. {
  76. return mileage;
  77. }
  78.  
  79. public void setMileage(int m)
  80. {
  81. mileage = m;
  82. }
  83.  
  84. public int getYear()
  85. {
  86. return year;
  87. }
  88.  
  89. public void setYear(int y)
  90. {
  91. year = y;
  92. }
  93.  
  94. public int getCylinder()
  95. {
  96. return cylinders;
  97. }
  98.  
  99. public void setCylinder(int c)
  100. {
  101. cylinders = c;
  102. }
  103.  
  104. public int getPrice()
  105. {
  106. return price;
  107. }
  108.  
  109. public void setPrice(int p)
  110. {
  111. price = p;
  112. }
  113.  
  114. public boolean getIsManual()
  115. {
  116. return isManual;
  117. }
  118.  
  119. public void setIsManual(boolean m)
  120. {
  121. isManual = m;
  122. }
  123. /*-----------------------------------------------------------------------------------------------------------------------*/
  124.  
  125. // Car Class implements Comparable
  126. public int compareTo(Object obj)
  127. {
  128. Car other = (Car)obj;
  129.  
  130. if(price > other.price)
  131. {
  132. return 1;
  133. }
  134. else if(price < other.price)
  135. {
  136. return -1;
  137. }
  138. else
  139. {
  140. return 0;
  141. }
  142. }
  143.  
  144. public String toString()
  145. {
  146. String result = "";
  147.  
  148. result += "The make for the car is " + make + ".\n";
  149. result += "The model for the car is " + model + ".\n";
  150. result += "The mileage for the car is " + mileage + ".\n";
  151. result += "The year for the car is " + year + ".\n";
  152. result += "The amount of cylinders the car has is " + cylinders + ".\n";
  153. result += "The price for the car is " + price + ".\n";
  154. if (isManual == true)
  155. {
  156. result += "The transmission for the car is manual.\n";
  157. }
  158. else
  159. {
  160. result += "The transmission for the car is automatic.\n";
  161. }
  162.  
  163. return result;
  164. }
  165. }
  166. -------------------------------------------------------------------------------------------------------------------------------
  167. // Sammy Samkough
  168. // Joes Used Cars
  169. // Spec: Joe is interested in maintaing information for each of his cars in his fleet.
  170. // Make, model, mileage, year, cylinders, price, and manual/automatic.
  171.  
  172. public class CarRunner
  173. {
  174. public static void main (String args[])
  175. {
  176. Car test = new Car("Mercedes", "Benz C-Class", 25000, 2015, 4, 40000, false);
  177.  
  178. System.out.println(test);
  179.  
  180. test = new Car("Honda", "Pilot", 60000, 2006, 4, 25000, false);
  181. }
  182. }
  183. /*
  184.  
  185. */
  186.  
  187. /*
  188. 1) Add a Car // adds a car to end of list
  189.  
  190. 2) Insert // adds a car at a given position
  191.  
  192. 3) Remove // remove a car from the lot
  193.  
  194. 4) Number of Cars on Lot // size
  195.  
  196. 5) Value of Cars on Lot // calculate total on the lot
  197.  
  198. 6) View a Car // use get method for ArrayList
  199.  
  200. 7) Sort by Price // Car class implements Comparable
  201.  
  202. 8) View the Lot // iterate thru the ArrayList
  203. */
  204. -------------------------------------------------------------------------------------------------------------------------------
  205. // Sammy Samkough
  206. // Joes Used Cars
  207. // Spec: Joe is interested in maintaing information for each of his cars in his fleet.
  208. // Make, model, mileage, year, cylinders, price, and manual/automatic.
  209.  
  210. import java.util.Scanner;
  211. import java.util.List;
  212. import java.util.ArrayList;
  213. import java.util.Collections;
  214.  
  215. public class CarLot
  216. {
  217. private ArrayList<Car> ray = new ArrayList<Car>();
  218.  
  219. public CarLot()
  220. {
  221. ray = new ArrayList<Car>();
  222. }
  223.  
  224. // adds a car to the end of the list
  225. public void add(String ma, String mo, int mi, int y, int c, int p, boolean im)
  226. {
  227. ray.add(new Car(ma, mo, mi, y, c, p, im));
  228. }
  229.  
  230. // adds a car at a given position
  231. public void insert(int pos, String ma, String mo, int mi, int y, int c, int p, boolean im)
  232. {
  233. ray.set(pos, new Car(ma, mo, mi, y, c, p, im));
  234. }
  235.  
  236. // removes a car from the lot
  237. public void remove(int pos)
  238. {
  239. ray.remove(pos);
  240. }
  241.  
  242. // the size
  243. public int numberOfCarsInLot()
  244. {
  245. int size = ray.size();
  246.  
  247. return size;
  248. }
  249.  
  250. // calculate the total on the lot
  251. public int valueOfCarsInLot()
  252. {
  253. int total = 0;
  254.  
  255. for (int i = 0; i < ray.size(); i++)
  256. {
  257. total += ray.get(i).getPrice();
  258. }
  259.  
  260. return total;
  261. }
  262.  
  263. // use get method for ArrayList
  264. public Car viewOfCar(int i)
  265. {
  266. return ray.get(i);
  267. }
  268.  
  269. // Car class implements Comparable
  270. public void sortCarByPrice()
  271. {
  272. List<Car> list = new ArrayList<Car>();
  273. Collections.sort(list);
  274. }
  275.  
  276. // iterate though the ArrayList
  277. public void viewTheLot()
  278. {
  279. for (int i = 0; i < ray.size(); i++)
  280. {
  281. System.out.println(ray.get(i));
  282. }
  283. }
  284.  
  285. public String toString()
  286. {
  287. return "" + ray;
  288. }
  289. }
  290. /*
  291.  
  292. */
  293. -------------------------------------------------------------------------------------------------------------------------------
  294. // Sammy Samkough
  295. // Joes Used Cars
  296. // Spec: Joe is interested in maintaing information for each of his cars in his fleet.
  297. // Make, model, mileage, year, cylinders, price, and manual/automatic.
  298.  
  299. import java.util.Scanner;
  300. import java.util.ArrayList;
  301. import java.util.Collections;
  302.  
  303. public class CarLotRunner
  304. {
  305. public static void main (String args[])
  306. {
  307. CarLot fleet = new CarLot();
  308.  
  309. fleet.add("Chevy", "Avalanche", 20000, 2013, 4, 26000, false);
  310. System.out.println("WE ADD A CHEVY AVALANCE: \n" + fleet); // 1
  311. System.out.println("--------------------------------------------------------------------------------");
  312. fleet.add("Audi", "R8", 20000, 2014, 8, 50000, false); // 1
  313. System.out.println("WE ADD AN AUDI R8 TO THE END OF THE LINE: \n" + fleet);
  314. System.out.println("--------------------------------------------------------------------------------");
  315. fleet.add("Mercedes", "Benz C-Class", 25000, 2015, 4, 40000, false); // 1
  316. System.out.println("WE ADD A MERCEDES BENZ C-CLASS TO THE END OF THE LINE: \n" + fleet);
  317. System.out.println("--------------------------------------------------------------------------------");
  318. fleet.insert(0, "Honda", "Pilot", 15000, 2006, 4, 25000, false); // 2
  319. System.out.println("WE INSERT A HONDA PILOT IN THE 1ST SPOT TO REMOVE THE CHEVY AVALANCE: \n" + fleet);
  320. System.out.println("--------------------------------------------------------------------------------");
  321. fleet.remove(1); // 3
  322. System.out.println("WE REMOVE THE AUDI R8 WHICH BRINGS ALL THE CARS DOWN ONE: \n" + fleet);
  323. System.out.println("--------------------------------------------------------------------------------");
  324. System.out.print("THERE ARE ");
  325. System.out.print(fleet.numberOfCarsInLot()); // 4
  326. System.out.println(" CARS IN THE LOT.");
  327. System.out.println("--------------------------------------------------------------------------------");
  328. System.out.print("THE VALUE OF THE CARS ARE $");
  329. System.out.print(fleet.valueOfCarsInLot()); // 5
  330. System.out.println(".");
  331. System.out.println("--------------------------------------------------------------------------------");
  332. System.out.println("HERE'S THE VIEW OF CAR #2: ");
  333. System.out.print(fleet.viewOfCar(1)); // 6
  334. System.out.println("--------------------------------------------------------------------------------");
  335. fleet.sortCarByPrice(); // 7
  336. System.out.println("WE ARE GOING TO SORT THE CARS BY PRICE NOW, HERE'S HOW THE FLEET LOOKS LIKE: \n" + fleet);
  337. System.out.println("--------------------------------------------------------------------------------");
  338. System.out.println("HERE'S THE VIEW OF THE LOT: ");
  339. fleet.viewTheLot(); // 8
  340. System.out.println("--------------------------------------------------------------------------------");
  341. System.out.println("HERE'S THE FINAL LOOK AT THE FLEET: \n" + fleet);
  342. }
  343. }
  344. /*
  345. WE ADD A CHEVY AVALANCE:
  346. [The make for the car is Chevy.
  347. The model for the car is Avalanche.
  348. The mileage for the car is 20000.
  349. The year for the car is 2013.
  350. The amount of cylinders the car has is 4.
  351. The price for the car is 26000.
  352. The transmission for the car is automatic.
  353. ]
  354. --------------------------------------------------------------------------------
  355.  
  356. WE ADD AN AUDI R8 TO THE END OF THE LINE:
  357. [The make for the car is Chevy.
  358. The model for the car is Avalanche.
  359. The mileage for the car is 20000.
  360. The year for the car is 2013.
  361. The amount of cylinders the car has is 4.
  362. The price for the car is 26000.
  363. The transmission for the car is automatic.
  364. , The make for the car is Audi.
  365. The model for the car is R8.
  366. The mileage for the car is 20000.
  367. The year for the car is 2014.
  368. The amount of cylinders the car has is 8.
  369. The price for the car is 50000.
  370. The transmission for the car is automatic.
  371. ]
  372. --------------------------------------------------------------------------------
  373.  
  374. WE ADD A MERCEDES BENZ C-CLASS TO THE END OF THE LINE:
  375. [The make for the car is Chevy.
  376. The model for the car is Avalanche.
  377. The mileage for the car is 20000.
  378. The year for the car is 2013.
  379. The amount of cylinders the car has is 4.
  380. The price for the car is 26000.
  381. The transmission for the car is automatic.
  382. , The make for the car is Audi.
  383. The model for the car is R8.
  384. The mileage for the car is 20000.
  385. The year for the car is 2014.
  386. The amount of cylinders the car has is 8.
  387. The price for the car is 50000.
  388. The transmission for the car is automatic.
  389. , The make for the car is Mercedes.
  390. The model for the car is Benz C-Class.
  391. The mileage for the car is 25000.
  392. The year for the car is 2015.
  393. The amount of cylinders the car has is 4.
  394. The price for the car is 40000.
  395. The transmission for the car is automatic.
  396. ]
  397. --------------------------------------------------------------------------------
  398.  
  399. WE INSERT A HONDA PILOT IN THE 1ST SPOT TO REMOVE THE CHEVY AVALANCE:
  400. [The make for the car is Honda.
  401. The model for the car is Pilot.
  402. The mileage for the car is 15000.
  403. The year for the car is 2006.
  404. The amount of cylinders the car has is 4.
  405. The price for the car is 25000.
  406. The transmission for the car is automatic.
  407. , The make for the car is Audi.
  408. The model for the car is R8.
  409. The mileage for the car is 20000.
  410. The year for the car is 2014.
  411. The amount of cylinders the car has is 8.
  412. The price for the car is 50000.
  413. The transmission for the car is automatic.
  414. , The make for the car is Mercedes.
  415. The model for the car is Benz C-Class.
  416. The mileage for the car is 25000.
  417. The year for the car is 2015.
  418. The amount of cylinders the car has is 4.
  419. The price for the car is 40000.
  420. The transmission for the car is automatic.
  421. ]
  422. --------------------------------------------------------------------------------
  423.  
  424. WE REMOVE THE AUDI R8 WHICH BRINGS ALL THE CARS DOWN ONE:
  425. [The make for the car is Honda.
  426. The model for the car is Pilot.
  427. The mileage for the car is 15000.
  428. The year for the car is 2006.
  429. The amount of cylinders the car has is 4.
  430. The price for the car is 25000.
  431. The transmission for the car is automatic.
  432. , The make for the car is Mercedes.
  433. The model for the car is Benz C-Class.
  434. The mileage for the car is 25000.
  435. The year for the car is 2015.
  436. The amount of cylinders the car has is 4.
  437. The price for the car is 40000.
  438. The transmission for the car is automatic.
  439. ]
  440. --------------------------------------------------------------------------------
  441.  
  442. THERE ARE 2 CARS IN THE LOT.
  443. --------------------------------------------------------------------------------
  444.  
  445. THE VALUE OF THE CARS ARE $65000.
  446. --------------------------------------------------------------------------------
  447.  
  448. HERE'S THE VIEW OF CAR #2:
  449. The make for the car is Mercedes.
  450. The model for the car is Benz C-Class.
  451. The mileage for the car is 25000.
  452. The year for the car is 2015.
  453. The amount of cylinders the car has is 4.
  454. The price for the car is 40000.
  455. The transmission for the car is automatic.
  456. --------------------------------------------------------------------------------
  457.  
  458. WE ARE GOING TO SORT THE CARS BY PRICE NOW, HERE'S HOW THE FLEET LOOKS LIKE:
  459. [The make for the car is Honda.
  460. The model for the car is Pilot.
  461. The mileage for the car is 15000.
  462. The year for the car is 2006.
  463. The amount of cylinders the car has is 4.
  464. The price for the car is 25000.
  465. The transmission for the car is automatic.
  466. , The make for the car is Mercedes.
  467. The model for the car is Benz C-Class.
  468. The mileage for the car is 25000.
  469. The year for the car is 2015.
  470. The amount of cylinders the car has is 4.
  471. The price for the car is 40000.
  472. The transmission for the car is automatic.
  473. ]
  474. --------------------------------------------------------------------------------
  475.  
  476. HERE'S THE VIEW OF THE LOT:
  477. The make for the car is Honda.
  478. The model for the car is Pilot.
  479. The mileage for the car is 15000.
  480. The year for the car is 2006.
  481. The amount of cylinders the car has is 4.
  482. The price for the car is 25000.
  483. The transmission for the car is automatic.
  484.  
  485. The make for the car is Mercedes.
  486. The model for the car is Benz C-Class.
  487. The mileage for the car is 25000.
  488. The year for the car is 2015.
  489. The amount of cylinders the car has is 4.
  490. The price for the car is 40000.
  491. The transmission for the car is automatic.
  492.  
  493. --------------------------------------------------------------------------------
  494.  
  495. HERE'S THE FINAL LOOK AT THE FLEET:
  496. [The make for the car is Honda.
  497. The model for the car is Pilot.
  498. The mileage for the car is 15000.
  499. The year for the car is 2006.
  500. The amount of cylinders the car has is 4.
  501. The price for the car is 25000.
  502. The transmission for the car is automatic.
  503. , The make for the car is Mercedes.
  504. The model for the car is Benz C-Class.
  505. The mileage for the car is 25000.
  506. The year for the car is 2015.
  507. The amount of cylinders the car has is 4.
  508. The price for the car is 40000.
  509. The transmission for the car is automatic.
  510. ]
  511. Press any key to continue . . .
  512. */
  513.  
  514. /*
  515. 1) Add a Car // adds a car to end of list
  516.  
  517. 2) Insert // adds a car at a given position
  518.  
  519. 3) Remove // remove a car from the lot
  520.  
  521. 4) Number of Cars on Lot // size
  522.  
  523. 5) Value of Cars on Lot // calculate total on the lot
  524.  
  525. 6) View a Car // use get method for ArrayList
  526.  
  527. 7) Sort by Price // Car class implements Comparable
  528.  
  529. 8) View the Lot // iterate thru the ArrayList
  530. */
  531. -------------------------------------------------------------------------------------------------------------------------------
  532. // Sammy Samkough
  533. // Joes Used Cars
  534. // Spec: Joe is interested in maintaing information for each of his cars in his fleet.
  535. // Make, model, mileage, year, cylinders, price, and manual/automatic.
  536.  
  537. import java.util.Scanner;
  538. import java.util.ArrayList;
  539. import java.util.Collections;
  540.  
  541. public class CarLotRunnerSwitch
  542. {
  543. public static void main (String args[])
  544. {
  545. CarLot fleet = new CarLot();
  546. Scanner sc = new Scanner(System.in);
  547. int choice = 0;
  548. String input = "";
  549. int point = 0;
  550. boolean running = true;
  551.  
  552. System.out.println("Hello and welcome to Joe's Used Cars! Type a number from 1-9 to choose what you wanna explore here!");
  553. while (running)
  554. {
  555. System.out.println("Here's the option list from what you can choose from:");
  556. System.out.println("1: Add a car");
  557. System.out.println("2: Insert a car");
  558. System.out.println("3: Remove a car");
  559. System.out.println("4: Number of cars in lot");
  560. System.out.println("5: Value of cars in lot");
  561. System.out.println("6: View of car");
  562. System.out.println("7: Sorting cars by price");
  563. System.out.println("8: Viewing the lot");
  564. System.out.println("9: Exit");
  565. System.out.println("Now please, choose one of them:");
  566. choice = sc.nextInt();
  567. switch (choice)
  568. {
  569. case 1: // add
  570. System.out.println("You chose the add a car option!");
  571. System.out.println("Type in your make, model, mileage, year, cylinders, price, and transmission(manual or automatic) for the car, in that order! Press enter after your done inputting the information for each spec.");
  572. fleet.add(sc.nextLine(), sc.nextLine(), sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextBoolean());
  573. System.out.println("--------------------------------------------------------------------------------------");
  574. break;
  575.  
  576.  
  577.  
  578. case 2: // insert
  579. System.out.println("You chose the insert a car option!");
  580. System.out.println("Please type in for me your point for the car you'd like to place it in, make, model, mileage, year, cylinders, and price for the car, in that order! Press enter after your done inputting the information for each spec.");
  581. fleet.insert(sc.nextInt(), sc.nextLine(), sc.nextLine(), sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextBoolean());
  582. System.out.println("--------------------------------------------------------------------------------------");
  583. break;
  584.  
  585.  
  586.  
  587. case 3: // remove
  588. System.out.println("You chose the remove a car option!");
  589. System.out.println("What's the point of the car that you'd like to remove?");
  590. point = sc.nextInt();
  591. fleet.remove(point);
  592. System.out.println("--------------------------------------------------------------------------------------");
  593. break;
  594.  
  595.  
  596. case 4: // number of cars in lot
  597. System.out.println("You chose the number of cars in lot option!");
  598. System.out.print("The number of cars in lot are: ");
  599. System.out.println(fleet.numberOfCarsInLot());
  600. System.out.println("--------------------------------------------------------------------------------------");
  601. break;
  602.  
  603.  
  604. case 5: // value of cars in lot
  605. System.out.println("You chose the value of cars in lot option!");
  606. System.out.print("The value of all of the cars combined in the car lot is: ");
  607. System.out.println(fleet.valueOfCarsInLot());
  608. System.out.println("--------------------------------------------------------------------------------------");
  609. break;
  610.  
  611.  
  612. case 6: // view a car
  613. System.out.println("You chose the view a car option!");
  614. System.out.print("Which car would you like to view?");
  615. int c = 0;
  616. c = sc.nextInt();
  617. System.out.print("The car is: ");
  618. System.out.println(fleet.viewOfCar(c));
  619. System.out.println("--------------------------------------------------------------------------------------");
  620. break;
  621.  
  622.  
  623. case 7: // sort by price
  624. System.out.println("You chose the sort by price option!");
  625. fleet.sortCarByPrice();
  626. System.out.println("--------------------------------------------------------------------------------------");
  627. break;
  628.  
  629.  
  630.  
  631. case 8: // view the lot
  632. System.out.println("You chose the view the lot option!");
  633. System.out.println(fleet.viewTheLot());
  634. System.out.println("--------------------------------------------------------------------------------------");
  635. break;
  636.  
  637.  
  638. case 9 : // exit
  639. System.out.println("You chose the exit option!");
  640. System.out.println("Would you like to exit (enter yes or no)?");
  641. input = sc.nextLine();
  642.  
  643. if (input.equalsIgnoreCase("yes"))
  644. {
  645. System.out.println("Ba-bye then!");
  646. running = false;
  647. }
  648. else
  649. {
  650. // continues
  651. }
  652. break;
  653.  
  654. default:
  655. System.out.println("Please input something we asked of you, not anything else.");
  656. }
  657. }
  658. }
  659. }
  660. /*
  661.  
  662. */
  663.  
  664. /*
  665. 1) Add a Car // adds a car to end of list
  666.  
  667. 2) Insert // adds a car at a given position
  668.  
  669. 3) Remove // remove a car from the lot
  670.  
  671. 4) Number of Cars on Lot // size
  672.  
  673. 5) Value of Cars on Lot // calculate total on the lot
  674.  
  675. 6) View a Car // use get method for ArrayList
  676.  
  677. 7) Sort by Price // Car class implements Comparable
  678.  
  679. 8) View the Lot // iterate thru the ArrayList
  680. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement