Advertisement
TheRightGuy

Advice?

Feb 17th, 2022
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.80 KB | None | 0 0
  1. public class App {
  2.     public static void main(String[] args) {
  3.         MovieStore movieStore = new MovieStore();
  4.         movieStore.start();
  5.     }
  6. }
  7.  
  8. public class Video {
  9.     private final String title;
  10.  
  11.     public String getTitle() {
  12.         return title;
  13.     }
  14.  
  15.     public Video(String name, LocalDate due) {
  16.         this.title = name;
  17.     }
  18. }
  19.  
  20. public class MovieStore {
  21.     private final ArrayList<Video> videos = new ArrayList<>();
  22.     private final Map<String, VideoStoreDates> overDueVideos = new HashMap<>();
  23.     private final Map<String, LocalDate> checkedOutVideos = new HashMap<>();
  24.  
  25.     public void start() {
  26.         getMoviesFromAFile();
  27.         getAllVideos();
  28.         randomCheckOutVideos();
  29.         getAllCheckedOutVideos();
  30.         storeAllOverDueVideos();
  31.         getAllOverDueVideos();
  32.     }
  33.  
  34.     private void getMoviesFromAFile() {
  35.         try {
  36.             BufferedReader bufferedReader = getBufferedReader();
  37.             addAllVideos(bufferedReader);
  38.         } catch (IOException e) {
  39.             throw new FileSystemNotFoundException("The file could not be found, check the file or adjust the location");
  40.         }
  41.     }
  42.  
  43.     private BufferedReader getBufferedReader() throws FileNotFoundException {
  44.         File file = new File("src/main/resources/MovieList.txt");
  45.         FileReader fileReader = new FileReader(file);
  46.         return new BufferedReader(fileReader);
  47.     }
  48.  
  49.     private void addAllVideos(BufferedReader bufferedReader) throws IOException {
  50.         String line;
  51.         int MAX_LINE = 22;
  52.         for (int i = 0; i < MAX_LINE; i++) {
  53.             line = bufferedReader.readLine().toLowerCase(Locale.ROOT);
  54.             videos.add(new Video(line, LocalDate.now()));
  55.         }
  56.     }
  57.  
  58.     private void getAllVideos() {
  59.         System.out.println("Videos available");
  60.         for (Video video : videos) {
  61.             System.out.println(video.getTitle());
  62.         }
  63.     }
  64.  
  65.     private void randomCheckOutVideos() {
  66.         int randomVideosCheckedOut = (int) (Math.random()*videos.size());
  67.         VideoStoreDates videoStoreDates = new VideoStoreDates();
  68.         for (int i = 0; i < randomVideosCheckedOut; i++) {
  69.             int randomVide = (int) (Math.random() * videos.size());
  70.             checkedOutVideos.put(videos.get(randomVide).getTitle(), videoStoreDates.getDue());
  71.         }
  72.     }
  73.  
  74.     private void getAllCheckedOutVideos() {
  75.         for (Map.Entry<String, LocalDate> entry : checkedOutVideos.entrySet()) {
  76.             System.out.println("Videos that are checked out '" + entry.getKey() + "' and due: " + entry.getValue());
  77.         }
  78.     }
  79.     private void storeAllOverDueVideos() {
  80.         VideoStoreDates videoStoreDates = new VideoStoreDates();
  81.         for (int i = 0; i < checkedOutVideos.size(); i++) {
  82.             int randomVideo = (int) (Math.random() * checkedOutVideos.size());
  83.             if (videoStoreDates.getOverDue().isAfter(videoStoreDates.getDue())) {
  84.                 overDueVideos.put(videos.get(randomVideo).getTitle(), videoStoreDates);
  85.             }
  86.         }
  87.     }
  88.  
  89.     private void getAllOverDueVideos() {
  90.         for (Map.Entry<String, VideoStoreDates> entry : overDueVideos.entrySet()) {
  91.             System.out.println("Videos that are overdue '" + entry.getKey() + "' Date due to be given back '" + entry.getValue().getDue() + "' date brought back '" + entry.getValue().getOverDue() + "'");
  92.         }
  93.     }
  94. }
  95.  
  96. public class VideoStoreDates {
  97.     private final int DUE_DATE = 1;
  98.     private final int OVERDUE_DATE = 3;
  99.     private final LocalDate due = LocalDate.now().plusWeeks(DUE_DATE);
  100.     private final LocalDate overDue = LocalDate.now().plusWeeks((int)(Math.random()*OVERDUE_DATE));
  101.  
  102.     public LocalDate getDue() {
  103.         return due;
  104.     }
  105.  
  106.     public LocalDate getOverDue() {
  107.         return overDue;
  108.     }
  109. }
  110.  
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement