Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
14 kB
2
Indexable
Never
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
import java.util.HashSet;
import java.io.File;
import java.io.FileNotFoundException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.time.format.DateTimeFormatter;

class Member {
	private String name;
	private String member_type;
	private ArrayList<Message> sentMessages;
	private ArrayList<Message> receivedMessages;

	public String getName() {
		return name;
	}

	public String getMemberType() {
		return member_type;
	}

	public int countMessagesSent() {
		return sentMessages.size();
	}

	public int countMessagesReceived() {
		return receivedMessages.size();
	}

	public void addSentMessages(Message message) {
		sentMessages.add(message);
	}

	public void addreceivedMessages(Message message) {
		receivedMessages.add(message);
	}
}

class Administrator extends Member {
	// public Integer extractStatisticsOfUse() {

	// }

	// public Double extractStandardDelayOfReplyByMember(Member member) {
		
	// }

	// public Double extractStandardDelayOfReplyByPeriod(int startId, int endId) {
		
	// }

	// public Double extractPercentageOfUnsuccessfulTransaction(Member member) {
		
	// }
}

class User extends Member {

}

class MemberState extends User {
	String memberStateCode;
	private CardIssuingAuthority cardIssuingAuthority;

	// public MemberState(String memberStateCode, String memberName) {
	// 	this.name = memberName;
	// 	this.memberStateCode = memberStateCode;
	// }
	
	// public String askCardStatus() {

	// }

	// public void sendCardStatusModificationRequest() {

	// }
}

class EnforcementAuthority extends User {
	private CardIssuingAuthority cardIssuingAuthority;

	// public String askCardStatus() {

	// }
}

class DriverCard {
	private String status;
    private String cardId;
    private String stateCode;
    private String lastName;
    private String firstName;
    private LocalDate dateOfBirth;

    public DriverCard(String cardId, String firstName, String lastName, String dateOfBirth, String stateCode, String cardStatus) {
		// Transform String to LocalDate
		// Local formatter = new SimpleDateFormat("yyyy/MM/dd");
		// formatter.setLenient(false);
		// formatter.parse("2023/12/19");
        this.status = cardStatus;
		java.lang.System.out.println(this.status);
        this.cardId = cardId;
		java.lang.System.out.println(this.cardId);
        this.stateCode = stateCode;
		java.lang.System.out.println(this.stateCode);
        this.lastName = lastName;
		java.lang.System.out.println(this.lastName);
        this.firstName = firstName;
		java.lang.System.out.println(this.firstName);
		DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy/MM/dd");
		java.lang.System.out.println(LocalDate.parse(dateOfBirth, format));
        this.dateOfBirth = LocalDate.parse(dateOfBirth, format);
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getCardId() {
    	return cardId;
    }
}

class Message {
	private Member sender;
    private Member receiver;
    private String content;

    public Message(Member sender, Member receiver, String content) {
        this.sender = sender;
        this.receiver = receiver;
        this.content = content;
    }
}

class System {
	private List<DriverCard> driverCards;
	private List<Member> members;
	private Map<Transaction, Set<DelayRule>> transactions;

	public void sendAlertMessage(Member receiver) {

	}

	public void checkTransactionDelay() {
		for (Map.Entry<Transaction, Set<DelayRule>> entry : transactions.entrySet()) {
			Transaction transaction = entry.getKey();
			Set<DelayRule> delayRules = entry.getValue();

		}
	}
}

class Transaction {
	private Member sender;
	private Member replier;
	private Member administrator;
	private boolean isDelayed;
	private boolean isUnsuccessful;
	private Integer statisticsOfUse;
	private double delayOfReply;

	public boolean isDelayed() {
		return this.isDelayed;
	}

	public void statisfyDelayRule() {

	}
}

class CardIssuingAuthority {
	private MemberState memberState;

	public void modifyCardStatu(DriverCard driverCard, Integer newStatus) {

	}

	public void issueCard() {

	}
}

class DelayRule {

}


public class TachoNetSystem {
	private static int num_of_member;
	private static int num_of_driver_card;
	private static int num_of_command;
	private static ArrayList<Member> members = new ArrayList<Member>();
	private static ArrayList<DriverCard> driverCards = new ArrayList<DriverCard>();
	private static Member initMember = new Member();

