src/VirusSpreadSimulator.java
/*
		 * Worker thread executes our own version of the doInBackground() method continuously in the background.
		 */
Worker thread executes our own version of the doInBackground() method continuously in the background.
Worker thread executes our own version of the doInBackground () method continuously in the background .
@Override
		protected Boolean doInBackground() throws Exception {
			FileWriter csvWriter = new FileWriter("Report.csv");
			csvWriter.append("Day");
			csvWriter.append(",");
			csvWriter.append("Population");
			csvWriter.append(",");
			csvWriter.append("Hospital Capacity");
			csvWriter.append(",");
			csvWriter.append("Infections");
			csvWriter.append(",");
			csvWriter.append("Patients");
			csvWriter.append(",");
			csvWriter.append("Deaths");
			csvWriter.append(",");
			csvWriter.append("Under Quarantine");
			csvWriter.append(",");
			csvWriter.append("Quarantine Level");
			csvWriter.append("\n");
			csvWriter.flush();
			csvWriter.close();
			while (!isCancelled()) {
				String day = String.valueOf(city.day);
				if (day.equals(newday))
				{
					csvWriter = new FileWriter("Report.csv", true);
					day = String.valueOf(city.day);
					days[city.day] = city.day; 
					infections[city.day] = city.getInfection(); 
					patients[city.day] = city.getHospital().getPatientNum(); 
					deaths[city.day] = city.getDeathNum(); 
					String infection = String.valueOf(city.getInfection());
					String death = String.valueOf(city.getDeathNum());
					String population = String.valueOf(city.getPsize());
					String patients = String.valueOf(city.getHospital().getPatientNum());
					String hospitalCapacity = String.valueOf(city.getHospital().getcapacity());
					String quarantineOrNot = "";
					String quarantineLevel = "";
					if (city.getState())
						quarantineOrNot = "True";
					else
						quarantineOrNot = "False";
					if (quarantineOrNot.equals("True"))
						quarantineLevel = String.valueOf(city.getQLevel());
					else
						quarantineLevel = "N/A";
					csvWriter.append(String.join(",", Arrays.asList(day, population, hospitalCapacity, infection, patients, death, quarantineOrNot, quarantineLevel)));
					csvWriter.append("\n");
					csvWriter.flush();
					csvWriter.close();
					newday = String.valueOf(Integer.parseInt(newday) + 1);
				}
				fifo.add((double)city.getInfection());
				if (fifo.size() > 3000) {
					fifo.removeFirst();
				}
				double[] array = new double[fifo.size()];
				for (int i = 0; i < fifo.size(); i++) {
					array[i] = fifo.get(i);
				}
				fifo1.add((double)city.getHospital().getPatientNum());
				if (fifo1.size() > 3000) {
					fifo1.removeFirst();
				}
				double[] array1 = new double[fifo1.size()];
				for (int i = 0; i < fifo1.size(); i++) {
					array1[i] = fifo1.get(i);
				}
				fifo2.add((double)city.getDeathNum());
				if (fifo2.size() > 3000) {
					fifo2.removeFirst();
				}
				double[] array2 = new double[fifo2.size()];
				for (int i = 0; i < fifo2.size(); i++) {
					array2[i] = fifo2.get(i);
				}
				publish(array, array1, array2);
				try {
					Thread.sleep(5);
				} catch (InterruptedException e) {
					System.out.println("Live Chart shut down.");
				}
			}
			return true;
		}
@ Override protected Boolean doInBackground ( ) throws Exception { FileWriter csvWriter = new FileWriter ( "Report.csv" ) ; csvWriter . append ( "Day" ) ; csvWriter . append ( "," ) ; csvWriter . append ( "Population" ) ; csvWriter . append ( "," ) ; csvWriter . append ( "Hospital Capacity" ) ; csvWriter . append ( "," ) ; csvWriter . append ( "Infections" ) ; csvWriter . append ( "," ) ; csvWriter . append ( "Patients" ) ; csvWriter . append ( "," ) ; csvWriter . append ( "Deaths" ) ; csvWriter . append ( "," ) ; csvWriter . append ( "Under Quarantine" ) ; csvWriter . append ( "," ) ; csvWriter . append ( "Quarantine Level" ) ; csvWriter . append ( "\n" ) ; csvWriter . flush ( ) ; csvWriter . close ( ) ; while ( ! isCancelled ( ) ) { String day = String . valueOf ( city . day ) ; if ( day . equals ( newday ) ) { csvWriter = new FileWriter ( "Report.csv" , true ) ; day = String . valueOf ( city . day ) ; days [ city . day ] = city . day ; infections [ city . day ] = city . getInfection ( ) ; patients [ city . day ] = city . getHospital ( ) . getPatientNum ( ) ; deaths [ city . day ] = city . getDeathNum ( ) ; String infection = String . valueOf ( city . getInfection ( ) ) ; String death = String . valueOf ( city . getDeathNum ( ) ) ; String population = String . valueOf ( city . getPsize ( ) ) ; String patients = String . valueOf ( city . getHospital ( ) . getPatientNum ( ) ) ; String hospitalCapacity = String . valueOf ( city . getHospital ( ) . getcapacity ( ) ) ; String quarantineOrNot = "" ; String quarantineLevel = "" ; if ( city . getState ( ) ) quarantineOrNot = "True" ; else quarantineOrNot = "False" ; if ( quarantineOrNot . equals ( "True" ) ) quarantineLevel = String . valueOf ( city . getQLevel ( ) ) ; else quarantineLevel = "N/A" ; csvWriter . append ( String . join ( "," , Arrays . asList ( day , population , hospitalCapacity , infection , patients , death , quarantineOrNot , quarantineLevel ) ) ) ; csvWriter . append ( "\n" ) ; csvWriter . flush ( ) ; csvWriter . close ( ) ; newday = String . valueOf ( Integer . parseInt ( newday ) + 1 ) ; } fifo . add ( ( double ) city . getInfection ( ) ) ; if ( fifo . size ( ) > 3000 ) { fifo . removeFirst ( ) ; } double [ ] array = new double [ fifo . size ( ) ] ; for ( int i = 0 ; i < fifo . size ( ) ; i ++ ) { array [ i ] = fifo . get ( i ) ; } fifo1 . add ( ( double ) city . getHospital ( ) . getPatientNum ( ) ) ; if ( fifo1 . size ( ) > 3000 ) { fifo1 . removeFirst ( ) ; } double [ ] array1 = new double [ fifo1 . size ( ) ] ; for ( int i = 0 ; i < fifo1 . size ( ) ; i ++ ) { array1 [ i ] = fifo1 . get ( i ) ; } fifo2 . add ( ( double ) city . getDeathNum ( ) ) ; if ( fifo2 . size ( ) > 3000 ) { fifo2 . removeFirst ( ) ; } double [ ] array2 = new double [ fifo2 . size ( ) ] ; for ( int i = 0 ; i < fifo2 . size ( ) ; i ++ ) { array2 [ i ] = fifo2 . get ( i ) ; } publish ( array , array1 , array2 ) ; try { Thread . sleep ( 5 ) ; } catch ( InterruptedException e ) { System . out . println ( "Live Chart shut down." ) ; } } return true ; }
Worker thread executes our own version of the doInBackground() method continuously in the background.
Worker thread executes our own version of the doInBackground () method continuously in the background .
// Write data to a csv file. // Keep track of day // Keep track of number of infections // Keep track of number of patients // Keep track of number of deaths // Caught when interrupt is called