Skip to content

Commit

Permalink
exit Marlis
Browse files Browse the repository at this point in the history
  • Loading branch information
MarlisDW committed Jul 5, 2024
1 parent 389c0b4 commit 50351c1
Show file tree
Hide file tree
Showing 4 changed files with 310 additions and 49 deletions.
55 changes: 24 additions & 31 deletions backend/financialLogic.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function calculateMonthlySavings(userRecord, month) {
return income - spending;
}

function generateResponse(message, userInfo, userContext) {
function Response(message, userInfo, userContext) {
if (!financialData) {
return 'Financial data not available.';
}
Expand All @@ -70,30 +70,23 @@ function generateResponse(message, userInfo, userContext) {

const lowerCaseMessage = message.toLowerCase();

if (lowerCaseMessage.includes('exit')) {
userContext.topic = null;
return 'You have exited the current topic. You can now choose a new topic: spendings, savings, overview stocks, income, other';
}

if (!userContext.topic) {
if (lowerCaseMessage.includes('spending')) {
userContext.topic = 'spending';
return 'What would you like to know about spendings? (e.g., total spendings, spendings for a specific month, spendings for a specific year)';
} else if (lowerCaseMessage.includes('savings')) {
userContext.topic = 'savings';
return 'What would you like to know about savings? (e.g., total savings, savings for a specific month)';
} else if (lowerCaseMessage.includes('overview')) {
userContext.topic = 'overview';
return 'What would you like to know? stocks, real estate, savings account, bonds, retirement funds. '
} else if (lowerCaseMessage.includes('income')) {
userContext.topic = 'income';
return 'What would you like to know about your income sources? (e.g., job, investments, other)';
} else if (lowerCaseMessage.includes('other')) {
userContext.topic = 'other';
return 'What would you like to know? (e.g., mortgage, debt, tax,...)';
} else {
return 'Please specify what you would like to know about (e.g., spendings, savings, overview stocks, income, other).';
}
if (userContext.topic !== 'spending' && message.includes('spending')) {
userContext.topic = 'spending';
return 'What would you like to know about spendings? (e.g., total spendings, spendings for a specific month, spendings for a specific year)';
} else if (userContext.topic !== 'savings' && message.includes('savings')) {
userContext.topic = 'savings';
return 'What would you like to know about savings? (e.g., total savings, savings for a specific month)';
} else if (userContext.topic !== 'overview' && message.includes('overview')) {
userContext.topic = 'overview';
return 'What would you like to know? stocks, real estate, savings account, bonds, retirement funds. '
} else if (userContext.topic !== 'income' && message.includes('income')) {
userContext.topic = 'income';
return 'What would you like to know about your income sources? (e.g., job, investments, other)';
} else if (userContext.topic !== 'other' && message.includes('other')) {
userContext.topic = 'other';
return 'What would you like to know? (e.g., mortgage, debt, tax,...)';
} else if (!userContext.topic) {
return 'Please specify what you would like to know about (e.g., spendings, savings, overview stocks, income, other).';
} else if (userContext.topic === 'spending') {
if (lowerCaseMessage.includes('total')) {
const totalSpending = Object.values(userRecord.spending).reduce((acc, val) => acc + val, 0);
Expand All @@ -108,14 +101,16 @@ function generateResponse(message, userInfo, userContext) {
const spending = userRecord.spendingOverYears[query];
return `Your spendings for ${query} are $${spending}.`;
} else {
return `I didn't understand that. Please specify a valid month or year.`;
return `I didn't understand that. Please specify a valid month or year. \nIf you want to know 2024 please use the keyword 'total'.`;
}
} else if (lowerCaseMessage.includes('from') && lowerCaseMessage.includes('to')) {
const [startMonth, endMonth] = lowerCaseMessage.split('from')[1].split('to').map(m => m.trim());
const totalSpending = calculateTotalSpending(userRecord, startMonth, endMonth);
return `Your total spendings from ${startMonth} to ${endMonth} are $${totalSpending}.`;
} else if (lowerCaseMessage.includes('savings')) {
userContext.topic = null;
} else {
return 'I didn\'t understand that. Please specify total spendings or spendings for a particular month, year, or range.';
return 'I didn\'t understand that. Please specify total spendings or spendings for a particular month, year, or range. \nUse the words total, for [month/year] or from [month/year] to [month/year]';
}
} else if (userContext.topic === 'savings') {
if (lowerCaseMessage.includes('total')) {
Expand All @@ -131,7 +126,7 @@ function generateResponse(message, userInfo, userContext) {
return `I didn't understand that. Please specify a valid month.`;
}
} else {
return 'I didn\'t understand that. Please specify total savings or savings for a particular month.';
return 'I didn\'t understand that. Please specify total savings or savings for a particular month. \nUse the words total or for.';
}
} else if (userContext.topic === 'income') {
const incomeSources = userRecord.incomeSources;
Expand Down Expand Up @@ -184,8 +179,6 @@ function generateResponse(message, userInfo, userContext) {
return `${userRecord.name}'s car loan details: ${JSON.stringify(userRecord.carLoan)}`;
case 'studentLoan':
return `${userRecord.name}'s student loan details: ${JSON.stringify(userRecord.studentLoan)}`;
case 'howFucked':
return `${userRecord.name}'s status: ${JSON.stringify(userRecord.HowFuckedAmI)}`;
case 'canIAffordRent':
return `${userRecord.name}'s rent status: ${JSON.stringify(userRecord.CanIAffordRent)}`;
case 'canIAffordToLive':
Expand All @@ -206,5 +199,5 @@ module.exports = {
loadFinancialData,
loadKeywords,
getFinancialData,
generateResponse
Response
};
30 changes: 15 additions & 15 deletions backend/financial_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"id": 1,
"name": "Saad",
"spendingOverYears": {
"2022": 5000,
"2023": 6000,
"2024": 7000
"2021": 5000,
"2022": 6000,
"2023": 7000
},
"savings": 5000,
"spending": {
Expand Down Expand Up @@ -62,9 +62,9 @@
"retirementFund": 35000
},
"spendingOverYears": {
"2022": 6000,
"2023": 7000,
"2024": 8000
"2021": 6000,
"2022": 7000,
"2023": 8000
},
"imageFiles": ["asset_graph.png", "spending_graph.png"],
"savings": 7000,
Expand Down Expand Up @@ -111,9 +111,9 @@
"retirementFund": 40000
},
"spendingOverYears": {
"2022": 7000,
"2023": 8000,
"2024": 9000
"2021": 7000,
"2022": 8000,
"2023": 9000
},
"imageFiles": ["asset_graph.png", "spending_graph.png"],
"savings": 10000,
Expand Down Expand Up @@ -152,9 +152,9 @@
"retirementFund": 45000
},
"spendingOverYears": {
"2022": 8000,
"2023": 9000,
"2024": 10000
"2021": 8000,
"2022": 9000,
"2023": 10000
},
"imageFiles": ["asset_graph.png", "spending_graph.png"],
"savings": 15000,
Expand Down Expand Up @@ -193,9 +193,9 @@
"retirementFund": 50000
},
"spendingOverYears": {
"2022": 9000,
"2023": 10000,
"2024": 11000
"2021": 9000,
"2022": 10000,
"2023": 11000
},
"imageFiles": ["asset_graph.png", "spending_graph.png"],
"savings": 20000,
Expand Down
6 changes: 3 additions & 3 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const http = require('http');
const socketIo = require('socket.io');
const bodyParser = require('body-parser');
const path = require('path');
const { loadFinancialData, loadKeywords, getFinancialData, generateResponse } = require('./financialLogic.js');
const { loadFinancialData, loadKeywords, getFinancialData, Response } = require('./financialLogic.js');

const app = express();
const server = http.createServer(app);
Expand Down Expand Up @@ -47,15 +47,15 @@ io.on('connection', (socket) => {
const financialData = getFinancialData();
const userRecord = financialData.users.find(u => u.name.toLowerCase() === userInfo.name.toLowerCase() && u.id === userInfo.id);
if (userRecord) {
socket.emit('message', `Hello, ${userInfo.name}! How can I assist you today? Would you like to ask about:\n- Spendings\n- Savings\n- Overview stocks\n- Income\n- Other\n by typing exit you leave there current questions.`);
socket.emit('message', `Hello, ${userInfo.name}! How can I assist you today? Would you like to ask about:\n- Spendings\n- Savings\n- Overview stocks\n- Income\n- Other`);
} else {
socket.emit('message', 'Sorry, the provided ID is incorrect for the name you provided.');
userInfo = {}; // Reset userInfo
socket.emit('message', 'Please provide your name again.');
}
} else {
// Once name and ID are provided, handle the user's queries
const response = generateResponse(message, userInfo, userContext);
const response = Response(message, userInfo, userContext);
socket.emit('message', response);
}
});
Expand Down
Loading

0 comments on commit 50351c1

Please sign in to comment.