	public static void main(String[] args) {
		// read input
		if (args.length == 0) {
			throw new ArrayIndexOutOfBoundsException("There is no input file.");
		}
		File fileObj = new File(args[0]);
		try {
			Scanner scan = new Scanner(fileObj);

			// read member input

			num_of_member = Integer.parseInt(scan.nextLine());
			java.lang.System.out.println(num_of_member);

			// check if [num_of_member] is nonnegative or not
			if (num_of_member < 0) {
				java.lang.System.out.println("Error");
			}

			String member_type;
			String member_state_code;
			String member_name;

			for (int i = 0 ; i < num_of_member ; i++) {
				member_type = scan.next();
				// java.lang.System.out.println(member_type);
				switch (member_type) {
					case "EnforcementAuthority":
						member_name = scan.next();
						// java.lang.System.out.println(member_name);
						// members.add(new EnforcementAuthority());
						break;
					case "Administrator":
						member_name = scan.next();
						// java.lang.System.out.println(member_name);
						// members.add(new Administrator());
						// administrators.add(new Administrator());
						break;
					case "MemberState":
						member_state_code = scan.next();
						member_name = scan.next();
						// java.lang.System.out.println(member_state_code);
						// java.lang.System.out.println(member_name);
						// members.add(new MemberState());
						break;
					default:
						// TODO
						java.lang.System.out.println("Error");
						break;
				}
			}

			HashSet<String> driverCards_id = new HashSet<String>();

			// read driver card input

			num_of_driver_card = scan.nextInt();

			// check if [num_of_driver_card] is nonnegative or not
			if (num_of_driver_card < 0) {
				java.lang.System.out.println("Error");
			}

			String driver_card_id;
			String first_name;
			String last_name;
			String date_of_birth;
			String driver_member_state_code;
			String card_status;


			for (int i = 0 ; i < num_of_driver_card ; i++) {
				driver_card_id = scan.next();
				// TODO
			 	// check driver_card_id
				first_name = scan.next();
				last_name = scan.next();
				date_of_birth = scan.next();
				driver_member_state_code = scan.next();
				card_status = scan.next();
				java.lang.System.out.println(driver_card_id);
				java.lang.System.out.println(first_name);
				java.lang.System.out.println(last_name);
				java.lang.System.out.println(date_of_birth);
				java.lang.System.out.println(driver_member_state_code);
				java.lang.System.out.println(card_status);
				DriverCard newCard = new DriverCard(driver_card_id, first_name, last_name, date_of_birth, driver_member_state_code, card_status);
				java.lang.System.out.println(newCard);
				driverCards.add(newCard);
				java.lang.System.out.println(driverCards.size());
			}

			// read command input

			num_of_command = scan.nextInt();

			// check if [num_of_command_input] is nonnegative or not
			if (num_of_command < 0) {
				java.lang.System.out.println("Error");
			}

			String command_type;

			for (int i = 0 ; i < num_of_command ; i++) {
				command_type = scan.nextLine();
				switch (command_type) {
					case "sendMessage":
						String sender_nameS = scan.next();
						String receiver_name = scan.next();
						String driverCardId = scan.next();
						sendMessage(sender_nameS, receiver_name, driverCardId);
						break;
					case "countMessage":
						String memberName = scan.next();
						String message_type = scan.next();
						countMessage(memberName, message_type);
						break;
					case "transaction":
						String sender_nameT = scan.next();
						String replier_name = scan.next();
						String administrator_name = scan.next();
						String is_successful = scan.next();
						String delay = scan.next();
						String delay_threshold = scan.next();
						String alert_receivers = scan.next();
						if (scan.hasNext()) {
							java.lang.System.out.println("Error");
						} else {
							Transaction(sender_nameT, replier_name, administrator_name, is_successful, delay, delay_threshold, alert_receivers);
						}
						break;
					case "getStatisticsOfUse":

						break;
					case "getStandardDelayOfReplyByMember":
						String member_name_1 = scan.next();
						String member_name_2 = scan.next();
						getStandardDelayOfReplyByMember(member_name_1, member_name_2);
						break;
					case "getStandardDelayOfReplyByPeriod":
						break;

					case "getPercentageOfUnsuccessfulTransaction":

						break;

					case "addNewMember":

						break;

					default:
						// TODO
						java.lang.System.out.println("Error");
						break;
				}
			}
			
			scan.close();
		} catch (Exception ex) {

		}
	}

