Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
16 kB
3
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.lang.Double;

class Member {
	private String name;
	private String member_type;
	private ArrayList<Message> sentMessages;
	private ArrayList<Message> receivedMessages;
	public ArrayList<Transaction> sentTransactions;
	public ArrayList<Transaction> receivedTransactions;

	public String getName() {
		return name;
	}

	public String getMemberType() {
		return member_type;
	}

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

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

	public int countTransactionsSent() {
		return sentTransactions.size();
	}

	public int countTransactionsReceived() {
		return receivedTransactions.size();
	}

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

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

	public void addSentTransactions(Transaction transaction) {
		sentTransactions.add(transaction);
	}

	public void addreceivedTransactions(Transaction transaction) {
		receivedTransactions.add(transaction);
	}

	public double countAverageDelay() {
		double sum = 0;
		for (Transaction t : sentTransactions) {
			sum += t.getDelay();
		}
		sum = (double) sum / sentTransactions.size();
		return sum;
	}

	public double countSumDelay() {
		double sum = 0;
		for (Transaction t : sentTransactions) {
			sum += t.getDelay();
		}
		return sum;
	}
}


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;
        this.cardId = cardId;
        this.stateCode = stateCode;
        this.lastName = lastName;
        this.firstName = firstName;
        this.dateOfBirth = LocalDate.parse(dateOfBirth);
    }

    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 Transaction {
	private Member sender;
	private Member replier;
	private Member administrator;
	private boolean isDelayed;
	private double delay;

	public Transaction(Member sender, Member replier, Member administrator, boolean isDelayed, double delay) {
		this.sender = sender;
		this.replier = replier;
		this.administrator = administrator;
		this.isDelayed = isDelayed;
		this.delay = delay;
	}

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

	public double getDelay() {
		return this.delay;
	}
}


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());

			// 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();
				switch (member_type) {
					case "EnforcementAuthority":
						member_name = scan.next();
						// members.add(new EnforcementAuthority());
						break;
					case "Administrator":
						member_name = scan.next();
						// members.add(new Administrator());
						// administrators.add(new Administrator());
						break;
					case "MemberState":
						member_state_code = scan.next();
						member_name = scan.next();
						// 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 = Integer.parseInt(scan.nextLine());

			// 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();
				driverCards.add(new DriverCard(driver_card_id, first_name, last_name, date_of_birth, driver_member_state_code, card_status));
			}

			// read command input

			num_of_command = Integer.parseInt(scan.nextLine());

			// 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.next();
				switch (command_type) {
					case "sendMessage":
						String sender_name = scan.next();
						String receiver_name = scan.next();
						String driverCardId = scan.next();
						sendMessage(sender_name, receiver_name, driverCardId);
						break;
					case "countMessage":
						String memberName = scan.next();
						String message_type = scan.next();
						countMessage(memberName, message_type);
						break;
					case "transaction":
						String sender_name = 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();
						ArrayList<String> alert_receivers = new ArrayList<String>();
						while (scan.hasNext()) {
							alert_receivers.add(scan.next());
						}
						Transaction(sender_name, replier_name, administrator_name, is_successful, delay, delay_threshold, alert_receivers);
						break;
					case "getStatisticsOfUse":
						String member_name_1 = scan.next();
						String member_name_2 = scan.next();
						getStatisticsOfUse(member_name_1, member_name_2);
						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":
						String member_name = scan.next();
						int start_id = Integer.parseInt(scan.next());
						int end_id = Integer.parseInt(scan.next());
						getStandardDelayOfReplyByPeriod(member_name, start_id, end_id);
						break;
					case "getPercentageOfUnsuccessfulTransaction":
						String member_name = scan.next();
						getPercentageOfUnsuccessfulTransaction(member_name);
						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");
			return;
		}

		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, ArrayList<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");
			return;
		}

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

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

		if (admin.getMemberType() != "administrator") {
			java.lang.System.out.println("Only administrators can administer a transaction");
			return;
		}

		if (sender.getName() == replier.getName() || sender.getName() == admin.getName() || admin.getName() == replier.getName()) {
			java.lang.System.out.println("The sender, replier, and administrator should not be the same member.");
			return;
		}

		double delay_ = new Double(delay).doubleValue();
		double delay_threshold_ = new Double(delay_threshold_).doubleValue();
		if (delay_ > delay_threshold_) {
			for (String s : alert_receivers) {
				if (s == "sender") {
					java.lang.System.out.println("Alert sender " + sender_name + ": transaction is delayed");
				}
				if (s == "replier") {
					java.lang.System.out.println("Alert replier " + replier_name + ": transaction is delayed");
				}
				if (s == "administrator") {
					java.lang.System.out.println("Alert administrator " + administrator_name + ": transaction is delayed");
				}
			}
		}

		sender.addSentTransactions(new Transaction(sender, replier, admin, delay_ > delay_threshold_, delay_));
		receiver.addreceivedTransactions(new Transaction(sender, replier, admin, delay_ > delay_threshold_, delay_));
	}

	// 4
	private static void getStatisticsOfUse(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");
			return;
		}

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

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

		// output
		java.lang.System.out.println("Transaction sent by " + member_name_2 + ": " + member2.countTransactionsSent());
		java.lang.System.out.println("Transaction replied by " + member_name_2 + ": " + member2.countTransactionsReceived());
	}

	// 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");
			return;
		}

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

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

		// delay
		java.lang.System.out.println("%.2f", "Average delay: " + member2.countAverageDelay());

	}

	private static void getStandardDelayOfReplyByPeriod(String member_name, int start_id, int end_id) {
		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");
			return;
		}	

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

		if (end_id < start_id || start_id < 0 || ) {
			java.lang.System.out.println("Illegal period");
			return;
		}

		double average_delay = member.countSumDelay() / (end_id - start_id + 1);
		java.lang.System.out.println("%.2f", "Average delay: " + average_delay);
	}
}
Leave a Comment