id
stringlengths
36
36
text
stringlengths
1
1.25M
5c99173e-34e4-43f4-ad05-1cff63d603a4
TreeNode(int x) { val = x; }
a4572253-c264-43cc-b137-172781e8172a
public List<Integer> inorderTraversal(TreeNode root) { List<Integer> res = new LinkedList<Integer>(); Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode cur = root; while(!stack.isEmpty() || cur!=null) { if(cur!=null) { stack.push(cur); cur = cur.left; } else{ cur = stack.pop(); res.add(cur.val); cur=cur.right; } } return res; }
146ec1be-1e74-4b97-a04b-2be53a4c82d7
public List<Integer> preorderTraversal(TreeNode root) { List<Integer> res = new LinkedList<Integer>(); if(root==null) return res; Stack<TreeNode> stack = new Stack<TreeNode>(); stack.push(root); while(!stack.isEmpty()) { TreeNode cur = stack.pop(); res.add(cur.val); if(cur.right!=null)stack.push(cur.right); if(cur.left!=null)stack.push(cur.left); } return res; }
d74a09c0-a3f8-4569-83dd-379b70165ec9
public ArrayList<Integer> postorderTraversal(TreeNode root) { TreeNode cur = root; Stack<TreeNode> path = new Stack<TreeNode>(); ArrayList<Integer> res = new ArrayList<Integer>(); if(root ==null) return res; path.add(root); while(!path.isEmpty()) { cur = path.pop(); res.add(0, cur.val); if(cur.left !=null) path.add(cur.left); if(cur.right !=null) path.add(cur.right); } return res; }
8cc04034-ba5a-4b6a-b3c6-e56099fa2c36
public Server(ClientProxy client) { this.client = client; }
3a61a707-7b5b-463f-bede-de4726fb7561
public void serve() throws IOException{ System.out.println("Kickin' ass, handlin' requests."); client.handleRequest(); client.close(); System.out.println("Client Served"); }
26babd2f-88cd-43cf-b7c3-35a6e7aeab25
public static void main(String[] args) { // Kolla vad detta betyder när vi analyserar säkerheten try { RecordHolder rh = new RecordHolder(); String[] patNames = {"A", "B", "C"}; String[] nurses = {"D", "E", "F"}; String[] docNames = {"G", "H", "I"}; String[] divs = {"Vattendroppen", "Solstrålen"}; Doctor[] docs = new Doctor[3]; int index = 0; CertificateFactory certF = CertificateFactory.getInstance("X.509"); for(String name : docNames){ Certificate cert = certF.generateCertificate(new FileInputStream("./keys/server/cert_" + name + ".pem")); docs[index] = new Doctor(name, divs[index/2], cert); rh.addDoctor(docs[index]); index++; } index = 0; rh.addPatient(new Patient(patNames[0], docs[0], certF.generateCertificate(new FileInputStream("./keys/server/cert_" + patNames[0] + ".pem")))); rh.addPatient(new Patient(patNames[1], docs[0], certF.generateCertificate(new FileInputStream("./keys/server/cert_" + patNames[0] + ".pem")))); rh.addPatient(new Patient(patNames[2], docs[2], certF.generateCertificate(new FileInputStream("./keys/server/cert_" + patNames[0] + ".pem")))); rh.addNurse(new Nurse("D", divs[0], certF.generateCertificate(new FileInputStream("./keys/server/cert_D.pem")))); rh.addNurse(new Nurse("E", divs[0], certF.generateCertificate(new FileInputStream("./keys/server/cert_E.pem")))); rh.addNurse(new Nurse("F", divs[1], certF.generateCertificate(new FileInputStream("./keys/server/cert_F.pem")))); rh.setGov(new Government("J", certF.generateCertificate(new FileInputStream("./keys/server/cert_J.pem")))); SSLContext ctx; KeyManagerFactory kmf; KeyStore ks; TrustManagerFactory tmf; char[] passphrase = "jagkan".toCharArray(); ctx = SSLContext.getInstance("SSLv3"); kmf = KeyManagerFactory.getInstance("SunX509"); ks = KeyStore.getInstance("JKS"); tmf = TrustManagerFactory.getInstance("SunX509"); ks.load(new FileInputStream("./keys/server/keystore"), passphrase); kmf.init(ks, passphrase); tmf.init(ks); ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); SSLServerSocketFactory factory = ctx.getServerSocketFactory(); SSLServerSocket serverSocket = (SSLServerSocket)factory.createServerSocket(PORT); serverSocket.setNeedClientAuth(true); System.out.println(rh.personsToString()); System.out.println("Server started. Listening on port " + PORT + "."); SSLSocket clientSocket = (SSLSocket)serverSocket.accept(); ClientProxy client = new ClientProxy(clientSocket, rh); Server server = new Server(client); server.serve(); } catch (UnrecoverableKeyException e){ e.printStackTrace(); } catch (CertificateException e){ e.printStackTrace(); } catch (NoSuchAlgorithmException e){ e.printStackTrace(); } catch (KeyStoreException e){ e.printStackTrace(); } catch (KeyManagementException e){ e.printStackTrace(); } catch (IOException e){ e.printStackTrace(); } }
8f076631-507f-4005-a59e-a46ceda66af1
public static void main(String[] args) { Command cmd = null; if(args[1].equals("add")) { cmd = new AddCommand(Integer.parseInt(args[2]), Integer.parseInt(args[3]), args[4]); } else if (args[1].equals("read")) { cmd = new ReadCommand(Integer.parseInt(args[2])); } else if (args[1].equals("write")) { cmd = new WriteCommand(Integer.parseInt(args[2]), args[3]); } else if (args[1].equals("remove")) { cmd = new RemoveCommand(Integer.parseInt(args[2])); } else { System.err.println("Usage: program command options"); } try{ SSLContext ctx; KeyManagerFactory kmf; KeyStore ks; TrustManagerFactory tmf; char[] passphrase = "jagkan".toCharArray(); ctx = SSLContext.getInstance("SSLv3"); kmf = KeyManagerFactory.getInstance("SunX509"); ks = KeyStore.getInstance("JKS"); tmf = TrustManagerFactory.getInstance("SunX509"); ks.load(new FileInputStream("./keys/client/ks_" + args[0]), passphrase); kmf.init(ks, passphrase); tmf.init(ks); ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); SSLSocketFactory factory = ctx.getSocketFactory(); SSLSocket s = (SSLSocket)factory.createSocket(HOST, PORT); s.startHandshake(); ServerProxy sp = new ServerProxy(s); String returnMessage = sp.sendCommand(cmd); sp.close(); System.out.println(returnMessage); } catch (UnrecoverableKeyException e){ e.printStackTrace(); } catch (CertificateException e){ e.printStackTrace(); } catch (NoSuchAlgorithmException e){ e.printStackTrace(); } catch (KeyStoreException e){ e.printStackTrace(); } catch (KeyManagementException e){ e.printStackTrace(); } catch (IOException e){ e.printStackTrace(); } }
a145ccd8-e9c1-479c-a0e0-73b4c93d3b4c
public ServerProxy(SSLSocket socket){ this.socket = socket; }
6f2ff50e-e0ad-4e1e-b8b9-bcb114081be1
public String sendCommand(Command cmd) throws IOException{ DataOutputStream out = new DataOutputStream(socket.getOutputStream()); DataInputStream in = new DataInputStream(socket.getInputStream()); cmd.write(out); int result = in.readInt(); int messageLength = in.readInt(); byte[] msgBytes = new byte[messageLength]; in.readFully(msgBytes); String message = new String(msgBytes); if (result == 1) { throw new IOException(message); } return message; }
13dace51-e653-41b0-a15d-80efeff01a85
public String sayHello() throws IOException{ DataOutputStream out = new DataOutputStream(socket.getOutputStream()); DataInputStream in = new DataInputStream(socket.getInputStream()); System.out.println("Writing: " + 4); out.writeInt(4); int result = in.readInt(); int messageLength = in.readInt(); System.out.println("Read: " + result + " " + messageLength); return "" + result + " " + messageLength; }
f7760216-d08c-49aa-8b6a-fe2d7f201cf6
public void close() throws IOException { socket.close(); }
7331a4ff-b983-4d69-9c11-5eb7613a0540
public ClientProxy(SSLSocket socket, RecordHolder rh) { this.socket = socket; this.recordHolder = rh; }
c9368278-bd5e-47af-a7df-8df00a8b68d1
public void handleRequest() { try { SSLSession session = socket.getSession(); Certificate cert = session.getPeerCertificates()[0]; DataOutputStream out = new DataOutputStream(socket.getOutputStream()); DataInputStream in = new DataInputStream(socket.getInputStream()); Command cmd = Command.parseCommand(in); System.out.println("Command parsed: " + cmd); cmd.execute(out, cert, recordHolder); System.out.println("Command Executed"); out.flush(); } catch (IOException e) { e.printStackTrace(); } }
9d0df3b5-d4f0-4733-8402-161646eef016
public void close() throws IOException { System.out.println("Closing Socket"); socket.close(); }
a35f82e8-4e09-470f-9aee-7780ce5c1d24
public Patient(String name, Doctor doctor, Certificate cert){ super(name, cert); this.doctor = doctor; }
7ad17295-2b3b-4c93-beb4-78111e1aba0b
public boolean allowedToRead(Record r){ return (r.getPatient().equals(this)); }
027afe57-5770-432e-beff-92673b0cccfc
public Doctor getDoctor(){ return doctor; }
66be90e0-f57b-4809-ae72-576168e9a5eb
protected String type(){ return "Patient"; }
ab5ba21c-42e8-42a1-9dd0-4f4a6412020b
public RemoveCommand(int record) { super(); this.record = record; }
e2862799-4c14-4a30-987d-359a1b4a803b
public void write(DataOutputStream out) throws IOException { out.writeInt(REMOVE); out.writeInt(record); }
e6cb32ae-d6fb-4b05-84fd-a16ea6b828f5
public void execute(DataOutputStream out, Certificate cert, RecordHolder records) throws IOException{ try{ records.removeRecord(cert, record); out.writeInt(0); String msg = "Removed record"; out.writeInt(msg.length()); out.writeBytes(msg); } catch (SecurityException e){ e.printStackTrace(); out.writeInt(1); out.writeInt(e.getMessage().length()); out.writeBytes(e.getMessage()); } }
5e388264-e9a6-4a03-8928-2d17a6028e50
public AddCommand(int patient, int nurse, String division) { super(); this.patient = patient; this.nurse = nurse; this.division = division; // Comment: en doktor som skriver felaktig division kommer bara själv kunna komma åt det igen }
5126284f-49f0-4c8a-b304-af1f9f6998be
public void write(DataOutputStream out) throws IOException { out.writeInt(ADD); out.writeInt(patient); out.writeInt(nurse); out.writeInt(division.length()); out.writeBytes(division); }
3029a39a-fec5-4e82-af7f-d15d459d7222
public void execute(DataOutputStream out, Certificate cert, RecordHolder records) throws IOException{ try{ records.addRecord(cert, patient, nurse, division); out.writeInt(0); String msg = "Added record"; out.writeInt(msg.length()); out.writeBytes(msg); } catch (SecurityException e){ out.writeInt(1); out.writeInt(e.getMessage().length()); out.writeBytes(e.getMessage()); } }
2c3b4edf-b6ac-461b-b6b6-b8cbd99ccb33
public String toString(){ return "add " + patient + " " + nurse; }
cbbd6dc2-72e6-4eb3-a3a7-52c5b36f80e9
private SecureRecord(Patient patient, Nurse nurse, String division) throws IOException{ super(patient, nurse, division); }
da5103be-1f2f-4798-bc77-30fa27777b50
public static SecureRecord createRecord(Person actor, Patient pat, Nurse nur, String div) throws SecurityException, IOException{ if(actor.allowedToCreate(pat)){ log.info("actor: " + actor.getName() + " with id: " + actor.getPersonId() + " created new record"); return new SecureRecord(pat, nur, div); } else { log.warning("actor: " + actor.getName() + " with id: " + actor.getPersonId() + " was denied to create record"); throw new SecurityException("Permission denied!"); } }
6d58d178-f450-476d-80d1-e361717482ef
public void appendData(String data){ }
3adce8dc-10cf-4eec-ada6-81a3eb75cd4d
public void appendData(Person actor, String data) throws SecurityException{ if(actor.allowedToWrite(this)){ super.appendData(data); log.info("actor: " + actor.getName() + " with id: " + actor.getPersonId() + " wrote new data to record: " + id); } else { log.warning("actor: " + actor.getName() + " with id: " + actor.getPersonId() + " was denied access to record: " + id); throw new SecurityException("Permission denied!"); } }
401ab403-3f4c-40ed-9232-4921ef3684ed
public String readData(){ return null; }
d8b35866-ad44-4b8e-b57e-8e8641d9d3fa
public String readData(Person actor) throws SecurityException{ String s = ""; if(actor.allowedToRead(this)){ s = super.readData(); log.info("Person: " + actor.getName() + " with id: " + actor.getPersonId() + " read record: " + id); } else { log.warning("Person: " + actor.getName() + " with id: " + actor.getPersonId() + " was denied access to record: " + id); throw new SecurityException("Permission denied!"); } return s; }
c27f3946-7b4d-4b1c-86b8-1041e7cfeaf5
public void removeData(Person actor) throws SecurityException{ if(actor.allowedToRemove(this)){ super.removeData(); log.info("Person: " + actor.getName() + " with id: " + actor.getPersonId() + " read record: " + id); } else { log.warning("Person: " + actor.getName() + " with id: " + actor.getPersonId() + " was denied access to record: " + id); throw new SecurityException("Permission denied!"); } }
48f1fa88-9e95-4107-86af-d5062ef103e1
public Record(Patient patient, Nurse nurse, String division) throws IOException{ this.patient = patient; this.division = division; this.nurse = nurse; id = RECORD_ID++; FileHandler fh = new FileHandler("./eventlog.log"); log = Logger.getLogger("eventlog"); log.addHandler(fh); }
3cd7cb30-a35e-4b64-b5b3-d9d9fcda0f58
public String getDivision(){ return division; }
840e447c-41ce-4b85-9578-88a7f40ecf42
public Patient getPatient(){ return patient; }
82058a9d-c75d-474a-85be-598e2e0304e1
public Nurse getNurse(){ return nurse; }
072e55a3-7b09-42e2-a4b4-bf7c0e794eb7
public void appendData(String data){ this.data += ";" + data; }
0ba88fdf-5138-429d-80c0-a620408d7903
public void removeData(){ data = ""; }
1c7b15f9-dffa-45a9-a3ec-da9102f07400
public String readData(){ return data; }
2ae2f17b-31ae-487e-af37-61b22db193d5
public int getRecordId(){ return id; }
6c18ff43-26ee-4a81-8419-c6853951aa3b
public Command(){ }
fcf879be-d21c-464e-bc61-19b6510ec948
public static Command parseCommand(DataInputStream in) throws IOException{ Command cmd = null; int type = in.readInt(); int record; int len; byte[] bytes; switch (type){ case ADD: int pat = in.readInt(); int nur = in.readInt(); len = in.readInt(); bytes = new byte[len]; in.readFully(bytes); cmd = new AddCommand(pat, nur, new String(bytes)); break; case WRITE: record = in.readInt(); len = in.readInt(); bytes = new byte[len]; in.readFully(bytes); String data = new String(bytes); cmd = new WriteCommand(record, data); break; case READ: record = in.readInt(); cmd = new ReadCommand(record); break; case REMOVE: record = in.readInt(); cmd = new RemoveCommand(record); break; default: throw new IOException("Could not parse input as a Command."); } return cmd; }
74fb1898-f1e9-4180-9278-338c608a67fe
public abstract void execute(DataOutputStream out, Certificate cert, RecordHolder holder) throws IOException;
70dd5654-ed0c-430d-a512-f0d5455e94de
public abstract void write(DataOutputStream out) throws IOException;
183ed50a-f862-44fd-9082-0777dbb3aaee
public String toString(){ return ""; }
77ffb3a6-c19f-4c8d-9893-85f5c46232b4
public Person(String name, Certificate cert){ this.name = name; id = PERSON_ID++; this.cert = cert; }
2ed441f5-4ec1-4112-9994-2931a26fb62e
public int getPersonId(){ return id; }
205d8147-b11c-4ad1-8fee-31cfb2c7590e
public String getName(){ return name; }
170a840d-64bf-4fc8-b117-442c08aa1357
public boolean allowedToRead(Record record){ return false; }
bccaf0b1-2358-4d88-87d0-1a23d598a26d
public boolean allowedToWrite(Record record){ return false; }
05fcb05a-bba9-4082-aaee-fdbcf20ce2cc
public boolean allowedToRemove(Record record){ return false; }
f65d5257-11fc-4934-b315-95fd24ce0d02
public boolean allowedToCreate(Patient patient){ return false; }
517fc6f5-2b43-4684-adf0-6fb778b543eb
public boolean equals(Person other){ return this.id == other.id; }
71b30d13-f2e4-4fc1-8d14-2d3ed064414f
public boolean hasCertificate(Certificate cert){ return (this.cert.equals(cert)); }
b19920ba-d99f-4c48-bc98-f37f29562f8e
public String toString(){ return id + " " + name + " " + type(); }
e2f09933-9d8c-4abb-a6a5-4eaba19babfa
protected abstract String type();
e1b005c5-e260-49c2-aab8-a24086b6dbc5
public Doctor(String name, String division, Certificate cert){ super(name, cert); this.division = division; }
81b7dc01-1260-4885-983e-5d3c23cf1649
public boolean allowedToRead(Record r){ Patient p = r.getPatient(); return (p.getDoctor().equals(this) || r.getDivision().equals(division)); }
f86266e5-984a-40ce-b23f-16d8c57c7823
public boolean allowedToWrite(Record r){ return (r.getPatient().getDoctor().equals(this)); }
9977ee31-b5f6-452f-8f3b-dab94aae57d5
public boolean allowedToCreate(Patient patient){ return (patient.getDoctor().equals(this)); }
d36ada6a-e414-4e65-8b33-69552caf50e7
protected String type(){ return "Doctor"; }
b317aa45-1b03-4141-a08b-61eb1f161a07
public Nurse(String name, String division, Certificate cert){ super(name, cert); this.division = division; }
4e24e9d8-0bcf-40a5-95b0-38783ae3a656
public boolean allowedToRead(Record record){ return (record.getNurse().equals(this) || record.getDivision().equals(division)); }
5d2ba8f2-82fd-476d-853b-77c22e0d0744
public boolean allowedToWrite(Record r){ return (r.getNurse().equals(this)); }
dfab52a3-cea6-4705-86b9-ed7dcb328990
protected String type(){ return "Nurse"; }
0d7af3e0-f151-48d4-83bd-1a7cc34a3a47
public ReadCommand(int record) { super(); this.record = record; }
7f7ab81f-2979-4065-aa64-7b71f9539612
public void write(DataOutputStream out) throws IOException { out.writeInt(READ); out.writeInt(record); }
8c1b375c-778a-489c-9a32-d3fa1bf5390f
public void execute(DataOutputStream out, Certificate cert, RecordHolder records) throws IOException{ try{ String data = records.readRecord(cert, record); out.writeInt(0); out.writeInt(data.length()); out.writeBytes(data); } catch (SecurityException e){ e.printStackTrace(); out.writeInt(1); out.writeInt(e.getMessage().length()); out.writeBytes(e.getMessage()); } }
91ffe4e7-ef96-4260-9f47-9b2f0b06d7fe
public Government(String name, Certificate cert) { super(name, cert); }
fe773abc-2893-49d5-8161-abd305d7eb6f
public boolean allowedToRead(Record record) { return true; }
a1f1a650-2a20-4bb5-a8a5-fc5ae438c175
public boolean allowedToRemove(Record record) { return true; }
e01fb210-5f32-4b4a-a90b-ffde7a8fd194
protected String type(){ return "Government"; }
9d931816-1a28-4c30-9123-76c2079b8a0c
public RecordHolder(){ records = new HashMap<Integer, SecureRecord>(); patients = new HashMap<Integer, Patient>(); nurses = new HashMap<Integer, Nurse>(); //doctors = new HashMap<Integer, Doctor>(); persons = new HashMap<Integer, Person>(); }
4110403a-2ef0-46b2-a150-6b73996c7970
private void addRecord(SecureRecord r, Doctor doctor){ records.put(r.getRecordId(), r); }
c45602ec-7b00-4c08-8e20-72211c263679
private Person getPerson(Certificate cert) throws SecurityException{ for(Person p : persons.values()){ if(p.hasCertificate(cert)){ return p; } } //TODO log this shit throw new SecurityException("Unknown certificate."); }
8b1b5a98-3e20-43d7-87a1-cc7606f7b73a
public void addRecord(Certificate cert, int patientID, int nurseID, String division) throws SecurityException, IOException { Person actor = getPerson(cert); Patient patient = patients.get(patientID); Nurse nurse = nurses.get(nurseID); SecureRecord secrec = SecureRecord.createRecord(actor, patient, nurse, division); records.put(secrec.getRecordId(), secrec); }
12320068-8501-4d27-8962-23daed29dc03
public String readRecord(Certificate cert, int id) throws SecurityException{ return records.get(id).readData(getPerson(cert)); }
fa437b6f-85c1-46cd-979f-c7ae93325ccb
public void writeToRecord(Certificate cert, int recordId, String s) throws SecurityException{ Person actor = getPerson(cert); records.get(recordId).appendData(actor, s); }
d339d58b-853c-4d24-9965-6ad0bc1025d6
public void removeRecord(Certificate cert, int recordId) throws SecurityException{ Person actor = getPerson(cert); records.get(recordId).removeData(actor); records.remove(recordId); }
faf11e40-85ab-476f-917e-da962b492a85
public void addPatient(Patient patient){ patients.put(patient.getPersonId(), patient); persons.put(patient.getPersonId(), patient); }
a98b43fe-edaf-43fa-bb9c-6c4f0d804eae
public void addNurse(Nurse nurse){ nurses.put(nurse.getPersonId(), nurse); persons.put(nurse.getPersonId(), nurse); }
5a4e29f9-bd52-4499-bc65-47cae2fa9976
public void addDoctor(Doctor doctor){ persons.put(doctor.getPersonId(), doctor); }
e580ee3c-593e-4863-acac-0422186eb437
public void setGov(Government gov){ persons.put(gov.getPersonId(), gov); }
cd79e109-9eff-4588-a92f-0bd6cc812d91
public String personsToString(){ StringBuilder sb = new StringBuilder(); for(Person p : persons.values()){ sb.append(p); sb.append("\n"); } return sb.toString(); }
4c0aba53-5c4a-4f05-a990-6087262c026c
public WriteCommand(int record, String data) { super(); this.record = record; this.data = data; }
ed2ef0fb-4fb8-41c4-bf2e-1cca5c53bdc3
public void write(DataOutputStream out) throws IOException { out.writeInt(WRITE); out.writeInt(record); out.writeInt(data.length()); out.writeBytes(data); }
36a07182-cd7f-486f-995a-bf97ca4c768c
public void execute(DataOutputStream out, Certificate cert, RecordHolder records) throws IOException { try{ records.writeToRecord(cert, record, data); out.writeInt(0); String msg = "Wrote to record"; out.writeInt(msg.length()); out.writeBytes(msg); } catch (SecurityException e){ e.printStackTrace(); out.writeInt(1); out.writeInt(e.getMessage().length()); out.writeBytes(e.getMessage()); } }
dfc6554b-f10b-466e-847f-a59d0c7778f9
public Money(long dollars, int cents, final boolean negative) { isNegative = negative; // Takes Absolute Value of dollars dollars = Math.abs(dollars); cents = Math.abs(cents); if (cents > 100) { // Rounds cents StringBuilder b = new StringBuilder(); String s = cents + ""; b.append(s.charAt(0)); b.append(s.charAt(1)); int i = Integer.parseInt(b.toString()); if (s.charAt(2) - '0' >= 5) { i++; } if (i == 100) { i = 0; dollars++; } this.cents = i; } else { this.cents = cents; } this.dollars = dollars; }
9941e57d-38a7-479d-a374-e975a856d69d
public static Money add(final Money first, final Money... varArgs) { long sum = 0; sum += getExpandedLong(first); for (Money m : varArgs) { sum += getExpandedLong(m); } return parseMoney(sum); }
5b888f70-e0ef-49f2-9fb7-88043643c3db
public static long getExpandedLong(final Money m) { long raw = m.getDollars() * 100 + m.getCents(); if (m.isNegative) { raw *= -1; } return raw; }
86bbb7d1-6159-482a-880a-6b1efbb1ce14
public static Money parseMoney(final long longAmt) { boolean neg = false; if (longAmt < 0) { neg = true; } return new Money(longAmt / 100, (int) (longAmt % 100), neg); }
085f8d7a-14cd-4027-b11e-f2ac9c299b50
public static Money parseMoney(String inputString) { long dollars; int cents; int numOfNegs = 0; boolean neg; for (char c : inputString.toCharArray()) { if (c == '-') { numOfNegs++; } } if (numOfNegs % 2 == 0) { neg = false; } else { neg = true; } inputString = inputString.replaceAll("[^0-9.]+", ""); String[] listOfNumbersString; if (inputString.contains(".") && !inputString.substring(inputString.indexOf(".") + 1).equals("")) { listOfNumbersString = inputString.split("\\."); dollars = Long.parseLong(listOfNumbersString[0]); cents = Integer.parseInt(listOfNumbersString[1]); } else { inputString = inputString.replaceAll("\\.", ""); dollars = Long.parseLong(inputString); cents = 0; } return new Money(dollars, cents, neg); }
bc44d954-e851-4d2e-a803-dc2e255098f0
@Override public boolean equals(final Object o) { if (o == null || !(o instanceof Money)) { return false; } Money m = (Money) o; if (dollars != m.getDollars() || cents != m.getCents() || isNegative != m.isNegative) { return false; } else { return true; } }
cc5dc340-0aa5-4962-95ad-29924e0f2604
public int getCents() { return cents; }
d3a540d3-0257-4c66-b525-a3d13b662e06
public long getDollars() { return dollars; }
6e6fe88e-247c-45dc-bc25-dcd9221a28d5
@Override public int hashCode() { int hash = Long.valueOf(dollars).hashCode() * 9 + Integer.valueOf(cents).hashCode(); if (isNegative) { hash *= -1; } return hash; }
db24a079-d708-4792-b1a9-f01f463cb2cb
public Money negate() { isNegative = !isNegative; return this; }
864ecc13-6cf8-4165-b7f6-d473c58043ff
public Money negateTemporarily() { return new Money(dollars, cents, !isNegative); }
e93d6727-f44f-4a5a-a98f-ed612a16bcab
@Override public String toString() { StringBuilder b = new StringBuilder(); if (isNegative) { b.append("-"); } b.append("$"); b.append(getDollars()); b.append("."); if (getCents() < 10) { b.append("0"); } b.append(getCents()); return b.toString(); }