	private static void sendMessage(String sender_name, String receiver_name, String driver_card_id) {
		boolean foundSenderName = false;
		boolean foundReceiverName = false;
		boolean foundDriverCardId = false;
		Member sender = initMember;
		Member receiver = initMember;

		for (Member member : members) {
			if (member.getName() == sender_name) {
				foundSenderName = true;
				sender = member;
				break;
			}

			if (member.getName() == receiver_name) {
				foundReceiverName = true;
				receiver = member;
				break;
			}
		}

		for (DriverCard driverCard : driverCards) {
			if (driverCard.getCardId() == driver_card_id) {
				foundDriverCardId = true;
				break;
			}
		}

		// handle errors

		if (foundSenderName == false) {
			java.lang.System.out.println("Member " + sender_name + " does not exist");
			return;
		}

		if (foundReceiverName == false) {
			java.lang.System.out.println("Member " + receiver_name + " does not exist");
			return;
		}

		if (sender_name == receiver_name) {
			java.lang.System.out.println("Member " + sender_name + " cannot send messages to itself");
			return;
		}

		if (foundDriverCardId == false) {
			java.lang.System.out.println("Driver card " + driver_card_id + " does not exist");
			return;
		}

		// send message
		Message message = new Message(sender, receiver, driver_card_id);
		sender.addSentMessages(message);
		receiver.addreceivedMessages(message);
	}

	private static void countMessage(String member_name, String message_type) {
		boolean foundMemberName = false;
		Member member = initMember;
		for (Member m : members) {
			if (m.getName() == member_name) {
				foundMemberName = true;
				member = m;
			}
		}

		// handle errors

		if (foundMemberName == false) {
			java.lang.System.out.println("Member " + member_name + " does not exist");
		}

		if (message_type != "SENT" || message_type != "RECEIVED") {
			java.lang.System.out.println("Illegal message type " + message_type);
			return;
		}

		// countMessage
		if (message_type == "SENT") {
			java.lang.System.out.println("Sent: " + member.countMessagesSent());
			return;
		} else if (message_type == "RECEIVED") {
			java.lang.System.out.println("Sent: " + member.countMessagesReceived());
			return;
		}
	}

	private static void Transaction(String sender_name, String replier_name, String administrator_name, String is_successful, String delay, String delay_threshold, String alert_receivers) {
		Member sender = initMember;
		Member replier = initMember;
		Member admin = initMember;
		boolean foundSender = false;
		boolean foundReplier = false;
		boolean foundAdmin = false;

		for (Member m : members) {
			if (m.getName() == sender_name) {
				foundSender = true;
				sender = m;
			}
			if (m.getName() == replier_name) {
				foundReplier = true;
				replier = m;
			}
			if (m.getName() == administrator_name) {
				foundAdmin = true;
				admin = m;
			}
		}

		if (foundSender == false) {
			java.lang.System.out.println("Member " + sender_name + " does not exist");
		}

		if (foundReplier == false) {
			java.lang.System.out.println("Member " + replier_name + " does not exist");
		}

		if (foundAdmin == false) {
			java.lang.System.out.println("Member " + administrator_name + " does not exist");
		}

	}

	// 5
	private static void getStandardDelayOfReplyByMember(String member_name_1, String member_name_2) {
		boolean foundMemberName1 = false;
		boolean foundMemberName2 = false;
		Member member1 = initMember;
		Member member2 = initMember;
		for (Member m : members) {
			if (m.getName() == member_name_1) {
				foundMemberName1 = true;
				member1 = m;
			}
			if (m.getName() == member_name_2) {
				foundMemberName2 = true;
				member2 = m;
			}
		}

		// handle errors

		if (foundMemberName1 == false) {
			java.lang.System.out.println("Member " + member_name_1 + " does not exist");
		}

		if (foundMemberName2 == false) {
			java.lang.System.out.println("Member " + member_name_2 + " does not exist");
		}

		if (member1.getMemberType() != "administrator") {
			java.lang.System.out.println("Only administrators can get standard delay of reply");
		}

		// delay
	}
}
Leave a Comment