Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package NEGOCIOACTUAL;
- import java.util.ArrayList;
- import javax.swing.JOptionPane;
- public class Solucion {
- private static Oferta dia[];
- private static ArrayList<Oferta> ofertas;
- public Solucion() {
- dia = new Oferta[24];
- ofertas = new ArrayList<Oferta>();
- }
- public Oferta[] getDia() {
- return dia;
- }
- public void setDia(Oferta[] arr) {
- dia = arr;
- }
- public boolean aceptarOferta(Oferta nueva) {
- if (nueva.horaEntrada >= 24 || nueva.horaSalida >= 24) {
- JOptionPane.showMessageDialog(null, "Hora fuera de rango , los dias no tienen mas de 24 horas ");
- return false;
- }
- if (nueva.horaEntrada - nueva.horaSalida == 0) {
- JOptionPane.showMessageDialog(null, "debe reservar como minimo una hora completa , intente nuevamente");
- return false;
- }
- if (nueva.horaEntrada - nueva.horaSalida > 0) {
- JOptionPane.showMessageDialog(null,"la reserva no puede abarcar dos dias distintos , intente nuevamente");
- return false;
- }
- solverGoloso(nueva);
- return true;
- }
- public static void solverGoloso(Oferta nueva){
- if (horasDisponibles(nueva)) {
- ofertaAceptada(nueva);
- }else{
- if(!mejorOpcion(nueva)){
- JOptionPane.showMessageDialog(null,"su oferta no fue registrada pobre asqueroso");
- for (int i = 0; i < dia.length; i++) {
- if (dia[i]==null) {
- System.out.println("null");
- }else{
- dia[i].getNombre();
- }
- }
- }
- }
- if (mejorOpcion(nueva)) {
- liberarHoras();
- ofertaAceptada(nueva);
- }
- }
- private static boolean mejorOpcion(Oferta nueva){
- //ofertas.clear();
- ofertas = new ArrayList<Oferta>();
- boolean ret = true ;
- double dinero =0;
- for (int i = nueva.horaEntrada; i < nueva.horaSalida; i++) {
- if (dia[i]!=null && !ofertas.contains(dia[i])) {
- ofertas.add(dia[i]);
- dinero +=dia[i].dinero;
- }
- if (dinero >= nueva.dinero ) {
- return false ;
- }
- }
- return ret ;
- }
- private static void ofertaAceptada(Oferta nueva) {
- for (int i = nueva.horaEntrada; i < nueva.horaSalida; i++) {
- dia[i]= nueva ;
- }
- JOptionPane.showMessageDialog(null,"Su oferta ingreso correctmente");
- for (int i = 0; i < dia.length; i++) {
- System.out.println(dia[i]);
- }
- }
- public static boolean horasDisponibles(Oferta nueva){
- boolean ret = true;
- for (int i = nueva.horaEntrada; i < nueva.horaSalida; i++) {
- if (dia[i]!=null) {
- ret = false ;
- }
- }
- return ret ;
- }
- static boolean verificarNull(Oferta oferta1, Oferta oferta2){
- if(oferta1 == null || oferta2 == null){
- return false;
- }
- return true;
- }
- static void liberarHoras() {
- for (Oferta oferta : ofertas) {
- for (Oferta ofertaDia : dia) {
- if (verificarNull(oferta, ofertaDia) == true) {
- if (ofertaDia.equals(oferta)) {
- ofertaDia = null;
- }
- }
- }
- }
- ofertas = null;
- }
- //FIN DE LA CLASE--------------------------------------------------------------------------------------------->>
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement