mirror of
https://github.com/mudabbir-ahmad/Prog3-question2.git
synced 2026-10-08 04:50:21 +00:00
Programming 3 Question 2
This commit is contained in:
commit
8bd093e9de
16 files changed
+370
No files matched your search
+39
@@ -0,0 +1,39 @@
|
|||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
.kotlin
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea/modules.xml
|
||||||
|
.idea/jarRepositories.xml
|
||||||
|
.idea/compiler.xml
|
||||||
|
.idea/libraries/
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### Eclipse ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### Mac OS ###
|
||||||
|
.DS_Store
|
||||||
Generated
+8
@@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Ignored default folder with query files
|
||||||
|
/queries/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Ask2AgentMigrationStateService">
|
||||||
|
<option name="migrationStatus" value="COMPLETED" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+7
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding">
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+14
@@ -0,0 +1,14 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
|
<component name="MavenProjectsManager">
|
||||||
|
<option name="originalFiles">
|
||||||
|
<list>
|
||||||
|
<option value="$PROJECT_DIR$/pom.xml" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+6
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>org.example</groupId>
|
||||||
|
<artifactId>Prog3-AssignmentQ2</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<name>Archetype - Prog3-AssignmentQ2</name>
|
||||||
|
<url>http://maven.apache.org</url>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
<archetype>
|
||||||
|
<id>Prog3-AssignmentQ2</id>
|
||||||
|
<sources>
|
||||||
|
<source>src/main/java/App.java</source>
|
||||||
|
</sources>
|
||||||
|
<testSources>
|
||||||
|
<source>src/test/java/AppTest.java</source>
|
||||||
|
</testSources>
|
||||||
|
</archetype>
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>$org.example</groupId>
|
||||||
|
<artifactId>$Prog3-AssignmentQ2</artifactId>
|
||||||
|
<version>$1.0-SNAPSHOT</version>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>3.8.1</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
package $org.example;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Taxables Question for Programming 3
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.stream.*;
|
||||||
|
|
||||||
|
public class App {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
TaxCalculator taxCalculator = (income) -> {
|
||||||
|
if (income <= 12_570) {
|
||||||
|
return 0;
|
||||||
|
} else if (income <= 50_270) {
|
||||||
|
return (income - 12_570) * 0.20;
|
||||||
|
} else {
|
||||||
|
return (50_270 - 12_570) * 0.20
|
||||||
|
+ (income - 50_270) * 0.40;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create Employee objects
|
||||||
|
List<Employee> employees = new ArrayList<>();
|
||||||
|
employees.add(new Employee("u1", "pass123", "Alice", "London", 30_000));
|
||||||
|
employees.add(new Employee("u2", "password", "Bob", "Leeds", 55_000));
|
||||||
|
employees.add(new Employee("u3", "hello", "Charlie", "Manchester", 12_000));
|
||||||
|
employees.add(new Employee("u4", "secure1234", "Daisy", "Bristol", 80_000));
|
||||||
|
|
||||||
|
// Apply tax calculation to all employees (Lambda + Stream)
|
||||||
|
employees.forEach(e ->
|
||||||
|
e.setTaxToPay(taxCalculator.calculate(e.getIncome()))
|
||||||
|
);
|
||||||
|
|
||||||
|
// Sort employees by income (ascending)
|
||||||
|
List<Employee> sortedByIncome = employees.stream()
|
||||||
|
.sorted(Comparator.comparingDouble(Employee::getIncome))
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
// Filter employees who pay tax (income above personal allowance)
|
||||||
|
List<Employee> taxableEmployees = employees.stream()
|
||||||
|
.filter(e -> e.getIncome() > 12_570)
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
// Optional output to demonstrate integration
|
||||||
|
sortedByIncome.forEach(e ->
|
||||||
|
System.out.println(e.getName() +
|
||||||
|
" | Income: £" + e.getIncome() +
|
||||||
|
" | Tax: £" + e.getTaxToPay())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package $org.example;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Employee class for Taxables Question
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class Employee extends User {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private String address;
|
||||||
|
private double income;
|
||||||
|
private double taxToPay;
|
||||||
|
|
||||||
|
public Employee(String username, String password,
|
||||||
|
String name, String address, double income) {
|
||||||
|
super(username, password);
|
||||||
|
this.name = name;
|
||||||
|
this.address = address;
|
||||||
|
this.income = income;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAddress() {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getIncome() {
|
||||||
|
return income;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getTaxToPay() {
|
||||||
|
return taxToPay;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTaxToPay(double taxToPay) {
|
||||||
|
this.taxToPay = taxToPay;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package $org.example;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Password Service for Taxables Question
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.stream.*;
|
||||||
|
|
||||||
|
public class PasswordService {
|
||||||
|
|
||||||
|
private List<String> passwords = new ArrayList<>();
|
||||||
|
|
||||||
|
public void addPassword(String password) {
|
||||||
|
passwords.add(password);
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getAveragePasswordLength() {
|
||||||
|
return passwords.stream()
|
||||||
|
.mapToInt(String::length)
|
||||||
|
.average()
|
||||||
|
.orElse(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package $org.example;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User class for Taxables Question
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class User {
|
||||||
|
protected String username;
|
||||||
|
protected String password;
|
||||||
|
|
||||||
|
public User(String username, String password) {
|
||||||
|
this.username = username;
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package $org.example;
|
||||||
|
|
||||||
|
import junit.framework.Test;
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit test for simple App.
|
||||||
|
*/
|
||||||
|
public class AppTest
|
||||||
|
extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Create the test case
|
||||||
|
*
|
||||||
|
* @param testName name of the test case
|
||||||
|
*/
|
||||||
|
public AppTest( String testName )
|
||||||
|
{
|
||||||
|
super( testName );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the suite of tests being tested
|
||||||
|
*/
|
||||||
|
public static Test suite()
|
||||||
|
{
|
||||||
|
return new TestSuite( AppTest.class );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rigourous Test :-)
|
||||||
|
*/
|
||||||
|
public void testApp()
|
||||||
|
{
|
||||||
|
assertTrue( true );
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class EmployeeTaxTest {
|
||||||
|
|
||||||
|
TaxCalculator taxCalculator = (income) -> {
|
||||||
|
if (income <= 12_570) return 0;
|
||||||
|
else if (income <= 50_270)
|
||||||
|
return (income - 12_570) * 0.20;
|
||||||
|
else
|
||||||
|
return (50_270 - 12_570) * 0.20
|
||||||
|
+ (income - 50_270) * 0.40;
|
||||||
|
};
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testPersonalAllowanceBand() {
|
||||||
|
assertEquals(0, taxCalculator.calculate(10_000));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testBasicRateBand() {
|
||||||
|
assertEquals(3_486, taxCalculator.calculate(30_000), 0.01);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testHigherRateBand() {
|
||||||
|
assertEquals(19_432, taxCalculator.calculate(80_000), 0.01);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSortingByIncome() {
|
||||||
|
List<Employee> employees = List.of(
|
||||||
|
new Employee("u1", "p1", "A", "X", 40_000),
|
||||||
|
new Employee("u2", "p2", "B", "Y", 20_000)
|
||||||
|
);
|
||||||
|
|
||||||
|
List<Employee> sorted = employees.stream()
|
||||||
|
.sorted(Comparator.comparingDouble(Employee::getIncome))
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
assertEquals(20_000, sorted.get(0).getIncome());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testFilteringHighEarners() {
|
||||||
|
List<Employee> employees = List.of(
|
||||||
|
new Employee("u1", "p1", "A", "X", 60_000),
|
||||||
|
new Employee("u2", "p2", "B", "Y", 30_000)
|
||||||
|
);
|
||||||
|
|
||||||
|
List<Employee> result = employees.stream()
|
||||||
|
.filter(e -> e.getIncome() > 50_000)
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
assertEquals(1, result.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class PasswordServiceTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testAveragePasswordLength() {
|
||||||
|
PasswordService service = new PasswordService();
|
||||||
|
service.addPassword("abc"); // 3
|
||||||
|
service.addPassword("password"); // 8
|
||||||
|
service.addPassword("hello"); // 5
|
||||||
|
|
||||||
|
assertEquals(5.33, service.getAveragePasswordLength(), 0.01);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user