Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*** Please Create a simple java program with these options(please do not use sql

ID: 3604477 • Letter: #

Question

*** Please Create a simple java program with these options(please do not use sql or http things inside the code it is a simple java program without gui) :****

#1 Register a company : Create username and password, company name, stock price and the time

#2 Login a registered user : after entering the password a message apears " your login is successfull" or "your username or password is incorrect"

#3 add the changes to the stock price, by adding the latest stock price and the time of the change

#4 display all the prices in reverse chronological order i.e., starting with the current

price to the beginning price of the day

save usernames and passwords into a file and when we chose #2 retrive the inputed user from the file and put it into a stack to check the correctness of user and passwords

file could be csv or whatever

Explanation / Answer

/*

Assuming that user data is store in user.csv and share data is store in share.csv

Code is pretty self explanatory

Time is stored in int format as HHMM

*/

import java.io.*;

import java.util.*;

class Company{

String name;

}

class User extends Company{

String user, passwd;

User(String name, String user, String passwd){

this.name = name;

this.user = user;

this.passwd = passwd;

}

}

class ShareInfo extends Company{

int time;

float price;

ShareInfo(String name, int time, float price){

this.name = name;

this.time = time;

this.price = price;

}

public String toString(){

return this.name + " price:"+ this.price +" at time:"+this.time;

}

}

class MainMenu{

public static void main(String a[]){

CompanyManager manager = new CompanyManager();

int ch=0;

Scanner sc = new Scanner(System.in);

while(ch!=5){

System.out.print(" 1.Register company 2.Login 3.Log share price 4.Display price in reverse order 5.Exit Please enter a choice:");

ch = sc.nextInt();

String name, user, passwd;

int time;

float price;

switch(ch){

case 1:

System.out.print("Please enter further details Company Name:");

name = sc.next();

System.out.print("User Name:");

user = sc.next();

System.out.print("User password:");

passwd = sc.next();

System.out.print("Log time:");

time = sc.nextInt();

System.out.print("Share Price:");

price = sc.nextFloat();

User u = new User(name , user, passwd);

ShareInfo s = new ShareInfo(name, time, price);

manager.register(u, s);

System.out.println(" Successfully registered!!");

break;

case 2:

System.out.print("User Name:");

user = sc.next();

System.out.print("User password:");

passwd = sc.next();

System.out.println(manager.authenticate(user, passwd)?"Your login is successfull":"Your username or password is incorrect");

break;

case 3:

System.out.print("Please enter further details Company Name:");

name = sc.next();

System.out.print("Log time:");

time = sc.nextInt();

System.out.print("Share Price:");

price = sc.nextFloat();

ShareInfo share = new ShareInfo(name, time, price);

manager.logSharePrice(share);

System.out.println("Done successfully!!");

break;

case 4:

System.out.print("Company Name:");

name = sc.next();

manager.printAllShares(name);

break;

}

}

}

}

class CompanyManager{

public int register(User user, ShareInfo shareInfo){

UserDataManager.registerUser(user);

ShareDataManager.logShareInfo(shareInfo);

return 1;

}

public boolean authenticate(String user, String passwd){

List<User> users = UserDataManager.getAll();

if(users == null)

return false;

Iterator itr=users.iterator();  

while(itr.hasNext()){

User u = (User)itr.next();

if(u.user.equals(user) && u.passwd.equals(passwd))

return true;

}

return false;

}

public void logSharePrice(ShareInfo shareInfo){

ShareDataManager.logShareInfo(shareInfo);

}

public void printAllShares(String companyName){

List<ShareInfo> shares = ShareDataManager.getAll(companyName);

if(shares == null || shares.size() == 0){

System.out.println("No share price exits for this company !!");

return;

}

this.sort(shares);

Iterator itr=shares.iterator();  

while(itr.hasNext()){

ShareInfo share =(ShareInfo)itr.next();

System.out.println(share);

}

}

private void sort(List<ShareInfo> shares){

Collections.sort(shares, new Comparator<ShareInfo>() {

public int compare(ShareInfo lhs, ShareInfo rhs) {

return lhs.time > rhs.time ? -1 : (lhs.time < rhs.time) ? 1 : 0;

}

});

}

}

class UserDataManager{

static String splitter = ",";

public static int registerUser(User user){

try{

File csvFile = new File("user.csv");

csvFile.createNewFile();

FileWriter fw = new FileWriter(csvFile, true);

BufferedWriter bw = new BufferedWriter(fw);

PrintWriter pw = new PrintWriter(bw);

StringBuilder sb = new StringBuilder();

sb.append(user.name);

sb.append(splitter);

sb.append(user.user);

sb.append(splitter);

sb.append(user.passwd);

sb.append(' ');

pw.write(sb.toString());

pw.close();

return 1;

}

catch(Exception e){

System.out.println("File not found");

return -1;

}

}

public static List<User> getAll(){

ArrayList<User> users= new ArrayList();

BufferedReader br = null;

try {

br = new BufferedReader(new FileReader("user.csv"));

StringBuilder sb = new StringBuilder();

String line = br.readLine();

while (line != null) {

String temp[]= line.split(splitter);

users.add(new User(temp[0], temp[1], temp[2]));

line = br.readLine();

}

br.close();

} catch(Exception e) {

return null;

}

return users;

}

}

class ShareDataManager{

static String splitter = ",";

public static int logShareInfo(ShareInfo shareInfo){

try{

File csvFile = new File("share.csv");

csvFile.createNewFile();

FileWriter fw = new FileWriter(csvFile, true);

BufferedWriter bw = new BufferedWriter(fw);

PrintWriter pw = new PrintWriter(bw);

StringBuilder sb = new StringBuilder();

sb.append(shareInfo.name);

sb.append(splitter);

sb.append(shareInfo.price);

sb.append(splitter);

sb.append(shareInfo.time);

sb.append(' ');

pw.write(sb.toString());

pw.close();

return 1;

}

catch(Exception e){

System.out.println("File not found");

return -1;

}

}

public static List<ShareInfo> getAll(String compName){

ArrayList<ShareInfo> shares= new ArrayList();

BufferedReader br = null;

try {

br = new BufferedReader(new FileReader("share.csv"));

StringBuilder sb = new StringBuilder();

String line = br.readLine();

while (line != null) {

String temp[]= line.split(splitter);

if(compName.equals(temp[0]))

shares.add(new ShareInfo(temp[0], Integer.parseInt(temp[2]), Float.parseFloat(temp[1])));

line = br.readLine();

}

br.close();

} catch(Exception e) {

return null;

}

return shares;

}

}